ble_nus.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Copyright (c) 2012 - 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. /**@file
  41. *
  42. * @defgroup ble_nus Nordic UART Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Nordic UART Service implementation.
  46. *
  47. * @details The Nordic UART Service is a simple GATT-based service with TX and RX characteristics.
  48. * Data received from the peer is passed to the application, and the data received
  49. * from the application of this service is sent to the peer as Handle Value
  50. * Notifications. This module demonstrates how to implement a custom GATT-based
  51. * service and characteristics using the SoftDevice. The service
  52. * is used by the application to send and receive ASCII text strings to and from the
  53. * peer.
  54. *
  55. * @note The application must register this module as BLE event observer using the
  56. * NRF_SDH_BLE_OBSERVER macro. Example:
  57. * @code
  58. * ble_nus_t instance;
  59. * NRF_SDH_BLE_OBSERVER(anything, BLE_NUS_BLE_OBSERVER_PRIO,
  60. * ble_nus_on_ble_evt, &instance);
  61. * @endcode
  62. */
  63. #ifndef BLE_NUS_H__
  64. #define BLE_NUS_H__
  65. #include <stdint.h>
  66. #include <stdbool.h>
  67. #include "sdk_config.h"
  68. #include "ble.h"
  69. #include "ble_srv_common.h"
  70. #include "nrf_sdh_ble.h"
  71. #include "ble_link_ctx_manager.h"
  72. #ifdef __cplusplus
  73. extern "C" {
  74. #endif
  75. /**@brief Macro for defining a ble_nus instance.
  76. *
  77. * @param _name Name of the instance.
  78. * @param[in] _nus_max_clients Maximum number of NUS clients connected at a time.
  79. * @hideinitializer
  80. */
  81. #define BLE_NUS_DEF(_name, _nus_max_clients) \
  82. BLE_LINK_CTX_MANAGER_DEF(CONCAT_2(_name, _link_ctx_storage), \
  83. (_nus_max_clients), \
  84. sizeof(ble_nus_client_context_t)); \
  85. static ble_nus_t _name = \
  86. { \
  87. .p_link_ctx_storage = &CONCAT_2(_name, _link_ctx_storage) \
  88. }; \
  89. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  90. BLE_NUS_BLE_OBSERVER_PRIO, \
  91. ble_nus_on_ble_evt, \
  92. &_name)
  93. #define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
  94. #define OPCODE_LENGTH 1
  95. #define HANDLE_LENGTH 2
  96. /**@brief Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  97. #if defined(NRF_SDH_BLE_GATT_MAX_MTU_SIZE) && (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 0)
  98. #define BLE_NUS_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
  99. #else
  100. #define BLE_NUS_MAX_DATA_LEN (BLE_GATT_MTU_SIZE_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH)
  101. #warning NRF_SDH_BLE_GATT_MAX_MTU_SIZE is not defined.
  102. #endif
  103. /**@brief Nordic UART Service event types. */
  104. typedef enum
  105. {
  106. BLE_NUS_EVT_RX_DATA, /**< Data received. */
  107. BLE_NUS_EVT_TX_RDY, /**< Service is ready to accept new data to be transmitted. */
  108. BLE_NUS_EVT_COMM_STARTED, /**< Notification has been enabled. */
  109. BLE_NUS_EVT_COMM_STOPPED, /**< Notification has been disabled. */
  110. } ble_nus_evt_type_t;
  111. /* Forward declaration of the ble_nus_t type. */
  112. typedef struct ble_nus_s ble_nus_t;
  113. /**@brief Nordic UART Service @ref BLE_NUS_EVT_RX_DATA event data.
  114. *
  115. * @details This structure is passed to an event when @ref BLE_NUS_EVT_RX_DATA occurs.
  116. */
  117. typedef struct
  118. {
  119. uint8_t const * p_data; /**< A pointer to the buffer with received data. */
  120. uint16_t length; /**< Length of received data. */
  121. } ble_nus_evt_rx_data_t;
  122. /**@brief Nordic UART Service client context structure.
  123. *
  124. * @details This structure contains state context related to hosts.
  125. */
  126. typedef struct
  127. {
  128. bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/
  129. } ble_nus_client_context_t;
  130. /**@brief Nordic UART Service event structure.
  131. *
  132. * @details This structure is passed to an event coming from service.
  133. */
  134. typedef struct
  135. {
  136. ble_nus_evt_type_t type; /**< Event type. */
  137. ble_nus_t * p_nus; /**< A pointer to the instance. */
  138. uint16_t conn_handle; /**< Connection handle. */
  139. ble_nus_client_context_t * p_link_ctx; /**< A pointer to the link context. */
  140. union
  141. {
  142. ble_nus_evt_rx_data_t rx_data; /**< @ref BLE_NUS_EVT_RX_DATA event data. */
  143. } params;
  144. } ble_nus_evt_t;
  145. /**@brief Nordic UART Service event handler type. */
  146. typedef void (* ble_nus_data_handler_t) (ble_nus_evt_t * p_evt);
  147. /**@brief Nordic UART Service initialization structure.
  148. *
  149. * @details This structure contains the initialization information for the service. The application
  150. * must fill this structure and pass it to the service using the @ref ble_nus_init
  151. * function.
  152. */
  153. typedef struct
  154. {
  155. ble_nus_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
  156. } ble_nus_init_t;
  157. /**@brief Nordic UART Service structure.
  158. *
  159. * @details This structure contains status information related to the service.
  160. */
  161. struct ble_nus_s
  162. {
  163. uint8_t uuid_type; /**< UUID type for Nordic UART Service Base UUID. */
  164. uint16_t service_handle; /**< Handle of Nordic UART Service (as provided by the SoftDevice). */
  165. ble_gatts_char_handles_t tx_handles; /**< Handles related to the TX characteristic (as provided by the SoftDevice). */
  166. ble_gatts_char_handles_t rx_handles; /**< Handles related to the RX characteristic (as provided by the SoftDevice). */
  167. blcm_link_ctx_storage_t * const p_link_ctx_storage; /**< Pointer to link context storage with handles of all current connections and its context. */
  168. ble_nus_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
  169. };
  170. /**@brief Function for initializing the Nordic UART Service.
  171. *
  172. * @param[out] p_nus Nordic UART Service structure. This structure must be supplied
  173. * by the application. It is initialized by this function and will
  174. * later be used to identify this particular service instance.
  175. * @param[in] p_nus_init Information needed to initialize the service.
  176. *
  177. * @retval NRF_SUCCESS If the service was successfully initialized. Otherwise, an error code is returned.
  178. * @retval NRF_ERROR_NULL If either of the pointers p_nus or p_nus_init is NULL.
  179. */
  180. uint32_t ble_nus_init(ble_nus_t * p_nus, ble_nus_init_t const * p_nus_init);
  181. /**@brief Function for handling the Nordic UART Service's BLE events.
  182. *
  183. * @details The Nordic UART Service expects the application to call this function each time an
  184. * event is received from the SoftDevice. This function processes the event if it
  185. * is relevant and calls the Nordic UART Service event handler of the
  186. * application if necessary.
  187. *
  188. * @param[in] p_ble_evt Event received from the SoftDevice.
  189. * @param[in] p_context Nordic UART Service structure.
  190. */
  191. void ble_nus_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  192. /**@brief Function for sending a data to the peer.
  193. *
  194. * @details This function sends the input string as an RX characteristic notification to the
  195. * peer.
  196. *
  197. * @param[in] p_nus Pointer to the Nordic UART Service structure.
  198. * @param[in] p_data String to be sent.
  199. * @param[in,out] p_length Pointer Length of the string. Amount of sent bytes.
  200. * @param[in] conn_handle Connection Handle of the destination client.
  201. *
  202. * @retval NRF_SUCCESS If the string was sent successfully. Otherwise, an error code is returned.
  203. */
  204. uint32_t ble_nus_data_send(ble_nus_t * p_nus,
  205. uint8_t * p_data,
  206. uint16_t * p_length,
  207. uint16_t conn_handle);
  208. #ifdef __cplusplus
  209. }
  210. #endif
  211. #endif // BLE_NUS_H__
  212. /** @} */