@@ -162,7 +162,7 @@ BLEManagerImpl BLEManagerImpl::sInstance;
162
162
CHIP_ERROR BLEManagerImpl::_Init ()
163
163
{
164
164
int err = 0 ;
165
- int id = 0 ;
165
+ int id = 0 ;
166
166
167
167
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled ;
168
168
mFlags .ClearAll ().Set (Flags::kAdvertisingEnabled , CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
@@ -241,15 +241,26 @@ void BLEManagerImpl::DriveBLEState()
241
241
{
242
242
mFlags .Clear (Flags::kAdvertisingRefreshNeeded );
243
243
err = StartAdvertising ();
244
- SuccessOrExit (err);
244
+ if (err != CHIP_NO_ERROR)
245
+ {
246
+ // Return prematurely but keep the CHIPoBLE service mode enabled to allow advertising retries
247
+ mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled ;
248
+ ChipLogError (DeviceLayer, " Could not start CHIPoBLE service due to error: %" CHIP_ERROR_FORMAT, err.Format ());
249
+ return ;
250
+ }
245
251
}
246
252
}
247
253
else
248
254
{
249
255
if (mFlags .Has (Flags::kAdvertising ))
250
256
{
251
257
err = StopAdvertising ();
252
- SuccessOrExit (err);
258
+ if (err != CHIP_NO_ERROR)
259
+ {
260
+ ChipLogError (DeviceLayer, " Disabling CHIPoBLE service due to error: %" CHIP_ERROR_FORMAT, err.Format ());
261
+ mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled ;
262
+ return ;
263
+ }
253
264
}
254
265
255
266
// If no connections are active unregister also CHIPoBLE GATT service
@@ -266,13 +277,6 @@ void BLEManagerImpl::DriveBLEState()
266
277
}
267
278
}
268
279
}
269
-
270
- exit :
271
- if (err != CHIP_NO_ERROR)
272
- {
273
- ChipLogError (DeviceLayer, " Disabling CHIPoBLE service due to error: %" CHIP_ERROR_FORMAT, err.Format ());
274
- mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled ;
275
- }
276
280
}
277
281
278
282
struct BLEManagerImpl ::ServiceData
@@ -338,37 +342,67 @@ inline CHIP_ERROR BLEManagerImpl::PrepareAdvertisingRequest()
338
342
else
339
343
{
340
344
ChipLogError (DeviceLayer, " Failed to start CHIPoBLE advertising: %d" , rc);
345
+ BLEManagerImpl ().StopAdvertising ();
341
346
}
342
347
};
343
348
344
349
return CHIP_NO_ERROR;
345
350
}
346
351
347
- CHIP_ERROR BLEManagerImpl::StartAdvertising ()
352
+ CHIP_ERROR BLEManagerImpl::RegisterGattService ()
348
353
{
349
- // Prepare advertising request
350
- ReturnErrorOnFailure (PrepareAdvertisingRequest ());
351
-
352
- // Register dynamically CHIPoBLE GATT service
354
+ // Register CHIPoBLE GATT service
353
355
if (!mFlags .Has (Flags::kChipoBleGattServiceRegister ))
354
356
{
355
357
int err = bt_gatt_service_register (&sChipoBleService );
356
-
357
358
if (err != 0 )
358
- ChipLogError (DeviceLayer, " Failed to register CHIPoBLE GATT service" );
359
+ {
360
+ ChipLogError (DeviceLayer, " Failed to register CHIPoBLE GATT service: %d" , err);
361
+ }
359
362
360
363
VerifyOrReturnError (err == 0 , MapErrorZephyr (err));
361
-
362
364
mFlags .Set (Flags::kChipoBleGattServiceRegister );
363
365
}
366
+ return CHIP_NO_ERROR;
367
+ }
368
+
369
+ CHIP_ERROR BLEManagerImpl::UnregisterGattService ()
370
+ {
371
+ // Unregister CHIPoBLE GATT service
372
+ if (mFlags .Has (Flags::kChipoBleGattServiceRegister ))
373
+ {
374
+ int err = bt_gatt_service_unregister (&sChipoBleService );
375
+ if (err != 0 )
376
+ {
377
+ ChipLogError (DeviceLayer, " Failed to unregister CHIPoBLE GATT service: %d" , err);
378
+ }
379
+
380
+ VerifyOrReturnError (err == 0 , MapErrorZephyr (err));
381
+ mFlags .Clear (Flags::kChipoBleGattServiceRegister );
382
+ }
383
+ return CHIP_NO_ERROR;
384
+ }
385
+
386
+ CHIP_ERROR BLEManagerImpl::StartAdvertising ()
387
+ {
388
+ // Prepare advertising request
389
+ ReturnErrorOnFailure (PrepareAdvertisingRequest ());
390
+ // We need to register GATT service before issuing the advertising to start
391
+ ReturnErrorOnFailure (RegisterGattService ());
364
392
365
393
// Initialize C3 characteristic data
366
394
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
367
395
ReturnErrorOnFailure (PrepareC3CharData ());
368
396
#endif
369
397
370
398
// Request advertising
371
- ReturnErrorOnFailure (BLEAdvertisingArbiter::InsertRequest (mAdvertisingRequest ));
399
+ CHIP_ERROR err = BLEAdvertisingArbiter::InsertRequest (mAdvertisingRequest );
400
+ if (CHIP_NO_ERROR != err)
401
+ {
402
+ // It makes not sense to keep GATT services registered after the advertising request failed
403
+ (void ) UnregisterGattService ();
404
+ return err;
405
+ }
372
406
373
407
// Transition to the Advertising state...
374
408
if (!mFlags .Has (Flags::kAdvertising ))
@@ -430,22 +464,23 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising()
430
464
DeviceLayer::SystemLayer ().CancelTimer (HandleSlowBLEAdvertisementInterval, this );
431
465
DeviceLayer::SystemLayer ().CancelTimer (HandleExtendedBLEAdvertisementInterval, this );
432
466
}
467
+ else
468
+ {
469
+ ChipLogProgress (DeviceLayer, " CHIPoBLE advertising already stopped" );
470
+ }
433
471
434
472
return CHIP_NO_ERROR;
435
473
}
436
474
437
475
CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled (bool val)
438
476
{
439
- if (mFlags .Has (Flags::kAdvertisingEnabled ) != val)
440
- {
441
- ChipLogDetail (DeviceLayer, " CHIPoBLE advertising set to %s" , val ? " on" : " off" );
477
+ ChipLogDetail (DeviceLayer, " CHIPoBLE advertising set to %s" , val ? " on" : " off" );
442
478
443
- mFlags .Set (Flags::kAdvertisingEnabled , val);
444
- // Ensure that each enabling/disabling of the standard advertising clears
445
- // the extended mode flag.
446
- mFlags .Set (Flags::kExtendedAdvertisingEnabled , false );
447
- PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
448
- }
479
+ mFlags .Set (Flags::kAdvertisingEnabled , val);
480
+ // Ensure that each enabling/disabling of the general advertising clears
481
+ // the extended mode, to make sure we always start fresh in the regular mode
482
+ mFlags .Set (Flags::kExtendedAdvertisingEnabled , false );
483
+ PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
449
484
450
485
return CHIP_NO_ERROR;
451
486
}
@@ -515,7 +550,10 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event)
515
550
516
551
ChipLogProgress (DeviceLayer, " BLE GAP connection terminated (reason 0x%02x)" , connEvent->HciResult );
517
552
518
- mMatterConnNum --;
553
+ if (mMatterConnNum > 0 )
554
+ {
555
+ mMatterConnNum --;
556
+ }
519
557
520
558
// If indications were enabled for this connection, record that they are now disabled and
521
559
// notify the BLE Layer of a disconnect.
@@ -907,7 +945,11 @@ void BLEManagerImpl::HandleDisconnect(struct bt_conn * conId, uint8_t reason)
907
945
908
946
PlatformMgr ().LockChipStack ();
909
947
910
- sInstance .mTotalConnNum --;
948
+ if (sInstance .mTotalConnNum > 0 )
949
+ {
950
+ sInstance .mTotalConnNum --;
951
+ }
952
+
911
953
ChipLogProgress (DeviceLayer, " Current number of connections: %u/%u" , sInstance .mTotalConnNum , CONFIG_BT_MAX_CONN);
912
954
913
955
VerifyOrExit (bt_conn_get_info (conId, &bt_info) == 0 , );
0 commit comments