@@ -54,6 +54,18 @@ namespace {
54
54
55
55
static constexpr System::Clock::Timeout kNewConnectionScanTimeout = System::Clock::Seconds16(20 );
56
56
static constexpr System::Clock::Timeout kConnectTimeout = System::Clock::Seconds16(20 );
57
+ static constexpr System::Clock::Timeout kFastAdvertiseTimeout =
58
+ System::Clock::Milliseconds32 (CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
59
+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
60
+ // The CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS specifies the transition time
61
+ // starting from advertisement commencement. Since the extended advertisement timer is started after
62
+ // the fast-to-slow transition, we have to subtract the time spent in fast advertising.
63
+ static constexpr System::Clock::Timeout kSlowAdvertiseTimeout = System::Clock::Milliseconds32(
64
+ CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS - CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
65
+ static_assert (CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS >=
66
+ CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME,
67
+ " The extended advertising interval change time must be greater than the fast advertising interval change time" );
68
+ #endif
57
69
58
70
const ChipBleUUID ChipUUID_CHIPoBLEChar_RX = { { 0x18 , 0xEE , 0x2E , 0xF5 , 0x26 , 0x3D , 0x45 , 0x59 , 0x95 , 0x9F , 0x4F , 0x9C , 0x42 , 0x9F ,
59
71
0x9D , 0x11 } };
@@ -91,7 +103,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
91
103
92
104
OnChipBleConnectReceived = HandleIncomingBleConnection;
93
105
94
- PlatformMgr ().ScheduleWork ( DriveBLEState, 0 );
106
+ DeviceLayer::SystemLayer ().ScheduleLambda ([ this ] { DriveBLEState (); } );
95
107
96
108
exit :
97
109
return err;
@@ -119,7 +131,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
119
131
mFlags .Set (Flags::kAdvertisingEnabled , val);
120
132
}
121
133
122
- PlatformMgr ().ScheduleWork ( DriveBLEState, 0 );
134
+ DeviceLayer::SystemLayer ().ScheduleLambda ([ this ] { DriveBLEState (); } );
123
135
124
136
return err;
125
137
}
@@ -138,7 +150,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
138
150
return CHIP_ERROR_INVALID_ARGUMENT;
139
151
}
140
152
mFlags .Set (Flags::kAdvertisingRefreshNeeded );
141
- PlatformMgr ().ScheduleWork ( DriveBLEState, 0 );
153
+ DeviceLayer::SystemLayer ().ScheduleLambda ([ this ] { DriveBLEState (); } );
142
154
return CHIP_NO_ERROR;
143
155
}
144
156
@@ -186,14 +198,9 @@ uint16_t BLEManagerImpl::_NumConnections()
186
198
187
199
CHIP_ERROR BLEManagerImpl::ConfigureBle (uint32_t aAdapterId, bool aIsCentral)
188
200
{
189
-
190
- mAdapterId = aAdapterId;
191
- mIsCentral = aIsCentral;
192
-
193
- mBLEAdvType = ChipAdvType::BLUEZ_ADV_TYPE_UNDIRECTED_CONNECTABLE_SCANNABLE;
194
- mBLEAdvDurationMs = 2 ;
195
- mpBLEAdvUUID = " 0xFFF6" ;
196
-
201
+ mAdapterId = aAdapterId;
202
+ mIsCentral = aIsCentral;
203
+ mpBLEAdvUUID = " 0xFFF6" ;
197
204
return CHIP_NO_ERROR;
198
205
}
199
206
@@ -278,17 +285,14 @@ void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEv
278
285
case DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete :
279
286
VerifyOrExit (apEvent->Platform .BLEPeripheralAdvStartComplete .mIsSuccess , err = CHIP_ERROR_INCORRECT_STATE);
280
287
sInstance .mFlags .Clear (Flags::kControlOpInProgress ).Clear (Flags::kAdvertisingRefreshNeeded );
281
-
282
- if (!sInstance .mFlags .Has (Flags::kAdvertising ))
283
- {
284
- sInstance .mFlags .Set (Flags::kAdvertising );
285
- }
286
-
288
+ // Start a timer to make sure that the fast advertising is stopped after specified timeout.
289
+ SuccessOrExit (err = DeviceLayer::SystemLayer ().StartTimer (kFastAdvertiseTimeout , HandleAdvertisingTimer, this ));
290
+ sInstance .mFlags .Set (Flags::kAdvertising );
287
291
break ;
288
292
case DeviceEventType::kPlatformLinuxBLEPeripheralAdvStopComplete :
289
293
VerifyOrExit (apEvent->Platform .BLEPeripheralAdvStopComplete .mIsSuccess , err = CHIP_ERROR_INCORRECT_STATE);
290
-
291
294
sInstance .mFlags .Clear (Flags::kControlOpInProgress ).Clear (Flags::kAdvertisingRefreshNeeded );
295
+ DeviceLayer::SystemLayer ().CancelTimer (HandleAdvertisingTimer, this );
292
296
293
297
// Transition to the not Advertising state...
294
298
if (sInstance .mFlags .Has (Flags::kAdvertising ))
@@ -311,6 +315,7 @@ void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEv
311
315
{
312
316
ChipLogError (DeviceLayer, " Disabling CHIPoBLE service due to error: %s" , ErrorStr (err));
313
317
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled ;
318
+ DeviceLayer::SystemLayer ().CancelTimer (HandleAdvertisingTimer, this );
314
319
sInstance .mFlags .Clear (Flags::kControlOpInProgress );
315
320
}
316
321
@@ -595,15 +600,29 @@ void BLEManagerImpl::DriveBLEState()
595
600
// Configure advertising data if it hasn't been done yet.
596
601
if (!mFlags .Has (Flags::kAdvertisingConfigured ))
597
602
{
598
- SuccessOrExit (err = mBLEAdvertisement .Init (mEndpoint , mBLEAdvType , mpBLEAdvUUID, mBLEAdvDurationMs , mDeviceName ));
603
+ SuccessOrExit (err = mBLEAdvertisement .Init (mEndpoint , mpBLEAdvUUID, mDeviceName ));
599
604
mFlags .Set (Flags::kAdvertisingConfigured );
600
605
}
601
606
602
- // Start advertising. This is an asynchronous step. BLE manager will be notified of
603
- // advertising start completion via a call to NotifyBLEPeripheralAdvStartComplete.
604
- SuccessOrExit (err = mBLEAdvertisement .Start ());
605
- mFlags .Set (Flags::kControlOpInProgress );
606
- ExitNow ();
607
+ // Setup service data for advertising.
608
+ auto serviceDataFlags = BluezAdvertisement::kServiceDataNone ;
609
+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
610
+ if (mFlags .Has (Flags::kExtAdvertisingEnabled ))
611
+ serviceDataFlags |= BluezAdvertisement::kServiceDataExtendedAnnouncement ;
612
+ #endif
613
+ SuccessOrExit (err = mBLEAdvertisement .SetupServiceData (serviceDataFlags));
614
+
615
+ // Set or update the advertising intervals.
616
+ SuccessOrExit (err = mBLEAdvertisement .SetIntervals (GetAdvertisingIntervals ()));
617
+
618
+ if (!mFlags .Has (Flags::kAdvertising ))
619
+ {
620
+ // Start advertising. This is an asynchronous step. BLE manager will be notified of
621
+ // advertising start completion via a call to NotifyBLEPeripheralAdvStartComplete.
622
+ SuccessOrExit (err = mBLEAdvertisement .Start ());
623
+ mFlags .Set (Flags::kControlOpInProgress );
624
+ ExitNow ();
625
+ }
607
626
}
608
627
}
609
628
@@ -627,11 +646,6 @@ void BLEManagerImpl::DriveBLEState()
627
646
}
628
647
}
629
648
630
- void BLEManagerImpl::DriveBLEState (intptr_t arg)
631
- {
632
- sInstance .DriveBLEState ();
633
- }
634
-
635
649
void BLEManagerImpl::NotifyChipConnectionClosed (BLE_CONNECTION_OBJECT conId)
636
650
{
637
651
ChipLogProgress (Ble, " Got notification regarding chip connection closure" );
@@ -641,6 +655,39 @@ void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId)
641
655
#endif
642
656
}
643
657
658
+ BluezAdvertisement::AdvertisingIntervals BLEManagerImpl::GetAdvertisingIntervals () const
659
+ {
660
+ if (mFlags .Has (Flags::kFastAdvertisingEnabled ))
661
+ return { CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN, CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX };
662
+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
663
+ if (mFlags .Has (Flags::kExtAdvertisingEnabled ))
664
+ return { CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN, CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX };
665
+ #endif
666
+ return { CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN, CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX };
667
+ }
668
+
669
+ void BLEManagerImpl::HandleAdvertisingTimer (chip::System::Layer *, void * appState)
670
+ {
671
+ auto * self = static_cast <BLEManagerImpl *>(appState);
672
+
673
+ if (self->mFlags .Has (Flags::kFastAdvertisingEnabled ))
674
+ {
675
+ ChipLogDetail (DeviceLayer, " bleAdv Timeout : Start slow advertisement" );
676
+ self->_SetAdvertisingMode (BLEAdvertisingMode::kSlowAdvertising );
677
+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
678
+ self->mFlags .Clear (Flags::kExtAdvertisingEnabled );
679
+ DeviceLayer::SystemLayer ().StartTimer (kSlowAdvertiseTimeout , HandleAdvertisingTimer, self);
680
+ }
681
+ else
682
+ {
683
+ ChipLogDetail (DeviceLayer, " bleAdv Timeout : Start extended advertisement" );
684
+ self->mFlags .Set (Flags::kExtAdvertisingEnabled );
685
+ // This will trigger advertising intervals update in the DriveBLEState() function.
686
+ self->_SetAdvertisingMode (BLEAdvertisingMode::kSlowAdvertising );
687
+ #endif
688
+ }
689
+ }
690
+
644
691
void BLEManagerImpl::InitiateScan (BleScanState scanType)
645
692
{
646
693
DriveBLEState ();
@@ -695,18 +742,13 @@ void BLEManagerImpl::CleanScanConfig()
695
742
mBLEScanConfig .mBleScanState = BleScanState::kNotScanning ;
696
743
}
697
744
698
- void BLEManagerImpl::InitiateScan (intptr_t arg)
699
- {
700
- sInstance .InitiateScan (static_cast <BleScanState>(arg));
701
- }
702
-
703
745
void BLEManagerImpl::NewConnection (BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator)
704
746
{
705
747
mBLEScanConfig .mDiscriminator = connDiscriminator;
706
748
mBLEScanConfig .mAppState = appState;
707
749
708
750
// Scan initiation performed async, to ensure that the BLE subsystem is initialized.
709
- PlatformMgr ().ScheduleWork (InitiateScan, static_cast < intptr_t > (BleScanState::kScanForDiscriminator ));
751
+ DeviceLayer::SystemLayer ().ScheduleLambda ([ this ] { InitiateScan (BleScanState::kScanForDiscriminator ); } );
710
752
}
711
753
712
754
CHIP_ERROR BLEManagerImpl::CancelConnection ()
0 commit comments