ser_sd_transport.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdbool.h>
  41. #include <stdint.h>
  42. #include <stddef.h>
  43. #include "ser_sd_transport.h"
  44. #include "ser_hal_transport.h"
  45. #include "nrf_error.h"
  46. #include "app_error.h"
  47. #include "ble_serialization.h"
  48. #include "ser_dbg_sd_str.h"
  49. #include "ser_app_power_system_off.h"
  50. #include "app_util.h"
  51. #define NRF_LOG_MODULE_NAME ser_xfer
  52. #include "nrf_log.h"
  53. NRF_LOG_MODULE_REGISTER();
  54. #ifdef BLE_STACK_SUPPORT_REQD
  55. /** SoftDevice event handler. */
  56. static ser_sd_transport_evt_handler_t m_ble_evt_handler = NULL;
  57. #endif // BLE_STACK_SUPPORT_REQD
  58. #ifdef ANT_STACK_SUPPORT_REQD
  59. /** SoftDevice event handler for ANT events. */
  60. static ser_sd_transport_evt_handler_t m_ant_evt_handler = NULL;
  61. #endif // ANT_STACK_SUPPORT_REQD
  62. /** 'One time' handler called in task context while waiting for response to scheduled command. */
  63. static ser_sd_transport_rsp_wait_handler_t m_ot_rsp_wait_handler = NULL;
  64. /** Handler called in task context while waiting for response to scheduled command. */
  65. static ser_sd_transport_rsp_wait_handler_t m_os_rsp_wait_handler = NULL;
  66. /** Handler called in serial peripheral interrupt context when response is received. */
  67. static ser_sd_transport_rsp_set_handler_t m_os_rsp_set_handler = NULL;
  68. /** Handler called when hal_transport notifies that packet reception has started. */
  69. static ser_sd_transport_rx_notification_handler_t m_rx_notify_handler = NULL;
  70. /** User decoder handler for expected response packet. */
  71. static ser_sd_transport_rsp_handler_t m_rsp_dec_handler = NULL;
  72. /** Flag indicated whether module is waiting for response packet. */
  73. static volatile bool m_rsp_wait = false;
  74. /** SoftDevice call return value decoded by user decoder handler. */
  75. static uint32_t m_return_value;
  76. /**@brief Function for handling the rx packets comming from hal_transport.
  77. *
  78. * @details
  79. * This function is called in serial peripheral interrupt context. Response packets are handled in
  80. * this context. Events are passed to the application and it is up to application in which context
  81. * they are handled.
  82. *
  83. * @param[in] p_data Pointer to received data.
  84. * @param[in] length Size of data.
  85. */
  86. static void ser_sd_transport_rx_packet_handler(uint8_t * p_data, uint16_t length)
  87. {
  88. if (p_data && (length >= SER_PKT_TYPE_SIZE))
  89. {
  90. const uint8_t packet_type = p_data[SER_PKT_TYPE_POS];
  91. p_data += SER_PKT_TYPE_SIZE;
  92. length -= SER_PKT_TYPE_SIZE;
  93. switch (packet_type)
  94. {
  95. case SER_PKT_TYPE_RESP:
  96. case SER_PKT_TYPE_DTM_RESP:
  97. #ifdef ANT_STACK_SUPPORT_REQD
  98. case SER_PKT_TYPE_ANT_RESP:
  99. #endif // ANT_STACK_SUPPORT_REQD
  100. if (m_rsp_wait)
  101. {
  102. m_return_value = m_rsp_dec_handler(p_data, length);
  103. (void)ser_sd_transport_rx_free(p_data);
  104. /* Reset response flag - cmd_write function is pending on it.*/
  105. m_rsp_wait = false;
  106. /* If os handler is set, signal os that response has arrived.*/
  107. if (m_os_rsp_set_handler)
  108. {
  109. m_os_rsp_set_handler();
  110. }
  111. }
  112. else
  113. {
  114. /* Unexpected packet. */
  115. (void)ser_sd_transport_rx_free(p_data);
  116. APP_ERROR_HANDLER(packet_type);
  117. }
  118. break;
  119. #ifdef BLE_STACK_SUPPORT_REQD
  120. case SER_PKT_TYPE_EVT:
  121. /* It is ensured during opening that handler is not NULL. No check needed. */
  122. NRF_LOG_DEBUG("[EVT]: %s ", (uint32_t)ser_dbg_sd_evt_str_get(uint16_decode(&p_data[SER_EVT_ID_POS]))); // p_data points to EVT_ID
  123. m_ble_evt_handler(p_data, length);
  124. break;
  125. #endif // BLE_STACK_SUPPORT_REQD
  126. #ifdef ANT_STACK_SUPPORT_REQD
  127. case SER_PKT_TYPE_ANT_EVT:
  128. /* It is ensured during opening that handler is not NULL. No check needed. */
  129. NRF_LOG_DEBUG("[ANT_EVT_ID]: %s ", (uint32_t)ser_dbg_sd_evt_str_get(uint16_decode(&p_data[SER_EVT_ID_POS]))); // p_data points to EVT_ID
  130. m_ant_evt_handler(p_data, length);
  131. break;
  132. #endif // ANT_STACK_SUPPORT_REQD
  133. default:
  134. (void)ser_sd_transport_rx_free(p_data);
  135. APP_ERROR_HANDLER(packet_type);
  136. break;
  137. }
  138. }
  139. }
  140. /**@brief Function for handling the event from hal_transport.
  141. *
  142. * @param[in] event Event from hal_transport.
  143. */
  144. static void ser_sd_transport_hal_handler(ser_hal_transport_evt_t event)
  145. {
  146. switch (event.evt_type)
  147. {
  148. case SER_HAL_TRANSP_EVT_RX_PKT_RECEIVED:
  149. ser_sd_transport_rx_packet_handler(event.evt_params.rx_pkt_received.p_buffer,
  150. event.evt_params.rx_pkt_received.num_of_bytes);
  151. break;
  152. case SER_HAL_TRANSP_EVT_RX_PKT_RECEIVING:
  153. if (m_rx_notify_handler)
  154. {
  155. m_rx_notify_handler();
  156. }
  157. break;
  158. case SER_HAL_TRANSP_EVT_TX_PKT_SENT:
  159. if (ser_app_power_system_off_get() == true)
  160. {
  161. ser_app_power_system_off_enter();
  162. }
  163. break;
  164. case SER_HAL_TRANSP_EVT_PHY_ERROR:
  165. if (m_rsp_wait)
  166. {
  167. m_return_value = NRF_ERROR_INTERNAL;
  168. /* Reset response flag - cmd_write function is pending on it.*/
  169. m_rsp_wait = false;
  170. /* If os handler is set, signal os that response has arrived.*/
  171. if (m_os_rsp_set_handler)
  172. {
  173. m_os_rsp_set_handler();
  174. }
  175. }
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. uint32_t ser_sd_transport_open(ser_sd_transport_evt_handler_t ble_evt_handler,
  182. ser_sd_transport_evt_handler_t ant_evt_handler,
  183. ser_sd_transport_rsp_wait_handler_t os_rsp_wait_handler,
  184. ser_sd_transport_rsp_set_handler_t os_rsp_set_handler,
  185. ser_sd_transport_rx_notification_handler_t rx_not_handler)
  186. {
  187. m_os_rsp_wait_handler = os_rsp_wait_handler;
  188. m_os_rsp_set_handler = os_rsp_set_handler;
  189. m_rx_notify_handler = rx_not_handler;
  190. m_ot_rsp_wait_handler = NULL;
  191. #ifdef ANT_STACK_SUPPORT_REQD
  192. m_ant_evt_handler = ant_evt_handler;
  193. if (m_ant_evt_handler == NULL)
  194. {
  195. return NRF_ERROR_INVALID_PARAM;
  196. }
  197. #else
  198. UNUSED_PARAMETER(ant_evt_handler);
  199. #endif // ANT_STACK_SUPPORT_REQD
  200. #ifdef BLE_STACK_SUPPORT_REQD
  201. m_ble_evt_handler = ble_evt_handler;
  202. if (m_ble_evt_handler == NULL)
  203. {
  204. return NRF_ERROR_INVALID_PARAM;
  205. }
  206. #else
  207. UNUSED_PARAMETER(ble_evt_handler);
  208. #endif // BLE_STACK_SUPPORT_REQD
  209. return ser_hal_transport_open(ser_sd_transport_hal_handler);
  210. }
  211. uint32_t ser_sd_transport_close(void)
  212. {
  213. #ifdef ANT_STACK_SUPPORT_REQD
  214. m_ant_evt_handler = NULL;
  215. #endif // ANT_STACK_SUPPORT_REQD
  216. #ifdef BLE_STACK_SUPPORT_REQD
  217. m_ble_evt_handler = NULL;
  218. #endif // BLE_STACK_SUPPORT_REQD
  219. m_os_rsp_wait_handler = NULL;
  220. m_os_rsp_set_handler = NULL;
  221. m_ot_rsp_wait_handler = NULL;
  222. ser_hal_transport_close();
  223. return NRF_SUCCESS;
  224. }
  225. uint32_t ser_sd_transport_ot_rsp_wait_handler_set(ser_sd_transport_rsp_wait_handler_t handler)
  226. {
  227. m_ot_rsp_wait_handler = handler;
  228. return NRF_SUCCESS;
  229. }
  230. bool ser_sd_transport_is_busy(void)
  231. {
  232. return m_rsp_wait;
  233. }
  234. uint32_t ser_sd_transport_tx_alloc(uint8_t * * pp_data, uint16_t * p_len)
  235. {
  236. uint32_t err_code;
  237. if (m_rsp_wait)
  238. {
  239. err_code = NRF_ERROR_BUSY;
  240. }
  241. else
  242. {
  243. err_code = ser_hal_transport_tx_pkt_alloc(pp_data, p_len);
  244. }
  245. return err_code;
  246. }
  247. uint32_t ser_sd_transport_tx_free(uint8_t * p_data)
  248. {
  249. return ser_hal_transport_tx_pkt_free(p_data);
  250. }
  251. uint32_t ser_sd_transport_rx_free(uint8_t * p_data)
  252. {
  253. p_data -= SER_PKT_TYPE_SIZE;
  254. return ser_hal_transport_rx_pkt_free(p_data);
  255. }
  256. uint32_t ser_sd_transport_cmd_write(const uint8_t * p_buffer,
  257. uint16_t length,
  258. ser_sd_transport_rsp_handler_t cmd_rsp_decode_callback)
  259. {
  260. uint32_t err_code = NRF_SUCCESS;
  261. m_rsp_wait = true;
  262. m_rsp_dec_handler = cmd_rsp_decode_callback;
  263. err_code = ser_hal_transport_tx_pkt_send(p_buffer, length);
  264. APP_ERROR_CHECK(err_code);
  265. /* Execute callback for response decoding only if one was provided.*/
  266. if ((err_code == NRF_SUCCESS) && cmd_rsp_decode_callback)
  267. {
  268. if (m_ot_rsp_wait_handler)
  269. {
  270. m_ot_rsp_wait_handler();
  271. m_ot_rsp_wait_handler = NULL;
  272. }
  273. m_os_rsp_wait_handler();
  274. err_code = m_return_value;
  275. }
  276. else
  277. {
  278. m_rsp_wait = false;
  279. }
  280. NRF_LOG_DEBUG("[SD_CALL]:%s, err_code= 0x%X", (uint32_t)ser_dbg_sd_call_str_get(p_buffer[1]), err_code);
  281. return err_code;
  282. }