ble_hts.c 13 KB

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