ble_bas_c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_BAS_C)
  42. #include "ble_bas_c.h"
  43. #include "ble_types.h"
  44. #include "ble_db_discovery.h"
  45. #include "ble_gattc.h"
  46. #define NRF_LOG_MODULE_NAME ble_bas_c
  47. #include "nrf_log.h"
  48. NRF_LOG_MODULE_REGISTER();
  49. /**@brief Function for intercepting errors of GATTC and BLE GATT Queue.
  50. *
  51. * @param[in] nrf_error Error code.
  52. * @param[in] p_ctx Parameter from the event handler.
  53. * @param[in] conn_handle Connection handle.
  54. */
  55. static void gatt_error_handler(uint32_t nrf_error,
  56. void * p_ctx,
  57. uint16_t conn_handle)
  58. {
  59. ble_bas_c_t * p_bas_c = (ble_bas_c_t *)p_ctx;
  60. NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
  61. if (p_bas_c->error_handler != NULL)
  62. {
  63. p_bas_c->error_handler(nrf_error);
  64. }
  65. }
  66. /**@brief Function for handling read response events.
  67. *
  68. * @details This function validates the read response and raises the appropriate
  69. * event to the application.
  70. *
  71. * @param[in] p_bas_c Pointer to the Battery Service Client Structure.
  72. * @param[in] p_ble_evt Pointer to the SoftDevice event.
  73. */
  74. static void on_read_rsp(ble_bas_c_t * p_bas_c, ble_evt_t const * p_ble_evt)
  75. {
  76. const ble_gattc_evt_read_rsp_t * p_response;
  77. // Check if the event is on the link for this instance.
  78. if (p_bas_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
  79. {
  80. return;
  81. }
  82. p_response = &p_ble_evt->evt.gattc_evt.params.read_rsp;
  83. if (p_response->handle == p_bas_c->peer_bas_db.bl_handle)
  84. {
  85. ble_bas_c_evt_t evt;
  86. evt.conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
  87. evt.evt_type = BLE_BAS_C_EVT_BATT_READ_RESP;
  88. evt.params.battery_level = p_response->data[0];
  89. p_bas_c->evt_handler(p_bas_c, &evt);
  90. }
  91. }
  92. /**@brief Function for handling Handle Value Notification received from the SoftDevice.
  93. *
  94. * @details This function handles the Handle Value Notification received from the SoftDevice
  95. * and checks whether it is a notification of the Battery Level measurement from the peer. If
  96. * it is, this function decodes the battery level measurement and sends it to the
  97. * application.
  98. *
  99. * @param[in] p_ble_bas_c Pointer to the Battery Service Client structure.
  100. * @param[in] p_ble_evt Pointer to the BLE event received.
  101. */
  102. static void on_hvx(ble_bas_c_t * p_ble_bas_c, ble_evt_t const * p_ble_evt)
  103. {
  104. // Check if the event is on the link for this instance.
  105. if (p_ble_bas_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
  106. {
  107. return;
  108. }
  109. // Check if this notification is a battery level notification.
  110. if (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_bas_c->peer_bas_db.bl_handle)
  111. {
  112. if (p_ble_evt->evt.gattc_evt.params.hvx.len == 1)
  113. {
  114. ble_bas_c_evt_t ble_bas_c_evt;
  115. ble_bas_c_evt.conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
  116. ble_bas_c_evt.evt_type = BLE_BAS_C_EVT_BATT_NOTIFICATION;
  117. ble_bas_c_evt.params.battery_level = p_ble_evt->evt.gattc_evt.params.hvx.data[0];
  118. p_ble_bas_c->evt_handler(p_ble_bas_c, &ble_bas_c_evt);
  119. }
  120. }
  121. }
  122. void ble_bas_on_db_disc_evt(ble_bas_c_t * p_ble_bas_c, const ble_db_discovery_evt_t * p_evt)
  123. {
  124. // Check if the Battery Service was discovered.
  125. if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE
  126. &&
  127. p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_BATTERY_SERVICE
  128. &&
  129. p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE)
  130. {
  131. // Find the CCCD Handle of the Battery Level characteristic.
  132. uint8_t i;
  133. ble_bas_c_evt_t evt;
  134. evt.evt_type = BLE_BAS_C_EVT_DISCOVERY_COMPLETE;
  135. evt.conn_handle = p_evt->conn_handle;
  136. for (i = 0; i < p_evt->params.discovered_db.char_count; i++)
  137. {
  138. if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
  139. BLE_UUID_BATTERY_LEVEL_CHAR)
  140. {
  141. // Found Battery Level characteristic. Store CCCD handle and break.
  142. evt.params.bas_db.bl_cccd_handle =
  143. p_evt->params.discovered_db.charateristics[i].cccd_handle;
  144. evt.params.bas_db.bl_handle =
  145. p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
  146. break;
  147. }
  148. }
  149. NRF_LOG_DEBUG("Battery Service discovered at peer.");
  150. //If the instance has been assigned prior to db_discovery, assign the db_handles.
  151. if (p_ble_bas_c->conn_handle != BLE_CONN_HANDLE_INVALID)
  152. {
  153. if ((p_ble_bas_c->peer_bas_db.bl_cccd_handle == BLE_GATT_HANDLE_INVALID)&&
  154. (p_ble_bas_c->peer_bas_db.bl_handle == BLE_GATT_HANDLE_INVALID))
  155. {
  156. p_ble_bas_c->peer_bas_db = evt.params.bas_db;
  157. }
  158. }
  159. p_ble_bas_c->evt_handler(p_ble_bas_c, &evt);
  160. }
  161. else if ((p_evt->evt_type == BLE_DB_DISCOVERY_SRV_NOT_FOUND) ||
  162. (p_evt->evt_type == BLE_DB_DISCOVERY_ERROR))
  163. {
  164. NRF_LOG_DEBUG("Battery Service discovery failure at peer. ");
  165. }
  166. else
  167. {
  168. // Do nothing.
  169. }
  170. }
  171. /**@brief Function for creating a message for writing to the CCCD.
  172. */
  173. static uint32_t cccd_configure(ble_bas_c_t * p_ble_bas_c, bool notification_enable)
  174. {
  175. NRF_LOG_DEBUG("Configuring CCCD. CCCD Handle = %d, Connection Handle = %d",
  176. p_ble_bas_c->peer_bas_db.bl_cccd_handle,
  177. p_ble_bas_c->conn_handle);
  178. nrf_ble_gq_req_t bas_c_req;
  179. uint8_t cccd[BLE_CCCD_VALUE_LEN];
  180. uint16_t cccd_val = notification_enable ? BLE_GATT_HVX_NOTIFICATION : 0;
  181. cccd[0] = LSB_16(cccd_val);
  182. cccd[1] = MSB_16(cccd_val);
  183. memset(&bas_c_req, 0, sizeof(bas_c_req));
  184. bas_c_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  185. bas_c_req.error_handler.cb = gatt_error_handler;
  186. bas_c_req.error_handler.p_ctx = p_ble_bas_c;
  187. bas_c_req.params.gattc_write.handle = p_ble_bas_c->peer_bas_db.bl_cccd_handle;
  188. bas_c_req.params.gattc_write.len = BLE_CCCD_VALUE_LEN;
  189. bas_c_req.params.gattc_write.p_value = cccd;
  190. bas_c_req.params.gattc_write.offset = 0;
  191. bas_c_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_REQ;
  192. return nrf_ble_gq_item_add(p_ble_bas_c->p_gatt_queue, &bas_c_req, p_ble_bas_c->conn_handle);
  193. }
  194. uint32_t ble_bas_c_init(ble_bas_c_t * p_ble_bas_c, ble_bas_c_init_t * p_ble_bas_c_init)
  195. {
  196. VERIFY_PARAM_NOT_NULL(p_ble_bas_c);
  197. VERIFY_PARAM_NOT_NULL(p_ble_bas_c_init);
  198. ble_uuid_t bas_uuid;
  199. bas_uuid.type = BLE_UUID_TYPE_BLE;
  200. bas_uuid.uuid = BLE_UUID_BATTERY_SERVICE;
  201. p_ble_bas_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  202. p_ble_bas_c->peer_bas_db.bl_cccd_handle = BLE_GATT_HANDLE_INVALID;
  203. p_ble_bas_c->peer_bas_db.bl_handle = BLE_GATT_HANDLE_INVALID;
  204. p_ble_bas_c->evt_handler = p_ble_bas_c_init->evt_handler;
  205. p_ble_bas_c->error_handler = p_ble_bas_c_init->error_handler;
  206. p_ble_bas_c->p_gatt_queue = p_ble_bas_c_init->p_gatt_queue;
  207. return ble_db_discovery_evt_register(&bas_uuid);
  208. }
  209. /**@brief Function for handling the Disconnected event received from the SoftDevice.
  210. *
  211. * @details This function checks whether the disconnect event is happening on the link
  212. * associated with the current instance of the module. If the event is happening,
  213. * the function sets the instance's conn_handle to invalid.
  214. *
  215. * @param[in] p_ble_bas_c Pointer to the Battery Service Client structure.
  216. * @param[in] p_ble_evt Pointer to the BLE event received.
  217. */
  218. static void on_disconnected(ble_bas_c_t * p_ble_bas_c, const ble_evt_t * p_ble_evt)
  219. {
  220. if (p_ble_bas_c->conn_handle == p_ble_evt->evt.gap_evt.conn_handle)
  221. {
  222. p_ble_bas_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  223. p_ble_bas_c->peer_bas_db.bl_cccd_handle = BLE_GATT_HANDLE_INVALID;
  224. p_ble_bas_c->peer_bas_db.bl_handle = BLE_GATT_HANDLE_INVALID;
  225. }
  226. }
  227. void ble_bas_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  228. {
  229. if ((p_ble_evt == NULL) || (p_context == NULL))
  230. {
  231. return;
  232. }
  233. ble_bas_c_t * p_ble_bas_c = (ble_bas_c_t *)p_context;
  234. switch (p_ble_evt->header.evt_id)
  235. {
  236. case BLE_GATTC_EVT_HVX:
  237. on_hvx(p_ble_bas_c, p_ble_evt);
  238. break;
  239. case BLE_GATTC_EVT_READ_RSP:
  240. on_read_rsp(p_ble_bas_c, p_ble_evt);
  241. break;
  242. case BLE_GAP_EVT_DISCONNECTED:
  243. on_disconnected(p_ble_bas_c, p_ble_evt);
  244. break;
  245. default:
  246. break;
  247. }
  248. }
  249. uint32_t ble_bas_c_bl_notif_enable(ble_bas_c_t * p_ble_bas_c)
  250. {
  251. VERIFY_PARAM_NOT_NULL(p_ble_bas_c);
  252. if (p_ble_bas_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  253. {
  254. return NRF_ERROR_INVALID_STATE;
  255. }
  256. return cccd_configure(p_ble_bas_c, true);
  257. }
  258. uint32_t ble_bas_c_bl_read(ble_bas_c_t * p_ble_bas_c)
  259. {
  260. VERIFY_PARAM_NOT_NULL(p_ble_bas_c);
  261. if (p_ble_bas_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  262. {
  263. return NRF_ERROR_INVALID_STATE;
  264. }
  265. nrf_ble_gq_req_t bas_c_req;
  266. memset(&bas_c_req, 0, sizeof(bas_c_req));
  267. bas_c_req.type = NRF_BLE_GQ_REQ_GATTC_READ;
  268. bas_c_req.error_handler.cb = gatt_error_handler;
  269. bas_c_req.error_handler.p_ctx = p_ble_bas_c;
  270. bas_c_req.params.gattc_read.handle = p_ble_bas_c->peer_bas_db.bl_handle;
  271. return nrf_ble_gq_item_add(p_ble_bas_c->p_gatt_queue, &bas_c_req, p_ble_bas_c->conn_handle);
  272. }
  273. uint32_t ble_bas_c_handles_assign(ble_bas_c_t * p_ble_bas_c,
  274. uint16_t conn_handle,
  275. ble_bas_c_db_t * p_peer_handles)
  276. {
  277. VERIFY_PARAM_NOT_NULL(p_ble_bas_c);
  278. p_ble_bas_c->conn_handle = conn_handle;
  279. if (p_peer_handles != NULL)
  280. {
  281. p_ble_bas_c->peer_bas_db = *p_peer_handles;
  282. }
  283. return nrf_ble_gq_conn_handle_register(p_ble_bas_c->p_gatt_queue, conn_handle);
  284. }
  285. #endif // NRF_MODULE_ENABLED(BLE_BAS_C)