components/esp-cryptoauthlib/cryptoauthlib/lib/hal/hal_linux_i2c_userspace.c
ATCA_STATUS hal_i2c_receive(ATCAIface iface, uint8_t *rxdata, uint16_t *rxlength)
{
    ATCAIfaceCfg *cfg = atgetifacecfg(iface);
    ATCAI2CMaster_t * hal_data = (ATCAI2CMaster_t*)atgetifacehaldat(iface);
    int f_i2c;  // I2C file descriptor
    uint16_t count;
    uint16_t rxdata_max_size = *rxlength;

    *rxlength = 0;
    if (rxdata_max_size < 1)
    {
        return ATCA_SMALL_BUFFER;
    }

    // Initiate I2C communication
    if ( (f_i2c = open(hal_data->i2c_file, O_RDWR)) < 0)
    {
        return ATCA_COMM_FAIL;
    }

    // Set Slave Address
    if (ioctl(f_i2c, I2C_SLAVE, cfg->atcai2c.slave_address >> 1) < 0)
    {
        close(f_i2c);
        return ATCA_COMM_FAIL;
    }

    // Receive count
    count = 1;
    if (read(f_i2c, rxdata, count) != count)
    {
        close(f_i2c);
        return ATCA_COMM_FAIL;
    }

    if (rxdata[0] < ATCA_RSP_SIZE_MIN)
    {
        return ATCA_INVALID_SIZE;
    }
    if (rxdata[0] > rxdata_max_size)
    {
        return ATCA_SMALL_BUFFER;
    }

    count = rxdata[0] - 1;
    // Receive data
    if (read(f_i2c, &rxdata[1], count) != count)
    {
        close(f_i2c);
        return ATCA_COMM_FAIL;
    }

    *rxlength = rxdata[0];

    close(f_i2c);
    return ATCA_SUCCESS;
}
/** \brief HAL implementation of I2C receive function
 * \param[in]    iface     Device to interact with.
 * \param[out]   rxdata    Data received will be returned here.
 * \param[inout] rxlength  As input, the size of the rxdata buffer.
 *                         As output, the number of bytes received.
 * \return ATCA_SUCCESS on success, otherwise an error code.
 */
\brief HAL implementation of I2C receive function
\param[in]    iface     Device to interact with.
\param[out]   rxdata    Data received will be returned here.
\param[inout] rxlength  As input, the size of the rxdata buffer.
As output, the number of bytes received.
\return ATCA_SUCCESS on success, otherwise an error code.
\ brief HAL implementation of I2C receive function \ param [ in ] iface Device to interact with . \ param [ out ] rxdata Data received will be returned here . \ param [ inout ] rxlength As input the size of the rxdata buffer . As output the number of bytes received . \ return ATCA_SUCCESS on success otherwise an error code .
ATCA_STATUS hal_i2c_receive(ATCAIface iface, uint8_t *rxdata, uint16_t *rxlength)
{
    ATCAIfaceCfg *cfg = atgetifacecfg(iface);
    ATCAI2CMaster_t * hal_data = (ATCAI2CMaster_t*)atgetifacehaldat(iface);
    int f_i2c;  
    uint16_t count;
    uint16_t rxdata_max_size = *rxlength;
    *rxlength = 0;
    if (rxdata_max_size < 1)
    {
        return ATCA_SMALL_BUFFER;
    }
    if ( (f_i2c = open(hal_data->i2c_file, O_RDWR)) < 0)
    {
        return ATCA_COMM_FAIL;
    }
    if (ioctl(f_i2c, I2C_SLAVE, cfg->atcai2c.slave_address >> 1) < 0)
    {
        close(f_i2c);
        return ATCA_COMM_FAIL;
    }
    count = 1;
    if (read(f_i2c, rxdata, count) != count)
    {
        close(f_i2c);
        return ATCA_COMM_FAIL;
    }
    if (rxdata[0] < ATCA_RSP_SIZE_MIN)
    {
        return ATCA_INVALID_SIZE;
    }
    if (rxdata[0] > rxdata_max_size)
    {
        return ATCA_SMALL_BUFFER;
    }
    count = rxdata[0] - 1;
    if (read(f_i2c, &rxdata[1], count) != count)
    {
        close(f_i2c);
        return ATCA_COMM_FAIL;
    }
    *rxlength = rxdata[0];
    close(f_i2c);
    return ATCA_SUCCESS;
}
ATCA_STATUS hal_i2c_receive ( ATCAIface iface , uint8_t * rxdata , uint16_t * rxlength ) { ATCAIfaceCfg * cfg = atgetifacecfg ( iface ) ; ATCAI2CMaster_t * hal_data = ( ATCAI2CMaster_t * ) atgetifacehaldat ( iface ) ; int f_i2c ; uint16_t count ; uint16_t rxdata_max_size = * rxlength ; * rxlength = 0 ; if ( rxdata_max_size < 1 ) { return ATCA_SMALL_BUFFER ; } if ( ( f_i2c = open ( hal_data -> i2c_file , O_RDWR ) ) < 0 ) { return ATCA_COMM_FAIL ; } if ( ioctl ( f_i2c , I2C_SLAVE , cfg -> atcai2c . slave_address >> 1 ) < 0 ) { close ( f_i2c ) ; return ATCA_COMM_FAIL ; } count = 1 ; if ( read ( f_i2c , rxdata , count ) != count ) { close ( f_i2c ) ; return ATCA_COMM_FAIL ; } if ( rxdata [ 0 ] < ATCA_RSP_SIZE_MIN ) { return ATCA_INVALID_SIZE ; } if ( rxdata [ 0 ] > rxdata_max_size ) { return ATCA_SMALL_BUFFER ; } count = rxdata [ 0 ] - 1 ; if ( read ( f_i2c , & rxdata [ 1 ] , count ) != count ) { close ( f_i2c ) ; return ATCA_COMM_FAIL ; } * rxlength = rxdata [ 0 ] ; close ( f_i2c ) ; return ATCA_SUCCESS ; }
\brief HAL implementation of I2C receive function
 \param[in]    iface     Device to interact with.
\ brief HAL implementation of I2C receive function \ param [ in ] iface Device to interact with .
// I2C file descriptor // Initiate I2C communication // Set Slave Address // Receive count // Receive data