ser_conn_handlers.c 7.3 KB

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