migrate advertising and scanning from SD v5 to v6 API

build ok but functionality not tested
This commit is contained in:
hathach committed 2018-05-21 18:26:51 +07:00
1 parent 694e48f8ca
commit da5e5b2b8a
5 files changed
+62 -36

No files matched your search

+31 -19
View File
@@ -47,7 +47,7 @@ BLEAdvertisingData::BLEAdvertisingData(void)
bool BLEAdvertisingData::addData(uint8_t type, const void* data, uint8_t len)
{
VERIFY( _count + len + 2 <= BLE_GAP_ADV_MAX_SIZE );
VERIFY( _count + len + 2 <= BLE_GAP_ADV_SET_DATA_SIZE_MAX );
uint8_t* adv_data = &_data[_count];
@@ -177,16 +177,16 @@ bool BLEAdvertisingData::addService(BLEClientService& service)
*/
bool BLEAdvertisingData::addName(void)
{
char name[BLE_GAP_ADV_MAX_SIZE+1];
char name[BLE_GAP_ADV_SET_DATA_SIZE_MAX+1];
uint8_t type = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME;
uint8_t len = Bluefruit.getName(name, sizeof(name));
// not enough for full name, chop it
if (_count + len + 2 > BLE_GAP_ADV_MAX_SIZE)
if (_count + len + 2 > BLE_GAP_ADV_SET_DATA_SIZE_MAX)
{
type = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME;
len = BLE_GAP_ADV_MAX_SIZE - (_count+2);
len = BLE_GAP_ADV_SET_DATA_SIZE_MAX - (_count+2);
}
VERIFY( addData(type, name, len) );
@@ -231,7 +231,7 @@ uint8_t* BLEAdvertisingData::getData(void)
bool BLEAdvertisingData::setData(uint8_t const * data, uint8_t count)
{
VERIFY( data && (count <= BLE_GAP_ADV_MAX_SIZE) );
VERIFY( data && (count <= BLE_GAP_ADV_SET_DATA_SIZE_MAX) );
memcpy(_data, data, count);
_count = count;
@@ -250,7 +250,8 @@ void BLEAdvertisingData::clearData(void)
*------------------------------------------------------------------*/
BLEAdvertising::BLEAdvertising(void)
{
_type = BLE_GAP_ADV_TYPE_ADV_IND;
_hdl = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
_type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
_start_if_disconnect = true;
_runnning = false;
@@ -337,15 +338,29 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout)
// ADV Params
ble_gap_adv_params_t adv_para =
{
.type = _type ,
.p_peer_addr = NULL , // Undirected advertisement
.fp = BLE_GAP_ADV_FP_ANY ,
.interval = interval , // advertising interval (in units of 0.625 ms)
.timeout = timeout ,
//.channel_mask = { 0, 0, 0 } , // Enable all 3 adv channels
.properties = {
.type = _type,
.anonymous = 0
},
.p_peer_addr = NULL , // Undirected advertisement
.interval = interval , // advertising interval (in units of 0.625 ms)
.duration = timeout*100 , // in 10-ms unit
.max_adv_evts = 0, // TODO can be used for fast/slow mode
.channel_mask = { 0, 0, 0, 0, 0 } , // 40 channel, set 1 to disable
.filter_policy = BLE_GAP_ADV_FP_ANY ,
//.primary_phy, .secondary_phy, .set_id, .scan_req_notification
};
VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para, CONN_CFG_PERIPHERAL), false );
// Configure Data
ble_gap_adv_data_t gap_adv =
{
.adv_data = { .p_data = _data, .len = _count },
.scan_rsp_data = { .p_data = Bluefruit.ScanResponse.getData(), .len = Bluefruit.ScanResponse.count() }
};
VERIFY_STATUS( sd_ble_gap_adv_set_configure(&_hdl, &gap_adv, &adv_para), false );
VERIFY_STATUS( sd_ble_gap_adv_start(_hdl, CONN_CFG_PERIPHERAL), false );
Bluefruit._startConnLed(); // start blinking
_runnning = true;
@@ -360,9 +375,6 @@ bool BLEAdvertising::start(uint16_t timeout)
{
_stop_timeout = _left_timeout = timeout;
// Configure Data
VERIFY_STATUS( sd_ble_gap_adv_data_set(_data, _count, Bluefruit.ScanResponse.getData(), Bluefruit.ScanResponse.count()), false );
// Initially advertising in fast mode
// Fast mode blink 2x than slow mode
Bluefruit.setConnLedInterval(CFG_ADV_BLINKY_INTERVAL/2);
@@ -373,7 +385,7 @@ bool BLEAdvertising::start(uint16_t timeout)
bool BLEAdvertising::stop(void)
{
VERIFY_STATUS( sd_ble_gap_adv_stop(), false);
VERIFY_STATUS( sd_ble_gap_adv_stop(_hdl), false);
_runnning = false;
Bluefruit._stopConnLed(); // stop blinking
@@ -412,8 +424,8 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
}
break;
case BLE_GAP_EVT_TIMEOUT:
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
case BLE_GAP_EVT_ADV_SET_TERMINATED:
if (evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)
{
_runnning = false;
@@ -72,7 +72,7 @@ class Advertisable
class BLEAdvertisingData
{
protected:
uint8_t _data[BLE_GAP_ADV_MAX_SIZE];
uint8_t _data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
uint8_t _count;
public:
@@ -148,6 +148,7 @@ public:
void _eventHandler(ble_evt_t* evt);
private:
uint8_t _hdl;
uint8_t _type;
bool _start_if_disconnect;
bool _runnning;
+18 -7
View File
@@ -36,8 +36,13 @@
#include "bluefruit.h"
static uint8_t _scan_data[BLE_GAP_SCAN_BUFFER_MAX];
BLEScanner::BLEScanner(void)
{
_report_data.p_data = _scan_data;
_report_data.len = BLE_GAP_SCAN_BUFFER_MAX;
_runnning = false;
_start_if_disconnect = true;
@@ -53,12 +58,18 @@ BLEScanner::BLEScanner(void)
_stop_cb = NULL;
_param = (ble_gap_scan_params_t) {
// TODO Extended Adv on secondary channels
.extended = 0,
.report_incomplete_evts = 0,
.active = 0,
.use_whitelist = 0,
.adv_dir_report = 0,
.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
.scan_phys = BLE_GAP_PHY_AUTO,
.interval = BLE_SCAN_INTERVAL_DFLT,
.window = BLE_SCAN_WINDOW_DFLT,
.timeout = 0, // no timeout
.timeout = 0, // no timeout, in 10 ms units
.channel_mask = { 0, 0, 0, 0, 0 }
};
}
@@ -107,7 +118,7 @@ ble_gap_scan_params_t* BLEScanner::getParams(void)
bool BLEScanner::start(uint16_t timeout)
{
_param.timeout = timeout;
VERIFY_STATUS( sd_ble_gap_scan_start(&_param), false );
VERIFY_STATUS( sd_ble_gap_scan_start(&_param, &_report_data), false );
Bluefruit._startConnLed(); // start blinking
_runnning = true;
@@ -133,7 +144,7 @@ bool BLEScanner::stop(void)
* @param scandata
* @param scanlen
* @param type
* @param buf
* @param buf Output buffer
* @param bufsize If bufsize is skipped (zero), len check will be skipped
* @return number of written bytes
*/
@@ -170,7 +181,7 @@ uint8_t BLEScanner::parseReportByType(const uint8_t* scandata, uint8_t scanlen,
uint8_t BLEScanner::parseReportByType(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* buf, uint8_t bufsize)
{
return parseReportByType(report->data, report->dlen, type, buf, bufsize);
return parseReportByType(report->data.p_data, report->data.len, type, buf, bufsize);
}
bool BLEScanner::checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEUuid ble_uuid)
@@ -199,7 +210,7 @@ bool BLEScanner::checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEU
for (int i=0; i<2; i++)
{
uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 };
uint8_t buffer[BLE_GAP_ADV_SET_DATA_SIZE_MAX] = { 0 };
uint8_t len = parseReportByType(report, type_arr[i], buffer);
uint8_t* ptr = buffer;
+9 -7
View File
@@ -96,15 +96,17 @@ public:
void _eventHandler(ble_evt_t* evt);
private:
bool _runnning;
bool _start_if_disconnect;
ble_data_t _report_data;
int8_t _filter_rssi;
bool _filter_msd_en; // since all value of manufacturer id is valid (0-FFFF)
uint16_t _filter_msd_id;
bool _runnning;
bool _start_if_disconnect;
BLEUuid* _filter_uuid;
uint8_t _filter_uuid_count;
int8_t _filter_rssi;
bool _filter_msd_en; // since all value of manufacturer id is valid (0-FFFF)
uint16_t _filter_msd_id;
BLEUuid* _filter_uuid;
uint8_t _filter_uuid_count;
rx_callback_t _rx_cb;
stop_callback_t _stop_cb;
+2 -2
View File
@@ -388,7 +388,7 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
VERIFY_STATUS ( sd_ble_gap_device_name_set(&sec_mode, (uint8_t const *) CFG_DEFAULT_NAME, strlen(CFG_DEFAULT_NAME)) );
VERIFY_STATUS( sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN) );
VERIFY_STATUS( sd_ble_gap_tx_power_set( CFG_BLE_TX_POWER_LEVEL ) );
VERIFY_STATUS( sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN, CONN_CFG_PERIPHERAL, CFG_BLE_TX_POWER_LEVEL ) );
/*------------- DFU OTA as built-in service -------------*/
_dfu_svc.begin();
@@ -449,7 +449,7 @@ bool AdafruitBluefruit::setTxPower(int8_t power)
VERIFY(i < sizeof(accepted));
// Apply
VERIFY_STATUS( sd_ble_gap_tx_power_set(power), false);
VERIFY_STATUS( sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN, CONN_CFG_PERIPHERAL, power), false );
_tx_power = power;
return true;