初始版本
This commit is contained in:
516
components/ble/ble_services/ble_dis_c/ble_dis_c.c
Normal file
516
components/ble/ble_services/ble_dis_c/ble_dis_c.c
Normal file
@@ -0,0 +1,516 @@
|
||||
/**
|
||||
* Copyright (c) 2017 - 2020, 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_DIS_C)
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ble.h"
|
||||
#include "ble_dis_c.h"
|
||||
#include "ble_gattc.h"
|
||||
#include "nrf_bitmask.h"
|
||||
#include "app_error.h"
|
||||
|
||||
#define NRF_LOG_MODULE_NAME ble_dis
|
||||
#include "nrf_log.h"
|
||||
NRF_LOG_MODULE_REGISTER();
|
||||
|
||||
|
||||
// Value Field lengths for System ID characteristic.
|
||||
#define BLE_DIS_C_MANUF_ID_LEN 5 /**< Length of Manufacturer ID field inside System ID characteristic. */
|
||||
#define BLE_DIS_C_OU_ID_LEN 3 /**< Length of Organizationally Unique ID field inside System ID characteristic. */
|
||||
|
||||
// Value Field lengths for PnP ID characteristic.
|
||||
#define BLE_DIS_C_VENDOR_ID_SRC_LEN 1 /**< Length of Vendor ID Source field inside PnP ID characteristic. */
|
||||
#define BLE_DIS_C_VENDOR_ID_LEN 2 /**< Length of Vendor ID field inside PnP ID characteristic. */
|
||||
#define BLE_DIS_C_PRODUCT_ID_LEN 2 /**< Length of Product ID field inside PnP ID characteristic. */
|
||||
#define BLE_DIS_C_PRODUCT_VER_LEN 2 /**< Length of Product Version field inside PnP ID characteristic. */
|
||||
|
||||
#define BLE_DIS_C_ALL_CHARS_DISABLED_MASK 0x0000 /**< All DIS characteristics should be disabled. */
|
||||
#define BLE_DIS_C_ALL_CHARS_ENABLED_MASK 0xFFFF /**< All DIS characteristics should be enabled. */
|
||||
|
||||
|
||||
/**@brief Function for interception of gattc errors.
|
||||
*
|
||||
* @param[in] nrf_error Error code returned by SoftDevice.
|
||||
* @param[in] p_contex Parameter from the event handler.
|
||||
* @param[in] conn_handle Connection handle.
|
||||
*/
|
||||
static void gatt_error_handler(uint32_t nrf_error, void * p_contex, uint16_t conn_handle)
|
||||
{
|
||||
UNUSED_PARAMETER(conn_handle);
|
||||
|
||||
ble_dis_c_t const * const p_ble_disc_c = (ble_dis_c_t *)p_contex;
|
||||
|
||||
if (p_ble_disc_c != NULL)
|
||||
{
|
||||
p_ble_disc_c->error_handler(nrf_error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**@brief Function for decoding System ID characteristic value.
|
||||
*
|
||||
* @param[in] p_data Pointer to System ID characteristic data.
|
||||
* @param[in] len Length of the System ID characteristic data.
|
||||
* @param[out] p_sys_id Decoded System ID characteristic.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the characteristic was initialized successfully.
|
||||
* @retval NRF_ERROR_INVALID_LENGTH Any parameter is NULL.
|
||||
*/
|
||||
static ret_code_t system_id_decode(uint8_t const * p_data,
|
||||
uint16_t len,
|
||||
ble_dis_sys_id_t * const p_sys_id)
|
||||
{
|
||||
uint16_t const expected_len = (BLE_DIS_C_MANUF_ID_LEN + BLE_DIS_C_OU_ID_LEN);
|
||||
|
||||
// Validate response length.
|
||||
if (expected_len != len)
|
||||
{
|
||||
NRF_LOG_ERROR("System ID characteristic data cannot be decoded.");
|
||||
NRF_LOG_ERROR("Expected data length != Received data length: %d != %d", expected_len, len);
|
||||
return NRF_ERROR_INVALID_LENGTH;
|
||||
}
|
||||
|
||||
// Decode Manufacturer ID.
|
||||
p_sys_id->manufacturer_id = uint40_decode(p_data);
|
||||
p_data += BLE_DIS_C_MANUF_ID_LEN;
|
||||
|
||||
// Decode Organizationally unique ID.
|
||||
p_sys_id->organizationally_unique_id = uint24_decode(p_data);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/**@brief Function for decoding PnP ID characteristic value.
|
||||
*
|
||||
* @param[in] p_data Pointer to PnP ID characteristic data.
|
||||
* @param[in] len Length of the PnP ID characteristic data.
|
||||
* @param[out] p_pnp_id Decoded PnP ID characteristic.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the characteristic was initialized successfully.
|
||||
* @retval NRF_ERROR_INVALID_LENGTH Any parameter is NULL.
|
||||
*/
|
||||
static ret_code_t pnp_id_decode(uint8_t const * p_data,
|
||||
uint16_t len,
|
||||
ble_dis_pnp_id_t * const p_pnp_id)
|
||||
{
|
||||
uint16_t const expected_len = (BLE_DIS_C_VENDOR_ID_SRC_LEN + BLE_DIS_C_VENDOR_ID_LEN +
|
||||
BLE_DIS_C_PRODUCT_ID_LEN + BLE_DIS_C_PRODUCT_VER_LEN);
|
||||
|
||||
// Validate response length.
|
||||
if (expected_len != len)
|
||||
{
|
||||
NRF_LOG_ERROR("PnP ID characteristic data cannot be decoded.");
|
||||
NRF_LOG_ERROR("Expected data length != Received data length: %d != %d", expected_len, len);
|
||||
return NRF_ERROR_INVALID_LENGTH;
|
||||
}
|
||||
|
||||
// Decode Vendor ID Source.
|
||||
p_pnp_id->vendor_id_source = p_data[0];
|
||||
p_data += BLE_DIS_C_VENDOR_ID_SRC_LEN;
|
||||
|
||||
// Decode Vendor ID.
|
||||
p_pnp_id->vendor_id = uint16_decode(p_data);
|
||||
p_data += BLE_DIS_C_VENDOR_ID_LEN;
|
||||
|
||||
// Decode Product ID.
|
||||
p_pnp_id->product_id = uint16_decode(p_data);
|
||||
p_data += BLE_DIS_C_PRODUCT_ID_LEN;
|
||||
|
||||
// Decode Product Version.
|
||||
p_pnp_id->product_version = uint16_decode(p_data);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/**@brief Function for matching DIS Client characteristic type with the provided response handle.
|
||||
*
|
||||
* @param[in] p_ble_dis_c Pointer to the Device Information Client Structure.
|
||||
* @param[in] response_handle Attribute handle from the response event.
|
||||
*/
|
||||
static ble_dis_c_char_type_t char_type_get(ble_dis_c_t * p_ble_dis_c, uint16_t response_handle)
|
||||
{
|
||||
for (ble_dis_c_char_type_t char_type = (ble_dis_c_char_type_t) 0;
|
||||
char_type < BLE_DIS_C_CHAR_TYPES_NUM;
|
||||
char_type++)
|
||||
{
|
||||
if (response_handle == p_ble_dis_c->handles[char_type])
|
||||
{
|
||||
return char_type;
|
||||
}
|
||||
}
|
||||
return BLE_DIS_C_CHAR_TYPES_NUM;
|
||||
}
|
||||
|
||||
|
||||
/**@brief Function for handling read response events.
|
||||
*
|
||||
* @details This function will validate the read response and raise the appropriate
|
||||
* event to the application.
|
||||
*
|
||||
* @param[in] p_ble_dis_c Pointer to the Device Information Client Structure.
|
||||
* @param[in] p_ble_evt Pointer to the SoftDevice event.
|
||||
*/
|
||||
static void on_read_rsp(ble_dis_c_t * p_ble_dis_c, ble_evt_t const * p_ble_evt)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
ble_gattc_evt_read_rsp_t const * p_response = &p_ble_evt->evt.gattc_evt.params.read_rsp;
|
||||
ble_dis_c_evt_t ble_dis_c_evt;
|
||||
ble_dis_c_char_type_t char_type;
|
||||
|
||||
// Check if the event is on the link for this instance and the event handler is present.
|
||||
if ((p_ble_dis_c->evt_handler == NULL) ||
|
||||
(p_ble_dis_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
char_type = char_type_get(p_ble_dis_c, p_response->handle);
|
||||
if (char_type < BLE_DIS_C_CHAR_TYPES_NUM) // Characteristic type is valid.
|
||||
{
|
||||
memset(&ble_dis_c_evt, 0, sizeof(ble_dis_c_evt_t));
|
||||
|
||||
ble_dis_c_evt.conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
|
||||
|
||||
if (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_SUCCESS)
|
||||
{
|
||||
ble_dis_c_evt_read_rsp_t * const p_dis_rsp = &ble_dis_c_evt.params.read_rsp;
|
||||
|
||||
ble_dis_c_evt.evt_type = BLE_DIS_C_EVT_DIS_C_READ_RSP;
|
||||
|
||||
p_dis_rsp->char_type = char_type;
|
||||
p_dis_rsp->handle = p_response->handle;
|
||||
|
||||
// Decode characteristic value.
|
||||
switch (char_type)
|
||||
{
|
||||
case BLE_DIS_C_MANUF_NAME:
|
||||
case BLE_DIS_C_MODEL_NUM:
|
||||
case BLE_DIS_C_SERIAL_NUM:
|
||||
case BLE_DIS_C_HW_REV:
|
||||
case BLE_DIS_C_FW_REV:
|
||||
case BLE_DIS_C_SW_REV:
|
||||
p_dis_rsp->content.string.p_data = (uint8_t *) p_response->data;
|
||||
p_dis_rsp->content.string.len = p_response->len;
|
||||
break;
|
||||
|
||||
case BLE_DIS_C_SYS_ID:
|
||||
err_code = system_id_decode(p_response->data,
|
||||
p_response->len,
|
||||
&p_dis_rsp->content.sys_id);
|
||||
if ((p_ble_dis_c->error_handler != NULL) && (err_code != NRF_SUCCESS))
|
||||
{
|
||||
p_ble_dis_c->error_handler(err_code);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_DIS_C_CERT_LIST:
|
||||
p_dis_rsp->content.cert_list.p_list = (uint8_t *) p_response->data;
|
||||
p_dis_rsp->content.cert_list.list_len = p_response->len;
|
||||
break;
|
||||
|
||||
case BLE_DIS_C_PNP_ID:
|
||||
err_code = pnp_id_decode(p_response->data,
|
||||
p_response->len,
|
||||
&p_dis_rsp->content.pnp_id);
|
||||
if ((p_ble_dis_c->error_handler != NULL) && (err_code != NRF_SUCCESS))
|
||||
{
|
||||
p_ble_dis_c->error_handler(err_code);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p_ble_dis_c->evt_handler(p_ble_dis_c, &ble_dis_c_evt);
|
||||
NRF_LOG_DEBUG("Received correct read response.");
|
||||
}
|
||||
else // Generate error event.
|
||||
{
|
||||
ble_dis_c_evt.evt_type = BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR;
|
||||
|
||||
ble_dis_c_evt.params.read_rsp_err.char_type = char_type;
|
||||
ble_dis_c_evt.params.read_rsp_err.err_handle = p_ble_evt->evt.gattc_evt.error_handle;
|
||||
ble_dis_c_evt.params.read_rsp_err.gatt_status = p_ble_evt->evt.gattc_evt.gatt_status;
|
||||
|
||||
p_ble_dis_c->evt_handler(p_ble_dis_c, &ble_dis_c_evt);
|
||||
NRF_LOG_ERROR("Read request failed: 0x%04X.", p_ble_evt->evt.gattc_evt.gatt_status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**@brief Function for handling Disconnected event received from the SoftDevice.
|
||||
*
|
||||
* @details This function checks if the disconnect event is happening on the link
|
||||
* associated with the current instance of the module. If so, it will set its
|
||||
* conn_handle to invalid.
|
||||
*
|
||||
* @param[in] p_ble_dis_c Pointer to the Device Information Client Structure.
|
||||
* @param[in] p_ble_evt Pointer to the BLE event received.
|
||||
*/
|
||||
static void on_disconnected(ble_dis_c_t * p_ble_dis_c, const ble_evt_t * p_ble_evt)
|
||||
{
|
||||
if (p_ble_dis_c->conn_handle == p_ble_evt->evt.gap_evt.conn_handle)
|
||||
{
|
||||
p_ble_dis_c->conn_handle = BLE_CONN_HANDLE_INVALID;
|
||||
|
||||
if (p_ble_dis_c->evt_handler != NULL)
|
||||
{
|
||||
ble_dis_c_evt_t dis_c_evt =
|
||||
{
|
||||
.evt_type = BLE_DIS_C_EVT_DISCONNECTED,
|
||||
.conn_handle = p_ble_evt->evt.gap_evt.conn_handle
|
||||
};
|
||||
|
||||
p_ble_dis_c->evt_handler(p_ble_dis_c, &dis_c_evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ret_code_t ble_dis_c_init(ble_dis_c_t * p_ble_dis_c, ble_dis_c_init_t * p_ble_dis_c_init)
|
||||
{
|
||||
ble_uuid_t dis_uuid;
|
||||
|
||||
VERIFY_PARAM_NOT_NULL(p_ble_dis_c);
|
||||
VERIFY_PARAM_NOT_NULL(p_ble_dis_c_init);
|
||||
|
||||
dis_uuid.type = BLE_UUID_TYPE_BLE;
|
||||
dis_uuid.uuid = BLE_UUID_DEVICE_INFORMATION_SERVICE;
|
||||
|
||||
p_ble_dis_c->conn_handle = BLE_CONN_HANDLE_INVALID;
|
||||
p_ble_dis_c->p_gatt_queue = p_ble_dis_c_init->p_gatt_queue;
|
||||
p_ble_dis_c->evt_handler = p_ble_dis_c_init->evt_handler;
|
||||
p_ble_dis_c->error_handler = p_ble_dis_c_init->error_handler;
|
||||
memset(p_ble_dis_c->handles, BLE_GATT_HANDLE_INVALID, sizeof(p_ble_dis_c->handles));
|
||||
|
||||
// Enable only selected characteristics if characteristic group is defined.
|
||||
if (p_ble_dis_c_init->char_group.p_char_type != NULL)
|
||||
{
|
||||
p_ble_dis_c->char_mask = BLE_DIS_C_ALL_CHARS_DISABLED_MASK;
|
||||
|
||||
for (uint8_t i = 0; i < p_ble_dis_c_init->char_group.size; i++)
|
||||
{
|
||||
nrf_bitmask_bit_set(p_ble_dis_c_init->char_group.p_char_type[i],
|
||||
&p_ble_dis_c->char_mask);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
p_ble_dis_c->char_mask = BLE_DIS_C_ALL_CHARS_ENABLED_MASK;
|
||||
}
|
||||
|
||||
return ble_db_discovery_evt_register(&dis_uuid);
|
||||
}
|
||||
|
||||
|
||||
void ble_dis_c_on_db_disc_evt(ble_dis_c_t * p_ble_dis_c, ble_db_discovery_evt_t * p_evt)
|
||||
{
|
||||
ble_gatt_db_char_t * p_chars = p_evt->params.discovered_db.charateristics;
|
||||
ble_dis_c_evt_t ble_dis_c_evt;
|
||||
|
||||
// Check if the service discovery is necessary for the link and if the event handler is present.
|
||||
if ((p_ble_dis_c->evt_handler == NULL) ||
|
||||
(p_ble_dis_c->conn_handle == p_evt->conn_handle))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the DIS was discovered.
|
||||
if ((p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE) &&
|
||||
(p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_DEVICE_INFORMATION_SERVICE) &&
|
||||
(p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE))
|
||||
{
|
||||
memset(&ble_dis_c_evt, 0, sizeof(ble_dis_c_evt_t));
|
||||
ble_dis_c_evt.evt_type = BLE_DIS_C_EVT_DISCOVERY_COMPLETE;
|
||||
ble_dis_c_evt.conn_handle = p_evt->conn_handle;
|
||||
|
||||
for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
|
||||
{
|
||||
switch (p_chars[i].characteristic.uuid.uuid)
|
||||
{
|
||||
case BLE_UUID_MANUFACTURER_NAME_STRING_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_MANUF_NAME] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_MODEL_NUMBER_STRING_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_MODEL_NUM] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_SERIAL_NUMBER_STRING_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_SERIAL_NUM] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_HARDWARE_REVISION_STRING_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_HW_REV] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_FIRMWARE_REVISION_STRING_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_FW_REV] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_SOFTWARE_REVISION_STRING_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_SW_REV] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_SYSTEM_ID_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_SYS_ID] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_CERT_LIST] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
case BLE_UUID_PNP_ID_CHAR:
|
||||
ble_dis_c_evt.params.disc_complete.handles[BLE_DIS_C_PNP_ID] =
|
||||
p_chars[i].characteristic.handle_value;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Forget handle values for disabled characteristics
|
||||
for (ble_dis_c_char_type_t char_type = (ble_dis_c_char_type_t) 0;
|
||||
char_type < BLE_DIS_C_CHAR_TYPES_NUM;
|
||||
char_type++)
|
||||
{
|
||||
if (!nrf_bitmask_bit_is_set(char_type, &p_ble_dis_c->char_mask))
|
||||
{
|
||||
ble_dis_c_evt.params.disc_complete.handles[char_type] = BLE_GATT_HANDLE_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
p_ble_dis_c->evt_handler(p_ble_dis_c, &ble_dis_c_evt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ble_dis_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
|
||||
{
|
||||
ble_dis_c_t * p_ble_dis_c = (ble_dis_c_t *) p_context;
|
||||
|
||||
if ((p_ble_dis_c == NULL) || (p_ble_evt == NULL))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (p_ble_dis_c->conn_handle == BLE_CONN_HANDLE_INVALID)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (p_ble_evt->header.evt_id)
|
||||
{
|
||||
case BLE_GATTC_EVT_READ_RSP:
|
||||
on_read_rsp(p_ble_dis_c, p_ble_evt);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
on_disconnected(p_ble_dis_c, p_ble_evt);
|
||||
break;
|
||||
|
||||
default:
|
||||
// No implementation needed.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ret_code_t ble_dis_c_read(ble_dis_c_t * p_ble_dis_c, ble_dis_c_char_type_t char_type)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
nrf_ble_gq_req_t dis_c_req;
|
||||
|
||||
VERIFY_PARAM_NOT_NULL(p_ble_dis_c);
|
||||
VERIFY_TRUE(char_type < BLE_DIS_C_CHAR_TYPES_NUM, NRF_ERROR_INVALID_PARAM);
|
||||
|
||||
if ((p_ble_dis_c->conn_handle == BLE_CONN_HANDLE_INVALID) ||
|
||||
(p_ble_dis_c->handles[char_type] == BLE_GATT_HANDLE_INVALID))
|
||||
{
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
memset(&dis_c_req, 0, sizeof(dis_c_req));
|
||||
dis_c_req.type = NRF_BLE_GQ_REQ_GATTC_READ;
|
||||
dis_c_req.error_handler.cb = gatt_error_handler;
|
||||
dis_c_req.error_handler.p_ctx = p_ble_dis_c;
|
||||
dis_c_req.params.gattc_read.handle = p_ble_dis_c->handles[char_type];
|
||||
|
||||
err_code = nrf_ble_gq_item_add(p_ble_dis_c->p_gatt_queue, &dis_c_req, p_ble_dis_c->conn_handle);
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t ble_dis_c_handles_assign(ble_dis_c_t * p_ble_dis_c,
|
||||
uint16_t conn_handle,
|
||||
ble_dis_c_handle_t const * p_peer_handles)
|
||||
{
|
||||
VERIFY_PARAM_NOT_NULL(p_ble_dis_c);
|
||||
|
||||
p_ble_dis_c->conn_handle = conn_handle;
|
||||
if (p_peer_handles != NULL)
|
||||
{
|
||||
memcpy(p_ble_dis_c->handles, p_peer_handles, sizeof(p_ble_dis_c->handles));
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(p_ble_dis_c->handles, BLE_GATT_HANDLE_INVALID, sizeof(p_ble_dis_c->handles));
|
||||
}
|
||||
return nrf_ble_gq_conn_handle_register(p_ble_dis_c->p_gatt_queue, conn_handle);
|
||||
}
|
||||
|
||||
|
||||
#endif // NRF_MODULE_ENABLED(BLE_DIS_C)
|
||||
|
||||
311
components/ble/ble_services/ble_dis_c/ble_dis_c.h
Normal file
311
components/ble/ble_services/ble_dis_c/ble_dis_c.h
Normal file
@@ -0,0 +1,311 @@
|
||||
/**
|
||||
* Copyright (c) 2017 - 2020, 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_dis_c Device Information Service Client
|
||||
* @{
|
||||
* @ingroup ble_sdk_srv
|
||||
* @brief Device information Service Client module.
|
||||
*
|
||||
* @details This module contains the APIs and types exposed by the Device information Service Client
|
||||
* module. These APIs and types can be used by the application to perform discovery of
|
||||
* the Device information Service at the peer and interact with it.
|
||||
*
|
||||
* @note The application must register this module as BLE event observer using the
|
||||
* NRF_SDH_BLE_OBSERVER macro. Example:
|
||||
* @code
|
||||
* ble_dis_c_t instance;
|
||||
* NRF_SDH_BLE_OBSERVER(anything, BLE_DIS_C_BLE_OBSERVER_PRIO,
|
||||
* ble_dis_c_on_ble_evt, &instance);
|
||||
* @endcode
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef BLE_DIS_C_H__
|
||||
#define BLE_DIS_C_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "ble.h"
|
||||
#include "ble_gatt.h"
|
||||
#include "ble_db_discovery.h"
|
||||
#include "ble_srv_common.h"
|
||||
#include "ble_dis.h"
|
||||
#include "nrf_ble_gq.h"
|
||||
#include "nrf_sdh_ble.h"
|
||||
|
||||
#include "sdk_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**@brief Macro for defining a ble_dis_c instance.
|
||||
*
|
||||
* @param _name Name of the instance.
|
||||
* @hideinitializer
|
||||
*/
|
||||
#define BLE_DIS_C_DEF(_name) \
|
||||
static ble_dis_c_t _name; \
|
||||
NRF_SDH_BLE_OBSERVER(_name ## _obs, \
|
||||
BLE_DIS_C_BLE_OBSERVER_PRIO, \
|
||||
ble_dis_c_on_ble_evt, &_name)
|
||||
|
||||
/** @brief Macro for defining multiple ble_dis_c instances.
|
||||
*
|
||||
* @param _name Name of the array of instances.
|
||||
* @param _cnt Number of instances to define.
|
||||
* @hideinitializer
|
||||
*/
|
||||
#define BLE_DIS_C_ARRAY_DEF(_name, _cnt) \
|
||||
static ble_dis_c_t _name[_cnt]; \
|
||||
NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
|
||||
BLE_DIS_C_BLE_OBSERVER_PRIO, \
|
||||
ble_dis_c_on_ble_evt, &_name, _cnt)
|
||||
|
||||
|
||||
/**@brief DIS Client event type. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_DIS_C_EVT_DISCOVERY_COMPLETE, /**< Event indicating that the DIS and its characteristics were discovered. See @ref ble_dis_c_evt_disc_complete_t. */
|
||||
BLE_DIS_C_EVT_DIS_C_READ_RSP, /**< Event indicating that the client has received a read response from a peer. See @ref ble_dis_c_evt_read_rsp_t. */
|
||||
BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR, /**< Event indicating that the client's read request has failed. See @ref ble_dis_c_evt_read_rsp_err_t. */
|
||||
BLE_DIS_C_EVT_DISCONNECTED /**< Event indicating that the DIS server has disconnected. */
|
||||
} ble_dis_c_evt_type_t;
|
||||
|
||||
/**@brief DIS Client characteristic type. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_DIS_C_MANUF_NAME, /**< Manufacturer Name String characteristic. */
|
||||
BLE_DIS_C_MODEL_NUM, /**< Model Number String characteristic. */
|
||||
BLE_DIS_C_SERIAL_NUM, /**< Serial Number String characteristic. */
|
||||
BLE_DIS_C_HW_REV, /**< Hardware Revision String characteristic. */
|
||||
BLE_DIS_C_FW_REV, /**< Firmware Revision String characteristic. */
|
||||
BLE_DIS_C_SW_REV, /**< Software Revision String characteristic. */
|
||||
BLE_DIS_C_SYS_ID, /**< System ID characteristic. */
|
||||
BLE_DIS_C_CERT_LIST, /**< IEEE 11073-20601 Regulatory Certification Data List characteristic. */
|
||||
BLE_DIS_C_PNP_ID, /**< PnP ID characteristic. */
|
||||
BLE_DIS_C_CHAR_TYPES_NUM /**< Number of all possible characteristic types. */
|
||||
} ble_dis_c_char_type_t;
|
||||
|
||||
/**@brief Attribute handle pointing to DIS characteristics on the connected peer device. */
|
||||
typedef uint16_t ble_dis_c_handle_t;
|
||||
|
||||
/**@brief Event structure for @ref BLE_DIS_C_EVT_DISCOVERY_COMPLETE. */
|
||||
typedef struct
|
||||
{
|
||||
ble_dis_c_handle_t handles[BLE_DIS_C_CHAR_TYPES_NUM]; /**< Handles on the connected peer device needed to interact with it. */
|
||||
} ble_dis_c_evt_disc_complete_t;
|
||||
|
||||
/**@brief Response data for string-based DIS characteristics. */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t * p_data; /**< Pointer to response data. */
|
||||
uint8_t len; /**< Response data length. */
|
||||
} ble_dis_c_string_t;
|
||||
|
||||
/**@brief Event structure for @ref BLE_DIS_C_EVT_DIS_C_READ_RSP. */
|
||||
typedef struct
|
||||
{
|
||||
ble_dis_c_char_type_t char_type; /**< Characteristic type. */
|
||||
ble_dis_c_handle_t handle; /**< Attribute handle from the response event. */
|
||||
union
|
||||
{
|
||||
ble_dis_c_string_t string; /**< String-based characteristics response data. Filled when char_type is in the following range: @ref BLE_DIS_C_MANUF_NAME - @ref BLE_DIS_C_SW_REV (inclusive). */
|
||||
ble_dis_sys_id_t sys_id; /**< System ID characteristic response data. Filled when char_type is @ref BLE_DIS_C_SYS_ID. */
|
||||
ble_dis_reg_cert_data_list_t cert_list; /**< IEEE 11073-20601 Regulatory Certification Data List characteristic response data. Filled when char_type is @ref BLE_DIS_C_CERT_LIST. */
|
||||
ble_dis_pnp_id_t pnp_id; /**< PnP ID characteristic response data. Filled when char_type is @ref BLE_DIS_C_PNP_ID. */
|
||||
} content;
|
||||
} ble_dis_c_evt_read_rsp_t;
|
||||
|
||||
/**@brief Event structure for @ref BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR. */
|
||||
typedef struct
|
||||
{
|
||||
ble_dis_c_char_type_t char_type; /**< Characteristic type. */
|
||||
ble_dis_c_handle_t err_handle; /**< Attribute handle from the response event. */
|
||||
uint16_t gatt_status; /**< GATT status code for the read operation, see @ref BLE_GATT_STATUS_CODES. */
|
||||
} ble_dis_c_evt_read_rsp_err_t;
|
||||
|
||||
/**@brief Structure containing the DIS event data received from the peer. */
|
||||
typedef struct
|
||||
{
|
||||
ble_dis_c_evt_type_t evt_type; /**< Type of the event. */
|
||||
uint16_t conn_handle; /**< Connection handle on which the @ref ble_dis_c_evt_t event occurred.*/
|
||||
union
|
||||
{
|
||||
ble_dis_c_evt_disc_complete_t disc_complete; /**< Discovery Complete Event Parameters. Filled when evt_type is @ref BLE_DIS_C_EVT_DISCOVERY_COMPLETE. */
|
||||
ble_dis_c_evt_read_rsp_t read_rsp; /**< Read Response Event Parameters. Filled when evt_type is @ref BLE_DIS_C_EVT_DIS_C_READ_RSP. */
|
||||
ble_dis_c_evt_read_rsp_err_t read_rsp_err; /**< Read Response Error Event Parameters. Filled when evt_type is @ref BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR. */
|
||||
} params;
|
||||
} ble_dis_c_evt_t;
|
||||
|
||||
// Forward declaration of the ble_dis_t type.
|
||||
typedef struct ble_dis_c_s ble_dis_c_t;
|
||||
|
||||
/**@brief Event handler type.
|
||||
*
|
||||
* @details This is the type of the event handler that should be provided by the application
|
||||
* of this module to receive events.
|
||||
*/
|
||||
typedef void (* ble_dis_c_evt_handler_t)(ble_dis_c_t * p_ble_dis_c, ble_dis_c_evt_t const * p_evt);
|
||||
|
||||
/**@brief DIS Client structure. */
|
||||
struct ble_dis_c_s
|
||||
{
|
||||
uint16_t conn_handle; /**< Handle of the current connection. Set with @ref ble_dis_c_handles_assign when connected. */
|
||||
uint16_t char_mask; /**< Mask with enabled DIS characteristics.*/
|
||||
ble_dis_c_handle_t handles[BLE_DIS_C_CHAR_TYPES_NUM]; /**< Handles on the connected peer device needed to interact with it. */
|
||||
ble_srv_error_handler_t error_handler; /**< Application error handler to be called in case of an error. */
|
||||
ble_dis_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the DIS. */
|
||||
nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
|
||||
};
|
||||
|
||||
/**@brief Structure describing the group of DIS characteristics with which this module can interact. */
|
||||
typedef struct
|
||||
{
|
||||
ble_dis_c_char_type_t * p_char_type; /**< Pointer to array with selected characteristics. */
|
||||
uint8_t size; /**< Group size. */
|
||||
} ble_dis_c_char_group_t;
|
||||
|
||||
/**@brief DIS Client initialization structure. */
|
||||
typedef struct
|
||||
{
|
||||
ble_dis_c_char_group_t char_group; /**< Group of DIS characteristics that should be enabled for this module instance. */
|
||||
ble_srv_error_handler_t error_handler; /**< Application error handler to be called in case of an error. */
|
||||
ble_dis_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the Device Information service. */
|
||||
nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
|
||||
} ble_dis_c_init_t;
|
||||
|
||||
|
||||
/**@brief Function for initializing the Device Information service client module.
|
||||
*
|
||||
* @details This function registers with the Database Discovery module
|
||||
* for the DIS. Doing so will make the Database Discovery
|
||||
* module look for the presence of a DIS instance at the peer when a
|
||||
* discovery is started.
|
||||
*
|
||||
* @param[in] p_ble_dis_c Pointer to the DIS client structure.
|
||||
* @param[in] p_ble_dis_c_init Pointer to the DIS initialization structure containing the
|
||||
* initialization information.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the module was initialized successfully.
|
||||
* @retval NRF_ERROR_NULL Any parameter is NULL.
|
||||
* @return If functions from other modules return errors to this function
|
||||
* (@ref ble_db_discovery_evt_register), the @ref nrf_error are propagated.
|
||||
*/
|
||||
ret_code_t ble_dis_c_init(ble_dis_c_t * p_ble_dis_c, ble_dis_c_init_t * p_ble_dis_c_init);
|
||||
|
||||
|
||||
/**@brief Function for handling events from the database discovery module.
|
||||
*
|
||||
* @details This function will handle an event from the database discovery module, and determine
|
||||
* if it relates to the discovery of DIS at the peer. If so, it will
|
||||
* call the application's event handler indicating that DIS has been
|
||||
* discovered at the peer. It also populates the event with the service related
|
||||
* information before providing it to the application.
|
||||
*
|
||||
* @param[in] p_ble_dis_c Pointer to the DIS client structure.
|
||||
* @param[in] p_evt Pointer to the event received from the database discovery module.
|
||||
*/
|
||||
void ble_dis_c_on_db_disc_evt(ble_dis_c_t * p_ble_dis_c, ble_db_discovery_evt_t * p_evt);
|
||||
|
||||
|
||||
/**@brief Function for handling BLE events from the SoftDevice.
|
||||
*
|
||||
* @details This function handles the BLE events received from the SoftDevice. If a BLE
|
||||
* event is relevant to the DIS module, it is used to update internal variables
|
||||
* and, if necessary, send events to the application.
|
||||
*
|
||||
* @param[in] p_ble_evt Pointer to the BLE event.
|
||||
* @param[in] p_context Pointer to the DIS client structure.
|
||||
*/
|
||||
void ble_dis_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
|
||||
|
||||
|
||||
/**@brief Function for reading different characteristics from DIS.
|
||||
*
|
||||
* @details This function can be used to read different characteristics that are available
|
||||
* inside DIS. The response data will be provided from the response event
|
||||
* @ref BLE_DIS_C_EVT_DIS_C_READ_RSP. The @ref BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR
|
||||
* event can be generated if the read operation is unsuccessful.
|
||||
*
|
||||
* @param[in] p_ble_dis_c Pointer to the DIS client structure.
|
||||
* @param[in] char_type Type of characteristic to read.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_NULL If a \p p_ble_dis_c was a NULL pointer.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If a \p char_type is not valid.
|
||||
* @retval NRF_ERROR_INVALID_STATE If connection handle or attribute handle is invalid.
|
||||
* @retval NRF_ERROR_NO_MEM If the client request queue is full.
|
||||
*/
|
||||
ret_code_t ble_dis_c_read(ble_dis_c_t * p_ble_dis_c, ble_dis_c_char_type_t char_type);
|
||||
|
||||
|
||||
/**@brief Function for assigning handles to this instance of dis_c.
|
||||
*
|
||||
* @details Call this function when a link has been established with a peer to
|
||||
* associate this link to this instance of the module. This makes it
|
||||
* possible to handle several links and associate each link to a particular
|
||||
* instance of this module. The connection handle and attribute handles will be
|
||||
* provided from the discovery event @ref BLE_DIS_C_EVT_DISCOVERY_COMPLETE.
|
||||
*
|
||||
* @param[in] p_ble_dis_c Pointer to the DIS client structure instance to associate with these
|
||||
* handles.
|
||||
* @param[in] conn_handle Connection handle associated with the given DIS Instance.
|
||||
* @param[in] p_peer_handles Attribute handles on the DIS server that you want this DIS client to
|
||||
* interact with.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_NULL If a \p p_ble_dis_c was a NULL pointer.
|
||||
*/
|
||||
ret_code_t ble_dis_c_handles_assign(ble_dis_c_t * p_ble_dis_c,
|
||||
uint16_t conn_handle,
|
||||
ble_dis_c_handle_t const * p_peer_handles);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BLE_DIS_C_H__
|
||||
|
||||
/** @} */
|
||||
Reference in New Issue
Block a user