ble_nus_c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_C)
  42. #include <stdlib.h>
  43. #include "ble.h"
  44. #include "ble_nus_c.h"
  45. #include "ble_gattc.h"
  46. #include "ble_srv_common.h"
  47. #include "app_error.h"
  48. #define NRF_LOG_MODULE_NAME ble_nus_c
  49. #include "nrf_log.h"
  50. NRF_LOG_MODULE_REGISTER();
  51. /**@brief Function for intercepting the errors of GATTC and the BLE GATT Queue.
  52. *
  53. * @param[in] nrf_error Error code.
  54. * @param[in] p_ctx Parameter from the event handler.
  55. * @param[in] conn_handle Connection handle.
  56. */
  57. static void gatt_error_handler(uint32_t nrf_error,
  58. void * p_ctx,
  59. uint16_t conn_handle)
  60. {
  61. ble_nus_c_t * p_ble_nus_c = (ble_nus_c_t *)p_ctx;
  62. NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
  63. if (p_ble_nus_c->error_handler != NULL)
  64. {
  65. p_ble_nus_c->error_handler(nrf_error);
  66. }
  67. }
  68. void ble_nus_c_on_db_disc_evt(ble_nus_c_t * p_ble_nus_c, ble_db_discovery_evt_t * p_evt)
  69. {
  70. ble_nus_c_evt_t nus_c_evt;
  71. memset(&nus_c_evt,0,sizeof(ble_nus_c_evt_t));
  72. ble_gatt_db_char_t * p_chars = p_evt->params.discovered_db.charateristics;
  73. // Check if the NUS was discovered.
  74. if ( (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE)
  75. && (p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_NUS_SERVICE)
  76. && (p_evt->params.discovered_db.srv_uuid.type == p_ble_nus_c->uuid_type))
  77. {
  78. for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
  79. {
  80. switch (p_chars[i].characteristic.uuid.uuid)
  81. {
  82. case BLE_UUID_NUS_RX_CHARACTERISTIC:
  83. nus_c_evt.handles.nus_rx_handle = p_chars[i].characteristic.handle_value;
  84. break;
  85. case BLE_UUID_NUS_TX_CHARACTERISTIC:
  86. nus_c_evt.handles.nus_tx_handle = p_chars[i].characteristic.handle_value;
  87. nus_c_evt.handles.nus_tx_cccd_handle = p_chars[i].cccd_handle;
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. if (p_ble_nus_c->evt_handler != NULL)
  94. {
  95. nus_c_evt.conn_handle = p_evt->conn_handle;
  96. nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCOVERY_COMPLETE;
  97. p_ble_nus_c->evt_handler(p_ble_nus_c, &nus_c_evt);
  98. }
  99. }
  100. }
  101. /**@brief Function for handling Handle Value Notification received from the SoftDevice.
  102. *
  103. * @details This function uses the Handle Value Notification received from the SoftDevice
  104. * and checks if it is a notification of the NUS TX characteristic from the peer.
  105. * If it is, this function decodes the data and sends it to the application.
  106. *
  107. * @param[in] p_ble_nus_c Pointer to the NUS Client structure.
  108. * @param[in] p_ble_evt Pointer to the BLE event received.
  109. */
  110. static void on_hvx(ble_nus_c_t * p_ble_nus_c, ble_evt_t const * p_ble_evt)
  111. {
  112. // HVX can only occur from client sending.
  113. if ( (p_ble_nus_c->handles.nus_tx_handle != BLE_GATT_HANDLE_INVALID)
  114. && (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_nus_c->handles.nus_tx_handle)
  115. && (p_ble_nus_c->evt_handler != NULL))
  116. {
  117. ble_nus_c_evt_t ble_nus_c_evt;
  118. ble_nus_c_evt.evt_type = BLE_NUS_C_EVT_NUS_TX_EVT;
  119. ble_nus_c_evt.p_data = (uint8_t *)p_ble_evt->evt.gattc_evt.params.hvx.data;
  120. ble_nus_c_evt.data_len = p_ble_evt->evt.gattc_evt.params.hvx.len;
  121. p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt);
  122. NRF_LOG_DEBUG("Client sending data.");
  123. }
  124. }
  125. uint32_t ble_nus_c_init(ble_nus_c_t * p_ble_nus_c, ble_nus_c_init_t * p_ble_nus_c_init)
  126. {
  127. uint32_t err_code;
  128. ble_uuid_t uart_uuid;
  129. ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
  130. VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
  131. VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init);
  132. VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init->p_gatt_queue);
  133. err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_ble_nus_c->uuid_type);
  134. VERIFY_SUCCESS(err_code);
  135. uart_uuid.type = p_ble_nus_c->uuid_type;
  136. uart_uuid.uuid = BLE_UUID_NUS_SERVICE;
  137. p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  138. p_ble_nus_c->evt_handler = p_ble_nus_c_init->evt_handler;
  139. p_ble_nus_c->error_handler = p_ble_nus_c_init->error_handler;
  140. p_ble_nus_c->handles.nus_tx_handle = BLE_GATT_HANDLE_INVALID;
  141. p_ble_nus_c->handles.nus_rx_handle = BLE_GATT_HANDLE_INVALID;
  142. p_ble_nus_c->p_gatt_queue = p_ble_nus_c_init->p_gatt_queue;
  143. return ble_db_discovery_evt_register(&uart_uuid);
  144. }
  145. void ble_nus_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  146. {
  147. ble_nus_c_t * p_ble_nus_c = (ble_nus_c_t *)p_context;
  148. if ((p_ble_nus_c == NULL) || (p_ble_evt == NULL))
  149. {
  150. return;
  151. }
  152. if ( (p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  153. ||(p_ble_nus_c->conn_handle != p_ble_evt->evt.gap_evt.conn_handle)
  154. )
  155. {
  156. return;
  157. }
  158. switch (p_ble_evt->header.evt_id)
  159. {
  160. case BLE_GATTC_EVT_HVX:
  161. on_hvx(p_ble_nus_c, p_ble_evt);
  162. break;
  163. case BLE_GAP_EVT_DISCONNECTED:
  164. if (p_ble_evt->evt.gap_evt.conn_handle == p_ble_nus_c->conn_handle
  165. && p_ble_nus_c->evt_handler != NULL)
  166. {
  167. ble_nus_c_evt_t nus_c_evt;
  168. nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCONNECTED;
  169. p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  170. p_ble_nus_c->evt_handler(p_ble_nus_c, &nus_c_evt);
  171. }
  172. break;
  173. default:
  174. // No implementation needed.
  175. break;
  176. }
  177. }
  178. /**@brief Function for creating a message for writing to the CCCD. */
  179. static uint32_t cccd_configure(ble_nus_c_t * p_ble_nus_c, bool notification_enable)
  180. {
  181. nrf_ble_gq_req_t cccd_req;
  182. uint8_t cccd[BLE_CCCD_VALUE_LEN];
  183. uint16_t cccd_val = notification_enable ? BLE_GATT_HVX_NOTIFICATION : 0;
  184. memset(&cccd_req, 0, sizeof(nrf_ble_gq_req_t));
  185. cccd[0] = LSB_16(cccd_val);
  186. cccd[1] = MSB_16(cccd_val);
  187. cccd_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  188. cccd_req.error_handler.cb = gatt_error_handler;
  189. cccd_req.error_handler.p_ctx = p_ble_nus_c;
  190. cccd_req.params.gattc_write.handle = p_ble_nus_c->handles.nus_tx_cccd_handle;
  191. cccd_req.params.gattc_write.len = BLE_CCCD_VALUE_LEN;
  192. cccd_req.params.gattc_write.offset = 0;
  193. cccd_req.params.gattc_write.p_value = cccd;
  194. cccd_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_REQ;
  195. cccd_req.params.gattc_write.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
  196. return nrf_ble_gq_item_add(p_ble_nus_c->p_gatt_queue, &cccd_req, p_ble_nus_c->conn_handle);
  197. }
  198. uint32_t ble_nus_c_tx_notif_enable(ble_nus_c_t * p_ble_nus_c)
  199. {
  200. VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
  201. if ( (p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  202. ||(p_ble_nus_c->handles.nus_tx_cccd_handle == BLE_GATT_HANDLE_INVALID)
  203. )
  204. {
  205. return NRF_ERROR_INVALID_STATE;
  206. }
  207. return cccd_configure(p_ble_nus_c, true);
  208. }
  209. uint32_t ble_nus_c_string_send(ble_nus_c_t * p_ble_nus_c, uint8_t * p_string, uint16_t length)
  210. {
  211. VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
  212. nrf_ble_gq_req_t write_req;
  213. memset(&write_req, 0, sizeof(nrf_ble_gq_req_t));
  214. if (length > BLE_NUS_MAX_DATA_LEN)
  215. {
  216. NRF_LOG_WARNING("Content too long.");
  217. return NRF_ERROR_INVALID_PARAM;
  218. }
  219. if (p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  220. {
  221. NRF_LOG_WARNING("Connection handle invalid.");
  222. return NRF_ERROR_INVALID_STATE;
  223. }
  224. write_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  225. write_req.error_handler.cb = gatt_error_handler;
  226. write_req.error_handler.p_ctx = p_ble_nus_c;
  227. write_req.params.gattc_write.handle = p_ble_nus_c->handles.nus_rx_handle;
  228. write_req.params.gattc_write.len = length;
  229. write_req.params.gattc_write.offset = 0;
  230. write_req.params.gattc_write.p_value = p_string;
  231. write_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_CMD;
  232. write_req.params.gattc_write.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
  233. return nrf_ble_gq_item_add(p_ble_nus_c->p_gatt_queue, &write_req, p_ble_nus_c->conn_handle);
  234. }
  235. uint32_t ble_nus_c_handles_assign(ble_nus_c_t * p_ble_nus,
  236. uint16_t conn_handle,
  237. ble_nus_c_handles_t const * p_peer_handles)
  238. {
  239. VERIFY_PARAM_NOT_NULL(p_ble_nus);
  240. p_ble_nus->conn_handle = conn_handle;
  241. if (p_peer_handles != NULL)
  242. {
  243. p_ble_nus->handles.nus_tx_cccd_handle = p_peer_handles->nus_tx_cccd_handle;
  244. p_ble_nus->handles.nus_tx_handle = p_peer_handles->nus_tx_handle;
  245. p_ble_nus->handles.nus_rx_handle = p_peer_handles->nus_rx_handle;
  246. }
  247. return nrf_ble_gq_conn_handle_register(p_ble_nus->p_gatt_queue, conn_handle);
  248. }
  249. #endif // NRF_MODULE_ENABLED(BLE_NUS_C)