remove ble_advdata dependency
This commit is contained in:
parent
3aa7651266
commit
85d39845af
@ -1,766 +0,0 @@
|
||||
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
|
||||
*
|
||||
* The information contained herein is property of Nordic Semiconductor ASA.
|
||||
* Terms and conditions of usage are described in detail in NORDIC
|
||||
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
|
||||
*
|
||||
* Licensees are granted free, non-transferable use of the information. NO
|
||||
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
|
||||
* the file.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ble_advdata.h"
|
||||
#include "ble_gap.h"
|
||||
#include "ble_srv_common.h"
|
||||
#include "sdk_common.h"
|
||||
|
||||
// NOTE: For now, Security Manager Out of Band Flags (OOB) are omitted from the advertising data.
|
||||
|
||||
// Types of LE Bluetooth Device Address AD type
|
||||
#define AD_TYPE_BLE_DEVICE_ADDR_TYPE_PUBLIC 0UL
|
||||
#define AD_TYPE_BLE_DEVICE_ADDR_TYPE_RANDOM 1UL
|
||||
|
||||
static uint32_t tk_value_encode(ble_advdata_tk_value_t * p_tk_value,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
int8_t i;
|
||||
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_TK_VALUE_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Encode LE Role.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_TK_VALUE_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SECURITY_MANAGER_TK_VALUE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
|
||||
for (i = AD_TYPE_TK_VALUE_DATA_SIZE - 1; i >= 0; i--, (*p_offset)++)
|
||||
{
|
||||
p_encoded_data[*p_offset] = p_tk_value->tk[i];
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t le_role_encode(ble_advdata_le_role_t le_role,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_LE_ROLE_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Encode LE Role.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_LE_ROLE_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LE_ROLE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
switch(le_role)
|
||||
{
|
||||
case BLE_ADVDATA_ROLE_ONLY_PERIPH:
|
||||
p_encoded_data[*p_offset] = 0;
|
||||
break;
|
||||
case BLE_ADVDATA_ROLE_ONLY_CENTRAL:
|
||||
p_encoded_data[*p_offset] = 1;
|
||||
break;
|
||||
case BLE_ADVDATA_ROLE_BOTH_PERIPH_PREFERRED:
|
||||
p_encoded_data[*p_offset] = 2;
|
||||
break;
|
||||
case BLE_ADVDATA_ROLE_BOTH_CENTRAL_PREFERRED:
|
||||
p_encoded_data[*p_offset] = 3;
|
||||
break;
|
||||
default:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
*p_offset += AD_TYPE_LE_ROLE_DATA_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t ble_device_addr_encode(uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
uint32_t err_code;
|
||||
ble_gap_addr_t device_addr;
|
||||
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_BLE_DEVICE_ADDR_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Get BLE address
|
||||
err_code = sd_ble_gap_addr_get(&device_addr);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
// Encode LE Bluetooth Device Address
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE +
|
||||
AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
memcpy(&p_encoded_data[*p_offset], &device_addr.addr[0], BLE_GAP_ADDR_LEN);
|
||||
*p_offset += BLE_GAP_ADDR_LEN;
|
||||
if(BLE_GAP_ADDR_TYPE_PUBLIC == device_addr.addr_type)
|
||||
{
|
||||
p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_PUBLIC;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_RANDOM;
|
||||
}
|
||||
*p_offset += AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t name_encode(const ble_advdata_t * p_advdata,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
uint32_t err_code;
|
||||
uint16_t rem_adv_data_len;
|
||||
uint16_t actual_length;
|
||||
uint8_t adv_data_format;
|
||||
|
||||
|
||||
// Validate parameters
|
||||
if((BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) && (0 == p_advdata->short_name_len))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// Check for buffer overflow.
|
||||
if ( (((*p_offset) + ADV_AD_DATA_OFFSET) > max_size) ||
|
||||
( (BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) &&
|
||||
(((*p_offset) + ADV_AD_DATA_OFFSET + p_advdata->short_name_len) > max_size)))
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
rem_adv_data_len = max_size - (*p_offset) - ADV_AD_DATA_OFFSET;
|
||||
actual_length = rem_adv_data_len;
|
||||
|
||||
// Get GAP device name and length
|
||||
err_code = sd_ble_gap_device_name_get(&p_encoded_data[(*p_offset) + ADV_AD_DATA_OFFSET],
|
||||
&actual_length);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
// Check if device intend to use short name and it can fit available data size.
|
||||
if ((p_advdata->name_type == BLE_ADVDATA_FULL_NAME) && (actual_length <= rem_adv_data_len))
|
||||
{
|
||||
// Complete device name can fit, setting Complete Name in Adv Data.
|
||||
adv_data_format = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Else short name needs to be used. Or application has requested use of short name.
|
||||
adv_data_format = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME;
|
||||
|
||||
// If application has set a preference on the short name size, it needs to be considered,
|
||||
// else fit what can be fit.
|
||||
if ((BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) &&
|
||||
(p_advdata->short_name_len <= rem_adv_data_len))
|
||||
{
|
||||
// Short name fits available size.
|
||||
actual_length = p_advdata->short_name_len;
|
||||
}
|
||||
// Else whatever can fit the data buffer will be packed.
|
||||
else
|
||||
{
|
||||
actual_length = rem_adv_data_len;
|
||||
}
|
||||
}
|
||||
|
||||
// There is only 1 byte intended to encode length which is (actual_length + ADV_AD_TYPE_FIELD_SIZE)
|
||||
if(actual_length > (0x00FF - ADV_AD_TYPE_FIELD_SIZE))
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Complete name field in encoded data.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + actual_length);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = adv_data_format;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
*p_offset += actual_length;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t appearance_encode(uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
uint32_t err_code;
|
||||
uint16_t appearance;
|
||||
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_APPEARANCE_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Get GAP appearance field.
|
||||
err_code = sd_ble_gap_appearance_get(&appearance);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
// Encode Length, AD Type and Appearance.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_APPEARANCE_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_APPEARANCE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
*p_offset += uint16_encode(appearance, &p_encoded_data[*p_offset]);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t flags_encode(int8_t flags,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_FLAGS_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Encode flags.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_FLAGS_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_FLAGS;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = flags;
|
||||
*p_offset += AD_TYPE_FLAGS_DATA_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t sec_mgr_oob_flags_encode(uint8_t oob_flags,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_OOB_FLAGS_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Encode flags.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_OOB_FLAGS_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SECURITY_MANAGER_OOB_FLAGS;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = oob_flags;
|
||||
*p_offset += AD_TYPE_OOB_FLAGS_DATA_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t tx_power_level_encode(int8_t tx_power_level,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_TX_POWER_LEVEL_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Encode TX Power Level.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE +
|
||||
AD_TYPE_TX_POWER_LEVEL_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_TX_POWER_LEVEL;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = tx_power_level;
|
||||
*p_offset += AD_TYPE_TX_POWER_LEVEL_DATA_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t uuid_list_sized_encode(const ble_advdata_uuid_list_t * p_uuid_list,
|
||||
uint8_t adv_type,
|
||||
uint8_t uuid_size,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
int i;
|
||||
bool is_heading_written = false;
|
||||
uint16_t start_pos = *p_offset;
|
||||
uint16_t length;
|
||||
|
||||
for (i = 0; i < p_uuid_list->uuid_cnt; i++)
|
||||
{
|
||||
uint32_t err_code;
|
||||
uint8_t encoded_size;
|
||||
ble_uuid_t uuid = p_uuid_list->p_uuids[i];
|
||||
|
||||
// Find encoded uuid size.
|
||||
err_code = sd_ble_uuid_encode(&uuid, &encoded_size, NULL);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
// Check size.
|
||||
if (encoded_size == uuid_size)
|
||||
{
|
||||
uint8_t heading_bytes = (is_heading_written) ? 0 : ADV_AD_DATA_OFFSET;
|
||||
|
||||
// Check for buffer overflow
|
||||
if (((*p_offset) + encoded_size + heading_bytes) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
if (!is_heading_written)
|
||||
{
|
||||
// Write AD structure heading.
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = adv_type;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
is_heading_written = true;
|
||||
}
|
||||
|
||||
// Write UUID.
|
||||
err_code = sd_ble_uuid_encode(&uuid, &encoded_size, &p_encoded_data[*p_offset]);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
*p_offset += encoded_size;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_heading_written)
|
||||
{
|
||||
// Write length.
|
||||
length = (*p_offset) - (start_pos + ADV_LENGTH_FIELD_SIZE);
|
||||
// There is only 1 byte intended to encode length
|
||||
if(length > 0x00FF)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
p_encoded_data[start_pos] = (uint8_t)length;
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t uuid_list_encode(const ble_advdata_uuid_list_t * p_uuid_list,
|
||||
uint8_t adv_type_16,
|
||||
uint8_t adv_type_128,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
// Encode 16 bit UUIDs.
|
||||
err_code = uuid_list_sized_encode(p_uuid_list,
|
||||
adv_type_16,
|
||||
sizeof(uint16_le_t),
|
||||
p_encoded_data,
|
||||
p_offset,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
// Encode 128 bit UUIDs.
|
||||
err_code = uuid_list_sized_encode(p_uuid_list,
|
||||
adv_type_128,
|
||||
sizeof(ble_uuid128_t),
|
||||
p_encoded_data,
|
||||
p_offset,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t conn_int_check(const ble_advdata_conn_int_t *p_conn_int)
|
||||
{
|
||||
// Check Minimum Connection Interval.
|
||||
if ((p_conn_int->min_conn_interval < 0x0006) ||
|
||||
(
|
||||
(p_conn_int->min_conn_interval > 0x0c80) &&
|
||||
(p_conn_int->min_conn_interval != 0xffff)
|
||||
)
|
||||
)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// Check Maximum Connection Interval.
|
||||
if ((p_conn_int->max_conn_interval < 0x0006) ||
|
||||
(
|
||||
(p_conn_int->max_conn_interval > 0x0c80) &&
|
||||
(p_conn_int->max_conn_interval != 0xffff)
|
||||
)
|
||||
)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// Make sure Minimum Connection Interval is not bigger than Maximum Connection Interval.
|
||||
if ((p_conn_int->min_conn_interval != 0xffff) &&
|
||||
(p_conn_int->max_conn_interval != 0xffff) &&
|
||||
(p_conn_int->min_conn_interval > p_conn_int->max_conn_interval)
|
||||
)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t conn_int_encode(const ble_advdata_conn_int_t * p_conn_int,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + AD_TYPE_CONN_INT_SIZE) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Check parameters.
|
||||
err_code = conn_int_check(p_conn_int);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
// Encode Length and AD Type.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_CONN_INT_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
|
||||
// Encode Minimum and Maximum Connection Intervals.
|
||||
*p_offset += uint16_encode(p_conn_int->min_conn_interval, &p_encoded_data[*p_offset]);
|
||||
*p_offset += uint16_encode(p_conn_int->max_conn_interval, &p_encoded_data[*p_offset]);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t manuf_specific_data_encode(const ble_advdata_manuf_data_t * p_manuf_sp_data,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
uint32_t data_size = AD_TYPE_MANUF_SPEC_DATA_ID_SIZE + p_manuf_sp_data->data.size;
|
||||
|
||||
// Check for buffer overflow.
|
||||
if (((*p_offset) + ADV_AD_DATA_OFFSET + data_size) > max_size)
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// There is only 1 byte intended to encode length which is (data_size + ADV_AD_TYPE_FIELD_SIZE)
|
||||
if(data_size > (0x00FF - ADV_AD_TYPE_FIELD_SIZE))
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Encode Length and AD Type.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + data_size);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
|
||||
// Encode Company Identifier.
|
||||
*p_offset += uint16_encode(p_manuf_sp_data->company_identifier, &p_encoded_data[*p_offset]);
|
||||
|
||||
// Encode additional manufacturer specific data.
|
||||
if (p_manuf_sp_data->data.size > 0)
|
||||
{
|
||||
if (p_manuf_sp_data->data.p_data == NULL)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
memcpy(&p_encoded_data[*p_offset], p_manuf_sp_data->data.p_data, p_manuf_sp_data->data.size);
|
||||
*p_offset += p_manuf_sp_data->data.size;
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
// Implemented only for 16-bit UUIDs
|
||||
static uint32_t service_data_encode(const ble_advdata_t * p_advdata,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
// Check parameter consistency.
|
||||
if (p_advdata->p_service_data_array == NULL)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
for (i = 0; i < p_advdata->service_data_count; i++)
|
||||
{
|
||||
ble_advdata_service_data_t * p_service_data;
|
||||
uint32_t data_size;
|
||||
|
||||
p_service_data = &p_advdata->p_service_data_array[i];
|
||||
// For now implemented only for 16-bit UUIDs
|
||||
data_size = AD_TYPE_SERV_DATA_16BIT_UUID_SIZE + p_service_data->data.size;
|
||||
|
||||
// There is only 1 byte intended to encode length which is (data_size + ADV_AD_TYPE_FIELD_SIZE)
|
||||
if(data_size > (0x00FF - ADV_AD_TYPE_FIELD_SIZE))
|
||||
{
|
||||
return NRF_ERROR_DATA_SIZE;
|
||||
}
|
||||
|
||||
// Encode Length and AD Type.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + data_size);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SERVICE_DATA;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
|
||||
// Encode service 16-bit UUID.
|
||||
*p_offset += uint16_encode(p_service_data->service_uuid, &p_encoded_data[*p_offset]);
|
||||
|
||||
// Encode additional service data.
|
||||
if (p_service_data->data.size > 0)
|
||||
{
|
||||
if (p_service_data->data.p_data == NULL)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
memcpy(&p_encoded_data[*p_offset], p_service_data->data.p_data, p_service_data->data.size);
|
||||
*p_offset += p_service_data->data.size;
|
||||
}
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
uint32_t adv_data_encode(ble_advdata_t const * const p_advdata,
|
||||
uint8_t * const p_encoded_data,
|
||||
uint16_t * const p_len)
|
||||
{
|
||||
uint32_t err_code = NRF_SUCCESS;
|
||||
uint16_t max_size = *p_len;
|
||||
*p_len = 0;
|
||||
|
||||
//Encode Security Manager OOB Flags
|
||||
if (p_advdata->p_sec_mgr_oob_flags != NULL)
|
||||
{
|
||||
err_code = sec_mgr_oob_flags_encode(*p_advdata->p_sec_mgr_oob_flags,
|
||||
p_encoded_data,
|
||||
p_len,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode Security Manager TK value
|
||||
if (NULL != p_advdata->p_tk_value)
|
||||
{
|
||||
err_code = tk_value_encode(p_advdata->p_tk_value, p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode LE Role
|
||||
if (BLE_ADVDATA_ROLE_NOT_PRESENT != p_advdata->le_role)
|
||||
{
|
||||
err_code = le_role_encode(p_advdata->le_role, p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode LE Bluetooth Device Address
|
||||
if (p_advdata->include_ble_device_addr)
|
||||
{
|
||||
err_code = ble_device_addr_encode(p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode appearance.
|
||||
if (p_advdata->include_appearance)
|
||||
{
|
||||
err_code = appearance_encode(p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
//Encode Flags
|
||||
if(p_advdata->flags != 0 )
|
||||
{
|
||||
err_code = flags_encode(p_advdata->flags, p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode TX power level.
|
||||
if (p_advdata->p_tx_power_level != NULL)
|
||||
{
|
||||
err_code = tx_power_level_encode(*p_advdata->p_tx_power_level,
|
||||
p_encoded_data,
|
||||
p_len,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode 'more available' uuid list.
|
||||
if (p_advdata->uuids_more_available.uuid_cnt > 0)
|
||||
{
|
||||
err_code = uuid_list_encode(&p_advdata->uuids_more_available,
|
||||
BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE,
|
||||
BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE,
|
||||
p_encoded_data,
|
||||
p_len,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode 'complete' uuid list.
|
||||
if (p_advdata->uuids_complete.uuid_cnt > 0)
|
||||
{
|
||||
err_code = uuid_list_encode(&p_advdata->uuids_complete,
|
||||
BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE,
|
||||
BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE,
|
||||
p_encoded_data,
|
||||
p_len,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode 'solicited service' uuid list.
|
||||
if (p_advdata->uuids_solicited.uuid_cnt > 0)
|
||||
{
|
||||
err_code = uuid_list_encode(&p_advdata->uuids_solicited,
|
||||
BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_16BIT,
|
||||
BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_128BIT,
|
||||
p_encoded_data,
|
||||
p_len,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode Slave Connection Interval Range.
|
||||
if (p_advdata->p_slave_conn_int != NULL)
|
||||
{
|
||||
err_code = conn_int_encode(p_advdata->p_slave_conn_int, p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode Manufacturer Specific Data.
|
||||
if (p_advdata->p_manuf_specific_data != NULL)
|
||||
{
|
||||
err_code = manuf_specific_data_encode(p_advdata->p_manuf_specific_data,
|
||||
p_encoded_data,
|
||||
p_len,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode Service Data.
|
||||
if (p_advdata->service_data_count > 0)
|
||||
{
|
||||
err_code = service_data_encode(p_advdata, p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode name. WARNING: it is encoded last on purpose since too long device name is truncated.
|
||||
if (p_advdata->name_type != BLE_ADVDATA_NO_NAME)
|
||||
{
|
||||
err_code = name_encode(p_advdata, p_encoded_data, p_len, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t advdata_check(const ble_advdata_t * p_advdata)
|
||||
{
|
||||
// Flags must be included in advertising data, and the BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED flag must be set.
|
||||
if (
|
||||
((p_advdata->flags & BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) == 0)
|
||||
)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t srdata_check(const ble_advdata_t * p_srdata)
|
||||
{
|
||||
// Flags shall not be included in the scan response data.
|
||||
if (p_srdata->flags)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
uint32_t ble_advdata_set(const ble_advdata_t * p_advdata, const ble_advdata_t * p_srdata)
|
||||
{
|
||||
enum { BLE_GAP_ADV_MAX_SIZE = BLE_GAP_ADV_SR_MAX_LEN_DEFAULT };
|
||||
|
||||
uint32_t err_code;
|
||||
uint16_t len_advdata = BLE_GAP_ADV_MAX_SIZE;
|
||||
uint16_t len_srdata = BLE_GAP_ADV_MAX_SIZE;
|
||||
uint8_t encoded_advdata[BLE_GAP_ADV_MAX_SIZE];
|
||||
uint8_t encoded_srdata[BLE_GAP_ADV_MAX_SIZE];
|
||||
uint8_t * p_encoded_advdata;
|
||||
uint8_t * p_encoded_srdata;
|
||||
|
||||
// Encode advertising data (if supplied).
|
||||
if (p_advdata != NULL)
|
||||
{
|
||||
err_code = advdata_check(p_advdata);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
err_code = adv_data_encode(p_advdata, encoded_advdata, &len_advdata);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
p_encoded_advdata = encoded_advdata;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_encoded_advdata = NULL;
|
||||
len_advdata = 0;
|
||||
}
|
||||
|
||||
// Encode scan response data (if supplied).
|
||||
if (p_srdata != NULL)
|
||||
{
|
||||
err_code = srdata_check(p_srdata);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
err_code = adv_data_encode(p_srdata, encoded_srdata, &len_srdata);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
p_encoded_srdata = encoded_srdata;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_encoded_srdata = NULL;
|
||||
len_srdata = 0;
|
||||
}
|
||||
|
||||
ble_data_t adv_data = { .p_data = p_encoded_advdata, .len = len_advdata };
|
||||
ble_data_t sr_data = { .p_data = p_encoded_srdata, .len = len_srdata };
|
||||
|
||||
// Pass encoded advertising data and/or scan response data to the stack.
|
||||
return sd_ble_gap_adv_data_set(BLE_GAP_ADV_SET_HANDLE_DEFAULT, &adv_data, &sr_data);
|
||||
}
|
@ -1,212 +0,0 @@
|
||||
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
|
||||
*
|
||||
* The information contained herein is property of Nordic Semiconductor ASA.
|
||||
* Terms and conditions of usage are described in detail in NORDIC
|
||||
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
|
||||
*
|
||||
* Licensees are granted free, non-transferable use of the information. NO
|
||||
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
|
||||
* the file.
|
||||
*
|
||||
*/
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @defgroup ble_sdk_lib_advdata Advertising and Scan Response Data Encoder
|
||||
* @{
|
||||
* @ingroup ble_sdk_lib
|
||||
* @brief Functions for encoding data in the Advertising and Scan Response Data format,
|
||||
* and for passing the data to the stack.
|
||||
*/
|
||||
|
||||
#ifndef BLE_ADVDATA_H__
|
||||
#define BLE_ADVDATA_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "ble.h"
|
||||
#include "app_util.h"
|
||||
|
||||
|
||||
#define ADV_LENGTH_FIELD_SIZE 1UL /**< Advertising Data and Scan Response format contains 1 octet for the length. */
|
||||
#define ADV_AD_TYPE_FIELD_SIZE 1UL /**< Advertising Data and Scan Response format contains 1 octet for the AD type. */
|
||||
#define ADV_AD_DATA_OFFSET (ADV_LENGTH_FIELD_SIZE + \
|
||||
ADV_AD_TYPE_FIELD_SIZE) /**< Offset for the AD data field of the Advertising Data and Scan Response format. */
|
||||
#define AD_TYPE_TK_VALUE_DATA_SIZE (sizeof(ble_advdata_tk_value_t)) /**< Data size (in octets) of the Security Manager TK value AD type. */
|
||||
#define AD_TYPE_TK_VALUE_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_TK_VALUE_DATA_SIZE) /**< Size (in octets) of the Security Manager TK value AD type. */
|
||||
#define AD_TYPE_LE_ROLE_DATA_SIZE 1UL /**< Data size (in octets) of the LE Bluetooth Device Address AD type. */
|
||||
#define AD_TYPE_LE_ROLE_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_LE_ROLE_DATA_SIZE) /**< Size (in octets) of the LE Bluetooth Device Address AD type. */
|
||||
#define AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE 1UL /**< Data size (in octets) of the Address type of the LE Bluetooth Device Address AD type. */
|
||||
#define AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE (BLE_GAP_ADDR_LEN + \
|
||||
AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE) /**< Data size (in octets) of the LE Bluetooth Device Address AD type. */
|
||||
#define AD_TYPE_BLE_DEVICE_ADDR_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE) /**< Size (in octets) of the LE Bluetooth Device Address AD type. */
|
||||
#define AD_TYPE_APPEARANCE_DATA_SIZE 2UL /**< Data size (in octets) of the Appearance AD type. */
|
||||
#define AD_TYPE_APPEARANCE_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_APPEARANCE_DATA_SIZE) /**< Size (in octets) of the Appearance AD type. */
|
||||
#define AD_TYPE_FLAGS_DATA_SIZE 1UL /**< Data size (in octets) of the Flags AD type. */
|
||||
#define AD_TYPE_FLAGS_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_FLAGS_DATA_SIZE) /**< Size (in octets) of the Flags AD type. */
|
||||
#define AD_TYPE_TX_POWER_LEVEL_DATA_SIZE 1UL /**< Data size (in octets) of the TX Power Level AD type. */
|
||||
#define AD_TYPE_TX_POWER_LEVEL_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_TX_POWER_LEVEL_DATA_SIZE) /**< Size (in octets) of the TX Power Level AD type. */
|
||||
#define AD_TYPE_CONN_INT_DATA_SIZE 4UL /**< Data size (in octets) of the Slave Connection Interval Range AD type. */
|
||||
#define AD_TYPE_CONN_INT_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_CONN_INT_DATA_SIZE) /**< Data size (in octets) of the Slave Connection Interval Range AD type. */
|
||||
#define AD_TYPE_MANUF_SPEC_DATA_ID_SIZE 2UL /**< Size (in octets) of the Company Identifier Code, which is a part of the Manufacturer Specific Data AD type. */
|
||||
#define AD_TYPE_SERV_DATA_16BIT_UUID_SIZE 2UL /**< Size (in octets) of the 16-bit UUID, which is a part of the Service Data AD type. */
|
||||
#define AD_TYPE_OOB_FLAGS_DATA_SIZE 1UL /**< Data size (in octets) of the Security Manager OOB Flags AD type. */
|
||||
#define AD_TYPE_OOB_FLAGS_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_OOB_FLAGS_DATA_SIZE) /**< Size (in octets) of the Security Manager OOB Flags AD type. */
|
||||
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_SET 1U /**< Security Manager OOB Flag set. Flag selection is done using _POS defines */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_CLEAR 0U /**< Security Manager OOB Flag clear. Flag selection is done using _POS defines */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_OOB_DATA_PRESENT_POS 0UL /**< Security Manager OOB Data Present Flag position. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_OOB_LE_SUPPORTED_POS 1UL /**< Security Manager OOB Low Energy Supported Flag position. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_SIM_LE_AND_EP_POS 2UL /**< Security Manager OOB Simultaneous LE and BR/EDR to Same Device Capable Flag position. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_ADDRESS_TYPE_PUBLIC 0UL /**< Security Manager OOB Public Address type. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_ADDRESS_TYPE_RANDOM 1UL /**< Security Manager OOB Random Address type. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_ADDRESS_TYPE_POS 3UL /**< Security Manager OOB Address type Flag (0 = Public Address, 1 = Random Address) position. */
|
||||
|
||||
|
||||
/**@brief Security Manager TK value. */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t tk[BLE_GAP_SEC_KEY_LEN]; /**< Array containing TK value. */
|
||||
} ble_advdata_tk_value_t;
|
||||
|
||||
/**@brief Advertising data LE Role types. This enumeration contains the options available for the LE role inside
|
||||
* the advertising data. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_ADVDATA_ROLE_NOT_PRESENT = 0, /**< LE Role AD structure not present. */
|
||||
BLE_ADVDATA_ROLE_ONLY_PERIPH, /**< Only Peripheral Role supported. */
|
||||
BLE_ADVDATA_ROLE_ONLY_CENTRAL, /**< Only Central Role supported. */
|
||||
BLE_ADVDATA_ROLE_BOTH_PERIPH_PREFERRED, /**< Peripheral and Central Role supported. Peripheral Role preferred for connection establishment. */
|
||||
BLE_ADVDATA_ROLE_BOTH_CENTRAL_PREFERRED /**< Peripheral and Central Role supported. Central Role preferred for connection establishment */
|
||||
} ble_advdata_le_role_t;
|
||||
|
||||
/**@brief Advertising data name type. This enumeration contains the options available for the device name inside
|
||||
* the advertising data. */
|
||||
typedef enum
|
||||
{
|
||||
BLE_ADVDATA_NO_NAME, /**< Include no device name in advertising data. */
|
||||
BLE_ADVDATA_SHORT_NAME, /**< Include short device name in advertising data. */
|
||||
BLE_ADVDATA_FULL_NAME /**< Include full device name in advertising data. */
|
||||
} ble_advdata_name_type_t;
|
||||
|
||||
/**@brief UUID list type. */
|
||||
typedef struct
|
||||
{
|
||||
uint16_t uuid_cnt; /**< Number of UUID entries. */
|
||||
ble_uuid_t * p_uuids; /**< Pointer to UUID array entries. */
|
||||
} ble_advdata_uuid_list_t;
|
||||
|
||||
/**@brief Connection interval range structure. */
|
||||
typedef struct
|
||||
{
|
||||
uint16_t min_conn_interval; /**< Minimum connection interval, in units of 1.25 ms, range 6 to 3200 (7.5 ms to 4 s). */
|
||||
uint16_t max_conn_interval; /**< Maximum connection interval, in units of 1.25 ms, range 6 to 3200 (7.5 ms to 4 s). The value 0xFFFF indicates no specific maximum. */
|
||||
} ble_advdata_conn_int_t;
|
||||
|
||||
/**@brief Manufacturer specific data structure. */
|
||||
typedef struct
|
||||
{
|
||||
uint16_t company_identifier; /**< Company identifier code. */
|
||||
uint8_array_t data; /**< Additional manufacturer specific data. */
|
||||
} ble_advdata_manuf_data_t;
|
||||
|
||||
/**@brief Service data structure. */
|
||||
typedef struct
|
||||
{
|
||||
uint16_t service_uuid; /**< Service UUID. */
|
||||
uint8_array_t data; /**< Additional service data. */
|
||||
} ble_advdata_service_data_t;
|
||||
|
||||
/**@brief Advertising data structure. This structure contains all options and data needed for encoding and
|
||||
* setting the advertising data. */
|
||||
typedef struct
|
||||
{
|
||||
ble_advdata_name_type_t name_type; /**< Type of device name. */
|
||||
uint8_t short_name_len; /**< Length of short device name (if short type is specified). */
|
||||
bool include_appearance; /**< Determines if Appearance shall be included. */
|
||||
uint8_t flags; /**< Advertising data Flags field. */
|
||||
int8_t * p_tx_power_level; /**< TX Power Level field. */
|
||||
ble_advdata_uuid_list_t uuids_more_available; /**< List of UUIDs in the 'More Available' list. */
|
||||
ble_advdata_uuid_list_t uuids_complete; /**< List of UUIDs in the 'Complete' list. */
|
||||
ble_advdata_uuid_list_t uuids_solicited; /**< List of solicited UUIDs. */
|
||||
ble_advdata_conn_int_t * p_slave_conn_int; /**< Slave Connection Interval Range. */
|
||||
ble_advdata_manuf_data_t * p_manuf_specific_data; /**< Manufacturer specific data. */
|
||||
ble_advdata_service_data_t * p_service_data_array; /**< Array of Service data structures. */
|
||||
uint8_t service_data_count; /**< Number of Service data structures. */
|
||||
bool include_ble_device_addr; /**< Determines if LE Bluetooth Device Address shall be included. */
|
||||
ble_advdata_le_role_t le_role; /**< LE Role field. Included when different from @ref BLE_ADVDATA_ROLE_NOT_PRESENT. @warning This field can be used only for NFC. For BLE advertising, set it to NULL. */
|
||||
ble_advdata_tk_value_t * p_tk_value; /**< Security Manager TK value field. Included when different from NULL. @warning This field can be used only for NFC. For BLE advertising, set it to NULL.*/
|
||||
uint8_t * p_sec_mgr_oob_flags; /**< Security Manager Out Of Band Flags field. Included when different from NULL. @warning This field can be used only for NFC. For BLE advertising, set it to NULL.*/
|
||||
} ble_advdata_t;
|
||||
|
||||
/**@brief Function for encoding data in the Advertising and Scan Response data format
|
||||
* (AD structures).
|
||||
*
|
||||
* @details This function encodes data into the Advertising and Scan Response data format
|
||||
* (AD structures) based on the selections in the supplied structures. This function can be used to
|
||||
* create a payload of Advertising packet or Scan Response packet, or a payload of NFC
|
||||
* message intended for initiating the Out-of-Band pairing.
|
||||
*
|
||||
* @param[in] p_advdata Pointer to the structure for specifying the content of encoded data.
|
||||
* @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
|
||||
* @param[in,out] p_len \c in: Size of \p p_encoded_data buffer.
|
||||
* \c out: Length of encoded data.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If the operation failed because a wrong parameter was provided in \p p_advdata.
|
||||
* @retval NRF_ERROR_DATA_SIZE If the operation failed because not all the requested data could fit into the
|
||||
* provided buffer or some encoded AD structure is too long and its
|
||||
* length cannot be encoded with one octet.
|
||||
*
|
||||
* @warning This API may override the application's request to use the long name and use a short name
|
||||
* instead. This truncation will occur in case the long name does not fit the provided buffer size.
|
||||
* The application can specify a preferred short name length if truncation is required.
|
||||
* For example, if the complete device name is ABCD_HRMonitor, the application can specify the short name
|
||||
* length to be 8, so that the short device name appears as ABCD_HRM instead of ABCD_HRMo or ABCD_HRMoni
|
||||
* if the available size for the short name is 9 or 12 respectively, to have a more appropriate short name.
|
||||
* However, it should be noted that this is just a preference that the application can specify, and
|
||||
* if the preference is too large to fit in the provided buffer, the name can be truncated further.
|
||||
*/
|
||||
uint32_t adv_data_encode(ble_advdata_t const * const p_advdata,
|
||||
uint8_t * const p_encoded_data,
|
||||
uint16_t * const p_len);
|
||||
|
||||
/**@brief Function for encoding and setting the advertising data and/or scan response data.
|
||||
*
|
||||
* @details This function encodes advertising data and/or scan response data based on the selections
|
||||
* in the supplied structures, and passes the encoded data to the stack.
|
||||
*
|
||||
* @param[in] p_advdata Structure for specifying the content of the advertising data.
|
||||
* Set to NULL if advertising data is not to be set.
|
||||
* @param[in] p_srdata Structure for specifying the content of the scan response data.
|
||||
* Set to NULL if scan response data is not to be set.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If the operation failed because a wrong parameter was provided in \p p_advdata.
|
||||
* @retval NRF_ERROR_DATA_SIZE If the operation failed because not all the requested data could fit into the
|
||||
* advertising packet. The maximum size of the advertisement packet
|
||||
* is @ref BLE_GAP_ADV_MAX_SIZE.
|
||||
*
|
||||
* @warning This API may override the application's request to use the long name and use a short name
|
||||
* instead. This truncation will occur in case the long name does not fit the provided buffer size.
|
||||
* The application can specify a preferred short name length if truncation is required.
|
||||
* For example, if the complete device name is ABCD_HRMonitor, the application can specify the short name
|
||||
* length to be 8, so that the short device name appears as ABCD_HRM instead of ABCD_HRMo or ABCD_HRMoni
|
||||
* if the available size for the short name is 9 or 12 respectively, to have a more appropriate short name.
|
||||
* However, it should be noted that this is just a preference that the application can specify, and
|
||||
* if the preference is too large to fit in the provided buffer, the name can be truncated further.
|
||||
*/
|
||||
uint32_t ble_advdata_set(const ble_advdata_t * p_advdata, const ble_advdata_t * p_srdata);
|
||||
|
||||
#endif // BLE_ADVDATA_H__
|
||||
|
||||
/** @} */
|
@ -22,7 +22,7 @@
|
||||
#include "app_error.h"
|
||||
#include "softdevice_handler.h"
|
||||
#include "ble_stack_handler_types.h"
|
||||
#include "ble_advdata.h"
|
||||
//#include "ble_advdata.h"
|
||||
#include "ble_l2cap.h"
|
||||
#include "ble_gap.h"
|
||||
#include "ble_gatt.h"
|
||||
@ -128,6 +128,8 @@ static ble_gap_addr_t const * m_whitelist[1];
|
||||
static ble_gap_id_key_t const * m_gap_ids[1];
|
||||
|
||||
// Adafruit
|
||||
static uint8_t _adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
|
||||
|
||||
extern void blinky_fast_set(bool isFast);
|
||||
extern void blinky_ota_connected(void);
|
||||
extern void blinky_ota_disconneted(void);
|
||||
@ -666,31 +668,35 @@ static void on_dfu_evt(ble_dfu_t * p_dfu, ble_dfu_evt_t * p_evt)
|
||||
}
|
||||
|
||||
|
||||
static void advertising_add(ble_data_t* adv_data, uint8_t type, const void* field, uint8_t len)
|
||||
{
|
||||
if ( adv_data->len + len + 2 > BLE_GAP_ADV_SET_DATA_SIZE_MAX ) return;
|
||||
|
||||
uint8_t* adv_buf = adv_data->p_data + adv_data->len;
|
||||
|
||||
// len (1+data), type, data
|
||||
*adv_buf++ = (len+1);
|
||||
*adv_buf++ = type;
|
||||
memcpy(adv_buf, field, len);
|
||||
|
||||
adv_data->len += (len + 2);
|
||||
}
|
||||
|
||||
/**@brief Function for the Advertising functionality initialization.
|
||||
*
|
||||
* @details Encodes the required advertising data and passes it to the stack.
|
||||
* Also builds a structure to be passed to the stack when starting advertising.
|
||||
*/
|
||||
static void advertising_init(uint8_t adv_flags)
|
||||
static void advertising_init(ble_data_t* adv_data, uint8_t adv_flags)
|
||||
{
|
||||
uint32_t err_code;
|
||||
ble_advdata_t advdata;
|
||||
ble_uuid_t service_uuid;
|
||||
uint8_t len;
|
||||
uint8_t uuid128[16];
|
||||
ble_uuid_t service_uuid = { .uuid = BLE_DFU_SERVICE_UUID, .type = m_dfu.uuid_type };
|
||||
sd_ble_uuid_encode(&service_uuid, &len, uuid128);
|
||||
|
||||
service_uuid.type = m_dfu.uuid_type;
|
||||
service_uuid.uuid = BLE_DFU_SERVICE_UUID;
|
||||
|
||||
// Build and set advertising data.
|
||||
memset(&advdata, 0, sizeof(advdata));
|
||||
|
||||
advdata.name_type = BLE_ADVDATA_FULL_NAME;
|
||||
advdata.include_appearance = false;
|
||||
advdata.flags = adv_flags;
|
||||
advdata.uuids_more_available.uuid_cnt = 1;
|
||||
advdata.uuids_more_available.p_uuids = &service_uuid;
|
||||
|
||||
err_code = ble_advdata_set(&advdata, NULL);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
advertising_add(adv_data, BLE_GAP_AD_TYPE_FLAGS, &adv_flags, 1);
|
||||
advertising_add(adv_data, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, DEVICE_NAME, strlen(DEVICE_NAME));
|
||||
advertising_add(adv_data, BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE, uuid128, 16);
|
||||
}
|
||||
|
||||
|
||||
@ -702,25 +708,25 @@ static void advertising_start(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
uint8_t adv_buf[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
|
||||
ble_gap_adv_data_t gap_adv =
|
||||
{
|
||||
.adv_data = { .p_data = adv_buf, .len = 0}
|
||||
};
|
||||
|
||||
// Initialize advertising parameters (used when starting advertising).
|
||||
memset(&m_adv_params, 0, sizeof(m_adv_params));
|
||||
|
||||
m_adv_params.properties.connectable = 1;
|
||||
m_adv_params.properties.scannable = 1;
|
||||
|
||||
m_adv_params.properties.legacy_pdu = 1;
|
||||
m_adv_params.properties.tx_power = 1;
|
||||
|
||||
if (m_ble_peer_data_valid)
|
||||
{
|
||||
ble_gap_irk_t empty_irk = {{0}};
|
||||
|
||||
if (memcmp(m_ble_peer_data.irk.irk, empty_irk.irk, sizeof(empty_irk.irk)) == 0)
|
||||
{
|
||||
advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);
|
||||
m_adv_params.properties.directed = 1;
|
||||
advertising_init(&gap_adv.adv_data, BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);
|
||||
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED_HIGH_DUTY_CYCLE;
|
||||
m_adv_params.p_peer_addr = &m_ble_peer_data.addr;
|
||||
m_adv_params.fp = BLE_GAP_ADV_FP_ANY;
|
||||
m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
|
||||
m_adv_params.interval = 0;
|
||||
m_adv_params.duration = 0;
|
||||
}
|
||||
@ -739,25 +745,27 @@ static void advertising_start(void)
|
||||
err_code = sd_ble_gap_device_identities_set(m_gap_ids, NULL, 1);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
advertising_init(BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
|
||||
m_adv_params.properties.directed = 0;
|
||||
m_adv_params.fp = BLE_GAP_ADV_FP_FILTER_CONNREQ;
|
||||
advertising_init(&gap_adv.adv_data, BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
|
||||
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
|
||||
m_adv_params.filter_policy = BLE_GAP_ADV_FP_FILTER_CONNREQ;
|
||||
m_adv_params.interval = APP_ADV_INTERVAL;
|
||||
m_adv_params.duration = APP_ADV_TIMEOUT_IN_SECONDS;
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||
m_adv_params.properties.directed = 0;
|
||||
advertising_init(&gap_adv.adv_data, BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
|
||||
m_adv_params.p_peer_addr = NULL;
|
||||
m_adv_params.fp = BLE_GAP_ADV_FP_ANY;
|
||||
m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
|
||||
m_adv_params.interval = APP_ADV_INTERVAL;
|
||||
m_adv_params.duration = APP_ADV_TIMEOUT_IN_SECONDS;
|
||||
}
|
||||
|
||||
err_code = sd_ble_gap_adv_start(BLE_GAP_ADV_SET_HANDLE_DEFAULT, &m_adv_params, BLE_CONN_CFG_HIGH_BANDWIDTH);
|
||||
err_code = sd_ble_gap_adv_set_configure(&_adv_handle, &gap_adv, &m_adv_params);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = sd_ble_gap_adv_start(_adv_handle, BLE_CONN_CFG_HIGH_BANDWIDTH);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
// led_on(ADVERTISING_LED_PIN_NO);
|
||||
@ -775,7 +783,7 @@ static void advertising_stop(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
err_code = sd_ble_gap_adv_stop(BLE_GAP_ADV_SET_HANDLE_DEFAULT);
|
||||
err_code = sd_ble_gap_adv_stop(_adv_handle);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
// led_off(ADVERTISING_LED_PIN_NO);
|
||||
@ -866,8 +874,9 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
|
||||
case BLE_GAP_EVT_ADV_SET_TERMINATED:
|
||||
//case BLE_GAP_EVT_TIMEOUT:
|
||||
if (p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)
|
||||
{
|
||||
m_is_advertising = false;
|
||||
m_direct_adv_cnt--;
|
||||
|
Loading…
Reference in New Issue
Block a user