#include "cryptoauthlib.h"
#include "atca_hal.h"
ATCA_STATUS hal_iface_init(ATCAIfaceCfg *cfg, ATCAHAL_t *hal)
{
ATCA_STATUS status = ATCA_COMM_FAIL;
switch (cfg->iface_type)
{
case ATCA_I2C_IFACE:
#ifdef ATCA_HAL_I2C
hal->halinit = &hal_i2c_init;
hal->halpostinit = &hal_i2c_post_init;
hal->halreceive = &hal_i2c_receive;
hal->halsend = &hal_i2c_send;
hal->halsleep = &hal_i2c_sleep;
hal->halwake = &hal_i2c_wake;
hal->halidle = &hal_i2c_idle;
hal->halrelease = &hal_i2c_release;
hal->hal_data = NULL;
status = ATCA_SUCCESS;
#endif
break;
case ATCA_SWI_IFACE:
#ifdef ATCA_HAL_SWI
hal->halinit = &hal_swi_init;
hal->halpostinit = &hal_swi_post_init;
hal->halreceive = &hal_swi_receive;
hal->halsend = &hal_swi_send;
hal->halsleep = &hal_swi_sleep;
hal->halwake = &hal_swi_wake;
hal->halidle = &hal_swi_idle;
hal->halrelease = &hal_swi_release;
hal->hal_data = NULL;
status = ATCA_SUCCESS;
#endif
break;
case ATCA_UART_IFACE:
#ifdef ATCA_HAL_UART
#endif
#ifdef ATCA_HAL_KIT_CDC
hal->halinit = &hal_kit_cdc_init;
hal->halpostinit = &hal_kit_cdc_post_init;
hal->halreceive = &hal_kit_cdc_receive;
hal->halsend = &hal_kit_cdc_send;
hal->halsleep = &hal_kit_cdc_sleep;
hal->halwake = &hal_kit_cdc_wake;
hal->halidle = &hal_kit_cdc_idle;
hal->halrelease = &hal_kit_cdc_release;
hal->hal_data = NULL;
status = ATCA_SUCCESS;
#endif
break;
case ATCA_SPI_IFACE:
#ifdef ATCA_HAL_SPI
#endif
break;
case ATCA_HID_IFACE:
#ifdef ATCA_HAL_KIT_HID
hal->halinit = &hal_kit_hid_init;
hal->halpostinit = &hal_kit_hid_post_init;
hal->halreceive = &hal_kit_hid_receive;
hal->halsend = &hal_kit_hid_send;
hal->halsleep = &hal_kit_hid_sleep;
hal->halwake = &hal_kit_hid_wake;
hal->halidle = &hal_kit_hid_idle;
hal->halrelease = &hal_kit_hid_release;
hal->hal_data = NULL;
status = ATCA_SUCCESS;
#endif
break;
case ATCA_CUSTOM_IFACE:
#ifdef ATCA_HAL_CUSTOM
hal->halinit = cfg->atcacustom.halinit;
hal->halpostinit = cfg->atcacustom.halpostinit;
hal->halreceive = cfg->atcacustom.halreceive;
hal->halsend = cfg->atcacustom.halsend;
hal->halsleep = cfg->atcacustom.halsleep;
hal->halwake = cfg->atcacustom.halwake;
hal->halidle = cfg->atcacustom.halidle;
hal->halrelease = cfg->atcacustom.halrelease;
hal->hal_data = NULL;
status = ATCA_SUCCESS;
#endif
break;
default:
break;
}
return status;
}
ATCA_STATUS hal_iface_release(ATCAIfaceType iface_type, void *hal_data)
{
ATCA_STATUS status = ATCA_GEN_FAIL;
switch (iface_type)
{
case ATCA_I2C_IFACE:
#ifdef ATCA_HAL_I2C
status = hal_i2c_release(hal_data);
#endif
break;
case ATCA_SWI_IFACE:
#ifdef ATCA_HAL_SWI
status = hal_swi_release(hal_data);
#endif
break;
case ATCA_UART_IFACE:
#ifdef ATCA_HAL_UART
#endif
#ifdef ATCA_HAL_KIT_CDC
status = hal_kit_cdc_release(hal_data);
#endif
break;
case ATCA_SPI_IFACE:
#ifdef ATCA_HAL_SPI
#endif
break;
case ATCA_HID_IFACE:
#ifdef ATCA_HAL_KIT_HID
status = hal_kit_hid_release(hal_data);
#endif
break;
case ATCA_CUSTOM_IFACE:
#ifdef ATCA_HAL_CUSTOM
status = ATCA_SUCCESS;
#endif
break;
default:
break;
}
return status;
}
ATCA_STATUS hal_check_wake(const uint8_t* response, int response_size)
{
const uint8_t expected_response[4] = { 0x04, 0x11, 0x33, 0x43 };
uint8_t selftest_fail_resp[4] = { 0x04, 0x07, 0xC4, 0x40 };
if (response_size != 4)
{
return ATCA_WAKE_FAILED;
}
if (memcmp(response, expected_response, 4) == 0)
{
return ATCA_SUCCESS;
}
if (memcmp(response, selftest_fail_resp, 4) == 0)
{
return ATCA_STATUS_SELFTEST_ERROR;
}
return ATCA_WAKE_FAILED;
}