From 348a1a0f9cec15ee6359c5b2a845f9cd9387c8f8 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Tue, 11 Aug 2020 12:18:33 +0200 Subject: [PATCH 1/6] [WIP] Implement and test multiple concurrent connections --- src/local/BLELocalDevice.cpp | 35 ++++++++++++++++++++ src/local/BLELocalDevice.h | 8 ++++- src/utility/ATT.cpp | 63 ++++++++++++++++++++++++++++++++++-- src/utility/ATT.h | 5 +++ 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/local/BLELocalDevice.cpp b/src/local/BLELocalDevice.cpp index 4dd96399..434f1c96 100644 --- a/src/local/BLELocalDevice.cpp +++ b/src/local/BLELocalDevice.cpp @@ -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(); diff --git a/src/local/BLELocalDevice.h b/src/local/BLELocalDevice.h index 03bd12b5..4b0ea26b 100644 --- a/src/local/BLELocalDevice.h +++ b/src/local/BLELocalDevice.h @@ -67,7 +67,13 @@ class BLELocalDevice { virtual void stopScan(); virtual BLEDevice central(); - virtual BLEDevice available(); + virtual BLEDevice available(); + int centralCount(); + BLEDevice peripheral(); + BLEDevice peripheral(int index); + int peripheralCount(); + BLEDevice available(); + virtual void setAdvertisingInterval(uint16_t advertisingInterval); virtual void setConnectionInterval(uint16_t minimumConnectionInterval, uint16_t maximumConnectionInterval); diff --git a/src/utility/ATT.cpp b/src/utility/ATT.cpp index 28c8d743..936215a0 100644 --- a/src/utility/ATT.cpp +++ b/src/utility/ATT.cpp @@ -497,19 +497,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; diff --git a/src/utility/ATT.h b/src/utility/ATT.h index ea71d592..eda24a56 100644 --- a/src/utility/ATT.h +++ b/src/utility/ATT.h @@ -67,6 +67,11 @@ class ATTClass { virtual bool disconnect(); virtual BLEDevice central(); + BLEDevice central(int index); + int centralCount(); + BLEDevice peripheral(); + BLEDevice peripheral(int index); + 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); From 41b8784bb8d3b070773aa565a945d9a2ed93be1d Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Mon, 17 Aug 2020 16:17:21 +0200 Subject: [PATCH 2/6] [WIP] Add comments and print for revisions --- src/local/BLELocalCharacteristic.cpp | 3 +++ src/utility/HCI.cpp | 4 ++++ src/utility/HCICordioTransport.cpp | 2 ++ 3 files changed, 9 insertions(+) diff --git a/src/local/BLELocalCharacteristic.cpp b/src/local/BLELocalCharacteristic.cpp index 333d00b2..8717065d 100644 --- a/src/local/BLELocalCharacteristic.cpp +++ b/src/local/BLELocalCharacteristic.cpp @@ -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(); } } diff --git a/src/utility/HCI.cpp b/src/utility/HCI.cpp index 233dd1a5..f2fd24f3 100644 --- a/src/utility/HCI.cpp +++ b/src/utility/HCI.cpp @@ -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, diff --git a/src/utility/HCICordioTransport.cpp b/src/utility/HCICordioTransport.cpp index ab92818b..cd68ed01 100644 --- a/src/utility/HCICordioTransport.cpp +++ b/src/utility/HCICordioTransport.cpp @@ -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; } From eab7ee236b31f261b4c3d7dc5de2fe7a5cc76214 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Thu, 27 Aug 2020 10:33:37 +0200 Subject: [PATCH 3/6] [WIP] Change supervision timeout - for nano33ble connection error reason --- src/utility/ATT.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utility/ATT.cpp b/src/utility/ATT.cpp index 936215a0..eaa58d35 100644 --- a/src/utility/ATT.cpp +++ b/src/utility/ATT.cpp @@ -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) { + 0x0006, 0x000c, 0x0000, 1000, 0x0004, 0x0006) != 0) { return false; } From 9dc54bbd979abf431d546140896c7bfa27bae429 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Thu, 27 Aug 2020 09:19:06 +0200 Subject: [PATCH 4/6] [WIP] Fix inconsistent stop scan - to be revisioned --- src/utility/GAP.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/utility/GAP.cpp b/src/utility/GAP.cpp index f3ee32fa..f5335a2b 100644 --- a/src/utility/GAP.cpp +++ b/src/utility/GAP.cpp @@ -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) { @@ -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; From eac6e12a37a82169641bc62d4352997c4c2ed85d Mon Sep 17 00:00:00 2001 From: giulcioffi Date: Wed, 28 Oct 2020 17:45:23 +0100 Subject: [PATCH 5/6] Increase min and max time intervals to allow the connections up to 5 BLE peripherals --- src/utility/ATT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/ATT.cpp b/src/utility/ATT.cpp index eaa58d35..26898988 100644 --- a/src/utility/ATT.cpp +++ b/src/utility/ATT.cpp @@ -111,7 +111,7 @@ 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, 1000, 0x0004, 0x0006) != 0) { + 0x001c, 0x0020, 0x0000, 1000, 0x0004, 0x0006) != 0) { return false; } From 896f79d7b741c9ea842da4cb72aeae9c9cbbda2a Mon Sep 17 00:00:00 2001 From: giulcioffi Date: Mon, 2 Nov 2020 10:20:12 +0100 Subject: [PATCH 6/6] Declare new functions for multi-connection as virtual --- src/local/BLELocalDevice.h | 13 ++++++------- src/utility/ATT.h | 10 +++++----- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/local/BLELocalDevice.h b/src/local/BLELocalDevice.h index 4b0ea26b..cabe0e7c 100644 --- a/src/local/BLELocalDevice.h +++ b/src/local/BLELocalDevice.h @@ -67,13 +67,12 @@ class BLELocalDevice { virtual void stopScan(); virtual BLEDevice central(); - virtual BLEDevice available(); - int centralCount(); - BLEDevice peripheral(); - BLEDevice peripheral(int index); - int peripheralCount(); - BLEDevice available(); - + 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); virtual void setConnectionInterval(uint16_t minimumConnectionInterval, uint16_t maximumConnectionInterval); diff --git a/src/utility/ATT.h b/src/utility/ATT.h index eda24a56..a8eb0162 100644 --- a/src/utility/ATT.h +++ b/src/utility/ATT.h @@ -67,11 +67,11 @@ class ATTClass { virtual bool disconnect(); virtual BLEDevice central(); - BLEDevice central(int index); - int centralCount(); - BLEDevice peripheral(); - BLEDevice peripheral(int index); - int peripheralCount(); + 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);