ble_hrs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /* Attention!
  41. * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
  42. * qualification listings, this section of source code must not be modified.
  43. */
  44. #include "sdk_common.h"
  45. #if NRF_MODULE_ENABLED(BLE_HRS)
  46. #include "ble_hrs.h"
  47. #include <string.h>
  48. #include "ble_srv_common.h"
  49. #define OPCODE_LENGTH 1 /**< Length of opcode inside Heart Rate Measurement packet. */
  50. #define HANDLE_LENGTH 2 /**< Length of handle inside Heart Rate Measurement packet. */
  51. #define MAX_HRM_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Heart Rate Measurement. */
  52. #define INITIAL_VALUE_HRM 0 /**< Initial Heart Rate Measurement value. */
  53. // Heart Rate Measurement flag bits
  54. #define HRM_FLAG_MASK_HR_VALUE_16BIT (0x01 << 0) /**< Heart Rate Value Format bit. */
  55. #define HRM_FLAG_MASK_SENSOR_CONTACT_DETECTED (0x01 << 1) /**< Sensor Contact Detected bit. */
  56. #define HRM_FLAG_MASK_SENSOR_CONTACT_SUPPORTED (0x01 << 2) /**< Sensor Contact Supported bit. */
  57. #define HRM_FLAG_MASK_EXPENDED_ENERGY_INCLUDED (0x01 << 3) /**< Energy Expended Status bit. Feature Not Supported */
  58. #define HRM_FLAG_MASK_RR_INTERVAL_INCLUDED (0x01 << 4) /**< RR-Interval bit. */
  59. /**@brief Function for handling the Connect event.
  60. *
  61. * @param[in] p_hrs Heart Rate Service structure.
  62. * @param[in] p_ble_evt Event received from the BLE stack.
  63. */
  64. static void on_connect(ble_hrs_t * p_hrs, ble_evt_t const * p_ble_evt)
  65. {
  66. p_hrs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  67. }
  68. /**@brief Function for handling the Disconnect event.
  69. *
  70. * @param[in] p_hrs Heart Rate Service structure.
  71. * @param[in] p_ble_evt Event received from the BLE stack.
  72. */
  73. static void on_disconnect(ble_hrs_t * p_hrs, ble_evt_t const * p_ble_evt)
  74. {
  75. UNUSED_PARAMETER(p_ble_evt);
  76. p_hrs->conn_handle = BLE_CONN_HANDLE_INVALID;
  77. }
  78. /**@brief Function for handling write events to the Heart Rate Measurement characteristic.
  79. *
  80. * @param[in] p_hrs Heart Rate Service structure.
  81. * @param[in] p_evt_write Write event received from the BLE stack.
  82. */
  83. static void on_hrm_cccd_write(ble_hrs_t * p_hrs, ble_gatts_evt_write_t const * p_evt_write)
  84. {
  85. if (p_evt_write->len == 2)
  86. {
  87. // CCCD written, update notification state
  88. if (p_hrs->evt_handler != NULL)
  89. {
  90. ble_hrs_evt_t evt;
  91. if (ble_srv_is_notification_enabled(p_evt_write->data))
  92. {
  93. evt.evt_type = BLE_HRS_EVT_NOTIFICATION_ENABLED;
  94. }
  95. else
  96. {
  97. evt.evt_type = BLE_HRS_EVT_NOTIFICATION_DISABLED;
  98. }
  99. p_hrs->evt_handler(p_hrs, &evt);
  100. }
  101. }
  102. }
  103. /**@brief Function for handling the Write event.
  104. *
  105. * @param[in] p_hrs Heart Rate Service structure.
  106. * @param[in] p_ble_evt Event received from the BLE stack.
  107. */
  108. static void on_write(ble_hrs_t * p_hrs, ble_evt_t const * p_ble_evt)
  109. {
  110. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  111. if (p_evt_write->handle == p_hrs->hrm_handles.cccd_handle)
  112. {
  113. on_hrm_cccd_write(p_hrs, p_evt_write);
  114. }
  115. }
  116. void ble_hrs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  117. {
  118. ble_hrs_t * p_hrs = (ble_hrs_t *) p_context;
  119. switch (p_ble_evt->header.evt_id)
  120. {
  121. case BLE_GAP_EVT_CONNECTED:
  122. on_connect(p_hrs, p_ble_evt);
  123. break;
  124. case BLE_GAP_EVT_DISCONNECTED:
  125. on_disconnect(p_hrs, p_ble_evt);
  126. break;
  127. case BLE_GATTS_EVT_WRITE:
  128. on_write(p_hrs, p_ble_evt);
  129. break;
  130. default:
  131. // No implementation needed.
  132. break;
  133. }
  134. }
  135. /**@brief Function for encoding a Heart Rate Measurement.
  136. *
  137. * @param[in] p_hrs Heart Rate Service structure.
  138. * @param[in] heart_rate Measurement to be encoded.
  139. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  140. *
  141. * @return Size of encoded data.
  142. */
  143. static uint8_t hrm_encode(ble_hrs_t * p_hrs, uint16_t heart_rate, uint8_t * p_encoded_buffer)
  144. {
  145. uint8_t flags = 0;
  146. uint8_t len = 1;
  147. int i;
  148. // Set sensor contact related flags
  149. if (p_hrs->is_sensor_contact_supported)
  150. {
  151. flags |= HRM_FLAG_MASK_SENSOR_CONTACT_SUPPORTED;
  152. }
  153. if (p_hrs->is_sensor_contact_detected)
  154. {
  155. flags |= HRM_FLAG_MASK_SENSOR_CONTACT_DETECTED;
  156. }
  157. // Encode heart rate measurement
  158. if (heart_rate > 0xff)
  159. {
  160. flags |= HRM_FLAG_MASK_HR_VALUE_16BIT;
  161. len += uint16_encode(heart_rate, &p_encoded_buffer[len]);
  162. }
  163. else
  164. {
  165. p_encoded_buffer[len++] = (uint8_t)heart_rate;
  166. }
  167. // Encode rr_interval values
  168. if (p_hrs->rr_interval_count > 0)
  169. {
  170. flags |= HRM_FLAG_MASK_RR_INTERVAL_INCLUDED;
  171. }
  172. for (i = 0; i < p_hrs->rr_interval_count; i++)
  173. {
  174. if (len + sizeof(uint16_t) > p_hrs->max_hrm_len)
  175. {
  176. // Not all stored rr_interval values can fit into the encoded hrm,
  177. // move the remaining values to the start of the buffer.
  178. memmove(&p_hrs->rr_interval[0],
  179. &p_hrs->rr_interval[i],
  180. (p_hrs->rr_interval_count - i) * sizeof(uint16_t));
  181. break;
  182. }
  183. len += uint16_encode(p_hrs->rr_interval[i], &p_encoded_buffer[len]);
  184. }
  185. p_hrs->rr_interval_count -= i;
  186. // Add flags
  187. p_encoded_buffer[0] = flags;
  188. return len;
  189. }
  190. uint32_t ble_hrs_init(ble_hrs_t * p_hrs, const ble_hrs_init_t * p_hrs_init)
  191. {
  192. uint32_t err_code;
  193. ble_uuid_t ble_uuid;
  194. ble_add_char_params_t add_char_params;
  195. uint8_t encoded_initial_hrm[MAX_HRM_LEN];
  196. // Initialize service structure
  197. p_hrs->evt_handler = p_hrs_init->evt_handler;
  198. p_hrs->is_sensor_contact_supported = p_hrs_init->is_sensor_contact_supported;
  199. p_hrs->conn_handle = BLE_CONN_HANDLE_INVALID;
  200. p_hrs->is_sensor_contact_detected = false;
  201. p_hrs->rr_interval_count = 0;
  202. p_hrs->max_hrm_len = MAX_HRM_LEN;
  203. // Add service
  204. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_HEART_RATE_SERVICE);
  205. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  206. &ble_uuid,
  207. &p_hrs->service_handle);
  208. if (err_code != NRF_SUCCESS)
  209. {
  210. return err_code;
  211. }
  212. // Add heart rate measurement characteristic
  213. memset(&add_char_params, 0, sizeof(add_char_params));
  214. add_char_params.uuid = BLE_UUID_HEART_RATE_MEASUREMENT_CHAR;
  215. add_char_params.max_len = MAX_HRM_LEN;
  216. add_char_params.init_len = hrm_encode(p_hrs, INITIAL_VALUE_HRM, encoded_initial_hrm);
  217. add_char_params.p_init_value = encoded_initial_hrm;
  218. add_char_params.is_var_len = true;
  219. add_char_params.char_props.notify = 1;
  220. add_char_params.cccd_write_access = p_hrs_init->hrm_cccd_wr_sec;
  221. err_code = characteristic_add(p_hrs->service_handle, &add_char_params, &(p_hrs->hrm_handles));
  222. if (err_code != NRF_SUCCESS)
  223. {
  224. return err_code;
  225. }
  226. if (p_hrs_init->p_body_sensor_location != NULL)
  227. {
  228. // Add body sensor location characteristic
  229. memset(&add_char_params, 0, sizeof(add_char_params));
  230. add_char_params.uuid = BLE_UUID_BODY_SENSOR_LOCATION_CHAR;
  231. add_char_params.max_len = sizeof(uint8_t);
  232. add_char_params.init_len = sizeof(uint8_t);
  233. add_char_params.p_init_value = p_hrs_init->p_body_sensor_location;
  234. add_char_params.char_props.read = 1;
  235. add_char_params.read_access = p_hrs_init->bsl_rd_sec;
  236. err_code = characteristic_add(p_hrs->service_handle, &add_char_params, &(p_hrs->bsl_handles));
  237. if (err_code != NRF_SUCCESS)
  238. {
  239. return err_code;
  240. }
  241. }
  242. return NRF_SUCCESS;
  243. }
  244. uint32_t ble_hrs_heart_rate_measurement_send(ble_hrs_t * p_hrs, uint16_t heart_rate)
  245. {
  246. uint32_t err_code;
  247. // Send value if connected and notifying
  248. if (p_hrs->conn_handle != BLE_CONN_HANDLE_INVALID)
  249. {
  250. uint8_t encoded_hrm[MAX_HRM_LEN];
  251. uint16_t len;
  252. uint16_t hvx_len;
  253. ble_gatts_hvx_params_t hvx_params;
  254. len = hrm_encode(p_hrs, heart_rate, encoded_hrm);
  255. hvx_len = len;
  256. memset(&hvx_params, 0, sizeof(hvx_params));
  257. hvx_params.handle = p_hrs->hrm_handles.value_handle;
  258. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  259. hvx_params.offset = 0;
  260. hvx_params.p_len = &hvx_len;
  261. hvx_params.p_data = encoded_hrm;
  262. err_code = sd_ble_gatts_hvx(p_hrs->conn_handle, &hvx_params);
  263. if ((err_code == NRF_SUCCESS) && (hvx_len != len))
  264. {
  265. err_code = NRF_ERROR_DATA_SIZE;
  266. }
  267. }
  268. else
  269. {
  270. err_code = NRF_ERROR_INVALID_STATE;
  271. }
  272. return err_code;
  273. }
  274. void ble_hrs_rr_interval_add(ble_hrs_t * p_hrs, uint16_t rr_interval)
  275. {
  276. if (p_hrs->rr_interval_count == BLE_HRS_MAX_BUFFERED_RR_INTERVALS)
  277. {
  278. // The rr_interval buffer is full, delete the oldest value
  279. memmove(&p_hrs->rr_interval[0],
  280. &p_hrs->rr_interval[1],
  281. (BLE_HRS_MAX_BUFFERED_RR_INTERVALS - 1) * sizeof(uint16_t));
  282. p_hrs->rr_interval_count--;
  283. }
  284. // Add new value
  285. p_hrs->rr_interval[p_hrs->rr_interval_count++] = rr_interval;
  286. }
  287. bool ble_hrs_rr_interval_buffer_is_full(ble_hrs_t * p_hrs)
  288. {
  289. return (p_hrs->rr_interval_count == BLE_HRS_MAX_BUFFERED_RR_INTERVALS);
  290. }
  291. uint32_t ble_hrs_sensor_contact_supported_set(ble_hrs_t * p_hrs, bool is_sensor_contact_supported)
  292. {
  293. // Check if we are connected to peer
  294. if (p_hrs->conn_handle == BLE_CONN_HANDLE_INVALID)
  295. {
  296. p_hrs->is_sensor_contact_supported = is_sensor_contact_supported;
  297. return NRF_SUCCESS;
  298. }
  299. else
  300. {
  301. return NRF_ERROR_INVALID_STATE;
  302. }
  303. }
  304. void ble_hrs_sensor_contact_detected_update(ble_hrs_t * p_hrs, bool is_sensor_contact_detected)
  305. {
  306. p_hrs->is_sensor_contact_detected = is_sensor_contact_detected;
  307. }
  308. uint32_t ble_hrs_body_sensor_location_set(ble_hrs_t * p_hrs, uint8_t body_sensor_location)
  309. {
  310. ble_gatts_value_t gatts_value;
  311. // Initialize value struct.
  312. memset(&gatts_value, 0, sizeof(gatts_value));
  313. gatts_value.len = sizeof(uint8_t);
  314. gatts_value.offset = 0;
  315. gatts_value.p_value = &body_sensor_location;
  316. return sd_ble_gatts_value_set(p_hrs->conn_handle, p_hrs->bsl_handles.value_handle, &gatts_value);
  317. }
  318. void ble_hrs_on_gatt_evt(ble_hrs_t * p_hrs, nrf_ble_gatt_evt_t const * p_gatt_evt)
  319. {
  320. if ( (p_hrs->conn_handle == p_gatt_evt->conn_handle)
  321. && (p_gatt_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
  322. {
  323. p_hrs->max_hrm_len = p_gatt_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  324. }
  325. }
  326. #endif // NRF_MODULE_ENABLED(BLE_HRS)