Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit b4903e2

Browse files
author
Jay Logue
committed
Fixed logic bugs in ESP32 BLEManager
-- Fixed a set of logic bugs in the ESP32 version of the BLEManager having to do with refreshing the WoBLE advertising state as the state of the device changes. These bugs were inadvertently introduced in the previous change to this code.
1 parent c63e5a1 commit b4903e2

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/adaptations/device-layer/ESP32/BLEManagerImpl.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId)
295295
// Release the associated connection state record.
296296
ReleaseConnectionState(conId);
297297

298-
// Arrange to re-enable connectable advertising in case it was disabled due to the
299-
// maximum connection limit being reached.
300-
ClearFlag(mFlags, kFlag_Advertising);
298+
// Force a refresh of the advertising state.
299+
SetFlag(mFlags, kFlag_AdvertisingRefreshNeeded);
300+
ClearFlag(mFlags, kFlag_AdvertisingConfigured);
301301
PlatformMgr().ScheduleWork(DriveBLEState, 0);
302302

303303
return (err == WEAVE_NO_ERROR);
@@ -439,24 +439,28 @@ void BLEManagerImpl::DriveBLEState(void)
439439
ExitNow();
440440
}
441441

442-
// Start advertising if needed...
442+
// If the application has enabled WoBLE and BLE advertising...
443443
if (mServiceMode == ConnectivityManager::kWoBLEServiceMode_Enabled && GetFlag(mFlags, kFlag_AdvertisingEnabled)
444444
#if WEAVE_DEVICE_CONFIG_WOBLE_SINGLE_CONNECTION
445445
// and no connections are active...
446446
&& (_NumConnections() == 0)
447447
#endif
448448
)
449449
{
450-
// Configure advertising data if needed.
451-
if (!GetFlag(mFlags, kFlag_AdvertisingConfigured))
452-
{
453-
err = ConfigureAdvertisingData();
454-
ExitNow();
455-
}
456-
457-
// Start advertising if needed.
450+
// Start/re-start advertising if not already advertising, or if the advertising state of the
451+
// ESP BLE layer needs to be refreshed.
458452
if (!GetFlag(mFlags, kFlag_Advertising) || GetFlag(mFlags, kFlag_AdvertisingRefreshNeeded))
459453
{
454+
// Configure advertising data if it hasn't been done yet. This is an asynchronous step which
455+
// must complete before advertising can be started. When that happens, this method will
456+
// be called again, and execution will proceed to the code below.
457+
if (!GetFlag(mFlags, kFlag_AdvertisingConfigured))
458+
{
459+
err = ConfigureAdvertisingData();
460+
ExitNow();
461+
}
462+
463+
// Start advertising. This is also an asynchronous step.
460464
err = StartAdvertising();
461465
ExitNow();
462466
}
@@ -845,12 +849,10 @@ void BLEManagerImpl::HandleGATTCommEvent(esp_gatts_cb_event_t event, esp_gatt_if
845849
// Allocate a connection state record for the new connection.
846850
GetConnectionState(param->mtu.conn_id, true);
847851

848-
// Receiving a connection stops the advertising processes. So record that the advertising state
849-
// in the ESP BLE layer needs to be refreshed. If advertising should to be re-enabled, this will
850-
// be handled in DriveBLEState() the next time it runs.
852+
// Receiving a connection stops the advertising processes. So force a refresh of the advertising
853+
// state.
851854
SetFlag(mFlags, kFlag_AdvertisingRefreshNeeded);
852-
853-
// Re-evaluate the advertising state in light of the new connection.
855+
ClearFlag(mFlags, kFlag_AdvertisingConfigured);
854856
PlatformMgr().ScheduleWork(DriveBLEState, 0);
855857

856858
break;
@@ -1100,9 +1102,9 @@ void BLEManagerImpl::HandleDisconnect(esp_ble_gatts_cb_param_t * param)
11001102
}
11011103
PlatformMgr().PostEvent(&event);
11021104

1103-
// Arrange to re-enable connectable advertising in case it was disabled due to the
1104-
// maximum connection limit being reached.
1105-
ClearFlag(mFlags, kFlag_Advertising);
1105+
// Force a refresh of the advertising state.
1106+
SetFlag(mFlags, kFlag_AdvertisingRefreshNeeded);
1107+
ClearFlag(mFlags, kFlag_AdvertisingConfigured);
11061108
PlatformMgr().ScheduleWork(DriveBLEState, 0);
11071109
}
11081110
}
@@ -1220,6 +1222,7 @@ void BLEManagerImpl::HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb
12201222
}
12211223

12221224
ClearFlag(sInstance.mFlags, kFlag_ControlOpInProgress);
1225+
ClearFlag(sInstance.mFlags, kFlag_AdvertisingRefreshNeeded);
12231226

12241227
// Transition to the Advertising state...
12251228
if (!GetFlag(sInstance.mFlags, kFlag_Advertising))
@@ -1248,6 +1251,7 @@ void BLEManagerImpl::HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb
12481251
}
12491252

12501253
ClearFlag(sInstance.mFlags, kFlag_ControlOpInProgress);
1254+
ClearFlag(sInstance.mFlags, kFlag_AdvertisingRefreshNeeded);
12511255

12521256
// Transition to the not Advertising state...
12531257
if (GetFlag(sInstance.mFlags, kFlag_Advertising))

src/adaptations/device-layer/include/Weave/DeviceLayer/ESP32/BLEManagerImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class BLEManagerImpl final
102102
kFlag_AdvertisingEnabled = 0x0100, /**< The application has enabled WoBLE advertising. */
103103
kFlag_FastAdvertisingEnabled = 0x0200, /**< The application has enabled fast advertising. */
104104
kFlag_UseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
105-
kFlag_AdvertisingRefreshNeeded = 0x0800, /**< The advertising state in ESP BLE layer needs to be updated. */
105+
kFlag_AdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
106106
};
107107

108108
enum

0 commit comments

Comments
 (0)