enhance advertising

- Separate BLEAdvertisingData & BLEAdvertising
- add setType(), setTimeout(), setInterval(), setIntervalMS()
- add setStopCallback() support
- Add option to only advertising for a specific time (in multiple of
timeout) using start(stop_sec)
- default timeout = 30 second, fast = 20 ms, slow = 152.5 ms
This commit is contained in:
hathach committed 2017-07-12 13:40:15 +07:00
1 parent 412a2c9e9d
commit 0302d88a7a
12 files changed
+264 -99

No files matched your search

+3
View File
@@ -7,6 +7,9 @@
- Change Bluefruit.peerAddr() to Bluefruit.getPeerAddr()
- Change BLEClientUart's rx callback
- Add connection handle to Bluefruit's connect and disconnect callback prototype
- BLEAdvertising enhancement
- Seperated BLEAdvertisingData and BLEAdvertising
- Add setTimeout(), setType()
## 0.6.5
@@ -117,7 +117,6 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
Serial.println();
Serial.println("Disconnected");
Serial.println("Bluefruit will auto start advertising (default)");
}
/**
@@ -86,7 +86,6 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
Serial.println();
Serial.println("Disconnected");
Serial.println("Bluefruit will auto start advertising (default)");
}
/**************************************************************************/
+157 -60
View File
@@ -35,57 +35,23 @@
/**************************************************************************/
#include "bluefruit.h"
#include "utility/AdaCallback.h"
#define GAP_ADV_INTERVAL_MS 20
#define GAP_ADV_TIMEOUT_S 30
#define BLE_ADV_INTERVAL_FAST_DFLT 32 // 20 ms (in 0.625 ms unit)
#define BLE_ADV_INTERVAL_SLOW_DFLT 244 // 152.5 ms (in 0.625 ms unit)
BLEAdvertising::BLEAdvertising(void)
#define BLE_ADV_TIMEOUT_DFLT 30 // in seconds
/*------------------------------------------------------------------*/
/* BLEAdvertisingData shared between ADV and ScanResponse
*------------------------------------------------------------------*/
BLEAdvertisingData::BLEAdvertisingData(void)
{
_count = 0;
varclr(_data);
}
bool BLEAdvertising::start(uint8_t mode)
{
(void) mode; // not used yet
// Only allow to call start with Bluefruit.Advertising.start()
VERIFY( this == &Bluefruit.Advertising );
// Configure Data
VERIFY_STATUS( sd_ble_gap_adv_data_set(_data, _count, Bluefruit.ScanResponse._data, Bluefruit.ScanResponse._count), false );
// ADV Params
ble_gap_adv_params_t adv_para =
{
.type = BLE_GAP_ADV_TYPE_ADV_IND,
.p_peer_addr = NULL , // Undirected advertisement
.fp = BLE_GAP_ADV_FP_ANY ,
.p_whitelist = NULL ,
.interval = MS1000TO625(GAP_ADV_INTERVAL_MS), // advertising interval (in units of 0.625 ms)
.timeout = GAP_ADV_TIMEOUT_S
};
VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para), false );
Bluefruit.startConnLed(); // start blinking
return ERROR_NONE;
}
bool BLEAdvertising::stop(void)
{
// Only allow to call stop with Bluefruit.Advertising.stop()
VERIFY( this == &Bluefruit.Advertising );
VERIFY_STATUS( sd_ble_gap_adv_stop(), false);
Bluefruit.stopConnLed(); // stop blinking
return true;
}
bool BLEAdvertising::addData(uint8_t type, const void* data, uint8_t len)
bool BLEAdvertisingData::addData(uint8_t type, const void* data, uint8_t len)
{
VERIFY( _count + len + 2 <= BLE_GAP_ADV_MAX_SIZE );
@@ -101,7 +67,7 @@ bool BLEAdvertising::addData(uint8_t type, const void* data, uint8_t len)
return true;
}
bool BLEAdvertising::addUuid(BLEUuid bleuuid)
bool BLEAdvertisingData::addUuid(BLEUuid bleuuid)
{
switch ( bleuuid.size() )
{
@@ -119,12 +85,12 @@ bool BLEAdvertising::addUuid(BLEUuid bleuuid)
return false;
}
bool BLEAdvertising::addService(BLEService& service)
bool BLEAdvertisingData::addService(BLEService& service)
{
return addUuid(service.uuid);
}
bool BLEAdvertising::addService(BLEClientService& service)
bool BLEAdvertisingData::addService(BLEClientService& service)
{
// Central service is added to Solicitation UUID
switch ( service.uuid.size() )
@@ -144,7 +110,7 @@ bool BLEAdvertising::addService(BLEClientService& service)
}
// Add Name to Adv packet, use setName() to set
bool BLEAdvertising::addName(void)
bool BLEAdvertisingData::addName(void)
{
const char* name = Bluefruit.getName();
@@ -162,41 +128,36 @@ bool BLEAdvertising::addName(void)
}
// tx power is set by setTxPower
bool BLEAdvertising::addTxPower(void)
bool BLEAdvertisingData::addTxPower(void)
{
int8_t tx_power = Bluefruit.getTxPower();
return addData(BLE_GAP_AD_TYPE_TX_POWER_LEVEL, &tx_power, 1);
}
bool BLEAdvertising::addFlags(uint8_t flags)
bool BLEAdvertisingData::addFlags(uint8_t flags)
{
return addData(BLE_GAP_AD_TYPE_FLAGS, &flags, 1);
}
bool BLEAdvertising::addAppearance(uint16_t appearance)
bool BLEAdvertisingData::addAppearance(uint16_t appearance)
{
return addData(BLE_GAP_AD_TYPE_APPEARANCE, &appearance, 2);
}
bool BLEAdvertising::setBeacon(BLEBeacon& beacon)
{
return beacon.start(*this);
}
/*------------------------------------------------------------------*/
/* CUSTOM API
*------------------------------------------------------------------*/
uint8_t BLEAdvertising::count(void)
uint8_t BLEAdvertisingData::count(void)
{
return _count;
}
char* BLEAdvertising::getData(void)
char* BLEAdvertisingData::getData(void)
{
return (char*) _data;
}
bool BLEAdvertising::setData(uint8_t const * data, uint8_t count)
bool BLEAdvertisingData::setData(uint8_t const * data, uint8_t count)
{
VERIFY( data && (count <= BLE_GAP_ADV_MAX_SIZE) );
@@ -206,9 +167,145 @@ bool BLEAdvertising::setData(uint8_t const * data, uint8_t count)
return true;
}
void BLEAdvertising::clearData(void)
void BLEAdvertisingData::clearData(void)
{
_count = 0;
varclr(_data);
}
/*------------------------------------------------------------------*/
/* BLEAdvertising only
*------------------------------------------------------------------*/
BLEAdvertising::BLEAdvertising(void)
{
_timeout_sec = BLE_ADV_TIMEOUT_DFLT;
_type = BLE_GAP_ADV_TYPE_ADV_IND;
_fast_interval = BLE_ADV_INTERVAL_FAST_DFLT;
_slow_interval = BLE_ADV_INTERVAL_SLOW_DFLT;
_started_sec = 0;
_stop_sec = 0;
_stop_cb = NULL;
}
void BLEAdvertising::setTimeout(uint16_t sec)
{
_timeout_sec = sec;
}
void BLEAdvertising::setType(uint8_t adv_type)
{
_type = adv_type;
}
void BLEAdvertising::setInterval(uint16_t fast, uint16_t slow)
{
_fast_interval = fast;
_slow_interval = slow;
}
void BLEAdvertising::setIntervalMS(uint16_t fast, uint16_t slow)
{
_fast_interval = MS1000TO625(fast);
_slow_interval = MS1000TO625(slow);
}
void BLEAdvertising::setStopCallback(stop_callback_t fp)
{
fp = _stop_cb;
}
bool BLEAdvertising::setBeacon(BLEBeacon& beacon)
{
return beacon.start(*this);
}
bool BLEAdvertising::_start(uint16_t interval)
{
// ADV Params
ble_gap_adv_params_t adv_para =
{
.type = _type ,
.p_peer_addr = NULL , // Undirected advertisement
.fp = BLE_GAP_ADV_FP_ANY ,
.p_whitelist = NULL ,
.interval = interval , // advertising interval (in units of 0.625 ms)
.timeout = _timeout_sec
};
VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para), false );
Bluefruit._startConnLed(); // start blinking
return true;
}
bool BLEAdvertising::start(uint32_t stop_sec)
{
// Configure Data
VERIFY_STATUS( sd_ble_gap_adv_data_set(_data, _count, Bluefruit.ScanResponse._data, Bluefruit.ScanResponse._count), false );
VERIFY( _start(_fast_interval) );
_started_sec = 0; // reset established time
return true;
}
bool BLEAdvertising::stop(void)
{
VERIFY_STATUS( sd_ble_gap_adv_stop(), false);
Bluefruit._stopConnLed(); // stop blinking
return true;
}
void BLEAdvertising::_eventHandler(ble_evt_t* evt)
{
switch ( evt->header.evt_id )
{
case BLE_GAP_EVT_CONNECTED:
{
ble_gap_evt_connected_t const * para = &evt->evt.gap_evt.params.connected;
if ( para->role == BLE_GAP_ROLE_PERIPH)
{
// Turn on Conn LED
Bluefruit._stopConnLed();
Bluefruit._setConnLed(true);
}
}
break;
case BLE_GAP_EVT_DISCONNECTED:
if ( BLE_GAP_ROLE_PERIPH == Bluefruit.Gap.getRole(evt->evt.common_evt.conn_handle) )
{
// Turn off Conn LED
Bluefruit._setConnLed(false);
}
break;
case BLE_GAP_EVT_TIMEOUT:
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
{
_started_sec += _timeout_sec;
// Continue to adv if stop is not configured or time not reach it yet
if ( (_stop_sec == 0) || (_started_sec < _stop_sec) )
{
_start(_slow_interval);
}else
{
Bluefruit._stopConnLed(); // stop blinking
// invoke stop callback
if (_stop_cb) ada_callback(NULL, _stop_cb);
}
}
break;
default: break;
}
}
+46 -13
View File
@@ -44,23 +44,16 @@
#include "BLEService.h"
#include "services/BLEBeacon.h"
//enum
//{
// BLE_ADV_MODE
//};
class BLEAdvertising
class BLEAdvertisingData
{
private:
protected:
uint8_t _data[BLE_GAP_ADV_MAX_SIZE];
uint8_t _count;
public:
BLEAdvertising(void);
bool start(uint8_t mode = 0);
bool stop (void);
BLEAdvertisingData(void);
/*------------- Adv Data -------------*/
bool addData(uint8_t type, const void* data, uint8_t len);
bool addFlags(uint8_t flags);
bool addTxPower(void);
@@ -71,13 +64,53 @@ public:
bool addService(BLEService& service);
bool addService(BLEClientService& service);
bool setBeacon(BLEBeacon& beacon);
// Custom API
uint8_t count(void);
char* getData(void);
bool setData(const uint8_t* data, uint8_t count);
void clearData(void);
};
class BLEAdvertising : public BLEAdvertisingData
{
public:
typedef void (*stop_callback_t) (void);
BLEAdvertising(void);
void setType(uint8_t adv_type);
void setTimeout(uint16_t sec);
void setStopCallback(stop_callback_t fp);
void setInterval(uint16_t fast, uint16_t slow);
void setIntervalMS(uint16_t fast, uint16_t slow);
bool setBeacon(BLEBeacon& beacon);
bool start(uint32_t stop_sec = 0);
bool stop (void);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal
* code. User should not call these directly
*------------------------------------------------------------------*/
void _eventHandler(ble_evt_t* evt);
private:
uint16_t _timeout_sec;
uint8_t _type;
uint16_t _fast_interval;
uint16_t _slow_interval;
uint16_t _started_sec;
uint16_t _stop_sec;
stop_callback_t _stop_cb;
// Internal function
bool _start(uint16_t interval);
};
+5 -5
View File
@@ -71,13 +71,13 @@ bool BLECentral::startScanning(uint16_t timeout)
{
_scan_param.timeout = timeout;
VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param), false );
Bluefruit.startConnLed(); // start blinking
Bluefruit._startConnLed(); // start blinking
return ERROR_NONE;
}
bool BLECentral::stopScanning(void)
{
Bluefruit.stopConnLed(); // stop blinking
Bluefruit._stopConnLed(); // stop blinking
VERIFY_STATUS( sd_ble_gap_scan_stop(), false );
return true;
}
@@ -245,15 +245,15 @@ void BLECentral::_event_handler(ble_evt_t* evt)
if (para->role == BLE_GAP_ROLE_CENTRAL)
{
Bluefruit.stopConnLed();
if (Bluefruit._led_conn) ledOn(LED_BLUE);
Bluefruit._stopConnLed();
Bluefruit._setConnLed(true);
}
}
break;
case BLE_GAP_EVT_DISCONNECTED:
// Note callback is invoked by BLEGap
if (Bluefruit._led_conn) ledOff(LED_BLUE);
Bluefruit._setConnLed(false);
_conn_hdl = BLE_CONN_HANDLE_INVALID;
startScanning();
break;
+6
View File
@@ -49,6 +49,11 @@ bool BLEGap::connected(uint16_t conn_handle)
return _peers[conn_handle].connected;
}
uint8_t BLEGap::getRole(uint16_t conn_handle)
{
return _peers[conn_handle].role;
}
void BLEGap::setConnectCallback(connect_callback_t fp, uint8_t role)
{
if (role == BLE_GAP_ROLE_PERIPH)
@@ -135,6 +140,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
{
ble_gap_evt_disconnected_t const* para = &evt->evt.gap_evt.params.disconnected;
// mark as disconnected, but keep the role for sub sequence event handler
peer->connected = false;
vSemaphoreDelete( peer->txpacket_sem );
+3
View File
@@ -50,12 +50,15 @@ class BLEGap
bool connected(uint16_t conn_handle);
uint8_t getRole(uint16_t conn_handle);
uint8_t getPeerAddr(uint16_t conn_handle, uint8_t addr[6]);
ble_gap_addr_t getPeerAddr(uint16_t conn_handle);
uint16_t getPeerName(uint16_t conn_handle, char* buf, uint16_t bufsize);
bool getTxPacket(uint16_t conn_handle);
/*------------- Callbacks -------------*/
void setConnectCallback (connect_callback_t fp, uint8_t role);
void setDisconnectCallback(disconnect_callback_t fp, uint8_t role);
+27 -17
View File
@@ -364,19 +364,6 @@ bool AdafruitBluefruit::setPIN(const char* pin)
}
)
/*------------------------------------------------------------------*/
/* Private Methods
*------------------------------------------------------------------*/
void AdafruitBluefruit::startConnLed(void)
{
if (_led_conn) xTimerStart(_led_blink_th, 0);
}
void AdafruitBluefruit::stopConnLed(void)
{
xTimerStop(_led_blink_th, 0);
}
/*------------------------------------------------------------------*/
/* Thread & SoftDevice Event handler
*------------------------------------------------------------------*/
@@ -457,6 +444,10 @@ void adafruit_ble_task(void* arg)
}
}
/**
* BLE event handler
* @param evt event
*/
void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
{
LOG_LV1(BLE, dbg_ble_event_str(evt->header.evt_id));
@@ -468,6 +459,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
// GAP handler
Gap._eventHandler(evt);
Advertising._eventHandler(evt);
/*------------- BLE Peripheral Events -------------*/
/* Only handle Peripheral events with matched connection handle
@@ -487,9 +479,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
if (para->role == BLE_GAP_ROLE_PERIPH)
{
stopConnLed();
if (_led_conn) ledOn(LED_BLUE);
_conn_hdl = evt->evt.gap_evt.conn_handle;
_conn_interval = para->conn_params.min_conn_interval;
@@ -515,7 +504,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
case BLE_GAP_EVT_DISCONNECTED:
// Note callback is invoked by BLEGap
if (_led_conn) ledOff(LED_BLUE);
// Save all configured cccd
if (_bonded) _saveBondedCCCD();
@@ -662,6 +650,28 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
// GATTs characteristics event handler
Gatt._eventHandler(evt);
}
/*------------------------------------------------------------------*/
/* Internal Connection LED
*------------------------------------------------------------------*/
void AdafruitBluefruit::_startConnLed(void)
{
if (_led_conn) xTimerStart(_led_blink_th, 0);
}
void AdafruitBluefruit::_stopConnLed(void)
{
xTimerStop(_led_blink_th, 0);
}
void AdafruitBluefruit::_setConnLed (bool on_off)
{
if (_led_conn)
{
digitalWrite(LED_BLUE, on_off ? LED_STATE_ON : (1-LED_STATE_ON) );
}
}
/*------------------------------------------------------------------*/
/* Bonds
*------------------------------------------------------------------*/
+12 -2
View File
@@ -106,10 +106,10 @@ class AdafruitBluefruit
*------------------------------------------------------------------*/
void autoConnLed (bool enabled);
void setConnLedInterval (uint32_t ms);
void startConnLed (void);
void stopConnLed (void);
void setName (const char* str);
char* getName (void);
bool setTxPower (int8_t power);
int8_t getTxPower (void);
@@ -140,6 +140,16 @@ class AdafruitBluefruit
COMMENT_OUT ( bool setPIN(const char* pin); )
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal
* code. User should not call these directly
*------------------------------------------------------------------*/
void _startConnLed (void);
void _stopConnLed (void);
void _setConnLed (bool on_off);
private:
/*------------- BLE para -------------*/
bool _prph_enabled;
@@ -66,6 +66,10 @@ void adafruit_callback_task(void* arg)
((BLEGap::disconnect_callback_t) func) ( (uint16_t) args[0], (uint8_t) args[1] );
break;
case BLEAdvertising_stop_callback_t:
((BLEAdvertising::stop_callback_t) func)();
break;
/*------------- Bluefruit -------------*/
/*------------- Central -------------*/
@@ -64,6 +64,7 @@ static_assert(sizeof(ada_callback_t) == 16, "Incorrect Size");
/* BLEGap */ \
XPAND(BLEGap , connect_callback_t ) \
XPAND(BLEGap , disconnect_callback_t ) \
XPAND(BLEAdvertising , stop_callback_t ) \
/* Bluefruit */ \
/* Central */ \
XPAND(BLECentral , scan_callback_t ) \