ser_sd_transport.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_app Application side code
  42. * @ingroup ble_sdk_lib_serialization
  43. * @brief SoftDevice handler and transport on the application side.
  44. */
  45. /** @file
  46. *
  47. * @defgroup ser_sd_transport Serialization SoftDevice Transport
  48. * @{
  49. * @ingroup ser_app
  50. *
  51. * @brief Serialization SoftDevice Transport on application side.
  52. *
  53. * @details This file contains declarations of functions and definitions of data structures and
  54. * identifiers (typedef enum) used as API of the serialization of SoftDevice. This layer
  55. * ensures atomic nature of SoftDevice calls (command and waiting for response). Packet
  56. * type field of incoming packets is handled in this layer - responses are handled by
  57. * ser_sd_transport (using response decoder handler provided for each SoftDevice call), but
  58. * events are forwarded to the user so it is up to the user to free the RX buffer.
  59. *
  60. */
  61. #ifndef SER_SD_TRANSPORT_H_
  62. #define SER_SD_TRANSPORT_H_
  63. #include <stdint.h>
  64. #include <stdbool.h>
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. typedef void (*ser_sd_transport_evt_handler_t)(uint8_t * p_buffer, uint16_t length);
  69. typedef void (*ser_sd_transport_rsp_wait_handler_t)(void);
  70. typedef void (*ser_sd_transport_rsp_set_handler_t)(void);
  71. typedef void (*ser_sd_transport_rx_notification_handler_t)(void);
  72. typedef uint32_t (*ser_sd_transport_rsp_handler_t)(const uint8_t * p_buffer, uint16_t length);
  73. /**@brief Function for opening the module.
  74. *
  75. * @note 'Wait for response' and 'Response set' callbacks can be set in RTOS environment.
  76. * It enables rescheduling while waiting for the Connectivity Chip response. In a nonOS environment,
  77. * usually 'Wait for response' will only be used for handling incoming events or forcing the
  78. * application to low power mode.
  79. *
  80. * @param[in] ble_evt_handler Handler to be called when ble event packet is received.
  81. * @param[in] ant_evt_handler Handler to be called when ant event packet is received.
  82. * @param[in] os_rsp_wait_handler Handler to be called after the request is send. It should
  83. * implement a 'Wait for signal' functionality in an OS environment.
  84. * @param[in] os_rsp_set_handler Handler to be called after response reception. It should
  85. * implement a 'Signal Set' functionality in an OS environment.
  86. * @param[in] rx_not_handler Handler to be called after the transport layer notifies that
  87. * an incoming RX packet is detected.
  88. *
  89. * @retval NRF_SUCCESS Operation success.
  90. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  91. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Parameter propagated from ser_hal_transport
  92. * opening or timer creation.
  93. * @retval NRF_ERROR_INVALID_STATE Operation failure. Parameter propagated from ser_hal_transport
  94. * opening or timer creation.
  95. * @retval NRF_ERROR_INTERNAL Operation failure. Parameter propagated from ser_hal_transport
  96. * opening or timer creation.
  97. * @retval NRF_ERROR_NO_MEM Operation failure. Parameter propagated from timer creation.
  98. */
  99. uint32_t ser_sd_transport_open(ser_sd_transport_evt_handler_t ble_evt_handler,
  100. ser_sd_transport_evt_handler_t ant_evt_handler,
  101. ser_sd_transport_rsp_wait_handler_t os_rsp_wait_handler,
  102. ser_sd_transport_rsp_set_handler_t os_rsp_set_handler,
  103. ser_sd_transport_rx_notification_handler_t rx_not_handler);
  104. /**@brief Function setting a 'One Time' handler to be called between sending the next request packet and
  105. * receiving the response packet.
  106. * @note It is intended to be used in a nonOS environment to implement concurrency.
  107. * @note It is a 'One Time' handler meaning that it is valid only for the next SoftDevice call processing.
  108. *
  109. *
  110. * @param[in] wait_handler Handler to be called after the request packet is sent.
  111. *
  112. * @retval NRF_SUCCESS Operation success.
  113. */
  114. uint32_t ser_sd_transport_ot_rsp_wait_handler_set(ser_sd_transport_rsp_wait_handler_t wait_handler);
  115. /**@brief Function for closing the module.
  116. *
  117. * @retval NRF_SUCCESS Operation success.
  118. */
  119. uint32_t ser_sd_transport_close(void);
  120. /**@brief Function for allocating a TX packet to be used for request command.
  121. *
  122. * @param[out] pp_data Pointer to the data pointer to be set to point to allocated buffer.
  123. * @param[out] p_len Pointer to allocated buffer length.
  124. *
  125. * @retval NRF_SUCCESS Operation success.
  126. */
  127. uint32_t ser_sd_transport_tx_alloc(uint8_t * * pp_data, uint16_t * p_len);
  128. /**@brief Function for freeing a TX packet.
  129. *
  130. * @note Function should be called once the command is processed.
  131. *
  132. * @param[out] p_data Pointer to the allocated TX buffer.
  133. *
  134. * @retval NRF_SUCCESS Operation success.
  135. */
  136. uint32_t ser_sd_transport_tx_free(uint8_t * p_data);
  137. /**@brief Function for freeing an RX event packet.
  138. *
  139. * @note Function should be called once the SoftDevice event buffer is processed.
  140. *
  141. * @param[out] p_data Pointer to the allocated RX buffer.
  142. *
  143. * @retval NRF_SUCCESS Operation success.
  144. */
  145. uint32_t ser_sd_transport_rx_free(uint8_t * p_data);
  146. /**@brief Function for checking if module is busy waiting for response from connectivity side.
  147. *
  148. * @retval true Module busy. Cannot accept the next command.
  149. * @retval false Module not busy. Can accept next the command.
  150. */
  151. bool ser_sd_transport_is_busy(void);
  152. /**@brief Function for handling a SoftDevice command.
  153. *
  154. * @note Function blocks task context until response is received and processed.
  155. * @note Non-blocking functionality can be achieved using OS handlers or a 'One Time' handler
  156. * @warning Function should not be called from interrupt context, which would block switching to
  157. * serial port interrupt.
  158. *
  159. * @param[in] p_buffer Pointer to command.
  160. * @param[in] length Pointer to allocated buffer length.
  161. * @param[in] cmd_resp_decode_callback Pointer to a function for decoding the response packet.
  162. *
  163. * @retval NRF_SUCCESS Operation success.
  164. */
  165. uint32_t ser_sd_transport_cmd_write(const uint8_t * p_buffer,
  166. uint16_t length,
  167. ser_sd_transport_rsp_handler_t cmd_resp_decode_callback);
  168. #ifdef __cplusplus
  169. }
  170. #endif
  171. #endif /* SER_SD_TRANSPORT_H_ */
  172. /** @} */