Skip to content

Commit 348a1a0

Browse files
polldogiulcioffi
authored andcommitted
[WIP] Implement and test multiple concurrent connections
1 parent fd5bc06 commit 348a1a0

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

src/local/BLELocalDevice.cpp

+35
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ class BLELocalDevice {
6767
virtual void stopScan();
6868

6969
virtual BLEDevice central();
70-
virtual BLEDevice available();
70+
virtual BLEDevice available();
71+
int centralCount();
72+
BLEDevice peripheral();
73+
BLEDevice peripheral(int index);
74+
int peripheralCount();
75+
BLEDevice available();
76+
7177

7278
virtual void setAdvertisingInterval(uint16_t advertisingInterval);
7379
virtual void setConnectionInterval(uint16_t minimumConnectionInterval, uint16_t maximumConnectionInterval);

src/utility/ATT.cpp

+61-2
Original file line numberDiff line numberDiff line change
@@ -497,19 +497,78 @@ bool ATTClass::disconnect()
497497
return (numDisconnects > 0);
498498
}
499499

500-
BLEDevice ATTClass::central()
500+
BLEDevice ATTClass::central()
501501
{
502+
return central(0);
503+
}
504+
505+
BLEDevice ATTClass::central(int index)
506+
{
507+
int currentIndex = 0;
502508
for (int i = 0; i < ATT_MAX_PEERS; i++) {
503509
if (_peers[i].connectionHandle == 0xffff || _peers[i].role != 0x01) {
504510
continue;
505511
}
506512

507-
return BLEDevice(_peers[i].addressType, _peers[i].address);
513+
if (currentIndex == index) {
514+
return BLEDevice(_peers[i].addressType, _peers[i].address);
515+
}
516+
currentIndex++;
508517
}
509518

510519
return BLEDevice();
511520
}
512521

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

src/utility/ATT.h

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class ATTClass {
6767
virtual bool disconnect();
6868

6969
virtual BLEDevice central();
70+
BLEDevice central(int index);
71+
int centralCount();
72+
BLEDevice peripheral();
73+
BLEDevice peripheral(int index);
74+
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);

0 commit comments

Comments
 (0)