Skip to content
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

Implement functions to retrieve multiple connected devices #105

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/local/BLELocalCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ int BLELocalCharacteristic::writeValue(const uint8_t value[], int length)

BLE.setAdvertisedServiceData(serviceUuid, value, length);

// TO BE REVISIONED
// could advertise also if connected
if (!ATT.connected() && GAP.advertising()) {
// if it is already advertising it should stop before requesting advertising again
BLE.advertise();
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/local/BLELocalDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,41 @@ BLEDevice BLELocalDevice::central()
return ATT.central();
}

BLEDevice BLELocalDevice::central(int index)
{
HCI.poll();

return ATT.central(index);
}

int BLELocalDevice::centralCount()
{
HCI.poll();

return ATT.centralCount();
}

BLEDevice BLELocalDevice::peripheral()
{
HCI.poll();

return ATT.peripheral();
}

BLEDevice BLELocalDevice::peripheral(int index)
{
HCI.poll();

return ATT.peripheral(index);
}

int BLELocalDevice::peripheralCount()
{
HCI.poll();

return ATT.peripheralCount();
}

BLEDevice BLELocalDevice::available()
{
HCI.poll();
Expand Down
5 changes: 5 additions & 0 deletions src/local/BLELocalDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class BLELocalDevice {
virtual void stopScan();

virtual BLEDevice central();
virtual BLEDevice central(int index);
virtual int centralCount();
virtual BLEDevice peripheral();
virtual BLEDevice peripheral(int index);
virtual int peripheralCount();
virtual BLEDevice available();

virtual void setAdvertisingInterval(uint16_t advertisingInterval);
Expand Down
66 changes: 63 additions & 3 deletions src/utility/ATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ ATTClass::~ATTClass()

bool ATTClass::connect(uint8_t peerBdaddrType, uint8_t peerBdaddr[6])
{
// original supervision timeout "0x00c8" seems to be too short for Nano 33 BLE (2 seconds)
if (HCI.leCreateConn(0x0060, 0x0030, 0x00, peerBdaddrType, peerBdaddr, 0x00,
0x0006, 0x000c, 0x0000, 0x00c8, 0x0004, 0x0006) != 0) {
0x001c, 0x0020, 0x0000, 1000, 0x0004, 0x0006) != 0) {
return false;
}

Expand Down Expand Up @@ -497,19 +498,78 @@ bool ATTClass::disconnect()
return (numDisconnects > 0);
}

BLEDevice ATTClass::central()
BLEDevice ATTClass::central()
{
return central(0);
}

BLEDevice ATTClass::central(int index)
{
int currentIndex = 0;
for (int i = 0; i < ATT_MAX_PEERS; i++) {
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x01) {
continue;
}

return BLEDevice(_peers[i].addressType, _peers[i].address);
if (currentIndex == index) {
return BLEDevice(_peers[i].addressType, _peers[i].address);
}
currentIndex++;
}

return BLEDevice();
}

int ATTClass::centralCount()
{
int count = 0;
for (int i = 0; i < ATT_MAX_PEERS; i++) {
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x01) {
continue;
}

count++;
}

return count;
}

BLEDevice ATTClass::peripheral()
{
return peripheral(0);
}

BLEDevice ATTClass::peripheral(int index)
{
int currentIndex = 0;
for (int i = 0; i < ATT_MAX_PEERS; i++) {
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x00) {
continue;
}

if (currentIndex == index) {
return BLEDevice(_peers[i].addressType, _peers[i].address);
}
currentIndex++;
}

return BLEDevice();
}

int ATTClass::peripheralCount()
{
int count = 0;
for (int i = 0; i < ATT_MAX_PEERS; i++) {
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x00) {
continue;
}

count++;
}

return count;
}

bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
{
int numNotifications = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/utility/ATT.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class ATTClass {
virtual bool disconnect();

virtual BLEDevice central();
virtual BLEDevice central(int index);
virtual int centralCount();
virtual BLEDevice peripheral();
virtual BLEDevice peripheral(int index);
virtual int peripheralCount();

virtual bool handleNotify(uint16_t handle, const uint8_t* value, int length);
virtual bool handleInd(uint16_t handle, const uint8_t* value, int length);
Expand Down
6 changes: 4 additions & 2 deletions src/utility/GAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ void GAPClass::stopAdvertise()

int GAPClass::scan(bool withDuplicates)
{
HCI.leSetScanEnable(false, true);
//HCI.leSetScanEnable(false, true);
stopScan();

// 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) {
Expand Down Expand Up @@ -129,7 +130,8 @@ int GAPClass::scanForAddress(String address, bool withDuplicates)

void GAPClass::stopScan()
{
HCI.leSetScanEnable(false, false);
//HCI.leSetScanEnable(false, false);
HCI.leSetScanEnable(false, true);

_scanning = false;

Expand Down
4 changes: 4 additions & 0 deletions src/utility/HCI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[])
uint16_t supervisionTimeout;
uint8_t masterClockAccuracy;
} *leConnectionComplete = (EvtLeConnectionComplete*)&pdata[sizeof(HCIEventHdr) + sizeof(LeMetaEventHeader)];

// CLIENT: 0x01 / PERIPHERAL: 0x00
Serial.println("role:");
Serial.println(leConnectionComplete->role);

if (leConnectionComplete->status == 0x00) {
ATT.addConnection(leConnectionComplete->handle,
Expand Down
2 changes: 2 additions & 0 deletions src/utility/HCICordioTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ size_t HCICordioTransportClass::write(const uint8_t* data, size_t length)
void HCICordioTransportClass::handleRxData(uint8_t* data, uint8_t len)
{
if (_rxBuf.availableForStore() < len) {
// This drop can cause many problems
Serial.println("DROP");
// drop!
return;
}
Expand Down