123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- #include <stdint.h>
- #include <string.h>
- #include "ble.h"
- #include "sdk_macros.h"
- #include "ble_srv_common.h"
- #include "nrf_ble_cgms.h"
- #include "cgms_meas.h"
- #include "cgms_db.h"
- static uint8_t cgms_meas_encode(nrf_ble_cgms_t * p_cgms,
- const nrf_ble_cgms_meas_t * p_meas,
- uint8_t * p_encoded_buffer)
- {
- uint8_t len = 2;
- uint8_t flags = p_meas->flags;
- len += uint16_encode(p_meas->glucose_concentration,
- &p_encoded_buffer[len]);
- len += uint16_encode(p_meas->time_offset,
- &p_encoded_buffer[len]);
- if (p_meas->sensor_status_annunciation.warning != 0)
- {
- p_encoded_buffer[len++] = p_meas->sensor_status_annunciation.warning;
- flags |= NRF_BLE_CGMS_STATUS_FLAGS_WARNING_OCT_PRESENT;
- }
- if (p_meas->sensor_status_annunciation.calib_temp != 0)
- {
- p_encoded_buffer[len++] = p_meas->sensor_status_annunciation.calib_temp;
- flags |= NRF_BLE_CGMS_STATUS_FLAGS_CALTEMP_OCT_PRESENT;
- }
- if (p_meas->sensor_status_annunciation.status != 0)
- {
- p_encoded_buffer[len++] = p_meas->sensor_status_annunciation.status;
- flags |= NRF_BLE_CGMS_STATUS_FLAGS_STATUS_OCT_PRESENT;
- }
-
- if (p_cgms->feature.feature & NRF_BLE_CGMS_FEAT_CGM_TREND_INFORMATION_SUPPORTED)
- {
- if (flags & NRF_BLE_CGMS_FLAG_TREND_INFO_PRESENT)
- {
- len += uint16_encode(p_meas->trend, &p_encoded_buffer[len]);
- }
- }
-
- if (p_cgms->feature.feature & NRF_BLE_CGMS_FEAT_CGM_QUALITY_SUPPORTED)
- {
- if (flags & NRF_BLE_CGMS_FLAGS_QUALITY_PRESENT)
- {
- len += uint16_encode(p_meas->quality, &p_encoded_buffer[len]);
- }
- }
- p_encoded_buffer[1] = flags;
- p_encoded_buffer[0] = len;
- return len;
- }
- ret_code_t cgms_meas_char_add(nrf_ble_cgms_t * p_cgms)
- {
- uint8_t num_recs;
- uint8_t encoded_cgms_meas[NRF_BLE_CGMS_MEAS_LEN_MAX];
- ble_add_char_params_t add_char_params;
- ble_cgms_rec_t initial_cgms_rec_value;
- memset(&add_char_params, 0, sizeof(add_char_params));
- memset(&initial_cgms_rec_value, 0, sizeof(ble_cgms_rec_t));
- num_recs = cgms_db_num_records_get();
- if (num_recs > 0)
- {
- uint32_t err_code = cgms_db_record_get(num_recs - 1, &initial_cgms_rec_value);
- if (err_code != NRF_SUCCESS)
- {
- return err_code;
- }
- }
- add_char_params.uuid = BLE_UUID_CGM_MEASUREMENT;
- add_char_params.max_len = NRF_BLE_CGMS_MEAS_LEN_MAX;
- add_char_params.init_len = cgms_meas_encode(p_cgms,
- &initial_cgms_rec_value.meas,
- encoded_cgms_meas);
- add_char_params.p_init_value = encoded_cgms_meas;
- add_char_params.is_var_len = true;
- add_char_params.char_props.notify = true;
- add_char_params.cccd_write_access = SEC_JUST_WORKS;
- return characteristic_add(p_cgms->service_handle,
- &add_char_params,
- &p_cgms->char_handles.measurment);
- }
- ret_code_t cgms_meas_send(nrf_ble_cgms_t * p_cgms, ble_cgms_rec_t * p_rec, uint8_t * p_count)
- {
- uint32_t err_code;
- uint8_t encoded_meas[NRF_BLE_CGMS_MEAS_LEN_MAX + NRF_BLE_CGMS_MEAS_REC_LEN_MAX];
- uint16_t len = 0;
- uint16_t hvx_len = NRF_BLE_CGMS_MEAS_LEN_MAX;
- int i;
- ble_gatts_hvx_params_t hvx_params;
- for (i = 0; i < *p_count; i++)
- {
- uint8_t meas_len = cgms_meas_encode(p_cgms, &(p_rec[i].meas), (encoded_meas + len));
- if (len + meas_len >= NRF_BLE_CGMS_MEAS_LEN_MAX)
- {
- break;
- }
- len += meas_len;
- }
- *p_count = i;
- hvx_len = len;
- memset(&hvx_params, 0, sizeof(hvx_params));
- hvx_params.handle = p_cgms->char_handles.measurment.value_handle;
- hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
- hvx_params.offset = 0;
- hvx_params.p_len = &hvx_len;
- hvx_params.p_data = encoded_meas;
- err_code = sd_ble_gatts_hvx(p_cgms->conn_handle, &hvx_params);
- if (err_code == NRF_SUCCESS)
- {
- if (hvx_len != len)
- {
- err_code = NRF_ERROR_DATA_SIZE;
- }
- else
- {
-
- p_cgms->racp_data.racp_proc_records_reported += *p_count;
- }
- }
- return err_code;
- }
- static void on_meas_cccd_write(nrf_ble_cgms_t * p_cgms, ble_gatts_evt_write_t const * p_evt_write)
- {
- if (p_evt_write->len == 2)
- {
-
- if (p_cgms->evt_handler != NULL)
- {
- nrf_ble_cgms_evt_t evt;
- if (ble_srv_is_notification_enabled(p_evt_write->data))
- {
- evt.evt_type = NRF_BLE_CGMS_EVT_NOTIFICATION_ENABLED;
- }
- else
- {
- evt.evt_type = NRF_BLE_CGMS_EVT_NOTIFICATION_DISABLED;
- }
- p_cgms->evt_handler(p_cgms, &evt);
- }
- }
- }
- void cgms_meas_on_write(nrf_ble_cgms_t * p_cgms, ble_gatts_evt_write_t const * p_evt_write)
- {
- if (p_evt_write->handle == p_cgms->char_handles.measurment.cccd_handle)
- {
- on_meas_cccd_write(p_cgms, p_evt_write);
- }
- }
|