ser_conn_handlers.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include <string.h>
  13. #include "app_error.h"
  14. #include "app_scheduler.h"
  15. #include "ser_config.h"
  16. #include "ser_conn_handlers.h"
  17. #include "ser_conn_event_encoder.h"
  18. #include "ser_conn_pkt_decoder.h"
  19. #include "ser_conn_dtm_cmd_decoder.h"
  20. /** @file
  21. *
  22. * @defgroup ser_conn_handlers Events handlers for the Connectivity Chip.
  23. * @{
  24. * @ingroup sdk_lib_serialization
  25. *
  26. * @brief A module to handle the Connectivity application events.
  27. *
  28. * @details There are two types of events in the Connectivity application: BLE events generated by
  29. * the SoftDevice and events generated by the HAL Transport layer.
  30. */
  31. /** Parameters of a received packet. */
  32. static ser_hal_transport_evt_rx_pkt_received_params_t m_rx_pkt_received_params;
  33. /** Indicator of received packet that should be process. */
  34. static bool m_rx_pkt_to_process = false;
  35. void ser_conn_hal_transport_event_handle(ser_hal_transport_evt_t event)
  36. {
  37. switch (event.evt_type)
  38. {
  39. case SER_HAL_TRANSP_EVT_TX_PKT_SENT:
  40. {
  41. /* SoftDevice event or response to received packet was sent, so unblock the application
  42. * scheduler to process a next event. */
  43. app_sched_resume();
  44. /* Check if chip is ready to enter DTM mode. */
  45. ser_conn_is_ready_to_enter_dtm();
  46. break;
  47. }
  48. case SER_HAL_TRANSP_EVT_RX_PKT_RECEIVING:
  49. {
  50. /* The connectivity side has started receiving a packet. Temporary block processing
  51. * SoftDevice events. It is going to be unblocked when a response for the packet will
  52. * be sent. This prevents communication block. */
  53. app_sched_pause();
  54. break;
  55. }
  56. case SER_HAL_TRANSP_EVT_RX_PKT_RECEIVED:
  57. {
  58. /* We can NOT add received packets as events to the application scheduler queue because
  59. * received packets have to be processed before SoftDevice events but the scheduler
  60. * queue do not have priorities. */
  61. memcpy(&m_rx_pkt_received_params, &event.evt_params.rx_pkt_received,
  62. sizeof (ser_hal_transport_evt_rx_pkt_received_params_t));
  63. m_rx_pkt_to_process = true;
  64. break;
  65. }
  66. case SER_HAL_TRANSP_EVT_RX_PKT_DROPPED:
  67. {
  68. APP_ERROR_CHECK(SER_WARNING_CODE);
  69. break;
  70. }
  71. case SER_HAL_TRANSP_EVT_PHY_ERROR:
  72. {
  73. APP_ERROR_CHECK(NRF_ERROR_FORBIDDEN);
  74. break;
  75. }
  76. default:
  77. {
  78. /* do nothing */
  79. break;
  80. }
  81. }
  82. }
  83. uint32_t ser_conn_rx_process(void)
  84. {
  85. uint32_t err_code = NRF_SUCCESS;
  86. if (m_rx_pkt_to_process)
  87. {
  88. /* No critical section needed on m_rx_pkt_to_process parameter because it is not possible
  89. * to get next packet before sending a response. */
  90. m_rx_pkt_to_process = false;
  91. err_code = ser_conn_received_pkt_process(&m_rx_pkt_received_params);
  92. }
  93. return err_code;
  94. }
  95. void ser_conn_ble_event_handle(ble_evt_t * p_ble_evt)
  96. {
  97. uint32_t err_code = NRF_SUCCESS;
  98. /* We can NOT encode and send BLE events here. SoftDevice handler implemented in
  99. * softdevice_handler.c pull all available BLE events at once but we need to reschedule between
  100. * encoding and sending every BLE event because sending a response on received packet has higher
  101. * priority than sending a BLE event. Solution for that is to put BLE events into application
  102. * scheduler queue to be processed at a later time. */
  103. err_code = app_sched_event_put(p_ble_evt, sizeof (ble_evt_hdr_t) + p_ble_evt->header.evt_len,
  104. ser_conn_ble_event_encoder);
  105. APP_ERROR_CHECK(err_code);
  106. }
  107. /** @} */