ser_conn_handlers.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Copyright (c) 2014 - 2019, 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 <string.h>
  41. #include "app_error.h"
  42. #include "app_scheduler.h"
  43. #include "ser_config.h"
  44. #include "ser_conn_handlers.h"
  45. #include "ser_conn_event_encoder.h"
  46. #include "ser_conn_pkt_decoder.h"
  47. #include "ser_conn_dtm_cmd_decoder.h"
  48. #include "nrf_sdh.h"
  49. #ifdef BLE_STACK_SUPPORT_REQD
  50. #include "conn_ble_gap_sec_keys.h"
  51. #include "conn_ble_user_mem.h"
  52. #include "conn_ble_l2cap_sdu_pool.h"
  53. #endif
  54. /** @file
  55. *
  56. * @defgroup ser_conn_handlers Events handlers for the Connectivity Chip.
  57. * @{
  58. * @ingroup sdk_lib_serialization
  59. *
  60. * @brief A module to handle the Connectivity application events.
  61. *
  62. * @details There are two types of events in the Connectivity application: BLE events generated by
  63. * the SoftDevice and events generated by the HAL Transport layer.
  64. */
  65. /** Parameters of a received packet. */
  66. static ser_hal_transport_evt_rx_pkt_received_params_t m_rx_pkt_received_params;
  67. /** Indicator of received packet that should be process. */
  68. static bool m_rx_pkt_to_process = false;
  69. static ser_conn_on_no_mem_t m_on_no_mem_handler;
  70. void ser_conn_on_no_mem_handler_set(ser_conn_on_no_mem_t handler)
  71. {
  72. m_on_no_mem_handler = handler;
  73. }
  74. void ser_conn_on_no_mem_handler(void)
  75. {
  76. if (m_on_no_mem_handler)
  77. {
  78. m_on_no_mem_handler();
  79. }
  80. }
  81. void ser_conn_hal_transport_event_handle(ser_hal_transport_evt_t event)
  82. {
  83. switch (event.evt_type)
  84. {
  85. case SER_HAL_TRANSP_EVT_TX_PKT_SENT:
  86. {
  87. /* SoftDevice event or response to received packet was sent, so unblock the application
  88. * scheduler to process a next event. */
  89. app_sched_resume();
  90. /* Check if chip is ready to enter DTM mode. */
  91. ser_conn_is_ready_to_enter_dtm();
  92. break;
  93. }
  94. case SER_HAL_TRANSP_EVT_RX_PKT_RECEIVING:
  95. {
  96. /* The connectivity side has started receiving a packet. Temporary block processing
  97. * SoftDevice events. It is going to be unblocked when a response for the packet will
  98. * be sent. This prevents communication block. */
  99. app_sched_pause();
  100. break;
  101. }
  102. case SER_HAL_TRANSP_EVT_RX_PKT_RECEIVED:
  103. {
  104. /* We can NOT add received packets as events to the application scheduler queue because
  105. * received packets have to be processed before SoftDevice events but the scheduler
  106. * queue do not have priorities. */
  107. memcpy(&m_rx_pkt_received_params, &event.evt_params.rx_pkt_received,
  108. sizeof (ser_hal_transport_evt_rx_pkt_received_params_t));
  109. m_rx_pkt_to_process = true;
  110. break;
  111. }
  112. case SER_HAL_TRANSP_EVT_RX_PKT_DROPPED:
  113. {
  114. APP_ERROR_CHECK(SER_WARNING_CODE);
  115. break;
  116. }
  117. case SER_HAL_TRANSP_EVT_PHY_ERROR:
  118. {
  119. APP_ERROR_CHECK(NRF_ERROR_FORBIDDEN);
  120. break;
  121. }
  122. default:
  123. {
  124. /* do nothing */
  125. break;
  126. }
  127. }
  128. }
  129. uint32_t ser_conn_rx_process(void)
  130. {
  131. uint32_t err_code = NRF_SUCCESS;
  132. if (m_rx_pkt_to_process)
  133. {
  134. /* No critical section needed on m_rx_pkt_to_process parameter because it is not possible
  135. * to get next packet before sending a response. */
  136. m_rx_pkt_to_process = false;
  137. err_code = ser_conn_received_pkt_process(&m_rx_pkt_received_params);
  138. }
  139. return err_code;
  140. }
  141. #ifdef BLE_STACK_SUPPORT_REQD
  142. void ser_conn_reset(void)
  143. {
  144. conn_ble_gap_sec_keys_init();
  145. conn_ble_user_mem_init();
  146. #ifndef S112
  147. conn_ble_l2cap_sdu_pool_init();
  148. #endif
  149. }
  150. NRF_SDH_BLE_OBSERVER(m_ble_observer, 0, ser_conn_ble_event_handle, NULL);
  151. void ser_conn_ble_event_handle(ble_evt_t const * p_ble_evt, void * p_context)
  152. {
  153. uint32_t err_code = NRF_SUCCESS;
  154. /* We can NOT encode and send BLE events here. SoftDevice handler implemented in
  155. * softdevice_handler.c pull all available BLE events at once but we need to reschedule between
  156. * encoding and sending every BLE event because sending a response on received packet has higher
  157. * priority than sending a BLE event. Solution for that is to put BLE events into application
  158. * scheduler queue to be processed at a later time. */
  159. err_code = app_sched_event_put(p_ble_evt, p_ble_evt->header.evt_len,
  160. ser_conn_ble_event_encoder);
  161. APP_ERROR_CHECK(err_code);
  162. uint16_t free_space = app_sched_queue_space_get();
  163. /* If scheduler queue is full in 75% (arbitrary value) stop pulling new events.
  164. * Some space in scheduler is left for other possible events (e.g. events from USB CDC ACM)
  165. */
  166. if (free_space < (SER_CONN_SCHED_QUEUE_SIZE / 4))
  167. {
  168. nrf_sdh_suspend();
  169. }
  170. }
  171. #endif // BLE_STACK_SUPPORT_REQD
  172. #ifdef ANT_STACK_SUPPORT_REQD
  173. NRF_SDH_ANT_OBSERVER(m_ant_observer, 0, ser_conn_ant_event_handle, NULL);
  174. void ser_conn_ant_event_handle(ant_evt_t * p_ant_evt, void * p_context)
  175. {
  176. uint32_t err_code = NRF_SUCCESS;
  177. /* We can NOT encode and send ANT events here. SoftDevice handler implemented in
  178. * softdevice_handler.c pull all available ANT events at once but we need to reschedule between
  179. * encoding and sending every ANT event because sending a response on received packet has higher
  180. * priority than sending an ANT event. Solution for that is to put ANT events into application
  181. * scheduler queue to be processed at a later time. */
  182. err_code = app_sched_event_put(p_ant_evt, sizeof (ant_evt_t),
  183. ser_conn_ant_event_encoder);
  184. APP_ERROR_CHECK(err_code);
  185. uint16_t free_space = app_sched_queue_space_get();
  186. if (!free_space)
  187. {
  188. // Queue is full. Do not pull new events.
  189. nrf_sdh_suspend();
  190. }
  191. }
  192. #endif // ANT_STACK_SUPPORT_REQD
  193. /** @} */