diff options
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs')
4 files changed, 1640 insertions, 0 deletions
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_cscs.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_cscs.c new file mode 100644 index 0000000..6e45d0f --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_cscs.c @@ -0,0 +1,489 @@ +/** + * 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 "ble_cscs.h" +#include <string.h> +#include "nordic_common.h" +#include "ble.h" +#include "ble_err.h" +#include "ble_srv_common.h" +#include "app_util.h" + +#define OPCODE_LENGTH 1 /**< Length of opcode inside Cycling Speed and Cadence Measurement packet. */ +#define HANDLE_LENGTH 2 /**< Length of handle inside Cycling Speed and Cadence Measurement packet. */ +#define MAX_CSCM_LEN (BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Cycling Speed and Cadence Measurement. */ + +// Cycling Speed and Cadence Measurement flag bits +#define CSC_MEAS_FLAG_MASK_WHEEL_REV_DATA_PRESENT (0x01 << 0) /**< Wheel revolution data present flag bit. */ +#define CSC_MEAS_FLAG_MASK_CRANK_REV_DATA_PRESENT (0x01 << 1) /**< Crank revolution data present flag bit. */ + + +/**@brief Function for handling the Connect event. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_connect(ble_cscs_t * p_cscs, ble_evt_t const * p_ble_evt) +{ + p_cscs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; +} + + +/**@brief Function for handling the Disconnect event. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_disconnect(ble_cscs_t * p_cscs, ble_evt_t const * p_ble_evt) +{ + UNUSED_PARAMETER(p_ble_evt); + p_cscs->conn_handle = BLE_CONN_HANDLE_INVALID; +} + + +/**@brief Function for handling write events to the CSCS Measurement characteristic. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_evt_write Write event received from the BLE stack. + */ +static void on_meas_cccd_write(ble_cscs_t * p_cscs, ble_gatts_evt_write_t const * p_evt_write) +{ + if (p_evt_write->len == 2) + { + // CCCD written, update notification state + if (p_cscs->evt_handler != NULL) + { + ble_cscs_evt_t evt; + + if (ble_srv_is_notification_enabled(p_evt_write->data)) + { + evt.evt_type = BLE_CSCS_EVT_NOTIFICATION_ENABLED; + } + else + { + evt.evt_type = BLE_CSCS_EVT_NOTIFICATION_DISABLED; + } + + p_cscs->evt_handler(p_cscs, &evt); + } + } +} + + +/**@brief Function for handling the Write event. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_write(ble_cscs_t * p_cscs, 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_cscs->meas_handles.cccd_handle) + { + on_meas_cccd_write(p_cscs, p_evt_write); + } +} + + +void ble_cscs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context) +{ + ble_cscs_t * p_cscs = (ble_cscs_t *)p_context; + + if (p_cscs == NULL || p_ble_evt == NULL) + { + return; + } + + + ble_sc_ctrlpt_on_ble_evt(&(p_cscs->ctrl_pt), p_ble_evt); + + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + on_connect(p_cscs, p_ble_evt); + break; + + case BLE_GAP_EVT_DISCONNECTED: + on_disconnect(p_cscs, p_ble_evt); + break; + + case BLE_GATTS_EVT_WRITE: + on_write(p_cscs, p_ble_evt); + break; + + default: + // No implementation needed. + break; + } +} + + +/**@brief Function for encoding a CSCS Measurement. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_csc_measurement Measurement to be encoded. + * @param[out] p_encoded_buffer Buffer where the encoded data will be written. + * + * @return Size of encoded data. + */ +static uint8_t csc_measurement_encode(ble_cscs_t * p_cscs, + ble_cscs_meas_t * p_csc_measurement, + uint8_t * p_encoded_buffer) +{ + uint8_t flags = 0; + uint8_t len = 1; + + // Cumulative Wheel Revolutions and Last Wheel Event Time Fields + if (p_cscs->feature & BLE_CSCS_FEATURE_WHEEL_REV_BIT) + { + if (p_csc_measurement->is_wheel_rev_data_present) + { + flags |= CSC_MEAS_FLAG_MASK_WHEEL_REV_DATA_PRESENT; + len += uint32_encode(p_csc_measurement->cumulative_wheel_revs, &p_encoded_buffer[len]); + len += uint16_encode(p_csc_measurement->last_wheel_event_time, &p_encoded_buffer[len]); + } + } + + // Cumulative Crank Revolutions and Last Crank Event Time Fields + if (p_cscs->feature & BLE_CSCS_FEATURE_CRANK_REV_BIT) + { + if (p_csc_measurement->is_crank_rev_data_present) + { + flags |= CSC_MEAS_FLAG_MASK_CRANK_REV_DATA_PRESENT; + len += uint16_encode(p_csc_measurement->cumulative_crank_revs, &p_encoded_buffer[len]); + len += uint16_encode(p_csc_measurement->last_crank_event_time, &p_encoded_buffer[len]); + } + } + + // Flags Field + p_encoded_buffer[0] = flags; + + return len; +} + + +/**@brief Function for adding CSC Measurement characteristics. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_cscs_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t csc_measurement_char_add(ble_cscs_t * p_cscs, ble_cscs_init_t const * p_cscs_init) +{ + 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_cscs_meas_t initial_scm = {0}; + uint8_t encoded_scm[MAX_CSCM_LEN]; + memset(&cccd_md, 0, sizeof(cccd_md)); + + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + cccd_md.write_perm = p_cscs_init->csc_meas_attr_md.cccd_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_CSC_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)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = csc_measurement_encode(p_cscs, &initial_scm, encoded_scm); + attr_char_value.init_offs = 0; + attr_char_value.max_len = MAX_CSCM_LEN; + attr_char_value.p_value = encoded_scm; + + return sd_ble_gatts_characteristic_add(p_cscs->service_handle, + &char_md, + &attr_char_value, + &p_cscs->meas_handles); +} + + +/**@brief Function for adding CSC Feature characteristics. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_cscs_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t csc_feature_char_add(ble_cscs_t * p_cscs, ble_cscs_init_t const * p_cscs_init) +{ + 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 init_value_encoded[2]; + uint8_t init_value_len; + + 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_CSC_FEATURE_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.read_perm = p_cscs_init->csc_feature_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)); + + init_value_len = uint16_encode(p_cscs_init->feature, &init_value_encoded[0]); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = init_value_len; + attr_char_value.init_offs = 0; + attr_char_value.max_len = init_value_len; + attr_char_value.p_value = init_value_encoded; + + return sd_ble_gatts_characteristic_add(p_cscs->service_handle, + &char_md, + &attr_char_value, + &p_cscs->feature_handles); +} + + +/**@brief Function for adding CSC Sensor Location characteristic. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_cscs_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t csc_sensor_loc_char_add(ble_cscs_t * p_cscs, ble_cscs_init_t const * p_cscs_init) +{ + 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 init_value_len; + uint8_t encoded_init_value[1]; + + 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_SENSOR_LOCATION_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.read_perm = p_cscs_init->csc_sensor_loc_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)); + + init_value_len = sizeof(uint8_t); + if (p_cscs_init->sensor_location != NULL) + { + encoded_init_value[0] = *p_cscs_init->sensor_location; + } + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = init_value_len; + attr_char_value.init_offs = 0; + attr_char_value.max_len = init_value_len; + attr_char_value.p_value = encoded_init_value; + + return sd_ble_gatts_characteristic_add(p_cscs->service_handle, + &char_md, + &attr_char_value, + &p_cscs->sensor_loc_handles); +} + + +uint32_t ble_cscs_init(ble_cscs_t * p_cscs, ble_cscs_init_t const * p_cscs_init) +{ + if (p_cscs == NULL || p_cscs_init == NULL) + { + return NRF_ERROR_NULL; + } + + uint32_t err_code; + ble_uuid_t ble_uuid; + ble_cs_ctrlpt_init_t sc_ctrlpt_init; + + // Initialize service structure + p_cscs->evt_handler = p_cscs_init->evt_handler; + p_cscs->conn_handle = BLE_CONN_HANDLE_INVALID; + p_cscs->feature = p_cscs_init->feature; + + // Add service + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_CYCLING_SPEED_AND_CADENCE); + + err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, + &ble_uuid, + &p_cscs->service_handle); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add cycling speed and cadence measurement characteristic + err_code = csc_measurement_char_add(p_cscs, p_cscs_init); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add cycling speed and cadence feature characteristic + err_code = csc_feature_char_add(p_cscs, p_cscs_init); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add Sensor Location characteristic (optional) + if (p_cscs_init->sensor_location != NULL) + { + err_code = csc_sensor_loc_char_add(p_cscs, p_cscs_init); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + + // Add speed and cadence control point characteristic + sc_ctrlpt_init.error_handler = p_cscs_init->error_handler; + sc_ctrlpt_init.size_list_supported_locations = p_cscs_init->size_list_supported_locations; + sc_ctrlpt_init.supported_functions = p_cscs_init->ctrplt_supported_functions; + sc_ctrlpt_init.evt_handler = p_cscs_init->ctrlpt_evt_handler; + sc_ctrlpt_init.list_supported_locations = p_cscs_init->list_supported_locations; + sc_ctrlpt_init.sc_ctrlpt_attr_md = p_cscs_init->csc_ctrlpt_attr_md; + sc_ctrlpt_init.sensor_location_handle = p_cscs->sensor_loc_handles.value_handle; + sc_ctrlpt_init.service_handle = p_cscs->service_handle; + + return ble_sc_ctrlpt_init(&p_cscs->ctrl_pt, &sc_ctrlpt_init); +} + + +uint32_t ble_cscs_measurement_send(ble_cscs_t * p_cscs, ble_cscs_meas_t * p_measurement) +{ + if (p_cscs == NULL || p_measurement == NULL) + { + return NRF_ERROR_NULL; + } + + uint32_t err_code; + + // Send value if connected and notifying + if (p_cscs->conn_handle != BLE_CONN_HANDLE_INVALID) + { + uint8_t encoded_csc_meas[MAX_CSCM_LEN]; + uint16_t len; + uint16_t hvx_len; + ble_gatts_hvx_params_t hvx_params; + + len = csc_measurement_encode(p_cscs, p_measurement, encoded_csc_meas); + hvx_len = len; + + memset(&hvx_params, 0, sizeof(hvx_params)); + + hvx_params.handle = p_cscs->meas_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_csc_meas; + + err_code = sd_ble_gatts_hvx(p_cscs->conn_handle, &hvx_params); + if ((err_code == NRF_SUCCESS) && (hvx_len != len)) + { + err_code = NRF_ERROR_DATA_SIZE; + } + } + else + { + err_code = NRF_ERROR_INVALID_STATE; + } + + return err_code; +} diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_cscs.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_cscs.h new file mode 100644 index 0000000..c62d09c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_cscs.h @@ -0,0 +1,218 @@ +/** + * 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_cscs Cycling Speed and Cadence Service + * @{ + * @ingroup ble_sdk_srv + * @brief Cycling Speed and Cadence Service module. + * + * @details This module implements the Cycling Speed and Cadence Service. If enabled, notification + * of the Cycling Speead and Candence Measurement is performed when the application + * calls ble_cscs_measurement_send(). + * + * To use this service, you need to provide the the supported features (@ref BLE_CSCS_FEATURES). + * If you choose to support Wheel revolution data (feature bit @ref BLE_CSCS_FEATURE_WHEEL_REV_BIT), + * you then need to support the 'setting of cumulative value' operation by the supporting the + * Speed and Cadence Control Point (@ref ble_sdk_srv_sc_ctrlpt) by setting the @ref BLE_SRV_SC_CTRLPT_CUM_VAL_OP_SUPPORTED + * bit of the ctrplt_supported_functions in the @ref ble_cscs_init_t structure. + * If you want to support the 'start autocalibration' control point feature, you need, after the @ref BLE_SC_CTRLPT_EVT_START_CALIBRATION + * has been received and the auto calibration is finished, to call the @ref ble_sc_ctrlpt_rsp_send to indicate that the operation is finished + * and thus be able to receive new control point operations. + * If you want to support the 'sensor location' related operation, you need to provide a list of supported location in the + * @ref ble_cscs_init_t structure. + * + * + * @note The application must register this module as BLE event observer using the + * NRF_SDH_BLE_OBSERVER macro. Example: + * @code + * ble_cscs_t instance; + * NRF_SDH_BLE_OBSERVER(anything, BLE_CSCS_BLE_OBSERVER_PRIO, + * ble_cscs_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_CSCS_H__ +#define BLE_CSCS_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "ble.h" +#include "ble_srv_common.h" +#include "ble_sc_ctrlpt.h" +#include "ble_sensor_location.h" +#include "nrf_sdh_ble.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/**@brief Macro for defining a ble_cscs instance. + * + * @param _name Name of the instance. + * @hideinitializer + */ +#define BLE_CSCS_DEF(_name) \ +static ble_cscs_t _name; \ +NRF_SDH_BLE_OBSERVER(_name ## _obs, \ + BLE_CSCS_BLE_OBSERVER_PRIO, \ + ble_cscs_on_ble_evt, &_name) + + +/** @defgroup BLE_CSCS_FEATURES Cycling Speed and Cadence Service feature bits + * @{ */ +#define BLE_CSCS_FEATURE_WHEEL_REV_BIT (0x01 << 0) /**< Wheel Revolution Data Supported bit. */ +#define BLE_CSCS_FEATURE_CRANK_REV_BIT (0x01 << 1) /**< Crank Revolution Data Supported bit. */ +#define BLE_CSCS_FEATURE_MULTIPLE_SENSORS_BIT (0x01 << 2) /**< Multiple Sensor Locations Supported bit. */ +/** @} */ + + +/**@brief Cycling Speed and Cadence Service event type. */ +typedef enum +{ + BLE_CSCS_EVT_NOTIFICATION_ENABLED, /**< Cycling Speed and Cadence value notification enabled event. */ + BLE_CSCS_EVT_NOTIFICATION_DISABLED /**< Cycling Speed and Cadence value notification disabled event. */ +} ble_cscs_evt_type_t; + +/**@brief Cycling Speed and Cadence Service event. */ +typedef struct +{ + ble_cscs_evt_type_t evt_type; /**< Type of event. */ +} ble_cscs_evt_t; + +// Forward declaration of the ble_csc_t type. +typedef struct ble_cscs_s ble_cscs_t; + +/**@brief Cycling Speed and Cadence Service event handler type. */ +typedef void (*ble_cscs_evt_handler_t) (ble_cscs_t * p_cscs, ble_cscs_evt_t * p_evt); + +/**@brief Cycling Speed and Cadence Service init structure. This contains all options and data +* needed for initialization of the service. */ +typedef struct +{ + ble_cscs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Cycling Speed and Cadence Service. */ + ble_srv_cccd_security_mode_t csc_meas_attr_md; /**< Initial security level for cycling speed and cadence measurement attribute */ + ble_srv_cccd_security_mode_t csc_ctrlpt_attr_md; /**< Initial security level for cycling speed and cadence control point attribute */ + ble_srv_security_mode_t csc_feature_attr_md; /**< Initial security level for feature attribute */ + uint16_t feature; /**< Initial value for features of sensor @ref BLE_CSCS_FEATURES. */ + uint8_t ctrplt_supported_functions; /**< Supported control point functionalities see @ref BLE_SRV_SC_CTRLPT_SUPP_FUNC. */ + ble_sc_ctrlpt_evt_handler_t ctrlpt_evt_handler; /**< Event handler */ + ble_sensor_location_t *list_supported_locations; /**< List of supported sensor locations.*/ + uint8_t size_list_supported_locations; /**< Number of supported sensor locations in the list.*/ + ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ + ble_sensor_location_t *sensor_location; /**< Initial Sensor Location, if NULL, sensor_location characteristic is not added*/ + ble_srv_cccd_security_mode_t csc_sensor_loc_attr_md; /**< Initial security level for sensor location attribute */ +} ble_cscs_init_t; + +/**@brief Cycling Speed and Cadence Service structure. This contains various status information for + * the service. */ +struct ble_cscs_s +{ + ble_cscs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Cycling Speed and Cadence Service. */ + uint16_t service_handle; /**< Handle of Cycling Speed and Cadence Service (as provided by the BLE stack). */ + ble_gatts_char_handles_t meas_handles; /**< Handles related to the Cycling Speed and Cadence Measurement characteristic. */ + ble_gatts_char_handles_t feature_handles; /**< Handles related to the Cycling Speed and Cadence feature characteristic. */ + ble_gatts_char_handles_t sensor_loc_handles; /**< Handles related to the Cycling Speed and Cadence Sensor Location 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; /**< Bit mask of features available on sensor. */ + ble_sc_ctrlpt_t ctrl_pt; /**< data for speed and cadence control point */ +}; + +/**@brief Cycling Speed and Cadence Service measurement structure. This contains a Cycling Speed and + * Cadence Service measurement. */ +typedef struct ble_cscs_meas_s +{ + bool is_wheel_rev_data_present; /**< True if Wheel Revolution Data is present in the measurement. */ + bool is_crank_rev_data_present; /**< True if Crank Revolution Data is present in the measurement. */ + uint32_t cumulative_wheel_revs; /**< Cumulative Wheel Revolutions. */ + uint16_t last_wheel_event_time; /**< Last Wheel Event Time. */ + uint16_t cumulative_crank_revs; /**< Cumulative Crank Revolutions. */ + uint16_t last_crank_event_time; /**< Last Crank Event Time. */ +} ble_cscs_meas_t; + + +/**@brief Function for initializing the Cycling Speed and Cadence Service. + * + * @param[out] p_cscs Cycling Speed and Cadence 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_cscs_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. + */ +uint32_t ble_cscs_init(ble_cscs_t * p_cscs, ble_cscs_init_t const * p_cscs_init); + + +/**@brief Function for handling the Application's BLE Stack events. + * + * @details Handles all events from the BLE stack of interest to the Cycling Speed and Cadence + * Service. + * + * @param[in] p_ble_evt Event received from the BLE stack. + * @param[in] p_context Cycling Speed and Cadence Service structure. + */ +void ble_cscs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context); + + +/**@brief Function for sending cycling speed and cadence measurement if notification has been enabled. + * + * @details The application calls this function after having performed a Cycling Speed and Cadence + * Service measurement. If notification has been enabled, the measurement data is encoded + * and sent to the client. + * + * @param[in] p_cscs Cycling Speed and Cadence Service structure. + * @param[in] p_measurement Pointer to new cycling speed and cadence measurement. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_cscs_measurement_send(ble_cscs_t * p_cscs, ble_cscs_meas_t * p_measurement); + + +#ifdef __cplusplus +} +#endif + +#endif // BLE_CSCS_H__ + +/** @} */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_sc_ctrlpt.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_sc_ctrlpt.c new file mode 100644 index 0000000..faef930 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_sc_ctrlpt.c @@ -0,0 +1,686 @@ +/** + * Copyright (c) 2013 - 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 "ble_sc_ctrlpt.h" +#include <string.h> +#include "nordic_common.h" +#include "ble.h" +#include "ble_err.h" +#include "ble_srv_common.h" +#include "app_util.h" + +#define SC_CTRLPT_NACK_PROC_ALREADY_IN_PROGRESS (BLE_GATT_STATUS_ATTERR_APP_BEGIN + 0) +#define SC_CTRLPT_NACK_CCCD_IMPROPERLY_CONFIGURED (BLE_GATT_STATUS_ATTERR_APP_BEGIN + 1) + +uint32_t ble_sc_ctrlpt_init(ble_sc_ctrlpt_t * p_sc_ctrlpt, + const ble_cs_ctrlpt_init_t * p_sc_ctrlpt_init) +{ + if (p_sc_ctrlpt == NULL || p_sc_ctrlpt_init == NULL) + { + return NRF_ERROR_NULL; + } + + 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; + + p_sc_ctrlpt->conn_handle = BLE_CONN_HANDLE_INVALID; + p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS; + + p_sc_ctrlpt->size_list_supported_locations = p_sc_ctrlpt_init->size_list_supported_locations; + + if ((p_sc_ctrlpt_init->size_list_supported_locations != 0) && + (p_sc_ctrlpt_init->list_supported_locations != NULL)) + { + memcpy(p_sc_ctrlpt->list_supported_locations, + p_sc_ctrlpt_init->list_supported_locations, + p_sc_ctrlpt->size_list_supported_locations * sizeof(ble_sensor_location_t)); + } + + p_sc_ctrlpt->service_handle = p_sc_ctrlpt_init->service_handle; + p_sc_ctrlpt->evt_handler = p_sc_ctrlpt_init->evt_handler; + p_sc_ctrlpt->supported_functions = p_sc_ctrlpt_init->supported_functions; + p_sc_ctrlpt->sensor_location_handle = p_sc_ctrlpt_init->sensor_location_handle; + p_sc_ctrlpt->error_handler = p_sc_ctrlpt_init->error_handler; + + memset(&cccd_md, 0, sizeof(cccd_md)); + + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + cccd_md.write_perm = p_sc_ctrlpt_init->sc_ctrlpt_attr_md.cccd_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_SC_CTRLPT_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.read_perm); + attr_md.write_perm = p_sc_ctrlpt_init->sc_ctrlpt_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_SC_CTRLPT_MAX_LEN; + attr_char_value.p_value = 0; + + return sd_ble_gatts_characteristic_add(p_sc_ctrlpt->service_handle, + &char_md, + &attr_char_value, + &p_sc_ctrlpt->sc_ctrlpt_handles); +} + + +/**@brief Decode an incoming control point write. + * + * @param[in] rcvd_val received write value + * @param[in] len value length + * @param[out] decoded_ctrlpt decoded control point structure + */ +static uint32_t sc_ctrlpt_decode(uint8_t const * p_rcvd_val, + uint8_t len, + ble_sc_ctrlpt_val_t * p_write_val) +{ + int pos = 0; + + if (len < BLE_SC_CTRLPT_MIN_LEN) + { + return NRF_ERROR_INVALID_PARAM; + } + + p_write_val->opcode = (ble_scpt_operator_t) p_rcvd_val[pos++]; + + switch (p_write_val->opcode) + { + case BLE_SCPT_REQUEST_SUPPORTED_SENSOR_LOCATIONS: + break; + + case BLE_SCPT_START_AUTOMATIC_CALIBRATION: + break; + + case BLE_SCPT_UPDATE_SENSOR_LOCATION: + p_write_val->location = (ble_sensor_location_t)p_rcvd_val[pos]; + break; + + case BLE_SCPT_SET_CUMULATIVE_VALUE: + p_write_val->cumulative_value = uint32_decode(&(p_rcvd_val[pos])); + break; + + default: + return NRF_ERROR_INVALID_PARAM; + } + return NRF_SUCCESS; +} + + +/**@brief encode a control point response indication. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @param[in] p_ctrlpt_rsp structure containing response data to be encoded + * @param[out] p_data pointer where data needs to be written + * @return size of encoded data + */ +static int ctrlpt_rsp_encode(ble_sc_ctrlpt_t * p_sc_ctrlpt, + ble_sc_ctrlpt_rsp_t * p_ctrlpt_rsp, + uint8_t * p_data) +{ + int len = 0; + + p_data[len++] = BLE_SCPT_RESPONSE_CODE; + p_data[len++] = p_ctrlpt_rsp->opcode; + p_data[len++] = p_ctrlpt_rsp->status; + + if (p_ctrlpt_rsp->status == BLE_SCPT_SUCCESS) + { + switch (p_ctrlpt_rsp->opcode) + { + case BLE_SCPT_REQUEST_SUPPORTED_SENSOR_LOCATIONS: + { + int i; + for (i = 0; i < p_sc_ctrlpt->size_list_supported_locations; i++) + { + p_data[len++] = p_sc_ctrlpt->list_supported_locations[i]; + } + break; + } + + default: + // No implementation needed. + break; + } + } + return len; +} + + +/**@brief check if a given sensor location is supported or not. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @param[in] location sensor location to check. + * @return true if the given location is found in the list of supported locations, false otherwise. + */ +static bool is_location_supported(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_sensor_location_t location) +{ + int i; + + for (i = 0; i < p_sc_ctrlpt->size_list_supported_locations; i++) + { + if (p_sc_ctrlpt->list_supported_locations[i] == location) + { + return true; + } + } + return false; +} + + +/**@brief check if the cccd is configured + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @return true if the sc_control point's cccd is correctly configured, false otherwise. + */ +static bool is_cccd_configured(ble_sc_ctrlpt_t * p_sc_ctrlpt) +{ + uint32_t err_code; + uint8_t cccd_value_buf[BLE_CCCD_VALUE_LEN]; + bool is_sccp_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_sc_ctrlpt->conn_handle, + p_sc_ctrlpt->sc_ctrlpt_handles.cccd_handle, + &gatts_value); + if (err_code != NRF_SUCCESS) + { + // Report error to application + if (p_sc_ctrlpt->error_handler != NULL) + { + p_sc_ctrlpt->error_handler(err_code); + } + } + + is_sccp_indic_enabled = ble_srv_is_indication_enabled(cccd_value_buf); + + return is_sccp_indic_enabled; +} + + +/**@brief sends a control point indication. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + */ +static void sc_ctrlpt_resp_send(ble_sc_ctrlpt_t * p_sc_ctrlpt) +{ + uint16_t hvx_len; + ble_gatts_hvx_params_t hvx_params; + uint32_t err_code; + + if (p_sc_ctrlpt->procedure_status == BLE_SCPT_INDICATION_PENDING) + { + hvx_len = p_sc_ctrlpt->response.len; + memset(&hvx_params, 0, sizeof(hvx_params)); + + hvx_params.handle = p_sc_ctrlpt->sc_ctrlpt_handles.value_handle; + hvx_params.type = BLE_GATT_HVX_INDICATION; + hvx_params.offset = 0; + hvx_params.p_len = &hvx_len; + hvx_params.p_data = p_sc_ctrlpt->response.encoded_ctrl_rsp; + + err_code = sd_ble_gatts_hvx(p_sc_ctrlpt->conn_handle, &hvx_params); + + // Error handling + if ((err_code == NRF_SUCCESS) && (hvx_len != p_sc_ctrlpt->response.len)) + { + err_code = NRF_ERROR_DATA_SIZE; + } + + switch (err_code) + { + case NRF_SUCCESS: + p_sc_ctrlpt->procedure_status = BLE_SCPT_IND_CONFIRM_PENDING; + // Wait for HVC event + break; + + case NRF_ERROR_RESOURCES: + // Wait for TX_COMPLETE event to retry transmission. + p_sc_ctrlpt->procedure_status = BLE_SCPT_INDICATION_PENDING; + break; + + default: + // Report error to application. + p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS; + if (p_sc_ctrlpt->error_handler != NULL) + { + p_sc_ctrlpt->error_handler(err_code); + } + break; + } + } +} + + +/**@brief Handle a write event to the Speed and Cadence Control Point. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @param[in] p_evt_write WRITE event to be handled. + */ +static void on_ctrlpt_write(ble_sc_ctrlpt_t * p_sc_ctrlpt, + ble_gatts_evt_write_t const * p_evt_write) +{ + ble_sc_ctrlpt_val_t rcvd_ctrlpt = + { BLE_SCPT_RESPONSE_CODE , 0, BLE_SENSOR_LOCATION_OTHER }; + + ble_sc_ctrlpt_rsp_t rsp; + uint32_t err_code; + ble_gatts_rw_authorize_reply_params_t auth_reply; + ble_sc_ctrlpt_evt_t evt; + + 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; + auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS; + auth_reply.params.write.update = 1; + + if (is_cccd_configured(p_sc_ctrlpt)) + { + if (p_sc_ctrlpt->procedure_status == BLE_SCPT_NO_PROC_IN_PROGRESS) + { + auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS; + } + else + { + auth_reply.params.write.gatt_status = SC_CTRLPT_NACK_PROC_ALREADY_IN_PROGRESS; + } + } + else + { + auth_reply.params.write.gatt_status = SC_CTRLPT_NACK_CCCD_IMPROPERLY_CONFIGURED; + } + + err_code = sd_ble_gatts_rw_authorize_reply(p_sc_ctrlpt->conn_handle, &auth_reply); + if (err_code != NRF_SUCCESS) + { + // Report error to application. + if (p_sc_ctrlpt->error_handler != NULL) + { + p_sc_ctrlpt->error_handler(err_code); + } + } + + if (auth_reply.params.write.gatt_status != BLE_GATT_STATUS_SUCCESS) + { + return; + } + + p_sc_ctrlpt->procedure_status = BLE_SCPT_INDICATION_PENDING; + rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED; + + err_code = sc_ctrlpt_decode(p_evt_write->data, p_evt_write->len, &rcvd_ctrlpt); + if (err_code != NRF_SUCCESS) + { + rsp.opcode = rcvd_ctrlpt.opcode; + rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED; + } + else + { + rsp.opcode = rcvd_ctrlpt.opcode; + + switch (rcvd_ctrlpt.opcode) + { + case BLE_SCPT_REQUEST_SUPPORTED_SENSOR_LOCATIONS: + if ((p_sc_ctrlpt->supported_functions & + BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED) == + BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED) + { + rsp.status = BLE_SCPT_SUCCESS; + } + else + { + rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED; + } + break; + + case BLE_SCPT_UPDATE_SENSOR_LOCATION: + if ((p_sc_ctrlpt->supported_functions & + BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED) == + BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED) + { + if (is_location_supported(p_sc_ctrlpt, rcvd_ctrlpt.location)) + { + ble_gatts_value_t gatts_value; + uint8_t rcvd_location = (uint8_t)rcvd_ctrlpt.location; + rsp.status = BLE_SCPT_SUCCESS; + + // Initialize value struct. + memset(&gatts_value, 0, sizeof(gatts_value)); + + gatts_value.len = sizeof(uint8_t); + gatts_value.offset = 0; + gatts_value.p_value = &rcvd_location; + + evt.evt_type = BLE_SC_CTRLPT_EVT_UPDATE_LOCATION; + evt.params.update_location = rcvd_ctrlpt.location; + if (p_sc_ctrlpt->evt_handler != NULL) + { + rsp.status = p_sc_ctrlpt->evt_handler(p_sc_ctrlpt, &evt); + } + if (rsp.status == BLE_SCPT_SUCCESS) + { + err_code = sd_ble_gatts_value_set(p_sc_ctrlpt->conn_handle, + p_sc_ctrlpt->sensor_location_handle, + &gatts_value); + if (err_code != NRF_SUCCESS) + { + // Report error to application + if (p_sc_ctrlpt->error_handler != NULL) + { + p_sc_ctrlpt->error_handler(err_code); + } + rsp.status = BLE_SCPT_OPERATION_FAILED; + } + } + } + else + { + rsp.status = BLE_SCPT_INVALID_PARAMETER; + } + } + else + { + rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED; + } + break; + + case BLE_SCPT_SET_CUMULATIVE_VALUE: + if ((p_sc_ctrlpt->supported_functions & + BLE_SRV_SC_CTRLPT_CUM_VAL_OP_SUPPORTED) == + BLE_SRV_SC_CTRLPT_CUM_VAL_OP_SUPPORTED) + { + rsp.status = BLE_SCPT_SUCCESS; + + evt.evt_type = BLE_SC_CTRLPT_EVT_SET_CUMUL_VALUE; + evt.params.cumulative_value = rcvd_ctrlpt.cumulative_value; + if (p_sc_ctrlpt->evt_handler != NULL) + { + rsp.status = p_sc_ctrlpt->evt_handler(p_sc_ctrlpt, &evt); + } + } + else + { + rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED; + } + break; + + case BLE_SCPT_START_AUTOMATIC_CALIBRATION: + if ((p_sc_ctrlpt->supported_functions & + BLE_SRV_SC_CTRLPT_START_CALIB_OP_SUPPORTED) == + BLE_SRV_SC_CTRLPT_START_CALIB_OP_SUPPORTED) + { + p_sc_ctrlpt->procedure_status = BLE_SCPT_AUTOMATIC_CALIB_IN_PROGRESS; + evt.evt_type = BLE_SC_CTRLPT_EVT_START_CALIBRATION; + if (p_sc_ctrlpt->evt_handler != NULL) + { + rsp.status = p_sc_ctrlpt->evt_handler(p_sc_ctrlpt, &evt); + if (rsp.status != BLE_SCPT_SUCCESS) + { + // If the application returns an error, the response is to be sent + // right away and the calibration is considered as not started. + p_sc_ctrlpt->procedure_status = BLE_SCPT_INDICATION_PENDING; + } + } + } + else + { + rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED; + } + break; + + default: + rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED; + break; + } + + } + + p_sc_ctrlpt->response.len = ctrlpt_rsp_encode(p_sc_ctrlpt, &rsp, + p_sc_ctrlpt->response.encoded_ctrl_rsp); + + + if (p_sc_ctrlpt->procedure_status == BLE_SCPT_INDICATION_PENDING) + { + sc_ctrlpt_resp_send(p_sc_ctrlpt); + } +} + + +/**@brief Authorize WRITE request event handler. + * + * @details Handles WRITE events from the BLE stack. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @param[in] p_gatts_evt GATTS Event received from the BLE stack. + * + */ +static void on_rw_authorize_request(ble_sc_ctrlpt_t * p_sc_ctrlpt, + 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_sc_ctrlpt->sc_ctrlpt_handles.value_handle) + { + on_ctrlpt_write(p_sc_ctrlpt, &p_auth_req->request.write); + } + } + } +} + + +/**@brief Tx Complete event handler. + * + * @details Tx Complete event handler. + * Handles WRITE events from the BLE stack and if an indication was pending try sending it + * again. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * + */ +static void on_tx_complete(ble_sc_ctrlpt_t * p_sc_ctrlpt) +{ + if (p_sc_ctrlpt->procedure_status == BLE_SCPT_INDICATION_PENDING) + { + sc_ctrlpt_resp_send(p_sc_ctrlpt); + } +} + + +/**@brief Function for handling the Connect event. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_connect(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt) +{ + p_sc_ctrlpt->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; + p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS; +} + + +/**@brief Function for handling the Disconnect event. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_disconnect(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt) +{ + UNUSED_PARAMETER(p_ble_evt); + p_sc_ctrlpt->conn_handle = BLE_CONN_HANDLE_INVALID; + p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS; +} + + +/**@brief Function for handling the BLE_GATTS_EVT_HVC event. + * + * @param[in] p_sc_ctrlpt SC Ctrlpt structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_sc_hvc_confirm(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt) +{ + if (p_ble_evt->evt.gatts_evt.params.hvc.handle == p_sc_ctrlpt->sc_ctrlpt_handles.value_handle) + { + if (p_sc_ctrlpt->procedure_status == BLE_SCPT_IND_CONFIRM_PENDING) + { + p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS; + } + } +} + + +void ble_sc_ctrlpt_on_ble_evt(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt) +{ + if (p_sc_ctrlpt == NULL || p_ble_evt == NULL) + { + return; + } + + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + on_connect(p_sc_ctrlpt, p_ble_evt); + break; + + case BLE_GAP_EVT_DISCONNECTED: + on_disconnect(p_sc_ctrlpt, p_ble_evt); + break; + + case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: + on_rw_authorize_request(p_sc_ctrlpt, &p_ble_evt->evt.gatts_evt); + break; + + case BLE_GATTS_EVT_HVC: + on_sc_hvc_confirm(p_sc_ctrlpt, p_ble_evt); + break; + + case BLE_GATTS_EVT_HVN_TX_COMPLETE: + on_tx_complete(p_sc_ctrlpt); + break; + + default: + break; + } +} + + +uint32_t ble_sc_ctrlpt_rsp_send(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_scpt_response_t response_status) +{ + if (p_sc_ctrlpt == NULL) + { + return NRF_ERROR_NULL; + } + + uint32_t err_code = NRF_SUCCESS; + ble_sc_ctrlpt_rsp_t rsp; + uint8_t encoded_ctrl_rsp[BLE_SC_CTRLPT_MAX_LEN]; + uint16_t hvx_len; + ble_gatts_hvx_params_t hvx_params; + + if (p_sc_ctrlpt->procedure_status != BLE_SCPT_AUTOMATIC_CALIB_IN_PROGRESS) + { + return NRF_ERROR_INVALID_STATE; + } + rsp.status = response_status; + rsp.opcode = BLE_SCPT_START_AUTOMATIC_CALIBRATION; + hvx_len = ctrlpt_rsp_encode(p_sc_ctrlpt, &rsp, encoded_ctrl_rsp); + + // Send indication + memset(&hvx_params, 0, sizeof(hvx_params)); + + hvx_params.handle = p_sc_ctrlpt->sc_ctrlpt_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_ctrl_rsp; + + err_code = sd_ble_gatts_hvx(p_sc_ctrlpt->conn_handle, &hvx_params); + + if (err_code == NRF_SUCCESS) + { + p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS; + } + return err_code; +} diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_sc_ctrlpt.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_sc_ctrlpt.h new file mode 100644 index 0000000..05a701d --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ble/ble_services/ble_cscs/ble_sc_ctrlpt.h @@ -0,0 +1,247 @@ +/** + * Copyright (c) 2013 - 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_sc_ctrlpt Speed and Cadence Control Point + * @{ + * @ingroup ble_sdk_srv + * @brief Speed and Cadence Control Point module. + * + * @details This module implements the Speed and Cadence control point behavior. It is used + * by the @ref ble_cscs module and the ble_sdk_srv_rsc module for control point + * mechanisms like setting a cumulative value, Start an automatic calibration, + * Update the sensor location or request the supported locations. + * + * @note Attention! + * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile + * qualification listings, this section of source code must not be modified. + */ + +#ifndef BLE_SC_CTRLPT_H__ +#define BLE_SC_CTRLPT_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "ble.h" +#include "ble_srv_common.h" +#include "ble_sensor_location.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_SC_CTRLPT_MAX_LEN 19 /**< maximum lenght for Speed and cadence control point characteristic value. */ +#define BLE_SC_CTRLPT_MIN_LEN 1 /**< minimum length for Speed and cadence control point characteristic value. */ + +// Forward declaration of the ble_sc_ctrlpt_t type. +typedef struct ble_sc_ctrlpt_s ble_sc_ctrlpt_t; + + +/**@brief Speed and Cadence Control Point event type. */ +typedef enum +{ + BLE_SC_CTRLPT_EVT_UPDATE_LOCATION, /**< rcvd update location opcode (the control point handles the change of location automatically, the event just informs the application in case it needs to adjust its algorithm). */ + BLE_SC_CTRLPT_EVT_SET_CUMUL_VALUE, /**< rcvd set cumulative value opcode, it is then up to the application to use the new cumulative value. */ + BLE_SC_CTRLPT_EVT_START_CALIBRATION, /**< rcvd start calibration opcode, the application needs, at the end ot the calibration to call ble_sc_ctrlpt_send_rsp. */ +} ble_sc_ctrlpt_evt_type_t; + + +/**@brief Speed and Cadence Control point event. */ +typedef struct +{ + ble_sc_ctrlpt_evt_type_t evt_type; /**< Type of event. */ + union + { + ble_sensor_location_t update_location; + uint32_t cumulative_value; + }params; +} ble_sc_ctrlpt_evt_t; + + +/** Speed and Cadence Control Point operator code (see RSC service specification)*/ +typedef enum { + BLE_SCPT_SET_CUMULATIVE_VALUE = 0x01, /**< Operator to set a given cumulative value. */ + BLE_SCPT_START_AUTOMATIC_CALIBRATION = 0x02, /**< Operator to start automatic calibration. */ + BLE_SCPT_UPDATE_SENSOR_LOCATION = 0x03, /**< Operator to update the sensor location. */ + BLE_SCPT_REQUEST_SUPPORTED_SENSOR_LOCATIONS = 0x04, /**< Operator to request the supported sensor locations. */ + BLE_SCPT_RESPONSE_CODE = 0x10, /**< Response Code. */ +} ble_scpt_operator_t; + + +/** Speed and Cadence Control Point response parameter (see RSC service specification)*/ +typedef enum { + BLE_SCPT_SUCCESS = 0x01, /**< Sucess Response. */ + BLE_SCPT_OP_CODE_NOT_SUPPORTED = 0x02, /**< Error Response received opcode not supported. */ + BLE_SCPT_INVALID_PARAMETER = 0x03, /**< Error Response received parameter invalid. */ + BLE_SCPT_OPERATION_FAILED = 0x04, /**< Error Response operation failed. */ +} ble_scpt_response_t; + + +/** Speed and Cadence Control Point procedure status (indicates is a procedure is in progress or not and which procedure is in progress*/ +typedef enum { + BLE_SCPT_NO_PROC_IN_PROGRESS = 0x00, /**< No procedure in progress. */ + BLE_SCPT_AUTOMATIC_CALIB_IN_PROGRESS = 0x01, /**< Automatic Calibration is in progress. */ + BLE_SCPT_INDICATION_PENDING = 0x02, /**< Control Point Indication is pending. */ + BLE_SCPT_IND_CONFIRM_PENDING = 0x03, /**< Waiting for the indication confirmation. */ +}ble_scpt_procedure_status_t; + +/**@brief Speed and Cadence Control point event handler type. */ +typedef ble_scpt_response_t (*ble_sc_ctrlpt_evt_handler_t) (ble_sc_ctrlpt_t * p_sc_ctrlpt, + ble_sc_ctrlpt_evt_t * p_evt); + + +typedef struct{ + ble_scpt_operator_t opcode; + uint32_t cumulative_value; + ble_sensor_location_t location; +}ble_sc_ctrlpt_val_t; + + +typedef struct{ + ble_scpt_operator_t opcode; + ble_scpt_response_t status; + ble_sensor_location_t location_list[BLE_NB_MAX_SENSOR_LOCATIONS]; +}ble_sc_ctrlpt_rsp_t; + + +/** + * \defgroup BLE_SRV_SC_CTRLPT_SUPP_FUNC Control point functionalities. + *@{ + */ +#define BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED 0x01 /**< Support for sensor location related operations */ +#define BLE_SRV_SC_CTRLPT_CUM_VAL_OP_SUPPORTED 0x02 /**< Support for setting cumulative value related operations */ +#define BLE_SRV_SC_CTRLPT_START_CALIB_OP_SUPPORTED 0x04 /**< Support for starting calibration related operations */ +/** + *@} + */ + +/**@brief Speed and Cadence Control Point init structure. This contains all options and data +* needed for initialization of the Speed and Cadence Control Point module. */ +typedef struct +{ + ble_srv_cccd_security_mode_t sc_ctrlpt_attr_md; /**< Initial security level for cycling speed and cadence control point attribute */ + uint8_t supported_functions; /**< supported control point functionalities see @ref BLE_SRV_SC_CTRLPT_SUPP_FUNC. */ + uint16_t service_handle; /**< Handle of the parent service (as provided by the BLE stack). */ + ble_sc_ctrlpt_evt_handler_t evt_handler; /**< event handler */ + ble_sensor_location_t *list_supported_locations; /**< list of supported sensor locations.*/ + uint8_t size_list_supported_locations; /**< number of supported sensor locations in the list.*/ + uint16_t sensor_location_handle; /**< handle for the sensor location characteristic (if sensor_location related operation are supported).*/ + ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ +} ble_cs_ctrlpt_init_t; + + +/**@brief Speed and Cadence Control Point response indication structure. */ +typedef struct +{ + ble_scpt_response_t status; /**< control point response status .*/ + uint8_t len; /**< control point response length .*/ + uint8_t encoded_ctrl_rsp[BLE_SC_CTRLPT_MAX_LEN]; /**< control point encoded response.*/ +}ble_sc_ctrlpt_resp_t; + + +/**@brief Speed and Cadence Control Point structure. This contains various status information for + * the Speed and Cadence Control Point behavior. */ +struct ble_sc_ctrlpt_s +{ + uint8_t supported_functions; /**< supported control point functionalities see @ref BLE_SRV_SC_CTRLPT_SUPP_FUNC. */ + uint16_t service_handle; /**< Handle of the parent service (as provided by the BLE stack). */ + ble_gatts_char_handles_t sc_ctrlpt_handles; /**< Handles related to the Speed and Cadence 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). */ + ble_sensor_location_t list_supported_locations[BLE_NB_MAX_SENSOR_LOCATIONS]; /**< list of supported sensor locations.*/ + uint8_t size_list_supported_locations; /**< number of supported sensor locations in the list.*/ + ble_sc_ctrlpt_evt_handler_t evt_handler; /**< Handle of the parent service (as provided by the BLE stack). */ + uint16_t sensor_location_handle; /**< handle for the sensor location characteristic (if sensor_location related operation are supported).*/ + ble_scpt_procedure_status_t procedure_status; /**< status of possible procedure*/ + ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ + ble_sc_ctrlpt_resp_t response; /**< pending response data.*/ +}; + +#define SCPT_OPCODE_POS 0 /**< Request opcode position. */ +#define SCPT_PARAMETER_POS 1 /**< Request parameter position. */ + +#define SCPT_RESPONSE_REQUEST_OPCODE_POS 1 /**< Response position of requested opcode. */ +#define SCPT_RESPONSE_CODE_POS 2 /**< Response position of response code. */ +#define SCPT_RESPONSE_PARAMETER 3 /**< Response position of response parameter. */ + +#define SCPT_MIN_RESPONSE_SIZE 3 /**< Minimum size for control point response. */ +#define SCPT_MAX_RESPONSE_SIZE (SCPT_MIN_RESPONSE_SIZE + NB_MAX_SENSOR_LOCATIONS) /**< Maximum size for control point response. */ + + +/**@brief Function for Initializing the Speed and Cadence Control Point. + * + * @details Function for Initializing the Speed and Cadence Control Point. + * @param[in] p_sc_ctrlpt Speed and Cadence Control Point structure. + * @param[in] p_sc_ctrlpt_init Information needed to initialize the control point behavior. + * + * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. + */ +uint32_t ble_sc_ctrlpt_init(ble_sc_ctrlpt_t * p_sc_ctrlpt, + ble_cs_ctrlpt_init_t const * p_sc_ctrlpt_init); + + +/**@brief Function for sending a control point response. + * + * @details Function for sending a control point response when the control point received was + * BLE_SCPT_START_AUTOMATIC_CALIBRATION. To be called after the calibration procedure is finished. + * + * @param[in] p_sc_ctrlpt Speed and Cadence Control Point structure. + * @param[in] response_status status to include in the control point response. + */ +uint32_t ble_sc_ctrlpt_rsp_send(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_scpt_response_t response_status); + + +/**@brief Speed and Cadence Control Point BLE stack event handler. + * + * @details Handles all events from the BLE stack of interest to the Speed and Cadence Control Point. + * + * @param[in] p_sc_ctrlpt Speed and Cadence Control Point structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +void ble_sc_ctrlpt_on_ble_evt(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt); + + + +#ifdef __cplusplus +} +#endif + +#endif // BLE_SC_CTRLPT_H__ + +/** @} */ |