From 77b1a0b8efc7b536bbf7a8cca85df9ebf3eee51c Mon Sep 17 00:00:00 2001 From: Florian Echtler Date: Mon, 23 Oct 2017 12:09:37 +0200 Subject: [PATCH 1/3] minor refactoring to prepare for advertisement updates --- src/BLEPeripheral.cpp | 11 +++++++---- src/BLEPeripheral.h | 4 ++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/BLEPeripheral.cpp b/src/BLEPeripheral.cpp index a730a81..cf2952f 100644 --- a/src/BLEPeripheral.cpp +++ b/src/BLEPeripheral.cpp @@ -68,12 +68,9 @@ BLEPeripheral::~BLEPeripheral() { } } -void BLEPeripheral::begin() { +unsigned char BLEPeripheral::updateAdvertismentData() { unsigned char advertisementDataSize = 0; - BLEEirData advertisementData[3]; - BLEEirData scanData; - scanData.length = 0; unsigned char remainingAdvertisementDataLength = BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH + 2; @@ -131,6 +128,11 @@ void BLEPeripheral::begin() { memcpy(scanData.data, this->_localName, scanData.length); } + return advertisementDataSize; +} + +void BLEPeripheral::begin() { + if (this->_localAttributes == NULL) { this->initLocalAttributes(); } @@ -158,6 +160,7 @@ void BLEPeripheral::begin() { this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic); } + int advertisementDataSize = updateAdvertismentData(); this->_device->begin(advertisementDataSize, advertisementData, scanData.length > 0 ? 1 : 0, &scanData, this->_localAttributes, this->_numLocalAttributes, diff --git a/src/BLEPeripheral.h b/src/BLEPeripheral.h index e9e8705..10d4b21 100644 --- a/src/BLEPeripheral.h +++ b/src/BLEPeripheral.h @@ -126,6 +126,7 @@ class BLEPeripheral : public BLEDeviceEventListener, private: void initLocalAttributes(); + unsigned char updateAdvertismentData(); private: BLEDevice* _device; @@ -158,6 +159,9 @@ class BLEPeripheral : public BLEDeviceEventListener, BLECentral _central; BLEPeripheralEventHandler _eventHandlers[4]; + + BLEEirData advertisementData[3]; + BLEEirData scanData; }; #endif From cd05402ecb2d632c11404d5ad806faddea7d5539 Mon Sep 17 00:00:00 2001 From: Florian Echtler Date: Mon, 23 Oct 2017 13:05:09 +0200 Subject: [PATCH 2/3] refactor advertisements, part 2 --- src/nRF51822.cpp | 85 ++++++++++++++++++++++++++---------------------- src/nRF51822.h | 5 +++ 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/nRF51822.cpp b/src/nRF51822.cpp index 420047c..6cae472 100644 --- a/src/nRF51822.cpp +++ b/src/nRF51822.cpp @@ -81,6 +81,52 @@ nRF51822::~nRF51822() { this->end(); } +void nRF51822::updateAdvertisementData(unsigned char advertisementDataSize, + BLEEirData *advertisementData, + unsigned char scanDataSize, + BLEEirData *scanData) +{ + unsigned char srData[31]; + unsigned char srDataLen = 0; + + this->_advDataLen = 0; + + // flags + this->_advData[this->_advDataLen + 0] = 2; + this->_advData[this->_advDataLen + 1] = 0x01; + this->_advData[this->_advDataLen + 2] = 0x06; + + this->_advDataLen += 3; + + if (advertisementDataSize && advertisementData) { + for (int i = 0; i < advertisementDataSize; i++) { + this->_advData[this->_advDataLen + 0] = advertisementData[i].length + 1; + this->_advData[this->_advDataLen + 1] = advertisementData[i].type; + this->_advDataLen += 2; + + memcpy(&this->_advData[this->_advDataLen], advertisementData[i].data, advertisementData[i].length); + + this->_advDataLen += advertisementData[i].length; + } + } + + if (scanDataSize && scanData) { + for (int i = 0; i < scanDataSize; i++) { + srData[srDataLen + 0] = scanData[i].length + 1; + srData[srDataLen + 1] = scanData[i].type; + srDataLen += 2; + + memcpy(&srData[srDataLen], scanData[i].data, scanData[i].length); + + srDataLen += scanData[i].length; + _hasScanData = true; + } + } + + sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen); +} + + void nRF51822::begin(unsigned char advertisementDataSize, BLEEirData *advertisementData, unsigned char scanDataSize, @@ -187,44 +233,7 @@ void nRF51822::begin(unsigned char advertisementDataSize, sd_ble_gap_ppcp_set(&gap_conn_params); sd_ble_gap_tx_power_set(0); - unsigned char srData[31]; - unsigned char srDataLen = 0; - - this->_advDataLen = 0; - - // flags - this->_advData[this->_advDataLen + 0] = 2; - this->_advData[this->_advDataLen + 1] = 0x01; - this->_advData[this->_advDataLen + 2] = 0x06; - - this->_advDataLen += 3; - - if (advertisementDataSize && advertisementData) { - for (int i = 0; i < advertisementDataSize; i++) { - this->_advData[this->_advDataLen + 0] = advertisementData[i].length + 1; - this->_advData[this->_advDataLen + 1] = advertisementData[i].type; - this->_advDataLen += 2; - - memcpy(&this->_advData[this->_advDataLen], advertisementData[i].data, advertisementData[i].length); - - this->_advDataLen += advertisementData[i].length; - } - } - - if (scanDataSize && scanData) { - for (int i = 0; i < scanDataSize; i++) { - srData[srDataLen + 0] = scanData[i].length + 1; - srData[srDataLen + 1] = scanData[i].type; - srDataLen += 2; - - memcpy(&srData[srDataLen], scanData[i].data, scanData[i].length); - - srDataLen += scanData[i].length; - _hasScanData = true; - } - } - - sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen); + updateAdvertisementData(advertisementDataSize, advertisementData, scanDataSize, scanData); sd_ble_gap_appearance_set(0); for (int i = 0; i < numLocalAttributes; i++) { diff --git a/src/nRF51822.h b/src/nRF51822.h index 3c66036..2982039 100644 --- a/src/nRF51822.h +++ b/src/nRF51822.h @@ -61,6 +61,11 @@ class nRF51822 : public BLEDevice BLERemoteAttribute** remoteAttributes, unsigned char numRemoteAttributes); + virtual void updateAdvertisementData(unsigned char advertisementDataSize, + BLEEirData *advertisementData, + unsigned char scanDataSize, + BLEEirData *scanData); + virtual void poll(); virtual void end(); From 6e7cb8a9fbb0276e2991305764992a2e7317a281 Mon Sep 17 00:00:00 2001 From: Florian Echtler Date: Mon, 23 Oct 2017 13:11:06 +0200 Subject: [PATCH 3/3] update advertising data when startAdvertising is called --- src/BLEDevice.h | 5 +++++ src/BLEPeripheral.cpp | 8 ++++++++ src/BLEPeripheral.h | 1 + 3 files changed, 14 insertions(+) diff --git a/src/BLEDevice.h b/src/BLEDevice.h index fb38781..3d7ea56 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -65,6 +65,11 @@ class BLEDevice BLERemoteAttribute** /*remoteAttributes*/, unsigned char /*numRemoteAttributes*/) { } + virtual void updateAdvertisementData(unsigned char /*advertisementDataSize*/, + BLEEirData * /*advertisementData*/, + unsigned char /*scanDataSize*/, + BLEEirData * /*scanData*/) { } + virtual void poll() { } virtual void end() { } diff --git a/src/BLEPeripheral.cpp b/src/BLEPeripheral.cpp index cf2952f..10adcaf 100644 --- a/src/BLEPeripheral.cpp +++ b/src/BLEPeripheral.cpp @@ -206,6 +206,14 @@ void BLEPeripheral::setBondStore(BLEBondStore& bondStore) { this->_device->setBondStore(bondStore); } +void BLEPeripheral::startAdvertising() { + int advertisementDataSize = updateAdvertismentData(); + this->_device->updateAdvertisementData( + advertisementDataSize, advertisementData, + scanData.length > 0 ? 1 : 0, &scanData); + this->_device->startAdvertising(); +} + void BLEPeripheral::setDeviceName(const char* deviceName) { this->_deviceNameCharacteristic.setValue(deviceName); } diff --git a/src/BLEPeripheral.h b/src/BLEPeripheral.h index 10d4b21..8054aab 100644 --- a/src/BLEPeripheral.h +++ b/src/BLEPeripheral.h @@ -80,6 +80,7 @@ class BLEPeripheral : public BLEDeviceEventListener, void setConnectable(bool connectable); void setBondStore(BLEBondStore& bondStore); + void startAdvertising(); void setDeviceName(const char* deviceName); void setAppearance(unsigned short appearance);