ble_hrs_c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /**@cond To Make Doxygen skip documentation generation for this file.
  41. * @{
  42. */
  43. #include "sdk_common.h"
  44. #if NRF_MODULE_ENABLED(BLE_HRS_C)
  45. #include "ble_hrs_c.h"
  46. #include "ble_db_discovery.h"
  47. #include "ble_types.h"
  48. #include "ble_gattc.h"
  49. #define NRF_LOG_MODULE_NAME ble_hrs_c
  50. #include "nrf_log.h"
  51. NRF_LOG_MODULE_REGISTER();
  52. #define HRM_FLAG_MASK_HR_16BIT (0x01 << 0) /**< Bit mask used to extract the type of heart rate value. This is used to find if the received heart rate is a 16 bit value or an 8 bit value. */
  53. #define HRM_FLAG_MASK_HR_RR_INT (0x01 << 4) /**< Bit mask used to extract the presence of RR_INTERVALS. This is used to find if the received measurement includes RR_INTERVALS. */
  54. /**@brief Function for interception of the errors of GATTC and the BLE GATT Queue.
  55. *
  56. * @param[in] nrf_error Error code.
  57. * @param[in] p_ctx Parameter from the event handler.
  58. * @param[in] conn_handle Connection handle.
  59. */
  60. static void gatt_error_handler(uint32_t nrf_error,
  61. void * p_ctx,
  62. uint16_t conn_handle)
  63. {
  64. ble_hrs_c_t * p_ble_hrs_c = (ble_hrs_c_t *)p_ctx;
  65. NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
  66. if (p_ble_hrs_c->error_handler != NULL)
  67. {
  68. p_ble_hrs_c->error_handler(nrf_error);
  69. }
  70. }
  71. /**@brief Function for handling Handle Value Notification received from the SoftDevice.
  72. *
  73. * @details This function uses the Handle Value Notification received from the SoftDevice
  74. * and checks whether it is a notification of the heart rate measurement from the peer. If
  75. * it is, this function decodes the heart rate measurement and sends it to the
  76. * application.
  77. *
  78. * @param[in] p_ble_hrs_c Pointer to the Heart Rate Client structure.
  79. * @param[in] p_ble_evt Pointer to the BLE event received.
  80. */
  81. static void on_hvx(ble_hrs_c_t * p_ble_hrs_c, const ble_evt_t * p_ble_evt)
  82. {
  83. // Check if the event is on the link for this instance.
  84. if (p_ble_hrs_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
  85. {
  86. NRF_LOG_DEBUG("Received HVX on link 0x%x, not associated to this instance. Ignore.",
  87. p_ble_evt->evt.gattc_evt.conn_handle);
  88. return;
  89. }
  90. NRF_LOG_DEBUG("Received HVX on link 0x%x, hrm_handle 0x%x",
  91. p_ble_evt->evt.gattc_evt.params.hvx.handle,
  92. p_ble_hrs_c->peer_hrs_db.hrm_handle);
  93. // Check if this is a heart rate notification.
  94. if (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_hrs_c->peer_hrs_db.hrm_handle)
  95. {
  96. ble_hrs_c_evt_t ble_hrs_c_evt;
  97. uint32_t index = 0;
  98. ble_hrs_c_evt.evt_type = BLE_HRS_C_EVT_HRM_NOTIFICATION;
  99. ble_hrs_c_evt.conn_handle = p_ble_hrs_c->conn_handle;
  100. ble_hrs_c_evt.params.hrm.rr_intervals_cnt = 0;
  101. if (!(p_ble_evt->evt.gattc_evt.params.hvx.data[index++] & HRM_FLAG_MASK_HR_16BIT))
  102. {
  103. // 8-bit heart rate value received.
  104. ble_hrs_c_evt.params.hrm.hr_value = p_ble_evt->evt.gattc_evt.params.hvx.data[index++]; //lint !e415 suppress Lint Warning 415: Likely access out of bond
  105. }
  106. else
  107. {
  108. // 16-bit heart rate value received.
  109. ble_hrs_c_evt.params.hrm.hr_value =
  110. uint16_decode(&(p_ble_evt->evt.gattc_evt.params.hvx.data[index]));
  111. index += sizeof(uint16_t);
  112. }
  113. if ((p_ble_evt->evt.gattc_evt.params.hvx.data[0] & HRM_FLAG_MASK_HR_RR_INT))
  114. {
  115. uint32_t i;
  116. /*lint --e{415} --e{416} --e{662} --e{661} -save suppress Warning 415: possible access out of bond */
  117. for (i = 0; i < BLE_HRS_C_RR_INTERVALS_MAX_CNT; i ++)
  118. {
  119. if (index >= p_ble_evt->evt.gattc_evt.params.hvx.len)
  120. {
  121. break;
  122. }
  123. ble_hrs_c_evt.params.hrm.rr_intervals[i] =
  124. uint16_decode(&(p_ble_evt->evt.gattc_evt.params.hvx.data[index]));
  125. index += sizeof(uint16_t);
  126. }
  127. /*lint -restore*/
  128. ble_hrs_c_evt.params.hrm.rr_intervals_cnt = (uint8_t)i;
  129. }
  130. p_ble_hrs_c->evt_handler(p_ble_hrs_c, &ble_hrs_c_evt);
  131. }
  132. }
  133. /**@brief Function for handling Disconnected event received from the SoftDevice.
  134. *
  135. * @details This function checks whether the disconnect event is happening on the link
  136. * associated with the current instance of the module. If the event is happening, the function sets the instance's
  137. * conn_handle to invalid.
  138. *
  139. * @param[in] p_ble_hrs_c Pointer to the Heart Rate Client structure.
  140. * @param[in] p_ble_evt Pointer to the BLE event received.
  141. */
  142. static void on_disconnected(ble_hrs_c_t * p_ble_hrs_c, const ble_evt_t * p_ble_evt)
  143. {
  144. if (p_ble_hrs_c->conn_handle == p_ble_evt->evt.gap_evt.conn_handle)
  145. {
  146. p_ble_hrs_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  147. p_ble_hrs_c->peer_hrs_db.hrm_cccd_handle = BLE_GATT_HANDLE_INVALID;
  148. p_ble_hrs_c->peer_hrs_db.hrm_handle = BLE_GATT_HANDLE_INVALID;
  149. }
  150. }
  151. void ble_hrs_on_db_disc_evt(ble_hrs_c_t * p_ble_hrs_c, const ble_db_discovery_evt_t * p_evt)
  152. {
  153. // Check if the Heart Rate Service was discovered.
  154. if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
  155. p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_HEART_RATE_SERVICE &&
  156. p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE)
  157. {
  158. // Find the CCCD Handle of the Heart Rate Measurement characteristic.
  159. uint32_t i;
  160. ble_hrs_c_evt_t evt;
  161. evt.evt_type = BLE_HRS_C_EVT_DISCOVERY_COMPLETE;
  162. evt.conn_handle = p_evt->conn_handle;
  163. for (i = 0; i < p_evt->params.discovered_db.char_count; i++)
  164. {
  165. if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
  166. BLE_UUID_HEART_RATE_MEASUREMENT_CHAR)
  167. {
  168. // Found Heart Rate characteristic. Store CCCD handle and break.
  169. evt.params.peer_db.hrm_cccd_handle =
  170. p_evt->params.discovered_db.charateristics[i].cccd_handle;
  171. evt.params.peer_db.hrm_handle =
  172. p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
  173. break;
  174. }
  175. }
  176. NRF_LOG_DEBUG("Heart Rate Service discovered at peer.");
  177. //If the instance has been assigned prior to db_discovery, assign the db_handles.
  178. if (p_ble_hrs_c->conn_handle != BLE_CONN_HANDLE_INVALID)
  179. {
  180. if ((p_ble_hrs_c->peer_hrs_db.hrm_cccd_handle == BLE_GATT_HANDLE_INVALID)&&
  181. (p_ble_hrs_c->peer_hrs_db.hrm_handle == BLE_GATT_HANDLE_INVALID))
  182. {
  183. p_ble_hrs_c->peer_hrs_db = evt.params.peer_db;
  184. }
  185. }
  186. p_ble_hrs_c->evt_handler(p_ble_hrs_c, &evt);
  187. }
  188. }
  189. uint32_t ble_hrs_c_init(ble_hrs_c_t * p_ble_hrs_c, ble_hrs_c_init_t * p_ble_hrs_c_init)
  190. {
  191. VERIFY_PARAM_NOT_NULL(p_ble_hrs_c);
  192. VERIFY_PARAM_NOT_NULL(p_ble_hrs_c_init);
  193. ble_uuid_t hrs_uuid;
  194. hrs_uuid.type = BLE_UUID_TYPE_BLE;
  195. hrs_uuid.uuid = BLE_UUID_HEART_RATE_SERVICE;
  196. p_ble_hrs_c->evt_handler = p_ble_hrs_c_init->evt_handler;
  197. p_ble_hrs_c->error_handler = p_ble_hrs_c_init->error_handler;
  198. p_ble_hrs_c->p_gatt_queue = p_ble_hrs_c_init->p_gatt_queue;
  199. p_ble_hrs_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  200. p_ble_hrs_c->peer_hrs_db.hrm_cccd_handle = BLE_GATT_HANDLE_INVALID;
  201. p_ble_hrs_c->peer_hrs_db.hrm_handle = BLE_GATT_HANDLE_INVALID;
  202. return ble_db_discovery_evt_register(&hrs_uuid);
  203. }
  204. void ble_hrs_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  205. {
  206. ble_hrs_c_t * p_ble_hrs_c = (ble_hrs_c_t *)p_context;
  207. if ((p_ble_hrs_c == NULL) || (p_ble_evt == NULL))
  208. {
  209. return;
  210. }
  211. switch (p_ble_evt->header.evt_id)
  212. {
  213. case BLE_GATTC_EVT_HVX:
  214. on_hvx(p_ble_hrs_c, p_ble_evt);
  215. break;
  216. case BLE_GAP_EVT_DISCONNECTED:
  217. on_disconnected(p_ble_hrs_c, p_ble_evt);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. /**@brief Function for creating a message for writing to the CCCD.
  224. */
  225. static uint32_t cccd_configure(ble_hrs_c_t * p_ble_hrs_c, bool enable)
  226. {
  227. NRF_LOG_DEBUG("Configuring CCCD. CCCD Handle = %d, Connection Handle = %d",
  228. p_ble_hrs_c->peer_hrs_db.hrm_cccd_handle,
  229. p_ble_hrs_c->conn_handle);
  230. nrf_ble_gq_req_t hrs_c_req;
  231. uint8_t cccd[BLE_CCCD_VALUE_LEN];
  232. uint16_t cccd_val = enable ? BLE_GATT_HVX_NOTIFICATION : 0;
  233. cccd[0] = LSB_16(cccd_val);
  234. cccd[1] = MSB_16(cccd_val);
  235. memset(&hrs_c_req, 0, sizeof(hrs_c_req));
  236. hrs_c_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  237. hrs_c_req.error_handler.cb = gatt_error_handler;
  238. hrs_c_req.error_handler.p_ctx = p_ble_hrs_c;
  239. hrs_c_req.params.gattc_write.handle = p_ble_hrs_c->peer_hrs_db.hrm_cccd_handle;
  240. hrs_c_req.params.gattc_write.len = BLE_CCCD_VALUE_LEN;
  241. hrs_c_req.params.gattc_write.p_value = cccd;
  242. hrs_c_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_REQ;
  243. return nrf_ble_gq_item_add(p_ble_hrs_c->p_gatt_queue, &hrs_c_req, p_ble_hrs_c->conn_handle);
  244. }
  245. uint32_t ble_hrs_c_hrm_notif_enable(ble_hrs_c_t * p_ble_hrs_c)
  246. {
  247. VERIFY_PARAM_NOT_NULL(p_ble_hrs_c);
  248. return cccd_configure(p_ble_hrs_c, true);
  249. }
  250. uint32_t ble_hrs_c_handles_assign(ble_hrs_c_t * p_ble_hrs_c,
  251. uint16_t conn_handle,
  252. const hrs_db_t * p_peer_hrs_handles)
  253. {
  254. VERIFY_PARAM_NOT_NULL(p_ble_hrs_c);
  255. p_ble_hrs_c->conn_handle = conn_handle;
  256. if (p_peer_hrs_handles != NULL)
  257. {
  258. p_ble_hrs_c->peer_hrs_db = *p_peer_hrs_handles;
  259. }
  260. return nrf_ble_gq_conn_handle_register(p_ble_hrs_c->p_gatt_queue, conn_handle);
  261. }
  262. /** @}
  263. * @endcond
  264. */
  265. #endif // NRF_MODULE_ENABLED(BLE_HRS_C)