ble_nus_c.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_c Nordic UART Service Client
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Nordic UART Service Client module.
  46. *
  47. * @details This module contains the APIs and types exposed by the Nordic UART Service Client
  48. * module. The application can use these APIs and types to perform the discovery of
  49. * the Nordic UART Service at the peer and to interact with it.
  50. *
  51. * @note The application must register this module as the BLE event observer by using the
  52. * NRF_SDH_BLE_OBSERVER macro. Example:
  53. * @code
  54. * ble_nus_c_t instance;
  55. * NRF_SDH_BLE_OBSERVER(anything, BLE_NUS_C_BLE_OBSERVER_PRIO,
  56. * ble_nus_c_on_ble_evt, &instance);
  57. * @endcode
  58. *
  59. */
  60. #ifndef BLE_NUS_C_H__
  61. #define BLE_NUS_C_H__
  62. #include <stdint.h>
  63. #include <stdbool.h>
  64. #include "ble.h"
  65. #include "ble_gatt.h"
  66. #include "ble_db_discovery.h"
  67. #include "ble_srv_common.h"
  68. #include "nrf_ble_gq.h"
  69. #include "nrf_sdh_ble.h"
  70. #include "sdk_config.h"
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. /**@brief Macro for defining a ble_nus_c instance.
  75. *
  76. * @param _name Name of the instance.
  77. * @hideinitializer
  78. */
  79. #define BLE_NUS_C_DEF(_name) \
  80. static ble_nus_c_t _name; \
  81. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  82. BLE_NUS_C_BLE_OBSERVER_PRIO, \
  83. ble_nus_c_on_ble_evt, &_name)
  84. /** @brief Macro for defining multiple ble_nus_c instances.
  85. *
  86. * @param _name Name of the array of instances.
  87. * @param _cnt Number of instances to define.
  88. * @hideinitializer
  89. */
  90. #define BLE_NUS_C_ARRAY_DEF(_name, _cnt) \
  91. static ble_nus_c_t _name[_cnt]; \
  92. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  93. BLE_NUS_C_BLE_OBSERVER_PRIO, \
  94. ble_nus_c_on_ble_evt, &_name, _cnt)
  95. #define NUS_BASE_UUID {{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E}} /**< Used vendor-specific UUID. */
  96. #define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
  97. #define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
  98. #define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
  99. #define OPCODE_LENGTH 1
  100. #define HANDLE_LENGTH 2
  101. /**@brief Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  102. #if defined(NRF_SDH_BLE_GATT_MAX_MTU_SIZE) && (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 0)
  103. #define BLE_NUS_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
  104. #else
  105. #define BLE_NUS_MAX_DATA_LEN (BLE_GATT_MTU_SIZE_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH)
  106. #warning NRF_SDH_BLE_GATT_MAX_MTU_SIZE is not defined.
  107. #endif
  108. /**@brief NUS Client event type. */
  109. typedef enum
  110. {
  111. BLE_NUS_C_EVT_DISCOVERY_COMPLETE, /**< Event indicating that the NUS service and its characteristics were found. */
  112. BLE_NUS_C_EVT_NUS_TX_EVT, /**< Event indicating that the central received something from a peer. */
  113. BLE_NUS_C_EVT_DISCONNECTED /**< Event indicating that the NUS server disconnected. */
  114. } ble_nus_c_evt_type_t;
  115. /**@brief Handles on the connected peer device needed to interact with it. */
  116. typedef struct
  117. {
  118. uint16_t nus_tx_handle; /**< Handle of the NUS TX characteristic, as provided by a discovery. */
  119. uint16_t nus_tx_cccd_handle; /**< Handle of the CCCD of the NUS TX characteristic, as provided by a discovery. */
  120. uint16_t nus_rx_handle; /**< Handle of the NUS RX characteristic, as provided by a discovery. */
  121. } ble_nus_c_handles_t;
  122. /**@brief Structure containing the NUS event data received from the peer. */
  123. typedef struct
  124. {
  125. ble_nus_c_evt_type_t evt_type;
  126. uint16_t conn_handle;
  127. uint16_t max_data_len;
  128. uint8_t * p_data;
  129. uint16_t data_len;
  130. ble_nus_c_handles_t handles; /**< Handles on which the Nordic UART service characteristics were discovered on the peer device. This is filled if the evt_type is @ref BLE_NUS_C_EVT_DISCOVERY_COMPLETE.*/
  131. } ble_nus_c_evt_t;
  132. // Forward declaration of the ble_nus_t type.
  133. typedef struct ble_nus_c_s ble_nus_c_t;
  134. /**@brief Event handler type.
  135. *
  136. * @details This is the type of the event handler that is to be provided by the application
  137. * of this module to receive events.
  138. */
  139. typedef void (* ble_nus_c_evt_handler_t)(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t const * p_evt);
  140. /**@brief NUS Client structure. */
  141. struct ble_nus_c_s
  142. {
  143. uint8_t uuid_type; /**< UUID type. */
  144. uint16_t conn_handle; /**< Handle of the current connection. Set with @ref ble_nus_c_handles_assign when connected. */
  145. ble_nus_c_handles_t handles; /**< Handles on the connected peer device needed to interact with it. */
  146. ble_nus_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the NUS. */
  147. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  148. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  149. };
  150. /**@brief NUS Client initialization structure. */
  151. typedef struct
  152. {
  153. ble_nus_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the NUS. */
  154. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  155. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  156. } ble_nus_c_init_t;
  157. /**@brief Function for initializing the Nordic UART client module.
  158. *
  159. * @details This function registers with the Database Discovery module
  160. * for the NUS. The Database Discovery module looks for the presence
  161. * of a NUS instance at the peer when a discovery is started.
  162. *
  163. * @param[in] p_ble_nus_c Pointer to the NUS client structure.
  164. * @param[in] p_ble_nus_c_init Pointer to the NUS initialization structure that contains the
  165. * initialization information.
  166. *
  167. * @retval NRF_SUCCESS If the module was initialized successfully.
  168. * @retval err_code Otherwise, this function propagates the error code
  169. * returned by the Database Discovery module API
  170. * @ref ble_db_discovery_evt_register.
  171. */
  172. uint32_t ble_nus_c_init(ble_nus_c_t * p_ble_nus_c, ble_nus_c_init_t * p_ble_nus_c_init);
  173. /**@brief Function for handling events from the Database Discovery module.
  174. *
  175. * @details This function handles an event from the Database Discovery module, and determines
  176. * whether it relates to the discovery of NUS at the peer. If it does, the function
  177. * calls the application's event handler to indicate that NUS was
  178. * discovered at the peer. The function also populates the event with service-related
  179. * information before providing it to the application.
  180. *
  181. * @param[in] p_ble_nus_c Pointer to the NUS client structure.
  182. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  183. */
  184. void ble_nus_c_on_db_disc_evt(ble_nus_c_t * p_ble_nus_c, ble_db_discovery_evt_t * p_evt);
  185. /**@brief Function for handling BLE events from the SoftDevice.
  186. *
  187. * @details This function handles the BLE events received from the SoftDevice. If a BLE
  188. * event is relevant to the NUS module, the function uses the event's data to update
  189. * internal variables and, if necessary, send events to the application.
  190. *
  191. * @param[in] p_ble_evt Pointer to the BLE event.
  192. * @param[in] p_context Pointer to the NUS client structure.
  193. */
  194. void ble_nus_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  195. /**@brief Function for requesting the peer to start sending notification of TX characteristic.
  196. *
  197. * @details This function enables notifications of the NUS TX characteristic at the peer
  198. * by writing to the CCCD of the NUS TX characteristic.
  199. *
  200. * @param p_ble_nus_c Pointer to the NUS client structure.
  201. *
  202. * @retval NRF_SUCCESS If the operation was successful.
  203. * @retval err_code Otherwise, this API propagates the error code returned by function @ref nrf_ble_gq_item_add.
  204. */
  205. uint32_t ble_nus_c_tx_notif_enable(ble_nus_c_t * p_ble_nus_c);
  206. /**@brief Function for sending a string to the server.
  207. *
  208. * @details This function writes the RX characteristic of the server.
  209. *
  210. * @param[in] p_ble_nus_c Pointer to the NUS client structure.
  211. * @param[in] p_string String to be sent.
  212. * @param[in] length Length of the string.
  213. *
  214. * @retval NRF_SUCCESS If the string was sent successfully.
  215. * @retval err_code Otherwise, this API propagates the error code returned by function @ref nrf_ble_gq_item_add.
  216. */
  217. uint32_t ble_nus_c_string_send(ble_nus_c_t * p_ble_nus_c, uint8_t * p_string, uint16_t length);
  218. /**@brief Function for assigning handles to this instance of nus_c.
  219. *
  220. * @details Call this function when a link has been established with a peer to
  221. * associate the link to this instance of the module. This makes it
  222. * possible to handle several links and associate each link to a particular
  223. * instance of this module. The connection handle and attribute handles are
  224. * provided from the discovery event @ref BLE_NUS_C_EVT_DISCOVERY_COMPLETE.
  225. *
  226. * @param[in] p_ble_nus_c Pointer to the NUS client structure instance to associate with these
  227. * handles.
  228. * @param[in] conn_handle Connection handle to associated with the given NUS Instance.
  229. * @param[in] p_peer_handles Attribute handles on the NUS server that you want this NUS client to
  230. * interact with.
  231. *
  232. * @retval NRF_SUCCESS If the operation was successful.
  233. * @retval NRF_ERROR_NULL If a p_nus was a NULL pointer.
  234. * @retval err_code Otherwise, this API propagates the error code returned
  235. * by function @ref nrf_ble_gq_item_add.
  236. */
  237. uint32_t ble_nus_c_handles_assign(ble_nus_c_t * p_ble_nus_c,
  238. uint16_t conn_handle,
  239. ble_nus_c_handles_t const * p_peer_handles);
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif // BLE_NUS_C_H__
  244. /** @} */