ble_bps.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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_BPS)
  46. #include "ble_bps.h"
  47. #include "ble_err.h"
  48. #include <string.h>
  49. #include "nordic_common.h"
  50. #include "ble_srv_common.h"
  51. #define OPCODE_LENGTH 1 /**< Length of opcode inside Blood Pressure Measurement packet. */
  52. #define HANDLE_LENGTH 2 /**< Length of handle inside Blood Pressure Measurement packet. */
  53. #define MAX_BPM_LEN (BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Blood Pressure Measurement. */
  54. // Blood Pressure Measurement Flags bits
  55. #define BPS_MEAS_BLOOD_PRESSURE_UNITS_FLAG_BIT (0x01 << 0) /**< Blood Pressure Units Flag bit. */
  56. #define BPS_MEAS_TIME_STAMP_FLAG_BIT (0x01 << 1) /**< Time Stamp Flag bit. */
  57. #define BPS_MEAS_PULSE_RATE_FLAG_BIT (0x01 << 2) /**< Pulse Rate Flag bit. */
  58. #define BPS_MEAS_USER_ID_FLAG_BIT (0x01 << 3) /**< User ID Flag bit. */
  59. #define BPS_MEAS_MEASUREMENT_STATUS_FLAG_BIT (0x01 << 4) /**< Measurement Status Flag bit. */
  60. /**@brief Function for interception of GATT errors and @ref nrf_ble_gq errors.
  61. *
  62. * @param[in] nrf_error Error code.
  63. * @param[in] p_ctx Parameter from the event handler.
  64. * @param[in] conn_handle Connection handle.
  65. */
  66. static void gatt_error_handler(uint32_t nrf_error,
  67. void * p_ctx,
  68. uint16_t conn_handle)
  69. {
  70. ble_bps_t * p_bps = (ble_bps_t *)p_ctx;
  71. if (p_bps->error_handler != NULL)
  72. {
  73. p_bps->error_handler(nrf_error);
  74. }
  75. }
  76. /**@brief Function for handling the Connect event.
  77. *
  78. * @param[in] p_bps Blood Pressure Service structure.
  79. * @param[in] p_ble_evt Event received from the BLE stack.
  80. */
  81. static void on_connect(ble_bps_t * p_bps, ble_evt_t const * p_ble_evt)
  82. {
  83. ret_code_t err_code;
  84. p_bps->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  85. err_code = nrf_ble_gq_conn_handle_register(p_bps->p_gatt_queue,
  86. p_ble_evt->evt.gap_evt.conn_handle);
  87. if ((p_bps->error_handler != NULL) &&
  88. (err_code != NRF_SUCCESS))
  89. {
  90. p_bps->error_handler(err_code);
  91. }
  92. }
  93. /**@brief Function for handling the Disconnect event.
  94. *
  95. * @param[in] p_bps Blood Pressure Service structure.
  96. * @param[in] p_ble_evt Event received from the BLE stack.
  97. */
  98. static void on_disconnect(ble_bps_t * p_bps, ble_evt_t const * p_ble_evt)
  99. {
  100. UNUSED_PARAMETER(p_ble_evt);
  101. p_bps->conn_handle = BLE_CONN_HANDLE_INVALID;
  102. }
  103. /**@brief Function for handling the write events to the Blood Pressure Measurement characteristic.
  104. *
  105. * @param[in] p_bps Blood Pressure Service structure.
  106. * @param[in] p_evt_write Write event received from the BLE stack.
  107. */
  108. static void on_cccd_write(ble_bps_t * p_bps, ble_gatts_evt_write_t const * p_evt_write)
  109. {
  110. if (p_evt_write->len == 2)
  111. {
  112. // CCCD written, update indication state
  113. if (p_bps->evt_handler != NULL)
  114. {
  115. ble_bps_evt_t evt;
  116. if (ble_srv_is_indication_enabled(p_evt_write->data))
  117. {
  118. evt.evt_type = BLE_BPS_EVT_INDICATION_ENABLED;
  119. }
  120. else
  121. {
  122. evt.evt_type = BLE_BPS_EVT_INDICATION_DISABLED;
  123. }
  124. p_bps->evt_handler(p_bps, &evt);
  125. }
  126. }
  127. }
  128. /**@brief Function for handling the Write event.
  129. *
  130. * @param[in] p_bps Blood Pressure Service structure.
  131. * @param[in] p_ble_evt Event received from the BLE stack.
  132. */
  133. static void on_write(ble_bps_t * p_bps, ble_evt_t const * p_ble_evt)
  134. {
  135. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  136. if (p_evt_write->handle == p_bps->meas_handles.cccd_handle)
  137. {
  138. on_cccd_write(p_bps, p_evt_write);
  139. }
  140. }
  141. /**@brief Function for handling the HVC event.
  142. *
  143. * @details Handles HVC events from the BLE stack.
  144. *
  145. * @param[in] p_bps Blood Pressure Service structure.
  146. * @param[in] p_ble_evt Event received from the BLE stack.
  147. */
  148. static void on_hvc(ble_bps_t * p_bps, ble_evt_t const * p_ble_evt)
  149. {
  150. ble_gatts_evt_hvc_t const * p_hvc = &p_ble_evt->evt.gatts_evt.params.hvc;
  151. if (p_hvc->handle == p_bps->meas_handles.value_handle)
  152. {
  153. ble_bps_evt_t evt;
  154. evt.evt_type = BLE_BPS_EVT_INDICATION_CONFIRMED;
  155. p_bps->evt_handler(p_bps, &evt);
  156. }
  157. }
  158. void ble_bps_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  159. {
  160. ble_bps_t * p_bps = (ble_bps_t *)p_context;
  161. switch (p_ble_evt->header.evt_id)
  162. {
  163. case BLE_GAP_EVT_CONNECTED:
  164. on_connect(p_bps, p_ble_evt);
  165. break;
  166. case BLE_GAP_EVT_DISCONNECTED:
  167. on_disconnect(p_bps, p_ble_evt);
  168. break;
  169. case BLE_GATTS_EVT_WRITE:
  170. on_write(p_bps, p_ble_evt);
  171. break;
  172. case BLE_GATTS_EVT_HVC:
  173. on_hvc(p_bps, p_ble_evt);
  174. break;
  175. default:
  176. // No implementation needed.
  177. break;
  178. }
  179. }
  180. /**@brief Function for encoding a Blood Pressure Measurement.
  181. *
  182. * @param[in] p_bps Blood Pressure Service structure.
  183. * @param[in] p_bps_meas Measurement to be encoded.
  184. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  185. *
  186. * @return Size of encoded data.
  187. */
  188. static uint8_t bps_measurement_encode(ble_bps_t * p_bps,
  189. ble_bps_meas_t * p_bps_meas,
  190. uint8_t * p_encoded_buffer)
  191. {
  192. uint8_t flags = 0;
  193. uint8_t len = 1;
  194. uint16_t encoded_sfloat;
  195. // Set measurement units flag
  196. if (p_bps_meas->blood_pressure_units_in_kpa)
  197. {
  198. flags |= BPS_MEAS_BLOOD_PRESSURE_UNITS_FLAG_BIT;
  199. }
  200. // Blood Pressure Measurement - Systolic
  201. encoded_sfloat = ((p_bps_meas->blood_pressure_systolic.exponent << 12) & 0xF000) |
  202. ((p_bps_meas->blood_pressure_systolic.mantissa << 0) & 0x0FFF);
  203. len += uint16_encode(encoded_sfloat, &p_encoded_buffer[len]);
  204. // Blood Pressure Measurement - Diastolic
  205. encoded_sfloat = ((p_bps_meas->blood_pressure_diastolic.exponent << 12) & 0xF000) |
  206. ((p_bps_meas->blood_pressure_diastolic.mantissa << 0) & 0x0FFF);
  207. len += uint16_encode(encoded_sfloat, &p_encoded_buffer[len]);
  208. // Blood Pressure Measurement - Mean Arterial Pressure
  209. encoded_sfloat = ((p_bps_meas->mean_arterial_pressure.exponent << 12) & 0xF000) |
  210. ((p_bps_meas->mean_arterial_pressure.mantissa << 0) & 0x0FFF);
  211. len += uint16_encode(encoded_sfloat, &p_encoded_buffer[len]);
  212. // Time Stamp field
  213. if (p_bps_meas->time_stamp_present)
  214. {
  215. flags |= BPS_MEAS_TIME_STAMP_FLAG_BIT;
  216. len += ble_date_time_encode(&p_bps_meas->time_stamp, &p_encoded_buffer[len]);
  217. }
  218. // Pulse Rate
  219. if (p_bps_meas->pulse_rate_present)
  220. {
  221. flags |= BPS_MEAS_PULSE_RATE_FLAG_BIT;
  222. encoded_sfloat = ((p_bps_meas->pulse_rate.exponent << 12) & 0xF000) |
  223. ((p_bps_meas->pulse_rate.mantissa << 0) & 0x0FFF);
  224. len += uint16_encode(encoded_sfloat, &p_encoded_buffer[len]);
  225. }
  226. // User ID
  227. if (p_bps_meas->user_id_present)
  228. {
  229. flags |= BPS_MEAS_USER_ID_FLAG_BIT;
  230. p_encoded_buffer[len++] = p_bps_meas->user_id;
  231. }
  232. // Measurement Status
  233. if (p_bps_meas->measurement_status_present)
  234. {
  235. flags |= BPS_MEAS_MEASUREMENT_STATUS_FLAG_BIT;
  236. len += uint16_encode(p_bps_meas->measurement_status, &p_encoded_buffer[len]);
  237. }
  238. // Flags field
  239. p_encoded_buffer[0] = flags;
  240. return len;
  241. }
  242. uint32_t ble_bps_init(ble_bps_t * p_bps, ble_bps_init_t const * p_bps_init)
  243. {
  244. VERIFY_PARAM_NOT_NULL(p_bps);
  245. VERIFY_PARAM_NOT_NULL(p_bps_init);
  246. VERIFY_PARAM_NOT_NULL(p_bps_init->p_gatt_queue);
  247. uint32_t err_code;
  248. uint8_t init_value_encoded[MAX_BPM_LEN];
  249. uint8_t initial_feature_len;
  250. ble_bps_meas_t initial_bpm;
  251. ble_uuid_t ble_uuid;
  252. ble_add_char_params_t add_char_params;
  253. // Initialize service structure
  254. p_bps->evt_handler = p_bps_init->evt_handler;
  255. p_bps->error_handler = p_bps_init->error_handler;
  256. p_bps->p_gatt_queue = p_bps_init->p_gatt_queue;
  257. p_bps->conn_handle = BLE_CONN_HANDLE_INVALID;
  258. p_bps->feature = p_bps_init->feature;
  259. // Add service
  260. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_BLOOD_PRESSURE_SERVICE);
  261. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_bps->service_handle);
  262. if (err_code != NRF_SUCCESS)
  263. {
  264. return err_code;
  265. }
  266. // Add measurement characteristic
  267. memset(&initial_bpm, 0, sizeof(initial_bpm));
  268. memset(&add_char_params, 0, sizeof(add_char_params));
  269. add_char_params.uuid = BLE_UUID_BLOOD_PRESSURE_MEASUREMENT_CHAR;
  270. add_char_params.max_len = MAX_BPM_LEN;
  271. add_char_params.is_var_len = true;
  272. add_char_params.init_len = bps_measurement_encode(p_bps, &initial_bpm, init_value_encoded);
  273. add_char_params.p_init_value = init_value_encoded;
  274. add_char_params.char_props.indicate = 1;
  275. add_char_params.cccd_write_access = p_bps_init->bp_cccd_wr_sec;
  276. err_code = characteristic_add(p_bps->service_handle, &add_char_params, &p_bps->meas_handles);
  277. if (err_code != NRF_SUCCESS)
  278. {
  279. return err_code;
  280. }
  281. // Add feature characteristic
  282. initial_feature_len = uint16_encode(p_bps_init->feature, init_value_encoded);
  283. memset(&add_char_params, 0, sizeof(add_char_params));
  284. add_char_params.uuid = BLE_UUID_BLOOD_PRESSURE_FEATURE_CHAR;
  285. add_char_params.max_len = initial_feature_len;
  286. add_char_params.init_len = initial_feature_len;
  287. add_char_params.p_init_value = init_value_encoded;
  288. add_char_params.char_props.read = 1;
  289. add_char_params.read_access = p_bps_init->bp_feature_rd_sec;
  290. err_code = characteristic_add(p_bps->service_handle, &add_char_params, &p_bps->feature_handles);
  291. if (err_code != NRF_SUCCESS)
  292. {
  293. return err_code;
  294. }
  295. return NRF_SUCCESS;
  296. }
  297. uint32_t ble_bps_measurement_send(ble_bps_t * p_bps, ble_bps_meas_t * p_bps_meas)
  298. {
  299. uint32_t err_code;
  300. // Send value if connected
  301. if (p_bps->conn_handle != BLE_CONN_HANDLE_INVALID)
  302. {
  303. uint8_t encoded_bps_meas[MAX_BPM_LEN];
  304. uint16_t len;
  305. nrf_ble_gq_req_t bps_req;
  306. len = bps_measurement_encode(p_bps, p_bps_meas, encoded_bps_meas);
  307. memset(&bps_req, 0, sizeof(nrf_ble_gq_req_t));
  308. bps_req.type = NRF_BLE_GQ_REQ_GATTS_HVX;
  309. bps_req.error_handler.cb = gatt_error_handler;
  310. bps_req.error_handler.p_ctx = p_bps;
  311. bps_req.params.gatts_hvx.handle = p_bps->meas_handles.value_handle;
  312. bps_req.params.gatts_hvx.offset = 0;
  313. bps_req.params.gatts_hvx.p_data = encoded_bps_meas;
  314. bps_req.params.gatts_hvx.p_len = &len;
  315. bps_req.params.gatts_hvx.type = BLE_GATT_HVX_INDICATION;
  316. err_code = nrf_ble_gq_item_add(p_bps->p_gatt_queue, &bps_req, p_bps->conn_handle);
  317. }
  318. else
  319. {
  320. err_code = NRF_ERROR_INVALID_STATE;
  321. }
  322. return err_code;
  323. }
  324. uint32_t ble_bps_is_indication_enabled(ble_bps_t * p_bps, bool * p_indication_enabled)
  325. {
  326. uint32_t err_code;
  327. uint8_t cccd_value_buf[BLE_CCCD_VALUE_LEN];
  328. ble_gatts_value_t gatts_value;
  329. // Initialize value struct.
  330. memset(&gatts_value, 0, sizeof(gatts_value));
  331. gatts_value.len = BLE_CCCD_VALUE_LEN;
  332. gatts_value.offset = 0;
  333. gatts_value.p_value = cccd_value_buf;
  334. err_code = sd_ble_gatts_value_get(p_bps->conn_handle,
  335. p_bps->meas_handles.cccd_handle,
  336. &gatts_value);
  337. if (err_code == NRF_SUCCESS)
  338. {
  339. *p_indication_enabled = ble_srv_is_indication_enabled(cccd_value_buf);
  340. }
  341. if (err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING)
  342. {
  343. *p_indication_enabled = false;
  344. return NRF_SUCCESS;
  345. }
  346. return err_code;
  347. }
  348. #endif // NRF_MODULE_ENABLED(BLE_BPS)