ser_conn_handlers.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  41. * @addtogroup ser_conn Connectivity application code
  42. * @ingroup ble_sdk_lib_serialization
  43. */
  44. /** @file
  45. *
  46. * @defgroup ser_conn_handlers Events handlers in the Connectivity Chip
  47. * @{
  48. * @ingroup ser_conn
  49. *
  50. * @brief Events handlers used to process high level events in the connectivity application.
  51. *
  52. * @details This file contains functions: processing the HAL Transport layer events, processing BLE
  53. * SoftDevice events, starting processing received packets.
  54. */
  55. #ifndef SER_CONN_HANDLERS_H__
  56. #define SER_CONN_HANDLERS_H__
  57. #include <stdint.h>
  58. #include "nordic_common.h"
  59. #include "app_util.h"
  60. #include "ser_hal_transport.h"
  61. #ifdef BLE_STACK_SUPPORT_REQD
  62. #include "ble.h"
  63. #include "nrf_sdh_ble.h"
  64. #endif // BLE_STACK_SUPPORT_REQD
  65. #ifdef ANT_STACK_SUPPORT_REQD
  66. #include "nrf_sdh_ant.h"
  67. #endif // ANT_STACK_SUPPORT_REQD
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71. /** Maximum number of events in the application scheduler queue. */
  72. #ifdef S112
  73. #define SER_CONN_SCHED_QUEUE_SIZE 8u
  74. #elif defined(SER_PHY_HCI_USB_CDC)
  75. #define SER_CONN_SCHED_QUEUE_SIZE 64u
  76. #else
  77. #define SER_CONN_SCHED_QUEUE_SIZE 16u
  78. #endif
  79. /** Maximum size of events data in the application scheduler queue aligned to 32 bits - this is
  80. * size of the buffers of the SoftDevice handler, which stores events pulled from the SoftDevice.
  81. */
  82. #if defined(BLE_STACK_SUPPORT_REQD) && defined(ANT_STACK_SUPPORT_REQD)
  83. #define STACK_EVENT_MAX_SIZE MAX(NRF_SDH_BLE_EVT_BUF_SIZE, NRF_SDH_ANT_EVT_BUF_SIZE)
  84. #elif defined(BLE_STACK_SUPPORT_REQD)
  85. #define STACK_EVENT_MAX_SIZE NRF_SDH_BLE_EVT_BUF_SIZE
  86. #elif defined(ANT_STACK_SUPPORT_REQD)
  87. #define STACK_EVENT_MAX_SIZE NRF_SDH_ANT_EVT_BUF_SIZE
  88. #endif
  89. #define SER_CONN_SCHED_MAX_EVENT_DATA_SIZE ((CEIL_DIV(MAX(STACK_EVENT_MAX_SIZE, \
  90. sizeof(uint32_t)), \
  91. sizeof(uint32_t))) * \
  92. sizeof(uint32_t))
  93. /** @brief Prototype for function called when there is no free TX buffer and system is blocked */
  94. typedef void (*ser_conn_on_no_mem_t)(void);
  95. /**
  96. * @brief A function for setting handler which should be called when serialization
  97. * is blocked waiting for TX buffer.
  98. *
  99. * @param handler Handler to be called whenever serialization failed to allocate TX buffer
  100. *
  101. */
  102. void ser_conn_on_no_mem_handler_set(ser_conn_on_no_mem_t handler);
  103. /**
  104. * @brief A function called when TX buffer allocation failed. Serialization is always allocating TX
  105. * buffer in main context expecting that it will be freed from interrupt context.
  106. */
  107. void ser_conn_on_no_mem_handler(void);
  108. /**@brief A function for processing the HAL Transport layer events.
  109. *
  110. * @param[in] event HAL Transport layer event.
  111. */
  112. void ser_conn_hal_transport_event_handle(ser_hal_transport_evt_t event);
  113. /**@brief A function to call the function to process a packet when it is fully received.
  114. *
  115. * @retval NRF_SUCCESS Operation success.
  116. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  117. * @retval NRF_ERROR_INTERNAL Operation failure. Internal error ocurred.
  118. */
  119. uint32_t ser_conn_rx_process(void);
  120. #ifdef BLE_STACK_SUPPORT_REQD
  121. /**@brief Reset serialization state */
  122. void ser_conn_reset(void);
  123. /**@brief A function for processing BLE SoftDevice events.
  124. *
  125. * @details BLE events are put into application scheduler queue to be processed at a later time.
  126. *
  127. * @param[in] p_ble_evt A pointer to a BLE event.
  128. * @param[in] p_context A parameter to the handler. Not used.
  129. */
  130. void ser_conn_ble_event_handle(ble_evt_t const * p_ble_evt, void * p_context);
  131. #endif // BLE_STACK_SUPPORT_REQD
  132. #ifdef ANT_STACK_SUPPORT_REQD
  133. /**@brief A function for processing ANT SoftDevice events.
  134. *
  135. * @details ANT events are put into application scheduler queue to be processed at a later time.
  136. *
  137. * @param[in] p_ant_evt A pointer to a BLE event.
  138. * @param[in] p_context A parameter to the handler. Not used.
  139. */
  140. void ser_conn_ant_event_handle(ant_evt_t * p_ant_evt, void * p_context);
  141. #endif // ANT_STACK_SUPPORT_REQD
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* SER_CONN_HANDLERS_H__ */
  146. /** @} */