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