Skip to content

Add static random address support for all ST BLE chips #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/Central/LedControl/LedControl.ino
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void setup() {
while (!Serial);

// configure the button pin as input
pinMode(buttonPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);

// initialize the BLE hardware
BLE.begin();
Expand Down
2 changes: 1 addition & 1 deletion examples/Peripheral/ButtonLED/ButtonLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void setup() {
while (!Serial);

pinMode(ledPin, OUTPUT); // use the LED as an output
pinMode(buttonPin, INPUT); // use button pin as an input
pinMode(buttonPin, INPUT_PULLUP); // use button pin as an input

// begin initialization
if (!BLE.begin()) {
Expand Down
8 changes: 6 additions & 2 deletions src/local/BLELocalDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include "utility/L2CAPSignaling.h"
#include "BLELocalDevice.h"

BLELocalDevice::BLELocalDevice(HCITransportInterface *HCITransport) :
_HCITransport(HCITransport)
BLELocalDevice::BLELocalDevice(HCITransportInterface *HCITransport, uint8_t ownBdaddrType) :
_HCITransport(HCITransport), _ownBdaddrType(ownBdaddrType)
{
_advertisingData.setFlags(BLEFlagsGeneralDiscoverable | BLEFlagsBREDRNotSupported);
}
Expand Down Expand Up @@ -76,6 +76,10 @@ int BLELocalDevice::begin()

GATT.begin();

GAP.setOwnBdaddrType(_ownBdaddrType);

ATT.setOwnBdaddrType(_ownBdaddrType);

return 1;
}

Expand Down
8 changes: 7 additions & 1 deletion src/local/BLELocalDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@
#include "BLEService.h"
#include "BLEAdvertisingData.h"

#define PUBLIC_ADDR (0)
#define STATIC_RANDOM_ADDR (1)
#define RESOLVABLE_PRIVATE_ADDR (2)
#define NON_RESOLVABLE_PRIVATE_ADDR (3)

class BLELocalDevice {
public:
BLELocalDevice(HCITransportInterface *HCITransport);
BLELocalDevice(HCITransportInterface *HCITransport, uint8_t ownBdaddrType = STATIC_RANDOM_ADDR);
virtual ~BLELocalDevice();

virtual int begin();
Expand Down Expand Up @@ -90,6 +95,7 @@ class BLELocalDevice {
HCITransportInterface *_HCITransport;
BLEAdvertisingData _advertisingData;
BLEAdvertisingData _scanResponseData;
uint8_t _ownBdaddrType;
};

extern BLELocalDevice& BLE;
Expand Down
7 changes: 6 additions & 1 deletion src/utility/ATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ ATTClass::~ATTClass()

bool ATTClass::connect(uint8_t peerBdaddrType, uint8_t peerBdaddr[6])
{
if (HCI.leCreateConn(0x0060, 0x0030, 0x00, peerBdaddrType, peerBdaddr, 0x00,
if (HCI.leCreateConn(0x0060, 0x0030, 0x00, peerBdaddrType, peerBdaddr, _ownBdaddrType,
0x0006, 0x000c, 0x0000, 0x00c8, 0x0004, 0x0006) != 0) {
return false;
}
Expand Down Expand Up @@ -1688,6 +1688,11 @@ void ATTClass::writeCmd(uint16_t connectionHandle, uint16_t handle, const uint8_
sendReq(connectionHandle, &writeReq, 3 + dataLen, NULL);
}

void ATTClass::setOwnBdaddrType(uint8_t ownBdaddrType)
{
_ownBdaddrType = ownBdaddrType;
}

#if !defined(FAKE_ATT)
ATTClass ATTObj;
ATTClass& ATT = ATTObj;
Expand Down
4 changes: 4 additions & 0 deletions src/utility/ATT.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class ATTClass {
virtual int writeReq(uint16_t connectionHandle, uint16_t handle, const uint8_t* data, uint8_t dataLen, uint8_t responseBuffer[]);
virtual void writeCmd(uint16_t connectionHandle, uint16_t handle, const uint8_t* data, uint8_t dataLen);

void setOwnBdaddrType(uint8_t ownBdaddrType);

private:
virtual void error(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]);
virtual void mtuReq(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]);
Expand Down Expand Up @@ -135,6 +137,8 @@ class ATTClass {
} _pendingResp;

BLEDeviceEventHandler _eventHandlers[2];

uint8_t _ownBdaddrType;
};

extern ATTClass& ATT;
Expand Down
11 changes: 8 additions & 3 deletions src/utility/GAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int GAPClass::advertise(uint8_t* advData, uint8_t advDataLen, uint8_t* scanData,

stopAdvertise();

if (HCI.leSetAdvertisingParameters(_advertisingInterval, _advertisingInterval, type, 0x00, 0x00, directBdaddr, 0x07, 0) != 0) {
if (HCI.leSetAdvertisingParameters(_advertisingInterval, _advertisingInterval, type, _ownBdaddrType, 0x00, directBdaddr, 0x07, 0) != 0) {
return 0;
}

Expand Down Expand Up @@ -91,8 +91,8 @@ int GAPClass::scan(bool withDuplicates)
}
}

// active scan, 10 ms scan interval (N * 0.625), 10 ms scan window (N * 0.625), public own address type, no filter
if (HCI.leSetScanParameters(0x01, 0x0010, 0x0010, 0x00, 0x00) != 0) {
// active scan, 10 ms scan interval (N * 0.625), 10 ms scan window (N * 0.625), public or static random own address type, no filter
if (HCI.leSetScanParameters(0x01, 0x0010, 0x0010, _ownBdaddrType, 0x00) != 0) {
return false;
}

Expand Down Expand Up @@ -270,6 +270,11 @@ bool GAPClass::matchesScanFilter(const BLEDevice& device)
return true;
}

void GAPClass::setOwnBdaddrType(uint8_t ownBdaddrType)
{
_ownBdaddrType = ownBdaddrType;
}

#if !defined(FAKE_GAP)
GAPClass GAPObj;
GAPClass& GAP = GAPObj;
Expand Down
3 changes: 3 additions & 0 deletions src/utility/GAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class GAPClass {

virtual void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler);

void setOwnBdaddrType(uint8_t ownBdaddrType);

protected:
friend class HCIClass;

Expand All @@ -67,6 +69,7 @@ class GAPClass {
String _scanNameFilter;
String _scanUuidFilter;
String _scanAddressFilter;
uint8_t _ownBdaddrType;
};

extern GAPClass& GAP;
Expand Down
Loading