Skip to content

Commit ce01ae4

Browse files
committed
Merge branch 'pr-105'
2 parents d785b11 + d973569 commit ce01ae4

File tree

8 files changed

+121
-5
lines changed

8 files changed

+121
-5
lines changed

src/local/BLELocalCharacteristic.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ int BLELocalCharacteristic::writeValue(const uint8_t value[], int length)
124124

125125
BLE.setAdvertisedServiceData(serviceUuid, value, length);
126126

127+
// TO BE REVISIONED
128+
// could advertise also if connected
127129
if (!ATT.connected() && GAP.advertising()) {
130+
// if it is already advertising it should stop before requesting advertising again
128131
BLE.advertise();
129132
}
130133
}

src/local/BLELocalDevice.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,41 @@ BLEDevice BLELocalDevice::central()
297297
return ATT.central();
298298
}
299299

300+
BLEDevice BLELocalDevice::central(int index)
301+
{
302+
HCI.poll();
303+
304+
return ATT.central(index);
305+
}
306+
307+
int BLELocalDevice::centralCount()
308+
{
309+
HCI.poll();
310+
311+
return ATT.centralCount();
312+
}
313+
314+
BLEDevice BLELocalDevice::peripheral()
315+
{
316+
HCI.poll();
317+
318+
return ATT.peripheral();
319+
}
320+
321+
BLEDevice BLELocalDevice::peripheral(int index)
322+
{
323+
HCI.poll();
324+
325+
return ATT.peripheral(index);
326+
}
327+
328+
int BLELocalDevice::peripheralCount()
329+
{
330+
HCI.poll();
331+
332+
return ATT.peripheralCount();
333+
}
334+
300335
BLEDevice BLELocalDevice::available()
301336
{
302337
HCI.poll();

src/local/BLELocalDevice.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class BLELocalDevice {
6767
virtual void stopScan();
6868

6969
virtual BLEDevice central();
70+
virtual BLEDevice central(int index);
71+
virtual int centralCount();
72+
virtual BLEDevice peripheral();
73+
virtual BLEDevice peripheral(int index);
74+
virtual int peripheralCount();
7075
virtual BLEDevice available();
7176

7277
virtual void setAdvertisingInterval(uint16_t advertisingInterval);

src/utility/ATT.cpp

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ ATTClass::~ATTClass()
109109

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

@@ -503,19 +504,78 @@ bool ATTClass::disconnect()
503504
return (numDisconnects > 0);
504505
}
505506

506-
BLEDevice ATTClass::central()
507+
BLEDevice ATTClass::central()
507508
{
509+
return central(0);
510+
}
511+
512+
BLEDevice ATTClass::central(int index)
513+
{
514+
int currentIndex = 0;
508515
for (int i = 0; i < ATT_MAX_PEERS; i++) {
509516
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x01) {
510517
continue;
511518
}
512519

513-
return BLEDevice(_peers[i].addressType, _peers[i].address);
520+
if (currentIndex == index) {
521+
return BLEDevice(_peers[i].addressType, _peers[i].address);
522+
}
523+
currentIndex++;
514524
}
515525

516526
return BLEDevice();
517527
}
518528

529+
int ATTClass::centralCount()
530+
{
531+
int count = 0;
532+
for (int i = 0; i < ATT_MAX_PEERS; i++) {
533+
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x01) {
534+
continue;
535+
}
536+
537+
count++;
538+
}
539+
540+
return count;
541+
}
542+
543+
BLEDevice ATTClass::peripheral()
544+
{
545+
return peripheral(0);
546+
}
547+
548+
BLEDevice ATTClass::peripheral(int index)
549+
{
550+
int currentIndex = 0;
551+
for (int i = 0; i < ATT_MAX_PEERS; i++) {
552+
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x00) {
553+
continue;
554+
}
555+
556+
if (currentIndex == index) {
557+
return BLEDevice(_peers[i].addressType, _peers[i].address);
558+
}
559+
currentIndex++;
560+
}
561+
562+
return BLEDevice();
563+
}
564+
565+
int ATTClass::peripheralCount()
566+
{
567+
int count = 0;
568+
for (int i = 0; i < ATT_MAX_PEERS; i++) {
569+
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x00) {
570+
continue;
571+
}
572+
573+
count++;
574+
}
575+
576+
return count;
577+
}
578+
519579
bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
520580
{
521581
int numNotifications = 0;

src/utility/ATT.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class ATTClass {
6767
virtual bool disconnect();
6868

6969
virtual BLEDevice central();
70+
virtual BLEDevice central(int index);
71+
virtual int centralCount();
72+
virtual BLEDevice peripheral();
73+
virtual BLEDevice peripheral(int index);
74+
virtual int peripheralCount();
7075

7176
virtual bool handleNotify(uint16_t handle, const uint8_t* value, int length);
7277
virtual bool handleInd(uint16_t handle, const uint8_t* value, int length);

src/utility/GAP.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ void GAPClass::stopAdvertise()
8484

8585
int GAPClass::scan(bool withDuplicates)
8686
{
87-
HCI.leSetScanEnable(false, true);
87+
//HCI.leSetScanEnable(false, true);
88+
stopScan();
8889

8990
// active scan, 10 ms scan interval (N * 0.625), 10 ms scan window (N * 0.625), public own address type, no filter
9091
if (HCI.leSetScanParameters(0x01, 0x0010, 0x0010, 0x00, 0x00) != 0) {
@@ -129,7 +130,8 @@ int GAPClass::scanForAddress(String address, bool withDuplicates)
129130

130131
void GAPClass::stopScan()
131132
{
132-
HCI.leSetScanEnable(false, false);
133+
//HCI.leSetScanEnable(false, false);
134+
HCI.leSetScanEnable(false, true);
133135

134136
_scanning = false;
135137

src/utility/HCI.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[])
627627
uint16_t supervisionTimeout;
628628
uint8_t masterClockAccuracy;
629629
} *leConnectionComplete = (EvtLeConnectionComplete*)&pdata[sizeof(HCIEventHdr) + sizeof(LeMetaEventHeader)];
630+
631+
// CLIENT: 0x01 / PERIPHERAL: 0x00
632+
Serial.println("role:");
633+
Serial.println(leConnectionComplete->role);
630634

631635
if (leConnectionComplete->status == 0x00) {
632636
ATT.addConnection(leConnectionComplete->handle,

src/utility/HCICordioTransport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ size_t HCICordioTransportClass::write(const uint8_t* data, size_t length)
287287
void HCICordioTransportClass::handleRxData(uint8_t* data, uint8_t len)
288288
{
289289
if (_rxBuf.availableForStore() < len) {
290+
// This drop can cause many problems
291+
Serial.println("DROP");
290292
// drop!
291293
return;
292294
}

0 commit comments

Comments
 (0)