diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-23 17:08:59 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-23 17:12:21 +0200 |
commit | 3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a (patch) | |
tree | ab49cc16ed0b853452c5c2ed2d3042416d628986 /thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls | |
download | iot-sensors-3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a.tar.gz iot-sensors-3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a.tar.bz2 iot-sensors-3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a.tar.xz iot-sensors-3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a.zip |
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls')
4 files changed, 1901 insertions, 0 deletions
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls.c new file mode 100644 index 0000000..2fe5f70 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls.c @@ -0,0 +1,1314 @@ +/** + * Copyright (c) 2012 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +/* Attention! + * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile + * qualification listings, this section of source code must not be modified. + */ + +#include "sdk_common.h" + +#if NRF_MODULE_ENABLED(BLE_GLS) + +#include "ble_gls.h" +#include <string.h> +#include "ble_gls_db.h" +#include "ble_racp.h" +#include "ble_srv_common.h" + + +#define OPERAND_FILTER_TYPE_SEQ_NUM 0x01 /**< Filter data using Sequence Number criteria. */ +#define OPERAND_FILTER_TYPE_FACING_TIME 0x02 /**< Filter data using User Facing Time criteria. */ +#define OPERAND_FILTER_TYPE_RFU_START 0x07 /**< Start of filter types reserved For Future Use range */ +#define OPERAND_FILTER_TYPE_RFU_END 0xFF /**< End of filter types reserved For Future Use range */ + +#define OPCODE_LENGTH 1 /**< Length of opcode inside Glucose Measurement packet. */ +#define HANDLE_LENGTH 2 /**< Length of handle inside Glucose Measurement packet. */ +#define MAX_GLM_LEN (BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Glucose Measurement. */ + +#define GLS_NACK_PROC_ALREADY_IN_PROGRESS BLE_GATT_STATUS_ATTERR_APP_BEGIN + 0 /**< Reply when a requested procedure is already in progress. */ +#define GLS_NACK_CCCD_IMPROPERLY_CONFIGURED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 1 /**< Reply when the a s CCCD is improperly configured. */ + +/**@brief Glucose Service communication state. */ +typedef enum +{ + STATE_NO_COMM, /**< The service is not in a communicating state. */ + STATE_RACP_PROC_ACTIVE, /**< Processing requested data. */ + STATE_RACP_RESPONSE_PENDING, /**< There is a RACP indication waiting to be sent. */ + STATE_RACP_RESPONSE_IND_VERIF /**< Waiting for a verification of a RACP indication. */ +} gls_state_t; + +static gls_state_t m_gls_state; /**< Current communication state. */ +static uint16_t m_next_seq_num; /**< Sequence number of the next database record. */ +static uint8_t m_racp_proc_operator; /**< Operator of current request. */ +static uint16_t m_racp_proc_seq_num; /**< Sequence number of current request. */ +static uint8_t m_racp_proc_record_ndx; /**< Current record index. */ +static uint8_t m_racp_proc_records_reported; /**< Number of reported records. */ +static uint8_t m_racp_proc_records_reported_since_txcomplete; /**< Number of reported records since last TX_COMPLETE event. */ +static ble_racp_value_t m_pending_racp_response; /**< RACP response to be sent. */ +static uint8_t m_pending_racp_response_operand[2]; /**< Operand of RACP response to be sent. */ + + +/**@brief Function for setting the GLS communication state. + * + * @param[in] new_state New communication state. + */ +static void state_set(gls_state_t new_state) +{ + m_gls_state = new_state; +} + + +/**@brief Function for setting the next sequence number by reading the last record in the data base. + * + * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. + */ +static uint32_t next_sequence_number_set(void) +{ + uint16_t num_records; + ble_gls_rec_t rec; + + num_records = ble_gls_db_num_records_get(); + if (num_records > 0) + { + // Get last record + uint32_t err_code = ble_gls_db_record_get(num_records - 1, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + m_next_seq_num = rec.meas.sequence_number + 1; + } + else + { + m_next_seq_num = 0; + } + + return NRF_SUCCESS; +} + + +/**@brief Function for encoding a Glucose measurement. + * + * @param[in] p_meas Measurement to be encoded. + * @param[out] p_encoded_buffer Pointer to buffer where the encoded measurement is to be stored. + * + * @return Size of encoded measurement. + */ +static uint8_t gls_meas_encode(const ble_gls_meas_t * p_meas, uint8_t * p_encoded_buffer) +{ + uint8_t len = 0; + + p_encoded_buffer[len++] = p_meas->flags; + + len += uint16_encode(p_meas->sequence_number, &p_encoded_buffer[len]); + len += ble_date_time_encode(&p_meas->base_time, &p_encoded_buffer[len]); + + if (p_meas->flags & BLE_GLS_MEAS_FLAG_TIME_OFFSET) + { + len += uint16_encode(p_meas->time_offset, &p_encoded_buffer[len]); + } + + if (p_meas->flags & BLE_GLS_MEAS_FLAG_CONC_TYPE_LOC) + { + uint16_t encoded_concentration; + + encoded_concentration = ((p_meas->glucose_concentration.exponent << 12) & 0xF000) | + ((p_meas->glucose_concentration.mantissa << 0) & 0x0FFF); + + p_encoded_buffer[len++] = (uint8_t)(encoded_concentration); + p_encoded_buffer[len++] = (uint8_t)(encoded_concentration >> 8); + p_encoded_buffer[len++] = (p_meas->sample_location << 4) | (p_meas->type & 0x0F); + } + + if (p_meas->flags & BLE_GLS_MEAS_FLAG_SENSOR_STATUS) + { + len += uint16_encode(p_meas->sensor_status_annunciation, &p_encoded_buffer[len]); + } + + return len; +} + + +/**@brief Function for adding the characteristic for a glucose measurement. + * + * @param[in] p_gls Service instance. + * + * @return NRF_SUCCESS if characteristic was successfully added, otherwise an error code. + */ +static uint32_t glucose_measurement_char_add(ble_gls_t * p_gls) +{ + ble_gatts_char_md_t char_md; + ble_gatts_attr_md_t cccd_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + ble_gls_rec_t initial_gls_rec_value; + uint8_t encoded_gls_meas[MAX_GLM_LEN]; + uint8_t num_recs; + memset(&cccd_md, 0, sizeof(cccd_md)); + + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&cccd_md.write_perm); + cccd_md.vloc = BLE_GATTS_VLOC_STACK; + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.notify = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = &cccd_md; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_GLUCOSE_MEASUREMENT_CHAR); + memset(&attr_md, 0, sizeof(attr_md)); + + BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); + + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 1; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + memset(&initial_gls_rec_value, 0, sizeof(initial_gls_rec_value)); + + num_recs = ble_gls_db_num_records_get(); + if (num_recs > 0) + { + uint32_t err_code = ble_gls_db_record_get(num_recs - 1, &initial_gls_rec_value); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = gls_meas_encode(&initial_gls_rec_value.meas, encoded_gls_meas); + attr_char_value.init_offs = 0; + attr_char_value.max_len = MAX_GLM_LEN; + attr_char_value.p_value = encoded_gls_meas; + + return sd_ble_gatts_characteristic_add(p_gls->service_handle, + &char_md, + &attr_char_value, + &p_gls->glm_handles); +} + + +/**@brief Function for adding the characteristic for a glucose feature. + * + * @param[in] p_gls Service instance. + * + * @return NRF_SUCCESS if characteristic was successfully added, otherwise an error code. + */ +static uint32_t glucose_feature_char_add(ble_gls_t * p_gls) +{ + ble_gatts_char_md_t char_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + uint8_t encoded_initial_feature[2]; + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.read = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = NULL; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_GLUCOSE_FEATURE_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); + + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 0; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + encoded_initial_feature[0] = (uint8_t)(p_gls->feature); + encoded_initial_feature[1] = (uint8_t)((p_gls->feature) >> 8); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = sizeof (uint16_t); + attr_char_value.init_offs = 0; + attr_char_value.max_len = sizeof (uint16_t); + attr_char_value.p_value = encoded_initial_feature; + + return sd_ble_gatts_characteristic_add(p_gls->service_handle, + &char_md, + &attr_char_value, + &p_gls->glf_handles); +} + + +/**@brief Function for adding the characteristic for a record access control point. + * + * @param[in] p_gls Service instance. + * + * @return NRF_SUCCESS if characteristic was successfully added, otherwise an error code. + */ +static uint32_t record_access_control_point_char_add(ble_gls_t * p_gls) +{ + ble_gatts_char_md_t char_md; + ble_gatts_attr_md_t cccd_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + + memset(&cccd_md, 0, sizeof(cccd_md)); + + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&cccd_md.write_perm); + cccd_md.vloc = BLE_GATTS_VLOC_STACK; + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.indicate = 1; + char_md.char_props.write = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = &cccd_md; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_RECORD_ACCESS_CONTROL_POINT_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.write_perm); + + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 1; + attr_md.vlen = 1; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = 0; + attr_char_value.init_offs = 0; + attr_char_value.max_len = BLE_GATT_ATT_MTU_DEFAULT; + attr_char_value.p_value = 0; + + return sd_ble_gatts_characteristic_add(p_gls->service_handle, + &char_md, + &attr_char_value, + &p_gls->racp_handles); +} + + +uint32_t ble_gls_init(ble_gls_t * p_gls, const ble_gls_init_t * p_gls_init) +{ + uint32_t err_code; + ble_uuid_t ble_uuid; + + // Initialize data base + err_code = ble_gls_db_init(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + err_code = next_sequence_number_set(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Initialize service structure + p_gls->evt_handler = p_gls_init->evt_handler; + p_gls->error_handler = p_gls_init->error_handler; + p_gls->feature = p_gls_init->feature; + p_gls->is_context_supported = p_gls_init->is_context_supported; + p_gls->conn_handle = BLE_CONN_HANDLE_INVALID; + + + // Initialize global variables + state_set(STATE_NO_COMM); + m_racp_proc_records_reported_since_txcomplete = 0; + + // Add service + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_GLUCOSE_SERVICE); + + err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_gls->service_handle); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add glucose measurement characteristic + err_code = glucose_measurement_char_add(p_gls); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add glucose measurement feature characteristic + err_code = glucose_feature_char_add(p_gls); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add record control access point characteristic + err_code = record_access_control_point_char_add(p_gls); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + return NRF_SUCCESS; +} + + +/**@brief Function for sending a response from the Record Access Control Point. + * + * @param[in] p_gls Service instance. + * @param[in] p_racp_val RACP value to be sent. + */ +static void racp_send(ble_gls_t * p_gls, ble_racp_value_t * p_racp_val) +{ + uint32_t err_code; + uint8_t encoded_resp[25]; + uint8_t len; + uint16_t hvx_len; + ble_gatts_hvx_params_t hvx_params; + + if ( + (m_gls_state != STATE_RACP_RESPONSE_PENDING) + && + (m_racp_proc_records_reported_since_txcomplete > 0) + ) + { + state_set(STATE_RACP_RESPONSE_PENDING); + return; + } + + // Send indication + len = ble_racp_encode(p_racp_val, encoded_resp); + hvx_len = len; + + memset(&hvx_params, 0, sizeof(hvx_params)); + + hvx_params.handle = p_gls->racp_handles.value_handle; + hvx_params.type = BLE_GATT_HVX_INDICATION; + hvx_params.offset = 0; + hvx_params.p_len = &hvx_len; + hvx_params.p_data = encoded_resp; + + err_code = sd_ble_gatts_hvx(p_gls->conn_handle, &hvx_params); + + // Error handling + if ((err_code == NRF_SUCCESS) && (hvx_len != len)) + { + err_code = NRF_ERROR_DATA_SIZE; + } + switch (err_code) + { + case NRF_SUCCESS: + // Wait for HVC event + state_set(STATE_RACP_RESPONSE_IND_VERIF); + break; + + case NRF_ERROR_BUSY: + // Wait for BLE_GATTS_EVT_HVC event to retry transmission + state_set(STATE_RACP_RESPONSE_PENDING); + break; + + case NRF_ERROR_INVALID_STATE: + // Make sure state machine returns to the default state + state_set(STATE_NO_COMM); + break; + + default: + // Report error to application + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + + // Make sure state machine returns to the default state + state_set(STATE_NO_COMM); + break; + } +} + + +/**@brief Function for sending a RACP response containing a Response Code Op Code and a Response Code Value. + * + * @param[in] p_gls Service instance. + * @param[in] opcode RACP Op Code. + * @param[in] value RACP Response Code Value. + */ +static void racp_response_code_send(ble_gls_t * p_gls, uint8_t opcode, uint8_t value) +{ + m_pending_racp_response.opcode = RACP_OPCODE_RESPONSE_CODE; + m_pending_racp_response.operator = RACP_OPERATOR_NULL; + m_pending_racp_response.operand_len = 2; + m_pending_racp_response.p_operand = m_pending_racp_response_operand; + + m_pending_racp_response_operand[0] = opcode; + m_pending_racp_response_operand[1] = value; + + racp_send(p_gls, &m_pending_racp_response); +} + + +/**@brief Function for sending a glucose measurement/context. + * + * @param[in] p_gls Service instance. + * @param[in] p_rec Measurement to be sent. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t glucose_meas_send(ble_gls_t * p_gls, ble_gls_rec_t * p_rec) +{ + uint32_t err_code; + uint8_t encoded_glm[MAX_GLM_LEN]; + uint16_t len; + uint16_t hvx_len; + ble_gatts_hvx_params_t hvx_params; + + len = gls_meas_encode(&p_rec->meas, encoded_glm); + hvx_len = len; + + memset(&hvx_params, 0, sizeof (hvx_params)); + + hvx_params.handle = p_gls->glm_handles.value_handle; + hvx_params.type = BLE_GATT_HVX_NOTIFICATION; + hvx_params.offset = 0; + hvx_params.p_len = &hvx_len; + hvx_params.p_data = encoded_glm; + + err_code = sd_ble_gatts_hvx(p_gls->conn_handle, &hvx_params); + if (err_code == NRF_SUCCESS) + { + if (hvx_len != len) + { + err_code = NRF_ERROR_DATA_SIZE; + } + else + { + // Measurement successfully sent + m_racp_proc_records_reported++; + m_racp_proc_records_reported_since_txcomplete++; + } + } + + return err_code; +} + + +/**@brief Function for responding to the ALL operation. + * + * @param[in] p_gls Service instance. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t racp_report_records_all(ble_gls_t * p_gls) +{ + uint16_t total_records = ble_gls_db_num_records_get(); + + if (m_racp_proc_record_ndx >= total_records) + { + state_set(STATE_NO_COMM); + } + else + { + uint32_t err_code; + ble_gls_rec_t rec; + + err_code = ble_gls_db_record_get(m_racp_proc_record_ndx, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + err_code = glucose_meas_send(p_gls, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + return NRF_SUCCESS; +} + + +/**@brief Function for responding to the FIRST or the LAST operation. + * + * @param[in] p_gls Service instance. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t racp_report_records_first_last(ble_gls_t * p_gls) +{ + uint32_t err_code; + ble_gls_rec_t rec; + uint16_t total_records; + + total_records = ble_gls_db_num_records_get(); + + if ((m_racp_proc_records_reported != 0) || (total_records == 0)) + { + state_set(STATE_NO_COMM); + } + else + { + if (m_racp_proc_operator == RACP_OPERATOR_FIRST) + { + err_code = ble_gls_db_record_get(0, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + else if (m_racp_proc_operator == RACP_OPERATOR_LAST) + { + err_code = ble_gls_db_record_get(total_records - 1, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + err_code = glucose_meas_send(p_gls, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + return NRF_SUCCESS; +} + + +/**@brief Function for responding to the GREATER_OR_EQUAL operation. + * + * @param[in] p_gls Service instance. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t racp_report_records_greater_or_equal(ble_gls_t * p_gls) +{ + uint16_t total_records = ble_gls_db_num_records_get(); + + while (m_racp_proc_record_ndx < total_records) + { + uint32_t err_code; + ble_gls_rec_t rec; + + err_code = ble_gls_db_record_get(m_racp_proc_record_ndx, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + if (rec.meas.sequence_number >= m_racp_proc_seq_num) + { + err_code = glucose_meas_send(p_gls, &rec); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + break; + } + m_racp_proc_record_ndx++; + } + if (m_racp_proc_record_ndx == total_records) + { + state_set(STATE_NO_COMM); + } + + return NRF_SUCCESS; +} + + +/**@brief Function for informing that the REPORT RECORDS procedure is completed. + * + * @param[in] p_gls Service instance. + */ +static void racp_report_records_completed(ble_gls_t * p_gls) +{ + uint8_t resp_code_value; + + if (m_racp_proc_records_reported > 0) + { + resp_code_value = RACP_RESPONSE_SUCCESS; + } + else + { + resp_code_value = RACP_RESPONSE_NO_RECORDS_FOUND; + } + + racp_response_code_send(p_gls, RACP_OPCODE_REPORT_RECS, resp_code_value); +} + + +/**@brief Function for the RACP report records procedure. + * + * @param[in] p_gls Service instance. + */ +static void racp_report_records_procedure(ble_gls_t * p_gls) +{ + uint32_t err_code; + + while (m_gls_state == STATE_RACP_PROC_ACTIVE) + { + // Execute requested procedure + switch (m_racp_proc_operator) + { + case RACP_OPERATOR_ALL: + err_code = racp_report_records_all(p_gls); + break; + + case RACP_OPERATOR_FIRST: + case RACP_OPERATOR_LAST: + err_code = racp_report_records_first_last(p_gls); + break; + + case RACP_OPERATOR_GREATER_OR_EQUAL: + err_code = racp_report_records_greater_or_equal(p_gls); + break; + + default: + // Report error to application + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(NRF_ERROR_INTERNAL); + } + + // Make sure state machine returns to the default state + state_set(STATE_NO_COMM); + return; + } + + // Error handling + switch (err_code) + { + case NRF_SUCCESS: + if (m_gls_state == STATE_RACP_PROC_ACTIVE) + { + m_racp_proc_record_ndx++; + } + else + { + racp_report_records_completed(p_gls); + } + break; + + case NRF_ERROR_RESOURCES: + // Wait for TX_COMPLETE event to resume transmission + return; + + case NRF_ERROR_INVALID_STATE: + // Notification is probably not enabled. Ignore request. + state_set(STATE_NO_COMM); + return; + + default: + // Report error to application + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + + // Make sure state machine returns to the default state + state_set(STATE_NO_COMM); + return; + } + } +} + + +/**@brief Function for testing if the received request is to be executed. + * + * @param[in] p_racp_request Request to be checked. + * @param[out] p_response_code Response code to be sent in case the request is rejected. + * RACP_RESPONSE_RESERVED is returned if the received message is + * to be rejected without sending a response. + * + * @return TRUE if the request is to be executed, FALSE if it is to be rejected. + * If it is to be rejected, p_response_code will contain the response code to be + * returned to the central. + */ +static bool is_request_to_be_executed(ble_racp_value_t const * p_racp_request, + uint8_t * p_response_code) +{ + *p_response_code = RACP_RESPONSE_RESERVED; + + if (p_racp_request->opcode == RACP_OPCODE_ABORT_OPERATION) + { + if (m_gls_state == STATE_RACP_PROC_ACTIVE) + { + if (p_racp_request->operator != RACP_OPERATOR_NULL) + { + *p_response_code = RACP_RESPONSE_INVALID_OPERATOR; + } + else if (p_racp_request->operand_len != 0) + { + *p_response_code = RACP_RESPONSE_INVALID_OPERAND; + } + else + { + *p_response_code = RACP_RESPONSE_SUCCESS; + } + } + else + { + *p_response_code = RACP_RESPONSE_ABORT_FAILED; + } + } + else if (m_gls_state != STATE_NO_COMM) + { + return false; + } + // Supported opcodes. + else if ((p_racp_request->opcode == RACP_OPCODE_REPORT_RECS) || + (p_racp_request->opcode == RACP_OPCODE_REPORT_NUM_RECS)) + { + switch (p_racp_request->operator) + { + // Operators WITHOUT a filter. + case RACP_OPERATOR_ALL: + case RACP_OPERATOR_FIRST: + case RACP_OPERATOR_LAST: + if (p_racp_request->operand_len != 0) + { + *p_response_code = RACP_RESPONSE_INVALID_OPERAND; + } + break; + + // Operators WITH a filter. + case RACP_OPERATOR_GREATER_OR_EQUAL: + if (p_racp_request->p_operand[0] == OPERAND_FILTER_TYPE_SEQ_NUM) + { + if (p_racp_request->operand_len != 3) + { + *p_response_code = RACP_RESPONSE_INVALID_OPERAND; + } + } + else if (p_racp_request->p_operand[0] == OPERAND_FILTER_TYPE_FACING_TIME) + { + *p_response_code = RACP_RESPONSE_OPERAND_UNSUPPORTED; + } + else if (p_racp_request->p_operand[0] >= OPERAND_FILTER_TYPE_RFU_START) + { + *p_response_code = RACP_RESPONSE_OPERAND_UNSUPPORTED; + } + else + { + *p_response_code = RACP_RESPONSE_INVALID_OPERAND; + } + break; + + // Unsupported operators. + case RACP_OPERATOR_LESS_OR_EQUAL: + case RACP_OPERATOR_RANGE: + *p_response_code = RACP_RESPONSE_OPERATOR_UNSUPPORTED; + break; + + // Invalid operators. + case RACP_OPERATOR_NULL: + default: + if (p_racp_request->operator >= RACP_OPERATOR_RFU_START) + { + *p_response_code = RACP_RESPONSE_OPERATOR_UNSUPPORTED; + } + else + { + *p_response_code = RACP_RESPONSE_INVALID_OPERATOR; + } + break; + } + } + // Unsupported opcodes, + else if (p_racp_request->opcode == RACP_OPCODE_DELETE_RECS) + { + *p_response_code = RACP_RESPONSE_OPCODE_UNSUPPORTED; + } + // Unknown opcodes. + else + { + *p_response_code = RACP_RESPONSE_OPCODE_UNSUPPORTED; + } + + // NOTE: The computation of the return value will change slightly when deferred write has been + // implemented in the stack. + return (*p_response_code == RACP_RESPONSE_RESERVED); +} + + +/**@brief Function for processing a REPORT RECORDS request. + * + * @param[in] p_gls Service instance. + * @param[in] p_racp_request Request to be executed. + */ +static void report_records_request_execute(ble_gls_t * p_gls, ble_racp_value_t * p_racp_request) +{ + uint16_t seq_num = (p_racp_request->p_operand[2] << 8) | p_racp_request->p_operand[1]; + + state_set(STATE_RACP_PROC_ACTIVE); + + m_racp_proc_record_ndx = 0; + m_racp_proc_operator = p_racp_request->operator; + m_racp_proc_records_reported = 0; + m_racp_proc_seq_num = seq_num; + + racp_report_records_procedure(p_gls); +} + + +/**@brief Function for processing a REPORT NUM RECORDS request. + * + * @param[in] p_gls Service instance. + * @param[in] p_racp_request Request to be executed. + */ +static void report_num_records_request_execute(ble_gls_t * p_gls, ble_racp_value_t * p_racp_request) +{ + uint16_t total_records; + uint16_t num_records; + + total_records = ble_gls_db_num_records_get(); + num_records = 0; + + if (p_racp_request->operator == RACP_OPERATOR_ALL) + { + num_records = total_records; + } + else if (p_racp_request->operator == RACP_OPERATOR_GREATER_OR_EQUAL) + { + uint16_t seq_num; + uint16_t i; + + seq_num = (p_racp_request->p_operand[2] << 8) | p_racp_request->p_operand[1]; + + for (i = 0; i < total_records; i++) + { + uint32_t err_code; + ble_gls_rec_t rec; + + err_code = ble_gls_db_record_get(i, &rec); + if (err_code != NRF_SUCCESS) + { + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + return; + } + + if (rec.meas.sequence_number >= seq_num) + { + num_records++; + } + } + } + else if ((p_racp_request->operator == RACP_OPERATOR_FIRST) || + (p_racp_request->operator == RACP_OPERATOR_LAST)) + { + if (total_records > 0) + { + num_records = 1; + } + } + + m_pending_racp_response.opcode = RACP_OPCODE_NUM_RECS_RESPONSE; + m_pending_racp_response.operator = RACP_OPERATOR_NULL; + m_pending_racp_response.operand_len = sizeof(uint16_t); + m_pending_racp_response.p_operand = m_pending_racp_response_operand; + + m_pending_racp_response_operand[0] = num_records & 0xFF; + m_pending_racp_response_operand[1] = num_records >> 8; + + racp_send(p_gls, &m_pending_racp_response); +} + + +/**@brief Function for checking if the CCCDs are configured. + * + * @param[in] p_gls Service instance. + * @param[in] p_are_cccd_configured boolean indicating if both cccds are configured + */ +uint32_t ble_gls_are_cccd_configured(ble_gls_t * p_gls, bool * p_are_cccd_configured) +{ + uint32_t err_code; + uint8_t cccd_value_buf[BLE_CCCD_VALUE_LEN]; + bool is_glm_notif_enabled = false; + bool is_racp_indic_enabled = false; + ble_gatts_value_t gatts_value; + + // Initialize value struct. + memset(&gatts_value, 0, sizeof(gatts_value)); + + gatts_value.len = BLE_CCCD_VALUE_LEN; + gatts_value.offset = 0; + gatts_value.p_value = cccd_value_buf; + + err_code = sd_ble_gatts_value_get(p_gls->conn_handle, + p_gls->glm_handles.cccd_handle, + &gatts_value); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + is_glm_notif_enabled = ble_srv_is_notification_enabled(cccd_value_buf); + + err_code = sd_ble_gatts_value_get(p_gls->conn_handle, + p_gls->racp_handles.cccd_handle, + &gatts_value); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + is_racp_indic_enabled = ble_srv_is_indication_enabled(cccd_value_buf); + if (is_racp_indic_enabled & is_glm_notif_enabled) + { + *p_are_cccd_configured = true; + } + else + { + *p_are_cccd_configured = false; + } + return NRF_SUCCESS; +} + + +/**@brief Function for handling a write event to the Record Access Control Point. + * + * @param[in] p_gls Service instance. + * @param[in] p_evt_write WRITE event to be handled. + */ +static void on_racp_value_write(ble_gls_t * p_gls, ble_gatts_evt_write_t const * p_evt_write) +{ + ble_racp_value_t racp_request; + uint8_t response_code; + ble_gatts_rw_authorize_reply_params_t auth_reply; + bool are_cccd_configured; + uint32_t err_code; + + auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE; + auth_reply.params.write.offset = 0; + auth_reply.params.write.len = 0; + auth_reply.params.write.p_data = NULL; + + err_code = ble_gls_are_cccd_configured(p_gls, &are_cccd_configured); + if (err_code != NRF_SUCCESS) + { + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + return; + } + + if (!are_cccd_configured) + { + auth_reply.params.write.gatt_status = GLS_NACK_CCCD_IMPROPERLY_CONFIGURED; + err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle, + &auth_reply); + + if (err_code != NRF_SUCCESS) + { + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + } + return; + } + + // Decode request. + ble_racp_decode(p_evt_write->len, (uint8_t*)p_evt_write->data, &racp_request); + + // Check if request is to be executed. + if (is_request_to_be_executed(&racp_request, &response_code)) + { + auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS; + auth_reply.params.write.update = 1; + + err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle, + &auth_reply); + + if (err_code != NRF_SUCCESS) + { + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + return; + } + // Execute request. + if (racp_request.opcode == RACP_OPCODE_REPORT_RECS) + { + report_records_request_execute(p_gls, &racp_request); + } + else if (racp_request.opcode == RACP_OPCODE_REPORT_NUM_RECS) + { + report_num_records_request_execute(p_gls, &racp_request); + } + } + else if (response_code != RACP_RESPONSE_RESERVED) + { + auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS; + auth_reply.params.write.update = 1; + err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle, + &auth_reply); + + if (err_code != NRF_SUCCESS) + { + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + return; + } + + // Abort any running procedure. + state_set(STATE_NO_COMM); + + // Respond with error code. + racp_response_code_send(p_gls, racp_request.opcode, response_code); + } + else + { + auth_reply.params.write.gatt_status = GLS_NACK_PROC_ALREADY_IN_PROGRESS; + err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle, + &auth_reply); + + if (err_code != NRF_SUCCESS) + { + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(err_code); + } + return; + } + } +} + + +/**@brief Function for handling the Glucose measurement CCCD write event. + * + * @param[in] p_gls Service instance. + * @param[in] p_evt_write WRITE event to be handled. + */ +static void on_glm_cccd_write(ble_gls_t * p_gls, ble_gatts_evt_write_t const * p_evt_write) +{ + if (p_evt_write->len == 2) + { + // CCCD written, update notification state + ble_gls_evt_t evt; + + if (ble_srv_is_notification_enabled(p_evt_write->data)) + { + evt.evt_type = BLE_GLS_EVT_NOTIFICATION_ENABLED; + } + else + { + evt.evt_type = BLE_GLS_EVT_NOTIFICATION_DISABLED; + } + + if (p_gls->evt_handler != NULL) + { + p_gls->evt_handler(p_gls, &evt); + } + } +} + + +/**@brief Function for handling the WRITE event. + * + * @details Handles WRITE events from the BLE stack. + * + * @param[in] p_gls Glucose Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_write(ble_gls_t * p_gls, ble_evt_t const * p_ble_evt) +{ + ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; + + if (p_evt_write->handle == p_gls->glm_handles.cccd_handle) + { + on_glm_cccd_write(p_gls, p_evt_write); + } + else if (p_evt_write->handle == p_gls->racp_handles.value_handle) + { + on_racp_value_write(p_gls, p_evt_write); + } +} + + +/**@brief Function for handling the TX_COMPLETE event. + * + * @details Handles TX_COMPLETE events from the BLE stack. + * + * @param[in] p_gls Glucose Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_tx_complete(ble_gls_t * p_gls, ble_evt_t const * p_ble_evt) +{ + m_racp_proc_records_reported_since_txcomplete = 0; + + if (m_gls_state == STATE_RACP_RESPONSE_PENDING) + { + racp_send(p_gls, &m_pending_racp_response); + } + else if (m_gls_state == STATE_RACP_PROC_ACTIVE) + { + racp_report_records_procedure(p_gls); + } +} + + +/**@brief Function for handling the HVC event. + * + * @details Handles HVC events from the BLE stack. + * + * @param[in] p_gls Glucose Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_hvc(ble_gls_t * p_gls, ble_evt_t const * p_ble_evt) +{ + ble_gatts_evt_hvc_t const * p_hvc = &p_ble_evt->evt.gatts_evt.params.hvc; + + if (p_hvc->handle == p_gls->racp_handles.value_handle) + { + if (m_gls_state == STATE_RACP_RESPONSE_IND_VERIF) + { + // Indication has been acknowledged. Return to default state. + state_set(STATE_NO_COMM); + } + else if (m_gls_state == STATE_RACP_RESPONSE_PENDING) + { + racp_send(p_gls, &m_pending_racp_response); + } + else + { + // We did not expect this event in this state. Report error to application. + if (p_gls->error_handler != NULL) + { + p_gls->error_handler(NRF_ERROR_INVALID_STATE); + } + } + } +} + + +static void on_rw_authorize_request(ble_gls_t * p_gls, ble_gatts_evt_t const * p_gatts_evt) +{ + ble_gatts_evt_rw_authorize_request_t const * p_auth_req = + &p_gatts_evt->params.authorize_request; + + if (p_auth_req->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) + { + if ( (p_gatts_evt->params.authorize_request.request.write.op + != BLE_GATTS_OP_PREP_WRITE_REQ) + && (p_gatts_evt->params.authorize_request.request.write.op + != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) + && (p_gatts_evt->params.authorize_request.request.write.op + != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL) + ) + { + if (p_auth_req->request.write.handle == p_gls->racp_handles.value_handle) + { + on_racp_value_write(p_gls, &p_auth_req->request.write); + } + } + } +} + +void ble_gls_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context) +{ + ble_gls_t * p_gls = (ble_gls_t *)p_context; + + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + p_gls->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; + state_set(STATE_NO_COMM); + break; + + case BLE_GAP_EVT_DISCONNECTED: + p_gls->conn_handle = BLE_CONN_HANDLE_INVALID; + break; + + case BLE_GATTS_EVT_WRITE: + on_write(p_gls, p_ble_evt); + break; + + case BLE_GATTS_EVT_HVN_TX_COMPLETE: + on_tx_complete(p_gls, p_ble_evt); + break; + + case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: + on_rw_authorize_request(p_gls, &p_ble_evt->evt.gatts_evt); + break; + + case BLE_GATTS_EVT_HVC: + on_hvc(p_gls, p_ble_evt); + break; + + default: + // No implementation needed. + break; + } +} + + +uint32_t ble_gls_glucose_new_meas(ble_gls_t * p_gls, ble_gls_rec_t * p_rec) +{ + p_rec->meas.sequence_number = m_next_seq_num++; + return ble_gls_db_record_add(p_rec); +} +#endif // NRF_MODULE_ENABLED(BLE_GLS) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls.h new file mode 100644 index 0000000..374d3a3 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls.h @@ -0,0 +1,323 @@ +/** + * Copyright (c) 2012 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +/** @file + * + * @defgroup ble_gls Glucose Service + * @{ + * @ingroup ble_sdk_srv + * @brief Glucose Service module. + * + * @details This module implements the Glucose Service. + * + * @note The application must register this module as BLE event observer using the + * NRF_SDH_BLE_OBSERVER macro. Example: + * @code + * ble_gls_t instance; + * NRF_SDH_BLE_OBSERVER(anything, BLE_GLS_BLE_OBSERVER_PRIO, + * ble_gls_on_ble_evt, &instance); + * @endcode + * + * @note Attention! + * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile + * qualification listings, this section of source code must not be modified. + */ + +#ifndef BLE_GLS_H__ +#define BLE_GLS_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "ble.h" +#include "ble_srv_common.h" +#include "ble_date_time.h" +#include "nrf_sdh_ble.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/**@brief Macro for defining a ble_gls instance. + * + * @param _name Name of the instance. + * @hideinitializer + */ +#define BLE_GLS_DEF(_name) \ +static ble_gls_t _name; \ +NRF_SDH_BLE_OBSERVER(_name ## _obs, \ + BLE_GLS_BLE_OBSERVER_PRIO, \ + ble_gls_on_ble_evt, &_name) + + +/**@brief Glucose feature */ +#define BLE_GLS_FEATURE_LOW_BATT 0x0001 /**< Low Battery Detection During Measurement Supported */ +#define BLE_GLS_FEATURE_MALFUNC 0x0002 /**< Sensor Malfunction Detection Supported */ +#define BLE_GLS_FEATURE_SAMPLE_SIZE 0x0004 /**< Sensor Sample Size Supported */ +#define BLE_GLS_FEATURE_INSERT_ERR 0x0008 /**< Sensor Strip Insertion Error Detection Supported */ +#define BLE_GLS_FEATURE_TYPE_ERR 0x0010 /**< Sensor Strip Type Error Detection Supported */ +#define BLE_GLS_FEATURE_RES_HIGH_LOW 0x0020 /**< Sensor Result High-Low Detection Supported */ +#define BLE_GLS_FEATURE_TEMP_HIGH_LOW 0x0040 /**< Sensor Temperature High-Low Detection Supported */ +#define BLE_GLS_FEATURE_READ_INT 0x0080 /**< Sensor Read Interrupt Detection Supported */ +#define BLE_GLS_FEATURE_GENERAL_FAULT 0x0100 /**< General Device Fault Supported */ +#define BLE_GLS_FEATURE_TIME_FAULT 0x0200 /**< Time Fault Supported */ +#define BLE_GLS_FEATURE_MULTI_BOND 0x0400 /**< Multiple Bond Supported */ + +/**@brief Glucose measurement flags */ +#define BLE_GLS_MEAS_FLAG_TIME_OFFSET 0x01 /**< Time Offset Present */ +#define BLE_GLS_MEAS_FLAG_CONC_TYPE_LOC 0x02 /**< Glucose Concentration, Type, and Sample Location Present */ +#define BLE_GLS_MEAS_FLAG_UNITS_KG_L 0x00 /**< Glucose Concentration Units kg/L */ +#define BLE_GLS_MEAS_FLAG_UNITS_MOL_L 0x04 /**< Glucose Concentration Units mol/L */ +#define BLE_GLS_MEAS_FLAG_SENSOR_STATUS 0x08 /**< Sensor Status Annunciation Present */ +#define BLE_GLS_MEAS_FLAG_CONTEXT_INFO 0x10 /**< Context Information Follows */ + +/**@brief Glucose measurement type */ +#define BLE_GLS_MEAS_TYPE_CAP_BLOOD 1 /**< Capillary whole blood */ +#define BLE_GLS_MEAS_TYPE_CAP_PLASMA 2 /**< Capillary plasma */ +#define BLE_GLS_MEAS_TYPE_VEN_BLOOD 3 /**< Venous whole blood */ +#define BLE_GLS_MEAS_TYPE_VEN_PLASMA 4 /**< Venous plasma */ +#define BLE_GLS_MEAS_TYPE_ART_BLOOD 5 /**< Arterial whole blood */ +#define BLE_GLS_MEAS_TYPE_ART_PLASMA 6 /**< Arterial plasma */ +#define BLE_GLS_MEAS_TYPE_UNDET_BLOOD 7 /**< Undetermined whole blood */ +#define BLE_GLS_MEAS_TYPE_UNDET_PLASMA 8 /**< Undetermined plasma */ +#define BLE_GLS_MEAS_TYPE_FLUID 9 /**< Interstitial fluid (ISF) */ +#define BLE_GLS_MEAS_TYPE_CONTROL 10 /**< Control solution */ + +/**@brief Glucose measurement location */ +#define BLE_GLS_MEAS_LOC_FINGER 1 /**< Finger */ +#define BLE_GLS_MEAS_LOC_AST 2 /**< Alternate Site Test (AST) */ +#define BLE_GLS_MEAS_LOC_EAR 3 /**< Earlobe */ +#define BLE_GLS_MEAS_LOC_CONTROL 4 /**< Control solution */ +#define BLE_GLS_MEAS_LOC_NOT_AVAIL 15 /**< Sample Location value not available */ + +/**@brief Glucose sensor status annunciation */ +#define BLE_GLS_MEAS_STATUS_BATT_LOW 0x0001 /**< Device battery low at time of measurement */ +#define BLE_GLS_MEAS_STATUS_SENSOR_FAULT 0x0002 /**< Sensor malfunction or faulting at time of measurement */ +#define BLE_GLS_MEAS_STATUS_SAMPLE_SIZE 0x0004 /**< Sample size for blood or control solution insufficient at time of measurement */ +#define BLE_GLS_MEAS_STATUS_STRIP_INSERT 0x0008 /**< Strip insertion error */ +#define BLE_GLS_MEAS_STATUS_STRIP_TYPE 0x0010 /**< Strip type incorrect for device */ +#define BLE_GLS_MEAS_STATUS_RESULT_HIGH 0x0020 /**< Sensor result higher than the device can process */ +#define BLE_GLS_MEAS_STATUS_RESULT_LOW 0x0040 /**< Sensor result lower than the device can process */ +#define BLE_GLS_MEAS_STATUS_TEMP_HIGH 0x0080 /**< Sensor temperature too high for valid test/result at time of measurement */ +#define BLE_GLS_MEAS_STATUS_TEMP_LOW 0x0100 /**< Sensor temperature too low for valid test/result at time of measurement */ +#define BLE_GLS_MEAS_STATUS_STRIP_PULL 0x0200 /**< Sensor read interrupted because strip was pulled too soon at time of measurement */ +#define BLE_GLS_MEAS_STATUS_GENERAL_FAULT 0x0400 /**< General device fault has occurred in the sensor */ +#define BLE_GLS_MEAS_STATUS_TIME_FAULT 0x0800 /**< Time fault has occurred in the sensor and time may be inaccurate */ + +/**@brief Glucose measurement context flags */ +#define BLE_GLS_CONTEXT_FLAG_CARB 0x01 /**< Carbohydrate id and carbohydrate present */ +#define BLE_GLS_CONTEXT_FLAG_MEAL 0x02 /**< Meal present */ +#define BLE_GLS_CONTEXT_FLAG_TESTER 0x04 /**< Tester-health present */ +#define BLE_GLS_CONTEXT_FLAG_EXERCISE 0x08 /**< Exercise duration and exercise intensity present */ +#define BLE_GLS_CONTEXT_FLAG_MED 0x10 /**< Medication ID and medication present */ +#define BLE_GLS_CONTEXT_FLAG_MED_KG 0x00 /**< Medication value units, kilograms */ +#define BLE_GLS_CONTEXT_FLAG_MED_L 0x20 /**< Medication value units, liters */ +#define BLE_GLS_CONTEXT_FLAG_HBA1C 0x40 /**< Hba1c present */ +#define BLE_GLS_CONTEXT_FLAG_EXT 0x80 /**< Extended flags present */ + +/**@brief Glucose measurement context carbohydrate ID */ +#define BLE_GLS_CONTEXT_CARB_BREAKFAST 1 /**< Breakfast */ +#define BLE_GLS_CONTEXT_CARB_LUNCH 2 /**< Lunch */ +#define BLE_GLS_CONTEXT_CARB_DINNER 3 /**< Dinner */ +#define BLE_GLS_CONTEXT_CARB_SNACK 4 /**< Snack */ +#define BLE_GLS_CONTEXT_CARB_DRINK 5 /**< Drink */ +#define BLE_GLS_CONTEXT_CARB_SUPPER 6 /**< Supper */ +#define BLE_GLS_CONTEXT_CARB_BRUNCH 7 /**< Brunch */ + +/**@brief Glucose measurement context meal */ +#define BLE_GLS_CONTEXT_MEAL_PREPRANDIAL 1 /**< Preprandial (before meal) */ +#define BLE_GLS_CONTEXT_MEAL_POSTPRANDIAL 2 /**< Postprandial (after meal) */ +#define BLE_GLS_CONTEXT_MEAL_FASTING 3 /**< Fasting */ +#define BLE_GLS_CONTEXT_MEAL_CASUAL 4 /**< Casual (snacks, drinks, etc.) */ +#define BLE_GLS_CONTEXT_MEAL_BEDTIME 5 /**< Bedtime */ + +/**@brief Glucose measurement context tester */ +#define BLE_GLS_CONTEXT_TESTER_SELF 1 /**< Self */ +#define BLE_GLS_CONTEXT_TESTER_PRO 2 /**< Health care professional */ +#define BLE_GLS_CONTEXT_TESTER_LAB 3 /**< Lab test */ +#define BLE_GLS_CONTEXT_TESTER_NOT_AVAIL 15 /**< Tester value not available */ + +/**@brief Glucose measurement context health */ +#define BLE_GLS_CONTEXT_HEALTH_MINOR 1 /**< Minor health issues */ +#define BLE_GLS_CONTEXT_HEALTH_MAJOR 2 /**< Major health issues */ +#define BLE_GLS_CONTEXT_HEALTH_MENSES 3 /**< During menses */ +#define BLE_GLS_CONTEXT_HEALTH_STRESS 4 /**< Under stress */ +#define BLE_GLS_CONTEXT_HEALTH_NONE 5 /**< No health issues */ +#define BLE_GLS_CONTEXT_HEALTH_NOT_AVAIL 15 /**< Health value not available */ + +/**@brief Glucose measurement context medication ID */ +#define BLE_GLS_CONTEXT_MED_RAPID 1 /**< Rapid acting insulin */ +#define BLE_GLS_CONTEXT_MED_SHORT 2 /**< Short acting insulin */ +#define BLE_GLS_CONTEXT_MED_INTERMED 3 /**< Intermediate acting insulin */ +#define BLE_GLS_CONTEXT_MED_LONG 4 /**< Long acting insulin */ +#define BLE_GLS_CONTEXT_MED_PREMIX 5 /**< Pre-mixed insulin */ + + +/**@brief SFLOAT format (IEEE-11073 16-bit FLOAT, meaning 4 bits for exponent (base 10) and 12 bits mantissa) */ +typedef struct +{ + int8_t exponent; /**< Base 10 exponent, should be using only 4 bits */ + int16_t mantissa; /**< Mantissa, should be using only 12 bits */ +} sfloat_t; + +/**@brief Glucose Service event type. */ +typedef enum +{ + BLE_GLS_EVT_NOTIFICATION_ENABLED, /**< Glucose value notification enabled event. */ + BLE_GLS_EVT_NOTIFICATION_DISABLED /**< Glucose value notification disabled event. */ +} ble_gls_evt_type_t; + +/**@brief Glucose Service event. */ +typedef struct +{ + ble_gls_evt_type_t evt_type; /**< Type of event. */ +} ble_gls_evt_t; + +// Forward declaration of the ble_gls_t type. +typedef struct ble_gls_s ble_gls_t; + +/**@brief Glucose Service event handler type. */ +typedef void (*ble_gls_evt_handler_t) (ble_gls_t * p_gls, ble_gls_evt_t * p_evt); + +/**@brief Glucose Measurement structure. This contains glucose measurement value. */ +typedef struct +{ + uint8_t flags; /**< Flags */ + uint16_t sequence_number; /**< Sequence number */ + ble_date_time_t base_time; /**< Time stamp */ + int16_t time_offset; /**< Time offset */ + sfloat_t glucose_concentration; /**< Glucose concentration */ + uint8_t type; /**< Type */ + uint8_t sample_location; /**< Sample location */ + uint16_t sensor_status_annunciation; /**< Sensor status annunciation */ +} ble_gls_meas_t; + +/**@brief Glucose measurement context structure */ +typedef struct +{ + uint8_t flags; /**< Flags */ + uint8_t extended_flags; /**< Extended Flags */ + uint8_t carbohydrate_id; /**< Carbohydrate ID */ + sfloat_t carbohydrate; /**< Carbohydrate */ + uint8_t meal; /**< Meal */ + uint8_t tester_and_health; /**< Tester and health */ + uint16_t exercise_duration; /**< Exercise Duration */ + uint8_t exercise_intensity; /**< Exercise Intensity */ + uint8_t medication_id; /**< Medication ID */ + sfloat_t medication; /**< Medication */ + uint16_t hba1c; /**< HbA1c */ +} ble_gls_meas_context_t; + +/**@brief Glucose measurement record */ +typedef struct +{ + ble_gls_meas_t meas; /**< Glucose measurement */ + ble_gls_meas_context_t context; /**< Glucose measurement context */ +} ble_gls_rec_t; + +/**@brief Glucose Service init structure. This contains all options and data needed for + * initialization of the service. */ +typedef struct +{ + ble_gls_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Glucose Service. */ + ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ + uint16_t feature; /**< Glucose Feature value indicating supported features. */ + bool is_context_supported; /**< Determines if optional Glucose Measurement Context is to be supported. */ +} ble_gls_init_t; + +/**@brief Glucose Service structure. This contains various status information for the service. */ +struct ble_gls_s +{ + ble_gls_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Glucose Service. */ + ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ + uint16_t service_handle; /**< Handle of Glucose Service (as provided by the BLE stack). */ + ble_gatts_char_handles_t glm_handles; /**< Handles related to the Glucose Measurement characteristic. */ + ble_gatts_char_handles_t glm_context_handles; /**< Handles related to the Glucose Measurement Context characteristic. */ + ble_gatts_char_handles_t glf_handles; /**< Handles related to the Glucose Feature characteristic. */ + ble_gatts_char_handles_t racp_handles; /**< Handles related to the Record Access Control Point characteristic. */ + uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */ + uint16_t feature; + bool is_context_supported; +}; + + +/**@brief Function for initializing the Glucose Service. + * + * @details This call allows the application to initialize the Glucose Service. + * + * @param[out] p_gls Glucose Service structure. This structure will have to be supplied by + * the application. It will be initialized by this function, and will later + * be used to identify this particular service instance. + * @param[in] p_gls_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. + */ +uint32_t ble_gls_init(ble_gls_t * p_gls, ble_gls_init_t const * p_gls_init); + + +/**@brief Function for handling the Application's BLE Stack events. + * + * @details Handles all events from the BLE stack of interest to the Glucose Service. + * + * @param[in] p_ble_evt Event received from the BLE stack. + * @param[in] p_context Glucose Service structure. + */ +void ble_gls_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context); + + +/**@brief Function for reporting a new glucose measurement to the glucose service module. + * + * @details The application calls this function after having performed a new glucose measurement. + * The new measurement is recorded in the RACP database. + * + * @param[in] p_gls Glucose Service structure. + * @param[in] p_rec Pointer to glucose record (measurement plus context). + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_gls_glucose_new_meas(ble_gls_t * p_gls, ble_gls_rec_t * p_rec); + + +#ifdef __cplusplus +} +#endif + +#endif // BLE_GLS_H__ + +/** @} */ + diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls_db.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls_db.c new file mode 100644 index 0000000..679a359 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls_db.c @@ -0,0 +1,143 @@ +/** + * Copyright (c) 2012 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(BLE_GLS) +#include "ble_gls_db.h" + + +typedef struct +{ + bool in_use_flag; + ble_gls_rec_t record; +} database_entry_t; + +static database_entry_t m_database[BLE_GLS_DB_MAX_RECORDS]; +static uint8_t m_database_crossref[BLE_GLS_DB_MAX_RECORDS]; +static uint16_t m_num_records; + + +uint32_t ble_gls_db_init(void) +{ + int i; + + for (i = 0; i < BLE_GLS_DB_MAX_RECORDS; i++) + { + m_database[i].in_use_flag = false; + m_database_crossref[i] = 0xFF; + } + + m_num_records = 0; + + return NRF_SUCCESS; +} + + +uint16_t ble_gls_db_num_records_get(void) +{ + return m_num_records; +} + + +uint32_t ble_gls_db_record_get(uint8_t rec_ndx, ble_gls_rec_t * p_rec) +{ + if (rec_ndx >= m_num_records) + { + return NRF_ERROR_INVALID_PARAM; + } + + // copy record to the specified memory + *p_rec = m_database[m_database_crossref[rec_ndx]].record; + + return NRF_SUCCESS; +} + + +uint32_t ble_gls_db_record_add(ble_gls_rec_t * p_rec) +{ + int i; + + if (m_num_records == BLE_GLS_DB_MAX_RECORDS) + { + return NRF_ERROR_NO_MEM; + } + + // find next available database entry + for (i = 0; i < BLE_GLS_DB_MAX_RECORDS; i++) + { + if (!m_database[i].in_use_flag) + { + m_database[i].in_use_flag = true; + m_database[i].record = *p_rec; + + m_database_crossref[m_num_records] = i; + m_num_records++; + + return NRF_SUCCESS; + } + } + + return NRF_ERROR_NO_MEM; +} + + +uint32_t ble_gls_db_record_delete(uint8_t rec_ndx) +{ + int i; + + if (rec_ndx >= m_num_records) + { + return NRF_ERROR_NOT_FOUND; + } + + // free entry + m_database[m_database_crossref[rec_ndx]].in_use_flag = false; + + // decrease number of records + m_num_records--; + + // remove cross reference index + for (i = rec_ndx; i < m_num_records; i++) + { + m_database_crossref[i] = m_database_crossref[i + 1]; + } + + return NRF_SUCCESS; +} +#endif // NRF_MODULE_ENABLED(BLE_GLS) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls_db.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls_db.h new file mode 100644 index 0000000..98465f0 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_gls/ble_gls_db.h @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2012 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +/** @file + * + * @defgroup ble_sdk_srv_gls_db Glucose Database Service + * @{ + * @ingroup ble_sdk_srv + * @brief Glucose Service module. + * + * @details This module implements at database of stored glucose measurement values. + * + * @note Attention! + * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile + * qualification listings, These APIs must not be modified. However, the corresponding + * functions' implementations can be modified. + */ + +#ifndef BLE_GLS_DB_H__ +#define BLE_GLS_DB_H__ + +#include <stdint.h> +#include "ble_gls.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_GLS_DB_MAX_RECORDS 20 + +/**@brief Function for initializing the glucose record database. + * + * @details This call initializes the database holding glucose records. + * + * @return NRF_SUCCESS on success. + */ +uint32_t ble_gls_db_init(void); + +/**@brief Function for getting the number of records in the database. + * + * @details This call returns the number of records in the database. + * + * @return Number of records in the database. + */ +uint16_t ble_gls_db_num_records_get(void); + +/**@brief Function for getting a record from the database. + * + * @details This call returns a specified record from the database. + * + * @param[in] record_num Index of the record to retrieve. + * @param[out] p_rec Pointer to record structure where retrieved record is copied to. + * + * @return NRF_SUCCESS on success. + */ +uint32_t ble_gls_db_record_get(uint8_t record_num, ble_gls_rec_t * p_rec); + +/**@brief Function for adding a record at the end of the database. + * + * @details This call adds a record as the last record in the database. + * + * @param[in] p_rec Pointer to record to add to database. + * + * @return NRF_SUCCESS on success. + */ +uint32_t ble_gls_db_record_add(ble_gls_rec_t * p_rec); + +/**@brief Function for deleting a database entry. + * + * @details This call deletes an record from the database. + * + * @param[in] record_num Index of record to delete. + * + * @return NRF_SUCCESS on success. + */ +uint32_t ble_gls_db_record_delete(uint8_t record_num); + + +#ifdef __cplusplus +} +#endif + +#endif // BLE_GLS_DB_H__ + +/** @} */ |