components/esp-cryptoauthlib/cryptoauthlib/lib/hal/hal_linux_i2c_userspace.c
ATCA_STATUS hal_i2c_init(void* hal, ATCAIfaceCfg* cfg)
{
    ATCAHAL_t *pHal = (ATCAHAL_t*)hal;
    ATCA_STATUS ret = ATCA_BAD_PARAM;

    if (!pHal || !cfg)
    {
        return ret;
    }

    if (pHal->hal_data)
    {
        ATCAI2CMaster_t * hal_data = (ATCAI2CMaster_t*)pHal->hal_data;

        // Assume the bus had already been initialized
        hal_data->ref_ct++;

        ret = ATCA_SUCCESS;
    }
    else
    {
        ATCAI2CMaster_t * hal_data = malloc(sizeof(ATCAI2CMaster_t));
        int bus = cfg->atcai2c.bus; // 0-based logical bus number

        if (hal_data)
        {
            hal_data->ref_ct = 1;  // buses are shared, this is the first instance

            (void)snprintf(hal_data->i2c_file, sizeof(hal_data->i2c_file) - 1, "/dev/i2c-%d", bus);

            pHal->hal_data = hal_data;

            ret = ATCA_SUCCESS;
        }
        else
        {
            ret = ATCA_ALLOC_FAILURE;
        }
    }

    return ret;

}
/** \brief HAL implementation of I2C init
 *
 * this implementation assumes I2C peripheral has been enabled by user. It only initialize an
 * I2C interface using given config.
 *
 *  \param[in] hal pointer to HAL specific data that is maintained by this HAL
 *  \param[in] cfg pointer to HAL specific configuration data that is used to initialize this HAL
 * \return ATCA_SUCCESS on success, otherwise an error code.
 */
\brief HAL implementation of I2C init
this implementation assumes I2C peripheral has been enabled by user. It only initialize an
I2C interface using given config.

\param[in] hal pointer to HAL specific data that is maintained by this HAL
\param[in] cfg pointer to HAL specific configuration data that is used to initialize this HAL
\return ATCA_SUCCESS on success, otherwise an error code.
\ brief HAL implementation of I2C init this implementation assumes I2C peripheral has been enabled by user . It only initialize an I2C interface using given config . \ param [ in ] hal pointer to HAL specific data that is maintained by this HAL \ param [ in ] cfg pointer to HAL specific configuration data that is used to initialize this HAL \ return ATCA_SUCCESS on success otherwise an error code .
ATCA_STATUS hal_i2c_init(void* hal, ATCAIfaceCfg* cfg)
{
    ATCAHAL_t *pHal = (ATCAHAL_t*)hal;
    ATCA_STATUS ret = ATCA_BAD_PARAM;
    if (!pHal || !cfg)
    {
        return ret;
    }
    if (pHal->hal_data)
    {
        ATCAI2CMaster_t * hal_data = (ATCAI2CMaster_t*)pHal->hal_data;
        hal_data->ref_ct++;
        ret = ATCA_SUCCESS;
    }
    else
    {
        ATCAI2CMaster_t * hal_data = malloc(sizeof(ATCAI2CMaster_t));
        int bus = cfg->atcai2c.bus; 
        if (hal_data)
        {
            hal_data->ref_ct = 1;  
            (void)snprintf(hal_data->i2c_file, sizeof(hal_data->i2c_file) - 1, "/dev/i2c-%d", bus);
            pHal->hal_data = hal_data;
            ret = ATCA_SUCCESS;
        }
        else
        {
            ret = ATCA_ALLOC_FAILURE;
        }
    }
    return ret;
}
ATCA_STATUS hal_i2c_init ( void * hal , ATCAIfaceCfg * cfg ) { ATCAHAL_t * pHal = ( ATCAHAL_t * ) hal ; ATCA_STATUS ret = ATCA_BAD_PARAM ; if ( ! pHal || ! cfg ) { return ret ; } if ( pHal -> hal_data ) { ATCAI2CMaster_t * hal_data = ( ATCAI2CMaster_t * ) pHal -> hal_data ; hal_data -> ref_ct ++ ; ret = ATCA_SUCCESS ; } else { ATCAI2CMaster_t * hal_data = malloc ( sizeof ( ATCAI2CMaster_t ) ) ; int bus = cfg -> atcai2c . bus ; if ( hal_data ) { hal_data -> ref_ct = 1 ; ( void ) snprintf ( hal_data -> i2c_file , sizeof ( hal_data -> i2c_file ) - 1 , " " , bus ) ; pHal -> hal_data = hal_data ; ret = ATCA_SUCCESS ; } else { ret = ATCA_ALLOC_FAILURE ; } } return ret ; }
\brief HAL implementation of I2C init
 this implementation assumes I2C peripheral has been enabled by user.
\ brief HAL implementation of I2C init this implementation assumes I2C peripheral has been enabled by user .
// Assume the bus had already been initialized // 0-based logical bus number // buses are shared, this is the first instance