123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- #include "optiga/comms/optiga_comms.h"
- #include "optiga/ifx_i2c/ifx_i2c.h"
-
- #define OPTIGA_COMMS_INUSE (0x01)
-
- #define OPTIGA_COMMS_FREE (0x00)
- static host_lib_status_t check_optiga_comms_state(optiga_comms_t *p_ctx);
- static void ifx_i2c_event_handler(void* upper_layer_ctx, host_lib_status_t event);
- host_lib_status_t optiga_comms_open(optiga_comms_t *p_ctx)
- {
- host_lib_status_t status = OPTIGA_COMMS_ERROR;
- if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
- {
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
- status = ifx_i2c_open((ifx_i2c_context_t*)(p_ctx->comms_ctx));
- if (IFX_I2C_STACK_SUCCESS != status)
- {
- p_ctx->state = OPTIGA_COMMS_FREE;
- }
- }
- return status;
- }
- host_lib_status_t optiga_comms_reset(optiga_comms_t *p_ctx,uint8_t reset_type)
- {
- host_lib_status_t status = OPTIGA_COMMS_ERROR;
- if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
- {
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
- status = ifx_i2c_reset((ifx_i2c_context_t*)(p_ctx->comms_ctx),(ifx_i2c_reset_type_t)reset_type);
- if (IFX_I2C_STACK_SUCCESS != status)
- {
- p_ctx->state = OPTIGA_COMMS_FREE;
- }
- }
- return status;
- }
- host_lib_status_t optiga_comms_transceive(optiga_comms_t *p_ctx,const uint8_t* p_data,
- const uint16_t* p_data_length,
- uint8_t* p_buffer, uint16_t* p_buffer_len)
- {
- host_lib_status_t status = OPTIGA_COMMS_ERROR;
- if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
- {
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
- status = (ifx_i2c_transceive((ifx_i2c_context_t*)(p_ctx->comms_ctx),p_data,p_data_length,p_buffer,p_buffer_len));
- if (IFX_I2C_STACK_SUCCESS != status)
- {
- p_ctx->state = OPTIGA_COMMS_FREE;
- }
- }
- return status;
- }
- host_lib_status_t optiga_comms_close(optiga_comms_t *p_ctx)
- {
- host_lib_status_t status = OPTIGA_COMMS_ERROR;
- if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
- {
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
- ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
- status = ifx_i2c_close((ifx_i2c_context_t*)(p_ctx->comms_ctx));
- if (IFX_I2C_STACK_SUCCESS != status)
- {
- p_ctx->state = OPTIGA_COMMS_FREE;
- }
- }
- return status;
- }
- static host_lib_status_t check_optiga_comms_state(optiga_comms_t *p_ctx)
- {
- host_lib_status_t status = OPTIGA_COMMS_ERROR;
- if ((NULL != p_ctx) && (p_ctx->state != OPTIGA_COMMS_INUSE))
- {
- p_ctx->state = OPTIGA_COMMS_INUSE;
- status = OPTIGA_COMMS_SUCCESS;
- }
- return status;
- }
- static void ifx_i2c_event_handler(void* upper_layer_ctx, host_lib_status_t event)
- {
- void* ctx = ((optiga_comms_t*)upper_layer_ctx)->upper_layer_ctx;
- ((optiga_comms_t*)upper_layer_ctx)->upper_layer_handler(ctx,event);
- ((optiga_comms_t*)upper_layer_ctx)->state = OPTIGA_COMMS_FREE;
- }
|