ble_nus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(BLE_NUS)
  42. #include "ble.h"
  43. #include "ble_nus.h"
  44. #include "ble_srv_common.h"
  45. #define NRF_LOG_MODULE_NAME ble_nus
  46. #if BLE_NUS_CONFIG_LOG_ENABLED
  47. #define NRF_LOG_LEVEL BLE_NUS_CONFIG_LOG_LEVEL
  48. #define NRF_LOG_INFO_COLOR BLE_NUS_CONFIG_INFO_COLOR
  49. #define NRF_LOG_DEBUG_COLOR BLE_NUS_CONFIG_DEBUG_COLOR
  50. #else // BLE_NUS_CONFIG_LOG_ENABLED
  51. #define NRF_LOG_LEVEL 0
  52. #endif // BLE_NUS_CONFIG_LOG_ENABLED
  53. #include "nrf_log.h"
  54. NRF_LOG_MODULE_REGISTER();
  55. #define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
  56. #define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
  57. #define BLE_NUS_MAX_RX_CHAR_LEN BLE_NUS_MAX_DATA_LEN /**< Maximum length of the RX Characteristic (in bytes). */
  58. #define BLE_NUS_MAX_TX_CHAR_LEN BLE_NUS_MAX_DATA_LEN /**< Maximum length of the TX Characteristic (in bytes). */
  59. #define NUS_BASE_UUID {{0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E}} /**< Used vendor specific UUID. */
  60. /**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the SoftDevice.
  61. *
  62. * @param[in] p_nus Nordic UART Service structure.
  63. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  64. */
  65. static void on_connect(ble_nus_t * p_nus, ble_evt_t const * p_ble_evt)
  66. {
  67. ret_code_t err_code;
  68. ble_nus_evt_t evt;
  69. ble_gatts_value_t gatts_val;
  70. uint8_t cccd_value[2];
  71. ble_nus_client_context_t * p_client = NULL;
  72. err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage,
  73. p_ble_evt->evt.gap_evt.conn_handle,
  74. (void *) &p_client);
  75. if (err_code != NRF_SUCCESS)
  76. {
  77. NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
  78. p_ble_evt->evt.gap_evt.conn_handle);
  79. }
  80. /* Check the hosts CCCD value to inform of readiness to send data using the RX characteristic */
  81. memset(&gatts_val, 0, sizeof(ble_gatts_value_t));
  82. gatts_val.p_value = cccd_value;
  83. gatts_val.len = sizeof(cccd_value);
  84. gatts_val.offset = 0;
  85. err_code = sd_ble_gatts_value_get(p_ble_evt->evt.gap_evt.conn_handle,
  86. p_nus->tx_handles.cccd_handle,
  87. &gatts_val);
  88. if ((err_code == NRF_SUCCESS) &&
  89. (p_nus->data_handler != NULL) &&
  90. ble_srv_is_notification_enabled(gatts_val.p_value))
  91. {
  92. if (p_client != NULL)
  93. {
  94. p_client->is_notification_enabled = true;
  95. }
  96. memset(&evt, 0, sizeof(ble_nus_evt_t));
  97. evt.type = BLE_NUS_EVT_COMM_STARTED;
  98. evt.p_nus = p_nus;
  99. evt.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  100. evt.p_link_ctx = p_client;
  101. p_nus->data_handler(&evt);
  102. }
  103. }
  104. /**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the SoftDevice.
  105. *
  106. * @param[in] p_nus Nordic UART Service structure.
  107. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  108. */
  109. static void on_write(ble_nus_t * p_nus, ble_evt_t const * p_ble_evt)
  110. {
  111. ret_code_t err_code;
  112. ble_nus_evt_t evt;
  113. ble_nus_client_context_t * p_client;
  114. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  115. err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage,
  116. p_ble_evt->evt.gatts_evt.conn_handle,
  117. (void *) &p_client);
  118. if (err_code != NRF_SUCCESS)
  119. {
  120. NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
  121. p_ble_evt->evt.gatts_evt.conn_handle);
  122. }
  123. memset(&evt, 0, sizeof(ble_nus_evt_t));
  124. evt.p_nus = p_nus;
  125. evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
  126. evt.p_link_ctx = p_client;
  127. if ((p_evt_write->handle == p_nus->tx_handles.cccd_handle) &&
  128. (p_evt_write->len == 2))
  129. {
  130. if (p_client != NULL)
  131. {
  132. if (ble_srv_is_notification_enabled(p_evt_write->data))
  133. {
  134. p_client->is_notification_enabled = true;
  135. evt.type = BLE_NUS_EVT_COMM_STARTED;
  136. }
  137. else
  138. {
  139. p_client->is_notification_enabled = false;
  140. evt.type = BLE_NUS_EVT_COMM_STOPPED;
  141. }
  142. if (p_nus->data_handler != NULL)
  143. {
  144. p_nus->data_handler(&evt);
  145. }
  146. }
  147. }
  148. else if ((p_evt_write->handle == p_nus->rx_handles.value_handle) &&
  149. (p_nus->data_handler != NULL))
  150. {
  151. evt.type = BLE_NUS_EVT_RX_DATA;
  152. evt.params.rx_data.p_data = p_evt_write->data;
  153. evt.params.rx_data.length = p_evt_write->len;
  154. p_nus->data_handler(&evt);
  155. }
  156. else
  157. {
  158. // Do Nothing. This event is not relevant for this service.
  159. }
  160. }
  161. /**@brief Function for handling the @ref BLE_GATTS_EVT_HVN_TX_COMPLETE event from the SoftDevice.
  162. *
  163. * @param[in] p_nus Nordic UART Service structure.
  164. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  165. */
  166. static void on_hvx_tx_complete(ble_nus_t * p_nus, ble_evt_t const * p_ble_evt)
  167. {
  168. ret_code_t err_code;
  169. ble_nus_evt_t evt;
  170. ble_nus_client_context_t * p_client;
  171. err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage,
  172. p_ble_evt->evt.gatts_evt.conn_handle,
  173. (void *) &p_client);
  174. if (err_code != NRF_SUCCESS)
  175. {
  176. NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
  177. p_ble_evt->evt.gatts_evt.conn_handle);
  178. return;
  179. }
  180. if ((p_client->is_notification_enabled) && (p_nus->data_handler != NULL))
  181. {
  182. memset(&evt, 0, sizeof(ble_nus_evt_t));
  183. evt.type = BLE_NUS_EVT_TX_RDY;
  184. evt.p_nus = p_nus;
  185. evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
  186. evt.p_link_ctx = p_client;
  187. p_nus->data_handler(&evt);
  188. }
  189. }
  190. void ble_nus_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  191. {
  192. if ((p_context == NULL) || (p_ble_evt == NULL))
  193. {
  194. return;
  195. }
  196. ble_nus_t * p_nus = (ble_nus_t *)p_context;
  197. switch (p_ble_evt->header.evt_id)
  198. {
  199. case BLE_GAP_EVT_CONNECTED:
  200. on_connect(p_nus, p_ble_evt);
  201. break;
  202. case BLE_GATTS_EVT_WRITE:
  203. on_write(p_nus, p_ble_evt);
  204. break;
  205. case BLE_GATTS_EVT_HVN_TX_COMPLETE:
  206. on_hvx_tx_complete(p_nus, p_ble_evt);
  207. break;
  208. default:
  209. // No implementation needed.
  210. break;
  211. }
  212. }
  213. uint32_t ble_nus_init(ble_nus_t * p_nus, ble_nus_init_t const * p_nus_init)
  214. {
  215. ret_code_t err_code;
  216. ble_uuid_t ble_uuid;
  217. ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
  218. ble_add_char_params_t add_char_params;
  219. VERIFY_PARAM_NOT_NULL(p_nus);
  220. VERIFY_PARAM_NOT_NULL(p_nus_init);
  221. // Initialize the service structure.
  222. p_nus->data_handler = p_nus_init->data_handler;
  223. /**@snippet [Adding proprietary Service to the SoftDevice] */
  224. // Add a custom base UUID.
  225. err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
  226. VERIFY_SUCCESS(err_code);
  227. ble_uuid.type = p_nus->uuid_type;
  228. ble_uuid.uuid = BLE_UUID_NUS_SERVICE;
  229. // Add the service.
  230. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  231. &ble_uuid,
  232. &p_nus->service_handle);
  233. /**@snippet [Adding proprietary Service to the SoftDevice] */
  234. VERIFY_SUCCESS(err_code);
  235. // Add the RX Characteristic.
  236. memset(&add_char_params, 0, sizeof(add_char_params));
  237. add_char_params.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
  238. add_char_params.uuid_type = p_nus->uuid_type;
  239. add_char_params.max_len = BLE_NUS_MAX_RX_CHAR_LEN;
  240. add_char_params.init_len = sizeof(uint8_t);
  241. add_char_params.is_var_len = true;
  242. add_char_params.char_props.write = 1;
  243. add_char_params.char_props.write_wo_resp = 1;
  244. add_char_params.read_access = SEC_OPEN;
  245. add_char_params.write_access = SEC_OPEN;
  246. err_code = characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->rx_handles);
  247. if (err_code != NRF_SUCCESS)
  248. {
  249. return err_code;
  250. }
  251. // Add the TX Characteristic.
  252. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  253. memset(&add_char_params, 0, sizeof(add_char_params));
  254. add_char_params.uuid = BLE_UUID_NUS_TX_CHARACTERISTIC;
  255. add_char_params.uuid_type = p_nus->uuid_type;
  256. add_char_params.max_len = BLE_NUS_MAX_TX_CHAR_LEN;
  257. add_char_params.init_len = sizeof(uint8_t);
  258. add_char_params.is_var_len = true;
  259. add_char_params.char_props.notify = 1;
  260. add_char_params.read_access = SEC_OPEN;
  261. add_char_params.write_access = SEC_OPEN;
  262. add_char_params.cccd_write_access = SEC_OPEN;
  263. return characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->tx_handles);
  264. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  265. }
  266. uint32_t ble_nus_data_send(ble_nus_t * p_nus,
  267. uint8_t * p_data,
  268. uint16_t * p_length,
  269. uint16_t conn_handle)
  270. {
  271. ret_code_t err_code;
  272. ble_gatts_hvx_params_t hvx_params;
  273. ble_nus_client_context_t * p_client;
  274. VERIFY_PARAM_NOT_NULL(p_nus);
  275. err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage, conn_handle, (void *) &p_client);
  276. VERIFY_SUCCESS(err_code);
  277. if ((conn_handle == BLE_CONN_HANDLE_INVALID) || (p_client == NULL))
  278. {
  279. return NRF_ERROR_NOT_FOUND;
  280. }
  281. if (!p_client->is_notification_enabled)
  282. {
  283. return NRF_ERROR_INVALID_STATE;
  284. }
  285. if (*p_length > BLE_NUS_MAX_DATA_LEN)
  286. {
  287. return NRF_ERROR_INVALID_PARAM;
  288. }
  289. memset(&hvx_params, 0, sizeof(hvx_params));
  290. hvx_params.handle = p_nus->tx_handles.value_handle;
  291. hvx_params.p_data = p_data;
  292. hvx_params.p_len = p_length;
  293. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  294. return sd_ble_gatts_hvx(conn_handle, &hvx_params);
  295. }
  296. #endif // NRF_MODULE_ENABLED(BLE_NUS)