optiga_comms.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file
  26. *
  27. * \brief This file implements optiga comms abstraction layer for IFX I2C Protocol.
  28. *
  29. * \addtogroup grOptigaComms
  30. * @{
  31. */
  32. /**********************************************************************************************************************
  33. * HEADER FILES
  34. *********************************************************************************************************************/
  35. #include "optiga/comms/optiga_comms.h"
  36. #include "optiga/ifx_i2c/ifx_i2c.h"
  37. /// @cond hidden
  38. /**********************************************************************************************************************
  39. * MACROS
  40. *********************************************************************************************************************/
  41. /// Optiga comms is in use
  42. #define OPTIGA_COMMS_INUSE (0x01)
  43. /// Optiga comms is free
  44. #define OPTIGA_COMMS_FREE (0x00)
  45. /**********************************************************************************************************************
  46. * LOCAL DATA
  47. *********************************************************************************************************************/
  48. /**********************************************************************************************************************
  49. * LOCAL ROUTINES
  50. *********************************************************************************************************************/
  51. static host_lib_status_t check_optiga_comms_state(optiga_comms_t *p_ctx);
  52. static void ifx_i2c_event_handler(void* upper_layer_ctx, host_lib_status_t event);
  53. /// @endcond
  54. /**********************************************************************************************************************
  55. * API IMPLEMENTATION
  56. *********************************************************************************************************************/
  57. /**
  58. * Initializes the commmunication with OPTIGA.<br>
  59. *
  60. *<b>Pre Conditions:</b>
  61. * - None<br>
  62. *
  63. *<b>API Details:</b>
  64. * - Initializes OPTIGA and establishes the communication channel.<br>
  65. * - Initializes the ifx i2c protocol stack and registers the event callbacks.<br>
  66. * - Negotiates the frame size and bit rate with the OPTIGA.<br>
  67. *<br>
  68. *
  69. *<b>User Input:</b><br>
  70. * - The input #optiga_comms_t p_ctx must not be NULL.<br>
  71. * - The following parameters in #optiga_comms_t must be initialized with appropriate values.<br>
  72. * - The <b>comms_ctx</b> must be initialized with a valid #ifx_i2c_context.<br>
  73. * - The <b>upper_layer_event_handler</b> parameter must be properly initialized.
  74. * This is invoked when #optiga_comms_open is asynchronously completed.<br>
  75. * - The <b>upper_layer_ctx</b> must be properly initialized.<br>
  76. *
  77. *<b>Notes:</b>
  78. * - None<br>
  79. *
  80. *<br>
  81. * \param[in,out] p_ctx Pointer to optiga comms context
  82. *
  83. * \retval #OPTIGA_COMMS_SUCCESS
  84. * \retval #OPTIGA_COMMS_ERROR
  85. */
  86. host_lib_status_t optiga_comms_open(optiga_comms_t *p_ctx)
  87. {
  88. host_lib_status_t status = OPTIGA_COMMS_ERROR;
  89. if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
  90. {
  91. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
  92. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
  93. status = ifx_i2c_open((ifx_i2c_context_t*)(p_ctx->comms_ctx));
  94. if (IFX_I2C_STACK_SUCCESS != status)
  95. {
  96. p_ctx->state = OPTIGA_COMMS_FREE;
  97. }
  98. }
  99. return status;
  100. }
  101. /**
  102. * Resets the OPTIGA.<br>
  103. *
  104. *<b>Pre Conditions:</b>
  105. * - Communication channel must be established with OPTIGA.<br>
  106. *
  107. *<b>API Details:</b>
  108. * - Resets the OPTIGA device.<br>
  109. * - Initializes the ifx i2c protocol stack.<br>
  110. * - Re-Initializes and negotiates the frame size and bit rate with the OPTIGA.
  111. * The values remain same as that in previous #optiga_comms_open().<br>
  112. *<br>
  113. *
  114. *<b>User Input:</b><br>
  115. * - The input #optiga_comms_t p_ctx must not be NULL.
  116. *
  117. *<b>Notes:</b>
  118. * For COLD and WARM reset type: If the gpio(vdd and/or reset) pins are not configured,
  119. * the API continues without returning error status<br>
  120. *
  121. *
  122. * \param[in,out] p_ctx Pointer to #optiga_comms_t
  123. * \param[in,out] reset_type type of reset
  124. *
  125. * \retval #OPTIGA_COMMS_SUCCESS
  126. * \retval #OPTIGA_COMMS_ERROR
  127. */
  128. host_lib_status_t optiga_comms_reset(optiga_comms_t *p_ctx,uint8_t reset_type)
  129. {
  130. host_lib_status_t status = OPTIGA_COMMS_ERROR;
  131. if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
  132. {
  133. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
  134. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
  135. status = ifx_i2c_reset((ifx_i2c_context_t*)(p_ctx->comms_ctx),(ifx_i2c_reset_type_t)reset_type);
  136. if (IFX_I2C_STACK_SUCCESS != status)
  137. {
  138. p_ctx->state = OPTIGA_COMMS_FREE;
  139. }
  140. }
  141. return status;
  142. }
  143. /**
  144. * Sends a command to OPTIGA and receives a response.<br>
  145. *
  146. *
  147. *<b>Pre Conditions:</b>
  148. * - Communication channel must be established with OPTIGA.<br>
  149. *
  150. *<b>API Details:</b>
  151. * - Transmit data(Command) to OPTIGA.<br>
  152. * - Receive data(Response) from OPTIGA.<br>
  153. *<br>
  154. *
  155. *<b>User Input:</b><br>
  156. * - The input #optiga_comms_t p_ctx must not be NULL.<br>
  157. * - The following parameters in #optiga_comms_t must be initialized with appropriate values <br>
  158. * - The <b>comms_ctx</b> must be initialized with a valid #ifx_i2c_context<br>
  159. * - The <b>upper_layer_event_handler</b> parameter must be properly initialized,
  160. * if it is different from that in #optiga_comms_open().
  161. * This is invoked when optiga_comms_transceive is asynchronously completed.<br>
  162. * - The <b>upper_layer_ctx</b> must be properly initialized,
  163. * if it is different from that in #optiga_comms_open().<br>
  164. *
  165. *<b>Notes:</b>
  166. * - The actual number of bytes received is stored in p_buffer_len. In case of error, p_buffer_len is set to 0.<br>
  167. * - If the size of p_buffer is zero or insufficient to copy the response bytes then
  168. * #IFX_I2C_STACK_MEM_ERROR error is returned.
  169. *
  170. *
  171. * \param[in,out] p_ctx Pointer to #optiga_comms_t
  172. * \param[in] p_data Pointer to the write data buffer
  173. * \param[in] p_data_length Pointer to the length of the write data buffer
  174. * \param[in,out] p_buffer Pointer to the receive data buffer
  175. * \param[in,out] p_buffer_len Pointer to the length of the receive data buffer
  176. *
  177. * \retval #OPTIGA_COMMS_SUCCESS
  178. * \retval #OPTIGA_COMMS_ERROR
  179. * \retval #IFX_I2C_STACK_MEM_ERROR
  180. */
  181. host_lib_status_t optiga_comms_transceive(optiga_comms_t *p_ctx,const uint8_t* p_data,
  182. const uint16_t* p_data_length,
  183. uint8_t* p_buffer, uint16_t* p_buffer_len)
  184. {
  185. host_lib_status_t status = OPTIGA_COMMS_ERROR;
  186. if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
  187. {
  188. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
  189. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
  190. status = (ifx_i2c_transceive((ifx_i2c_context_t*)(p_ctx->comms_ctx),p_data,p_data_length,p_buffer,p_buffer_len));
  191. if (IFX_I2C_STACK_SUCCESS != status)
  192. {
  193. p_ctx->state = OPTIGA_COMMS_FREE;
  194. }
  195. }
  196. return status;
  197. }
  198. /**
  199. * Closes the communication with OPTIGA.<br>
  200. *
  201. *<b>Pre Conditions:</b>
  202. * - None<br>
  203. *
  204. *<b>API Details:</b>
  205. * - De-Initializes the OPTIGA and closes the communication channel.<br>
  206. * - Power downs the OPTIGA.<br>
  207. *<br>
  208. *
  209. *<b>User Input:</b><br>
  210. * - The input #optiga_comms_t p_ctx must not be NULL.<br>
  211. * - The #optiga_comms_t comms_ctx must be initialized with a valid #ifx_i2c_context<br>
  212. *
  213. * \param[in,out] p_ctx Pointer to #optiga_comms_t
  214. *
  215. * \retval #OPTIGA_COMMS_SUCCESS
  216. * \retval #OPTIGA_COMMS_ERROR
  217. */
  218. host_lib_status_t optiga_comms_close(optiga_comms_t *p_ctx)
  219. {
  220. host_lib_status_t status = OPTIGA_COMMS_ERROR;
  221. if (OPTIGA_COMMS_SUCCESS == check_optiga_comms_state(p_ctx))
  222. {
  223. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->p_upper_layer_ctx = (void*)p_ctx;
  224. ((ifx_i2c_context_t*)(p_ctx->comms_ctx))->upper_layer_event_handler = ifx_i2c_event_handler;
  225. status = ifx_i2c_close((ifx_i2c_context_t*)(p_ctx->comms_ctx));
  226. if (IFX_I2C_STACK_SUCCESS != status)
  227. {
  228. p_ctx->state = OPTIGA_COMMS_FREE;
  229. }
  230. }
  231. return status;
  232. }
  233. /// @cond hidden
  234. static host_lib_status_t check_optiga_comms_state(optiga_comms_t *p_ctx)
  235. {
  236. host_lib_status_t status = OPTIGA_COMMS_ERROR;
  237. if ((NULL != p_ctx) && (p_ctx->state != OPTIGA_COMMS_INUSE))
  238. {
  239. p_ctx->state = OPTIGA_COMMS_INUSE;
  240. status = OPTIGA_COMMS_SUCCESS;
  241. }
  242. return status;
  243. }
  244. //lint --e{818} suppress "This is ignored as upper layer handler function prototype requires this argument"
  245. static void ifx_i2c_event_handler(void* upper_layer_ctx, host_lib_status_t event)
  246. {
  247. void* ctx = ((optiga_comms_t*)upper_layer_ctx)->upper_layer_ctx;
  248. ((optiga_comms_t*)upper_layer_ctx)->upper_layer_handler(ctx,event);
  249. ((optiga_comms_t*)upper_layer_ctx)->state = OPTIGA_COMMS_FREE;
  250. }
  251. /// @endcond
  252. /**
  253. * @}
  254. */