ble_lbs_c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_LBS_C)
  42. #include "ble_lbs_c.h"
  43. #include "ble_db_discovery.h"
  44. #include "ble_types.h"
  45. #include "ble_gattc.h"
  46. #define NRF_LOG_MODULE_NAME ble_lbs_c
  47. #include "nrf_log.h"
  48. NRF_LOG_MODULE_REGISTER();
  49. #define WRITE_MESSAGE_LENGTH BLE_CCCD_VALUE_LEN /**< Length of the write message for CCCD. */
  50. /**@brief Function for intercepting the errors of GATTC and the BLE GATT Queue.
  51. *
  52. * @param[in] nrf_error Error code.
  53. * @param[in] p_ctx Parameter from the event handler.
  54. * @param[in] conn_handle Connection handle.
  55. */
  56. static void gatt_error_handler(uint32_t nrf_error,
  57. void * p_ctx,
  58. uint16_t conn_handle)
  59. {
  60. ble_lbs_c_t * p_ble_lbs_c = (ble_lbs_c_t *)p_ctx;
  61. NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
  62. if (p_ble_lbs_c->error_handler != NULL)
  63. {
  64. p_ble_lbs_c->error_handler(nrf_error);
  65. }
  66. }
  67. /**@brief Function for handling Handle Value Notification received from the SoftDevice.
  68. *
  69. * @details This function uses the Handle Value Notification received from the SoftDevice
  70. * and checks whether it is a notification of Button state from the peer. If
  71. * it is, this function decodes the state of the button and sends it to the
  72. * application.
  73. *
  74. * @param[in] p_ble_lbs_c Pointer to the Led Button Client structure.
  75. * @param[in] p_ble_evt Pointer to the BLE event received.
  76. */
  77. static void on_hvx(ble_lbs_c_t * p_ble_lbs_c, ble_evt_t const * p_ble_evt)
  78. {
  79. // Check if the event is on the link for this instance.
  80. if (p_ble_lbs_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
  81. {
  82. return;
  83. }
  84. // Check if this is a Button notification.
  85. if (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_lbs_c->peer_lbs_db.button_handle)
  86. {
  87. if (p_ble_evt->evt.gattc_evt.params.hvx.len == 1)
  88. {
  89. ble_lbs_c_evt_t ble_lbs_c_evt;
  90. ble_lbs_c_evt.evt_type = BLE_LBS_C_EVT_BUTTON_NOTIFICATION;
  91. ble_lbs_c_evt.conn_handle = p_ble_lbs_c->conn_handle;
  92. ble_lbs_c_evt.params.button.button_state = p_ble_evt->evt.gattc_evt.params.hvx.data[0];
  93. p_ble_lbs_c->evt_handler(p_ble_lbs_c, &ble_lbs_c_evt);
  94. }
  95. }
  96. }
  97. /**@brief Function for handling the Disconnected event received from the SoftDevice.
  98. *
  99. * @details This function checks whether the disconnect event is happening on the link
  100. * associated with the current instance of the module. If the event is happening, the function sets the instance's
  101. * conn_handle to invalid.
  102. *
  103. * @param[in] p_ble_lbs_c Pointer to the Led Button Client structure.
  104. * @param[in] p_ble_evt Pointer to the BLE event received.
  105. */
  106. static void on_disconnected(ble_lbs_c_t * p_ble_lbs_c, ble_evt_t const * p_ble_evt)
  107. {
  108. if (p_ble_lbs_c->conn_handle == p_ble_evt->evt.gap_evt.conn_handle)
  109. {
  110. p_ble_lbs_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  111. p_ble_lbs_c->peer_lbs_db.button_cccd_handle = BLE_GATT_HANDLE_INVALID;
  112. p_ble_lbs_c->peer_lbs_db.button_handle = BLE_GATT_HANDLE_INVALID;
  113. p_ble_lbs_c->peer_lbs_db.led_handle = BLE_GATT_HANDLE_INVALID;
  114. }
  115. }
  116. void ble_lbs_on_db_disc_evt(ble_lbs_c_t * p_ble_lbs_c, ble_db_discovery_evt_t const * p_evt)
  117. {
  118. // Check if the LED Button Service was discovered.
  119. if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
  120. p_evt->params.discovered_db.srv_uuid.uuid == LBS_UUID_SERVICE &&
  121. p_evt->params.discovered_db.srv_uuid.type == p_ble_lbs_c->uuid_type)
  122. {
  123. ble_lbs_c_evt_t evt;
  124. evt.evt_type = BLE_LBS_C_EVT_DISCOVERY_COMPLETE;
  125. evt.conn_handle = p_evt->conn_handle;
  126. for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
  127. {
  128. const ble_gatt_db_char_t * p_char = &(p_evt->params.discovered_db.charateristics[i]);
  129. switch (p_char->characteristic.uuid.uuid)
  130. {
  131. case LBS_UUID_LED_CHAR:
  132. evt.params.peer_db.led_handle = p_char->characteristic.handle_value;
  133. break;
  134. case LBS_UUID_BUTTON_CHAR:
  135. evt.params.peer_db.button_handle = p_char->characteristic.handle_value;
  136. evt.params.peer_db.button_cccd_handle = p_char->cccd_handle;
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. NRF_LOG_DEBUG("LED Button Service discovered at peer.");
  143. //If the instance was assigned prior to db_discovery, assign the db_handles
  144. if (p_ble_lbs_c->conn_handle != BLE_CONN_HANDLE_INVALID)
  145. {
  146. if ((p_ble_lbs_c->peer_lbs_db.led_handle == BLE_GATT_HANDLE_INVALID)&&
  147. (p_ble_lbs_c->peer_lbs_db.button_handle == BLE_GATT_HANDLE_INVALID)&&
  148. (p_ble_lbs_c->peer_lbs_db.button_cccd_handle == BLE_GATT_HANDLE_INVALID))
  149. {
  150. p_ble_lbs_c->peer_lbs_db = evt.params.peer_db;
  151. }
  152. }
  153. p_ble_lbs_c->evt_handler(p_ble_lbs_c, &evt);
  154. }
  155. }
  156. uint32_t ble_lbs_c_init(ble_lbs_c_t * p_ble_lbs_c, ble_lbs_c_init_t * p_ble_lbs_c_init)
  157. {
  158. uint32_t err_code;
  159. ble_uuid_t lbs_uuid;
  160. ble_uuid128_t lbs_base_uuid = {LBS_UUID_BASE};
  161. VERIFY_PARAM_NOT_NULL(p_ble_lbs_c);
  162. VERIFY_PARAM_NOT_NULL(p_ble_lbs_c_init);
  163. VERIFY_PARAM_NOT_NULL(p_ble_lbs_c_init->evt_handler);
  164. VERIFY_PARAM_NOT_NULL(p_ble_lbs_c_init->p_gatt_queue);
  165. p_ble_lbs_c->peer_lbs_db.button_cccd_handle = BLE_GATT_HANDLE_INVALID;
  166. p_ble_lbs_c->peer_lbs_db.button_handle = BLE_GATT_HANDLE_INVALID;
  167. p_ble_lbs_c->peer_lbs_db.led_handle = BLE_GATT_HANDLE_INVALID;
  168. p_ble_lbs_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  169. p_ble_lbs_c->evt_handler = p_ble_lbs_c_init->evt_handler;
  170. p_ble_lbs_c->p_gatt_queue = p_ble_lbs_c_init->p_gatt_queue;
  171. p_ble_lbs_c->error_handler = p_ble_lbs_c_init->error_handler;
  172. err_code = sd_ble_uuid_vs_add(&lbs_base_uuid, &p_ble_lbs_c->uuid_type);
  173. if (err_code != NRF_SUCCESS)
  174. {
  175. return err_code;
  176. }
  177. VERIFY_SUCCESS(err_code);
  178. lbs_uuid.type = p_ble_lbs_c->uuid_type;
  179. lbs_uuid.uuid = LBS_UUID_SERVICE;
  180. return ble_db_discovery_evt_register(&lbs_uuid);
  181. }
  182. void ble_lbs_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  183. {
  184. if ((p_context == NULL) || (p_ble_evt == NULL))
  185. {
  186. return;
  187. }
  188. ble_lbs_c_t * p_ble_lbs_c = (ble_lbs_c_t *)p_context;
  189. switch (p_ble_evt->header.evt_id)
  190. {
  191. case BLE_GATTC_EVT_HVX:
  192. on_hvx(p_ble_lbs_c, p_ble_evt);
  193. break;
  194. case BLE_GAP_EVT_DISCONNECTED:
  195. on_disconnected(p_ble_lbs_c, p_ble_evt);
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. /**@brief Function for configuring the CCCD.
  202. *
  203. * @param[in] p_ble_lbs_c Pointer to the LED Button Client structure.
  204. * @param[in] enable Whether to enable or disable the CCCD.
  205. *
  206. * @return NRF_SUCCESS if the CCCD configure was successfully sent to the peer.
  207. */
  208. static uint32_t cccd_configure(ble_lbs_c_t * p_ble_lbs_c, bool enable)
  209. {
  210. NRF_LOG_DEBUG("Configuring CCCD. CCCD Handle = %d, Connection Handle = %d",
  211. p_ble_lbs_c->peer_lbs_db.button_cccd_handle,
  212. p_ble_lbs_c->conn_handle);
  213. nrf_ble_gq_req_t cccd_req;
  214. uint16_t cccd_val = enable ? BLE_GATT_HVX_NOTIFICATION : 0;
  215. uint8_t cccd[WRITE_MESSAGE_LENGTH];
  216. cccd[0] = LSB_16(cccd_val);
  217. cccd[1] = MSB_16(cccd_val);
  218. cccd_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  219. cccd_req.error_handler.cb = gatt_error_handler;
  220. cccd_req.error_handler.p_ctx = p_ble_lbs_c;
  221. cccd_req.params.gattc_write.handle = p_ble_lbs_c->peer_lbs_db.button_cccd_handle;
  222. cccd_req.params.gattc_write.len = WRITE_MESSAGE_LENGTH;
  223. cccd_req.params.gattc_write.offset = 0;
  224. cccd_req.params.gattc_write.p_value = cccd;
  225. cccd_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_REQ;
  226. return nrf_ble_gq_item_add(p_ble_lbs_c->p_gatt_queue, &cccd_req, p_ble_lbs_c->conn_handle);
  227. }
  228. uint32_t ble_lbs_c_button_notif_enable(ble_lbs_c_t * p_ble_lbs_c)
  229. {
  230. VERIFY_PARAM_NOT_NULL(p_ble_lbs_c);
  231. if (p_ble_lbs_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  232. {
  233. return NRF_ERROR_INVALID_STATE;
  234. }
  235. return cccd_configure(p_ble_lbs_c,
  236. true);
  237. }
  238. uint32_t ble_lbs_led_status_send(ble_lbs_c_t * p_ble_lbs_c, uint8_t status)
  239. {
  240. VERIFY_PARAM_NOT_NULL(p_ble_lbs_c);
  241. if (p_ble_lbs_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  242. {
  243. return NRF_ERROR_INVALID_STATE;
  244. }
  245. NRF_LOG_DEBUG("Writing LED status 0x%x", status);
  246. nrf_ble_gq_req_t write_req;
  247. memset(&write_req, 0, sizeof(nrf_ble_gq_req_t));
  248. write_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  249. write_req.error_handler.cb = gatt_error_handler;
  250. write_req.error_handler.p_ctx = p_ble_lbs_c;
  251. write_req.params.gattc_write.handle = p_ble_lbs_c->peer_lbs_db.led_handle;
  252. write_req.params.gattc_write.len = sizeof(status);
  253. write_req.params.gattc_write.p_value = &status;
  254. write_req.params.gattc_write.offset = 0;
  255. write_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_CMD;
  256. return nrf_ble_gq_item_add(p_ble_lbs_c->p_gatt_queue, &write_req, p_ble_lbs_c->conn_handle);
  257. }
  258. uint32_t ble_lbs_c_handles_assign(ble_lbs_c_t * p_ble_lbs_c,
  259. uint16_t conn_handle,
  260. const lbs_db_t * p_peer_handles)
  261. {
  262. VERIFY_PARAM_NOT_NULL(p_ble_lbs_c);
  263. p_ble_lbs_c->conn_handle = conn_handle;
  264. if (p_peer_handles != NULL)
  265. {
  266. p_ble_lbs_c->peer_lbs_db = *p_peer_handles;
  267. }
  268. return nrf_ble_gq_conn_handle_register(p_ble_lbs_c->p_gatt_queue, conn_handle);
  269. }
  270. #endif // NRF_MODULE_ENABLED(BLE_LBS_C)