@@ -152,7 +152,6 @@ const uint16_t CHIPoBLEGATTAttrCount = sizeof(CHIPoBLEGATTAttrs) / sizeof(CHIPoB
152152ChipDeviceScanner & mDeviceScanner = Internal::ChipDeviceScanner::GetInstance();
153153#endif
154154BLEManagerImpl BLEManagerImpl::sInstance ;
155- constexpr System::Clock::Timeout BLEManagerImpl::kFastAdvertiseTimeout ;
156155#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
157156static esp_gattc_char_elem_t * char_elem_result = NULL ;
158157static esp_gattc_descr_elem_t * descr_elem_result = NULL ;
@@ -235,6 +234,23 @@ CHIP_ERROR BLEManagerImpl::_Init()
235234 return err;
236235}
237236
237+ void BLEManagerImpl::_Shutdown ()
238+ {
239+ CancelBleAdvTimeoutTimer ();
240+
241+ BleLayer::Shutdown ();
242+ mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled ;
243+
244+ // selectively setting kGATTServiceStarted flag, in order to notify the state machine to stop the CHIPoBLE gatt service
245+ mFlags .ClearAll ().Set (Flags::kGATTServiceStarted );
246+
247+ #ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
248+ OnChipBleConnectReceived = nullptr ;
249+ #endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
250+
251+ PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
252+ }
253+
238254CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled (bool val)
239255{
240256 CHIP_ERROR err = CHIP_NO_ERROR;
@@ -243,8 +259,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
243259
244260 if (val)
245261 {
246- mAdvertiseStartTime = System::SystemClock ().GetMonotonicTimestamp ();
247- ReturnErrorOnFailure (DeviceLayer::SystemLayer ().StartTimer (kFastAdvertiseTimeout , HandleFastAdvertisementTimer, this ));
262+ StartBleAdvTimeoutTimer (CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
248263 }
249264 mFlags .Set (Flags::kFastAdvertisingEnabled , val);
250265 mFlags .Set (Flags::kAdvertisingRefreshNeeded , 1 );
@@ -254,21 +269,29 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
254269 return err;
255270}
256271
257- void BLEManagerImpl::HandleFastAdvertisementTimer (System::Layer * systemLayer , void * context )
272+ void BLEManagerImpl::BleAdvTimeoutHandler (System::Layer *, void *)
258273{
259- static_cast <BLEManagerImpl *>(context)->HandleFastAdvertisementTimer ();
260- }
261-
262- void BLEManagerImpl::HandleFastAdvertisementTimer ()
263- {
264- System::Clock::Timestamp currentTimestamp = System::SystemClock ().GetMonotonicTimestamp ();
265-
266- if (currentTimestamp - mAdvertiseStartTime >= kFastAdvertiseTimeout )
274+ if (BLEMgrImpl ().mFlags .Has (Flags::kFastAdvertisingEnabled ))
275+ {
276+ ChipLogProgress (DeviceLayer, " bleAdv Timeout : Start slow advertisement" );
277+ BLEMgrImpl ().mFlags .Set (Flags::kFastAdvertisingEnabled , 0 );
278+ BLEMgrImpl ().mFlags .Set (Flags::kAdvertisingRefreshNeeded , 1 );
279+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
280+ BLEMgrImpl ().mFlags .Clear (Flags::kExtAdvertisingEnabled );
281+ BLEMgrImpl ().StartBleAdvTimeoutTimer (CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS);
282+ #endif
283+ }
284+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
285+ else
267286 {
268- mFlags .Clear (Flags::kFastAdvertisingEnabled );
269- mFlags .Set (Flags::kAdvertisingRefreshNeeded );
270- PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
287+ ChipLogProgress (DeviceLayer, " bleAdv Timeout : Start extended advertisement" );
288+ BLEMgrImpl ().mFlags .Set (Flags::kAdvertising );
289+ BLEMgrImpl ().mFlags .Set (Flags::kExtAdvertisingEnabled );
290+ BLEMgr ().SetAdvertisingMode (BLEAdvertisingMode::kSlowAdvertising );
291+ BLEMgrImpl ().mFlags .Set (Flags::kAdvertisingRefreshNeeded , 1 );
271292 }
293+ #endif
294+ PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
272295}
273296
274297CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode (BLEAdvertisingMode mode)
@@ -853,8 +876,8 @@ bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUU
853876#endif
854877
855878 // Set param need_confirm as false will send notification, otherwise indication.
856- err = MapBLEError (
857- esp_ble_gatts_send_indicate ( mAppIf , conId, mTXCharAttrHandle , data-> DataLength (), data->Start (), true /* need_confirm */ ));
879+ err = MapBLEError (esp_ble_gatts_send_indicate ( mAppIf , conId, mTXCharAttrHandle , static_cast < uint16_t >(data-> DataLength ()),
880+ data->Start (), true /* need_confirm */ ));
858881 if (err != CHIP_NO_ERROR)
859882 {
860883 ChipLogError (DeviceLayer, " esp_ble_gatts_send_indicate() failed: %s" , ErrorStr (err));
@@ -936,6 +959,20 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
936959 }
937960}
938961
962+ void BLEManagerImpl::CancelBleAdvTimeoutTimer (void )
963+ {
964+ SystemLayer ().CancelTimer (BleAdvTimeoutHandler, nullptr );
965+ }
966+
967+ void BLEManagerImpl::StartBleAdvTimeoutTimer (uint32_t aTimeoutInMs)
968+ {
969+ CHIP_ERROR err = SystemLayer ().StartTimer (System::Clock::Milliseconds32 (aTimeoutInMs), BleAdvTimeoutHandler, nullptr );
970+ if ((err != CHIP_NO_ERROR))
971+ {
972+ ChipLogError (DeviceLayer, " Failed to start BledAdv timeout timer" );
973+ }
974+ }
975+
939976void BLEManagerImpl::DriveBLEState (void )
940977{
941978 CHIP_ERROR err = CHIP_NO_ERROR;
@@ -1059,7 +1096,8 @@ void BLEManagerImpl::DriveBLEState(void)
10591096 ExitNow ();
10601097 }
10611098
1062- mFlags .Set (Flags::kControlOpInProgress );
1099+ DeinitESPBleLayer ();
1100+ mFlags .ClearAll ();
10631101
10641102 ExitNow ();
10651103 }
@@ -1162,6 +1200,23 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void)
11621200 return err;
11631201}
11641202
1203+ esp_err_t bluedroid_set_random_address ()
1204+ {
1205+ esp_bd_addr_t rand_addr;
1206+
1207+ esp_fill_random (rand_addr, sizeof (esp_bd_addr_t ));
1208+ rand_addr[0 ] = (rand_addr[0 ] & 0x3F ) | 0xC0 ;
1209+
1210+ esp_err_t ret = esp_ble_gap_set_rand_addr (rand_addr);
1211+ if (ret != ESP_OK)
1212+ {
1213+ ChipLogError (DeviceLayer, " Failed to set random address: %s" , esp_err_to_name (ret));
1214+ return ret;
1215+ }
1216+
1217+ return ESP_OK;
1218+ }
1219+
11651220CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData (void )
11661221{
11671222 CHIP_ERROR err;
@@ -1187,6 +1242,27 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
11871242 ExitNow ();
11881243 }
11891244
1245+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
1246+ // Check for extended advertisement interval and redact VID/PID if past the initial period.
1247+ if (mFlags .Has (Flags::kExtAdvertisingEnabled ))
1248+ {
1249+ deviceIdInfo.SetVendorId (0 );
1250+ deviceIdInfo.SetProductId (0 );
1251+ deviceIdInfo.SetExtendedAnnouncementFlag (true );
1252+ }
1253+ #endif
1254+
1255+ #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
1256+ if (!mFlags .Has (Flags::kExtAdvertisingEnabled ))
1257+ {
1258+ deviceIdInfo.SetAdditionalDataFlag (true );
1259+ }
1260+ else
1261+ {
1262+ deviceIdInfo.SetAdditionalDataFlag (false );
1263+ }
1264+ #endif
1265+
11901266 memset (advData, 0 , sizeof (advData));
11911267 advData[index++] = 0x02 ; // length
11921268 advData[index++] = CHIP_ADV_DATA_TYPE_FLAGS; // AD type : flags
@@ -1216,12 +1292,16 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
12161292 ExitNow ();
12171293 }
12181294
1295+ bluedroid_set_random_address ();
12191296 mFlags .Set (Flags::kControlOpInProgress );
12201297
12211298exit:
12221299 return err;
12231300}
12241301
1302+ // TODO(#36919): Fix the Bluedroid ShutDown flow for ESP32.
1303+ void BLEManagerImpl::DeinitESPBleLayer () {}
1304+
12251305CHIP_ERROR BLEManagerImpl::StartAdvertising (void )
12261306{
12271307 CHIP_ERROR err;
@@ -1252,8 +1332,23 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
12521332 }
12531333 else
12541334 {
1335+ #if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
1336+ if (!mFlags .Has (Flags::kExtAdvertisingEnabled ))
1337+ {
1338+ advertParams.adv_int_min = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
1339+ advertParams.adv_int_max = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
1340+ }
1341+ else
1342+ {
1343+ advertParams.adv_int_min = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN;
1344+ advertParams.adv_int_max = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX;
1345+ }
1346+ #else
1347+
12551348 advertParams.adv_int_min = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
12561349 advertParams.adv_int_max = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
1350+
1351+ #endif
12571352 }
12581353
12591354 ChipLogProgress (DeviceLayer, " Configuring CHIPoBLE advertising (interval %" PRIu32 " ms, %sconnectable, device name %s)" ,
0 commit comments