diff --git a/src/BLEDevice.h b/src/BLEDevice.h index dcfa1cf..fb38781 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -11,6 +11,13 @@ #include "BLERemoteCharacteristic.h" #include "BLERemoteService.h" +struct BLEEirData +{ + unsigned char length; + unsigned char type; + unsigned char data[BLE_EIR_DATA_MAX_VALUE_LENGTH]; +}; + class BLEDevice; class BLEDeviceEventListener @@ -49,12 +56,10 @@ class BLEDevice void setConnectable(bool connectable); void setBondStore(BLEBondStore& bondStore); - virtual void begin(unsigned char /*advertisementDataType*/, - unsigned char /*advertisementDataLength*/, - const unsigned char* /*advertisementData*/, - unsigned char /*scanDataType*/, - unsigned char /*scanDataLength*/, - const unsigned char* /*scanData*/, + virtual void begin(unsigned char /*advertisementDataSize*/, + BLEEirData * /*advertisementData*/, + unsigned char /*scanDataSize*/, + BLEEirData * /*scanData*/, BLELocalAttribute** /*localAttributes*/, unsigned char /*numLocalAttributes*/, BLERemoteAttribute** /*remoteAttributes*/, diff --git a/src/BLEDeviceLimits.h b/src/BLEDeviceLimits.h index ecad714..5cd7563 100644 --- a/src/BLEDeviceLimits.h +++ b/src/BLEDeviceLimits.h @@ -22,6 +22,7 @@ #define BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH 26 #define BLE_SCAN_DATA_MAX_VALUE_LENGTH 29 +#define BLE_EIR_DATA_MAX_VALUE_LENGTH 29 #define BLE_ATTRIBUTE_MAX_VALUE_LENGTH 20 #define BLE_REMOTE_ATTRIBUTE_MAX_VALUE_LENGTH 22 @@ -29,6 +30,7 @@ #define BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH 20 #define BLE_SCAN_DATA_MAX_VALUE_LENGTH 20 +#define BLE_EIR_DATA_MAX_VALUE_LENGTH 20 #define BLE_ATTRIBUTE_MAX_VALUE_LENGTH 20 #define BLE_REMOTE_ATTRIBUTE_MAX_VALUE_LENGTH 22 diff --git a/src/BLEPeripheral.cpp b/src/BLEPeripheral.cpp index 947a039..f9440da 100644 --- a/src/BLEPeripheral.cpp +++ b/src/BLEPeripheral.cpp @@ -69,52 +69,64 @@ BLEPeripheral::~BLEPeripheral() { } void BLEPeripheral::begin() { - unsigned char advertisementDataType = 0; - unsigned char scanDataType = 0; + unsigned char advertisementDataSize = 0; - unsigned char advertisementDataLength = 0; - unsigned char scanDataLength = 0; - - unsigned char advertisementData[BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH]; - unsigned char scanData[BLE_SCAN_DATA_MAX_VALUE_LENGTH]; + BLEEirData advertisementData[3]; + BLEEirData scanData = { 0 }; + unsigned char remainingAdvertisementDataLength = BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH + 2; if (this->_serviceSolicitationUuid){ BLEUuid serviceSolicitationUuid = BLEUuid(this->_serviceSolicitationUuid); - advertisementDataLength = serviceSolicitationUuid.length(); - advertisementDataType = (advertisementDataLength > 2) ? 0x15 : 0x14; + unsigned char uuidLength = serviceSolicitationUuid.length(); + advertisementData[advertisementDataSize].length = uuidLength; + advertisementData[advertisementDataSize].type = (uuidLength > 2) ? 0x15 : 0x14; - memcpy(advertisementData, serviceSolicitationUuid.data(), advertisementDataLength); - } else if (this->_advertisedServiceUuid){ + memcpy(advertisementData[advertisementDataSize].data, serviceSolicitationUuid.data(), uuidLength); + advertisementDataSize += 1; + remainingAdvertisementDataLength -= uuidLength + 2; + } + if (this->_advertisedServiceUuid){ BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid); - advertisementDataLength = advertisedServiceUuid.length(); - advertisementDataType = (advertisementDataLength > 2) ? 0x06 : 0x02; - - memcpy(advertisementData, advertisedServiceUuid.data(), advertisementDataLength); - } else if (this->_manufacturerData && this->_manufacturerDataLength > 0) { - advertisementDataLength = this->_manufacturerDataLength; + unsigned char uuidLength = advertisedServiceUuid.length(); + if (uuidLength + 2 <= remainingAdvertisementDataLength) { + advertisementData[advertisementDataSize].length = uuidLength; + advertisementData[advertisementDataSize].type = (uuidLength > 2) ? 0x06 : 0x02; - if (advertisementDataLength > sizeof(advertisementData)) { - advertisementDataLength = sizeof(advertisementData); + memcpy(advertisementData[advertisementDataSize].data, advertisedServiceUuid.data(), uuidLength); + advertisementDataSize += 1; + remainingAdvertisementDataLength -= uuidLength + 2; } + } + if (this->_manufacturerData && this->_manufacturerDataLength > 0) { + if (remainingAdvertisementDataLength >= 3) { + unsigned char dataLength = this->_manufacturerDataLength; + + if (dataLength + 2 > remainingAdvertisementDataLength) { + dataLength = remainingAdvertisementDataLength - 2; + } - advertisementDataType = 0xff; + advertisementData[advertisementDataSize].length = dataLength; + advertisementData[advertisementDataSize].type = 0xff; - memcpy(advertisementData, this->_manufacturerData, advertisementDataLength); + memcpy(advertisementData[advertisementDataSize].data, this->_manufacturerData, dataLength); + advertisementDataSize += 1; + remainingAdvertisementDataLength -= dataLength + 2; + } } if (this->_localName){ unsigned char localNameLength = strlen(this->_localName); - scanDataLength = localNameLength; + scanData.length = localNameLength; - if (scanDataLength > sizeof(scanData)) { - scanDataLength = sizeof(scanData); + if (scanData.length > BLE_SCAN_DATA_MAX_VALUE_LENGTH) { + scanData.length = BLE_SCAN_DATA_MAX_VALUE_LENGTH; } - scanDataType = (localNameLength > scanDataLength) ? 0x08 : 0x09; + scanData.type = (localNameLength > scanData.length) ? 0x08 : 0x09; - memcpy(scanData, this->_localName, scanDataLength); + memcpy(scanData.data, this->_localName, scanData.length); } if (this->_localAttributes == NULL) { @@ -144,8 +156,8 @@ void BLEPeripheral::begin() { this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic); } - this->_device->begin(advertisementDataType, advertisementDataLength, advertisementData, - scanDataType, scanDataLength, scanData, + this->_device->begin(advertisementDataSize, advertisementData, + scanData.length > 0 ? 1 : 0, &scanData, this->_localAttributes, this->_numLocalAttributes, this->_remoteAttributes, this->_numRemoteAttributes); diff --git a/src/nRF51822.cpp b/src/nRF51822.cpp index fce0a8f..93c57b1 100644 --- a/src/nRF51822.cpp +++ b/src/nRF51822.cpp @@ -76,12 +76,10 @@ nRF51822::~nRF51822() { this->end(); } -void nRF51822::begin(unsigned char advertisementDataType, - unsigned char advertisementDataLength, - const unsigned char* advertisementData, - unsigned char scanDataType, - unsigned char scanDataLength, - const unsigned char* scanData, +void nRF51822::begin(unsigned char advertisementDataSize, + BLEEirData *advertisementData, + unsigned char scanDataSize, + BLEEirData *scanData, BLELocalAttribute** localAttributes, unsigned char numLocalAttributes, BLERemoteAttribute** remoteAttributes, @@ -196,22 +194,28 @@ void nRF51822::begin(unsigned char advertisementDataType, this->_advDataLen += 3; - if (advertisementDataType && advertisementDataLength && advertisementData) { - this->_advData[this->_advDataLen + 0] = advertisementDataLength + 1; - this->_advData[this->_advDataLen + 1] = advertisementDataType; - this->_advDataLen += 2; + 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, advertisementDataLength); + memcpy(&this->_advData[this->_advDataLen], advertisementData[i].data, advertisementData[i].length); - this->_advDataLen += advertisementDataLength; + this->_advDataLen += advertisementData[i].length; + } } - if (scanDataType && scanDataLength && scanData) { - srData[0] = scanDataLength + 1; - srData[1] = scanDataType; - memcpy(&srData[2], scanData, scanDataLength); + 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 = 2 + scanDataLength; + srDataLen += scanData[i].length; + } } sd_ble_gap_adv_data_set(this->_advData, this->_advDataLen, srData, srDataLen); diff --git a/src/nRF51822.h b/src/nRF51822.h index 5d220a1..f3b09dc 100644 --- a/src/nRF51822.h +++ b/src/nRF51822.h @@ -52,12 +52,10 @@ class nRF51822 : public BLEDevice virtual ~nRF51822(); - virtual void begin(unsigned char advertisementDataType, - unsigned char advertisementDataLength, - const unsigned char* advertisementData, - unsigned char scanDataType, - unsigned char scanDataLength, - const unsigned char* scanData, + virtual void begin(unsigned char advertisementDataSize, + BLEEirData *advertisementData, + unsigned char scanDataSize, + BLEEirData *scanData, BLELocalAttribute** localAttributes, unsigned char numLocalAttributes, BLERemoteAttribute** remoteAttributes, diff --git a/src/nRF8001.cpp b/src/nRF8001.cpp index e44b932..a82d7d2 100644 --- a/src/nRF8001.cpp +++ b/src/nRF8001.cpp @@ -143,12 +143,10 @@ nRF8001::~nRF8001() { this->end(); } -void nRF8001::begin(unsigned char advertisementDataType, - unsigned char advertisementDataLength, - const unsigned char* advertisementData, - unsigned char scanDataType, - unsigned char scanDataLength, - const unsigned char* scanData, +void nRF8001::begin(unsigned char advertisementDataSize, + BLEEirData *advertisementData, + unsigned char scanDataSize, + BLEEirData *scanData, BLELocalAttribute** localAttributes, unsigned char numLocalAttributes, BLERemoteAttribute** remoteAttributes, @@ -261,8 +259,8 @@ void nRF8001::begin(unsigned char advertisementDataType, setupMsg.status_byte = 0; - bool hasAdvertisementData = advertisementDataType && advertisementDataLength && advertisementData; - bool hasScanData = scanDataType && scanDataLength && scanData; + bool hasAdvertisementData = advertisementDataSize && advertisementData; + bool hasScanData = scanDataSize && scanData; for (int i = 0; i < NB_BASE_SETUP_MESSAGES; i++) { int setupMsgSize = pgm_read_byte_near(&baseSetupMsgs[i].buffer[0]) + 2; @@ -305,13 +303,13 @@ void nRF8001::begin(unsigned char advertisementDataType, setupMsgData->data[0] |= 0x01; } } else if (i == 5 && hasAdvertisementData) { - setupMsgData->data[0] = advertisementDataType; - setupMsgData->data[1] = advertisementDataLength; - memcpy(&setupMsgData->data[2], advertisementData, advertisementDataLength); + setupMsgData->data[0] = advertisementData[0].type; + setupMsgData->data[1] = advertisementData[0].length; + memcpy(&setupMsgData->data[2], advertisementData[0].data, advertisementData[0].length); } else if (i == 6 && hasScanData) { - setupMsgData->data[0] = scanDataType; - setupMsgData->data[1] = scanDataLength; - memcpy(&setupMsgData->data[2], scanData, scanDataLength); + setupMsgData->data[0] = scanData[0].type; + setupMsgData->data[1] = scanData[0].length; + memcpy(&setupMsgData->data[2], scanData[0].data, scanData[0].length); } this->sendSetupMessage(&setupMsg); diff --git a/src/nRF8001.h b/src/nRF8001.h index 0cbf755..9db02fe 100644 --- a/src/nRF8001.h +++ b/src/nRF8001.h @@ -46,12 +46,10 @@ class nRF8001 : protected BLEDevice virtual ~nRF8001(); - virtual void begin(unsigned char advertisementDataType, - unsigned char advertisementDataLength, - const unsigned char* advertisementData, - unsigned char scanDataType, - unsigned char scanDataLength, - const unsigned char* scanData, + virtual void begin(unsigned char advertisementDataSize, + BLEEirData *advertisementData, + unsigned char scanDataSize, + BLEEirData *scanData, BLELocalAttribute** localAttributes, unsigned char numLocalAttributes, BLERemoteAttribute** remoteAttributes,