You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
12 KiB

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