cgms_sst.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <time.h>
  43. #include "ble.h"
  44. #include "ble_srv_common.h"
  45. #include "nrf_ble_cgms.h"
  46. #include "cgms_sst.h"
  47. void sst_decode(ble_cgms_sst_t * p_sst, const uint8_t * p_data, const uint16_t len)
  48. {
  49. uint32_t index;
  50. if (len != NRF_BLE_CGMS_SST_LEN)
  51. {
  52. return;
  53. }
  54. index = ble_date_time_decode(&p_sst->date_time, p_data);
  55. p_sst->time_zone = p_data[index++];
  56. p_sst->dst = p_data[index++];
  57. }
  58. void convert_ble_time_c_time(ble_cgms_sst_t * p_sst, struct tm * p_c_time_date)
  59. {
  60. p_c_time_date->tm_sec = p_sst->date_time.seconds;
  61. p_c_time_date->tm_min = p_sst->date_time.minutes;
  62. p_c_time_date->tm_hour = p_sst->date_time.hours;
  63. p_c_time_date->tm_mday = p_sst->date_time.day;
  64. p_c_time_date->tm_mon = p_sst->date_time.month;
  65. p_c_time_date->tm_year = p_sst->date_time.year - 1900;
  66. // Ignore daylight saving for this conversion.
  67. p_c_time_date->tm_isdst = 0;
  68. }
  69. void calc_sst(uint16_t offset, struct tm * p_c_time_date)
  70. {
  71. time_t c_time_in_sec;
  72. c_time_in_sec = mktime(p_c_time_date);
  73. c_time_in_sec = c_time_in_sec - (offset * 60);
  74. *p_c_time_date = *(localtime(&c_time_in_sec));
  75. if (p_c_time_date->tm_isdst == 1)
  76. {
  77. // Daylight saving time is not used and must be removed.
  78. p_c_time_date->tm_hour = p_c_time_date->tm_hour - 1;
  79. p_c_time_date->tm_isdst = 0;
  80. }
  81. }
  82. static void convert_c_time_ble_time(ble_cgms_sst_t * p_sst, struct tm * p_c_time_date)
  83. {
  84. p_sst->date_time.seconds = p_c_time_date->tm_sec;
  85. p_sst->date_time.minutes = p_c_time_date->tm_min;
  86. p_sst->date_time.hours = p_c_time_date->tm_hour;
  87. p_sst->date_time.day = p_c_time_date->tm_mday;
  88. p_sst->date_time.month = p_c_time_date->tm_mon;
  89. p_sst->date_time.year = p_c_time_date->tm_year + 1900;
  90. }
  91. static uint8_t sst_encode(ble_cgms_sst_t * p_sst, uint8_t * p_encoded_sst)
  92. {
  93. uint8_t len;
  94. len = ble_date_time_encode(&p_sst->date_time, p_encoded_sst);
  95. p_encoded_sst[len++] = p_sst->time_zone;
  96. p_encoded_sst[len++] = p_sst->dst;
  97. return len;
  98. }
  99. static ret_code_t cgm_update_sst(nrf_ble_cgms_t * p_cgms, ble_gatts_evt_write_t const * p_evt_write)
  100. {
  101. ble_cgms_sst_t sst;
  102. struct tm c_time_and_date;
  103. memset(&sst, 0, sizeof(ble_cgms_sst_t));
  104. sst_decode(&sst, p_evt_write->data, p_evt_write->len);
  105. convert_ble_time_c_time(&sst, &c_time_and_date);
  106. calc_sst(p_cgms->sensor_status.time_offset, &c_time_and_date);
  107. convert_c_time_ble_time(&sst, &c_time_and_date);
  108. return cgms_sst_set(p_cgms, &sst);
  109. }
  110. /**@brief Function for handling the Glucose session start time write event.
  111. *
  112. * @param[in] p_cgms Service instance.
  113. * @param[in] p_evt_write WRITE event to be handled.
  114. */
  115. static void on_sst_value_write(nrf_ble_cgms_t * p_cgms, ble_gatts_evt_write_t const * p_evt_write)
  116. {
  117. ble_gatts_rw_authorize_reply_params_t auth_reply;
  118. uint32_t err_code;
  119. memset(&auth_reply, 0, sizeof(auth_reply));
  120. auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  121. auth_reply.params.write.update = 1;
  122. auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  123. err_code = sd_ble_gatts_rw_authorize_reply(p_cgms->conn_handle, &auth_reply);
  124. if (err_code != NRF_SUCCESS)
  125. {
  126. if (p_cgms->error_handler != NULL)
  127. {
  128. p_cgms->error_handler(err_code);
  129. }
  130. }
  131. err_code = cgm_update_sst(p_cgms, p_evt_write);
  132. if (err_code != NRF_SUCCESS)
  133. {
  134. if (p_cgms->error_handler != NULL)
  135. {
  136. p_cgms->error_handler(err_code);
  137. }
  138. }
  139. }
  140. /**@brief Function for adding a characteristic for the Session Start Time.
  141. *
  142. * @param[in] p_cgms Service instance.
  143. *
  144. * @return NRF_SUCCESS if characteristic was successfully added, otherwise an error code.
  145. */
  146. ret_code_t cgms_sst_char_add(nrf_ble_cgms_t * p_cgms)
  147. {
  148. ble_add_char_params_t add_char_params;
  149. uint8_t init_value[NRF_BLE_CGMS_SST_LEN] = {0};
  150. memset(&add_char_params, 0, sizeof(add_char_params));
  151. add_char_params.uuid = BLE_UUID_CGM_SESSION_START_TIME;
  152. add_char_params.max_len = NRF_BLE_CGMS_SST_LEN;
  153. add_char_params.init_len = NRF_BLE_CGMS_SST_LEN;
  154. add_char_params.p_init_value = init_value;
  155. add_char_params.read_access = SEC_JUST_WORKS;
  156. add_char_params.write_access = SEC_JUST_WORKS;
  157. add_char_params.is_defered_write = 1;
  158. add_char_params.char_props.write = true;
  159. add_char_params.char_props.read = true;
  160. return characteristic_add(p_cgms->service_handle,
  161. &add_char_params,
  162. &p_cgms->char_handles.sst);
  163. }
  164. ret_code_t cgms_sst_set(nrf_ble_cgms_t * p_cgms, ble_cgms_sst_t * p_sst)
  165. {
  166. uint16_t conn_handle;
  167. uint16_t value_handle;
  168. ble_gatts_value_t sst_val;
  169. uint8_t encoded_start_session_time[NRF_BLE_CGMS_SST_LEN];
  170. uint8_t gatts_value_set_len = 0;
  171. gatts_value_set_len = sst_encode(p_sst, encoded_start_session_time);
  172. conn_handle = p_cgms->conn_handle;
  173. value_handle = p_cgms->char_handles.sst.value_handle;
  174. memset(&sst_val, 0, sizeof(ble_gatts_value_t));
  175. sst_val.len = gatts_value_set_len;
  176. sst_val.p_value = encoded_start_session_time;
  177. sst_val.offset = 0;
  178. return (sd_ble_gatts_value_set(conn_handle, value_handle, &sst_val));
  179. }
  180. void cgms_sst_on_rw_auth_req(nrf_ble_cgms_t * p_cgms,
  181. ble_gatts_evt_rw_authorize_request_t const * p_auth_req)
  182. {
  183. if (p_auth_req->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
  184. {
  185. if (p_auth_req->request.write.handle == p_cgms->char_handles.sst.value_handle)
  186. {
  187. on_sst_value_write(p_cgms, &p_auth_req->request.write);
  188. }
  189. }
  190. }