cgms_meas.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Copyright (c) 2016 - 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 <stdint.h>
  41. #include <string.h>
  42. #include "ble.h"
  43. #include "sdk_macros.h"
  44. #include "ble_srv_common.h"
  45. #include "nrf_ble_cgms.h"
  46. #include "cgms_meas.h"
  47. #include "cgms_db.h"
  48. /**@brief Function for encoding a Glucose measurement.
  49. *
  50. * @param[in] p_meas Measurement to be encoded.
  51. * @param[out] p_encoded_buffer Pointer to buffer where the encoded measurement is to be stored.
  52. *
  53. * @return Size of encoded measurement.
  54. */
  55. static uint8_t cgms_meas_encode(nrf_ble_cgms_t * p_cgms,
  56. const nrf_ble_cgms_meas_t * p_meas,
  57. uint8_t * p_encoded_buffer)
  58. {
  59. uint8_t len = 2;
  60. uint8_t flags = p_meas->flags;
  61. len += uint16_encode(p_meas->glucose_concentration,
  62. &p_encoded_buffer[len]);
  63. len += uint16_encode(p_meas->time_offset,
  64. &p_encoded_buffer[len]);
  65. if (p_meas->sensor_status_annunciation.warning != 0)
  66. {
  67. p_encoded_buffer[len++] = p_meas->sensor_status_annunciation.warning;
  68. flags |= NRF_BLE_CGMS_STATUS_FLAGS_WARNING_OCT_PRESENT;
  69. }
  70. if (p_meas->sensor_status_annunciation.calib_temp != 0)
  71. {
  72. p_encoded_buffer[len++] = p_meas->sensor_status_annunciation.calib_temp;
  73. flags |= NRF_BLE_CGMS_STATUS_FLAGS_CALTEMP_OCT_PRESENT;
  74. }
  75. if (p_meas->sensor_status_annunciation.status != 0)
  76. {
  77. p_encoded_buffer[len++] = p_meas->sensor_status_annunciation.status;
  78. flags |= NRF_BLE_CGMS_STATUS_FLAGS_STATUS_OCT_PRESENT;
  79. }
  80. // Trend field
  81. if (p_cgms->feature.feature & NRF_BLE_CGMS_FEAT_CGM_TREND_INFORMATION_SUPPORTED)
  82. {
  83. if (flags & NRF_BLE_CGMS_FLAG_TREND_INFO_PRESENT)
  84. {
  85. len += uint16_encode(p_meas->trend, &p_encoded_buffer[len]);
  86. }
  87. }
  88. // Quality field
  89. if (p_cgms->feature.feature & NRF_BLE_CGMS_FEAT_CGM_QUALITY_SUPPORTED)
  90. {
  91. if (flags & NRF_BLE_CGMS_FLAGS_QUALITY_PRESENT)
  92. {
  93. len += uint16_encode(p_meas->quality, &p_encoded_buffer[len]);
  94. }
  95. }
  96. p_encoded_buffer[1] = flags;
  97. p_encoded_buffer[0] = len;
  98. return len;
  99. }
  100. /**@brief Function for adding a characteristic for the Continuous Glucose Meter Measurement.
  101. *
  102. * @param[in] p_cgms Service instance.
  103. *
  104. * @return NRF_SUCCESS if characteristic was successfully added, otherwise an error code.
  105. */
  106. ret_code_t cgms_meas_char_add(nrf_ble_cgms_t * p_cgms)
  107. {
  108. uint8_t num_recs;
  109. uint8_t encoded_cgms_meas[NRF_BLE_CGMS_MEAS_LEN_MAX];
  110. ble_add_char_params_t add_char_params;
  111. ble_cgms_rec_t initial_cgms_rec_value;
  112. memset(&add_char_params, 0, sizeof(add_char_params));
  113. memset(&initial_cgms_rec_value, 0, sizeof(ble_cgms_rec_t));
  114. num_recs = cgms_db_num_records_get();
  115. if (num_recs > 0)
  116. {
  117. uint32_t err_code = cgms_db_record_get(num_recs - 1, &initial_cgms_rec_value);
  118. if (err_code != NRF_SUCCESS)
  119. {
  120. return err_code;
  121. }
  122. }
  123. add_char_params.uuid = BLE_UUID_CGM_MEASUREMENT;
  124. add_char_params.max_len = NRF_BLE_CGMS_MEAS_LEN_MAX;
  125. add_char_params.init_len = cgms_meas_encode(p_cgms,
  126. &initial_cgms_rec_value.meas,
  127. encoded_cgms_meas);
  128. add_char_params.p_init_value = encoded_cgms_meas;
  129. add_char_params.is_var_len = true;
  130. add_char_params.char_props.notify = true;
  131. add_char_params.cccd_write_access = SEC_JUST_WORKS;
  132. return characteristic_add(p_cgms->service_handle,
  133. &add_char_params,
  134. &p_cgms->char_handles.measurment);
  135. }
  136. ret_code_t cgms_meas_send(nrf_ble_cgms_t * p_cgms, ble_cgms_rec_t * p_rec, uint8_t * p_count)
  137. {
  138. uint32_t err_code;
  139. uint8_t encoded_meas[NRF_BLE_CGMS_MEAS_LEN_MAX + NRF_BLE_CGMS_MEAS_REC_LEN_MAX];
  140. uint16_t len = 0;
  141. uint16_t hvx_len = NRF_BLE_CGMS_MEAS_LEN_MAX;
  142. int i;
  143. ble_gatts_hvx_params_t hvx_params;
  144. for (i = 0; i < *p_count; i++)
  145. {
  146. uint8_t meas_len = cgms_meas_encode(p_cgms, &(p_rec[i].meas), (encoded_meas + len));
  147. if (len + meas_len >= NRF_BLE_CGMS_MEAS_LEN_MAX)
  148. {
  149. break;
  150. }
  151. len += meas_len;
  152. }
  153. *p_count = i;
  154. hvx_len = len;
  155. memset(&hvx_params, 0, sizeof(hvx_params));
  156. hvx_params.handle = p_cgms->char_handles.measurment.value_handle;
  157. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  158. hvx_params.offset = 0;
  159. hvx_params.p_len = &hvx_len;
  160. hvx_params.p_data = encoded_meas;
  161. err_code = sd_ble_gatts_hvx(p_cgms->conn_handle, &hvx_params);
  162. if (err_code == NRF_SUCCESS)
  163. {
  164. if (hvx_len != len)
  165. {
  166. err_code = NRF_ERROR_DATA_SIZE;
  167. }
  168. else
  169. {
  170. // Measurement successfully sent
  171. p_cgms->racp_data.racp_proc_records_reported += *p_count;
  172. }
  173. }
  174. return err_code;
  175. }
  176. /**@brief Function for handling the Glucose measurement CCCD write event.
  177. *
  178. * @param[in] p_cgms Service instance.
  179. * @param[in] p_evt_write WRITE event to be handled.
  180. */
  181. static void on_meas_cccd_write(nrf_ble_cgms_t * p_cgms, ble_gatts_evt_write_t const * p_evt_write)
  182. {
  183. if (p_evt_write->len == 2)
  184. {
  185. // CCCD written, update notification state
  186. if (p_cgms->evt_handler != NULL)
  187. {
  188. nrf_ble_cgms_evt_t evt;
  189. if (ble_srv_is_notification_enabled(p_evt_write->data))
  190. {
  191. evt.evt_type = NRF_BLE_CGMS_EVT_NOTIFICATION_ENABLED;
  192. }
  193. else
  194. {
  195. evt.evt_type = NRF_BLE_CGMS_EVT_NOTIFICATION_DISABLED;
  196. }
  197. p_cgms->evt_handler(p_cgms, &evt);
  198. }
  199. }
  200. }
  201. /**@brief Function for handling the WRITE event.
  202. *
  203. * @details Handles WRITE events from the BLE stack.
  204. *
  205. * @param[in] p_cgms Glucose Service structure.
  206. * @param[in] p_ble_evt Event received from the BLE stack.
  207. */
  208. void cgms_meas_on_write(nrf_ble_cgms_t * p_cgms, ble_gatts_evt_write_t const * p_evt_write)
  209. {
  210. if (p_evt_write->handle == p_cgms->char_handles.measurment.cccd_handle)
  211. {
  212. on_meas_cccd_write(p_cgms, p_evt_write);
  213. }
  214. }