Skip to content

Commit 0a7f833

Browse files
[nrf noup] BLE Extended Advertisment option
This commit implements solution for new config CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING which allow device to be visibly longer for the commissioning. Signed-off-by: Patryk Lipinski <patryk.lipinski@nordicsemi.no>
1 parent 854bbf3 commit 0a7f833

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

src/include/platform/CHIPDeviceConfig.h

+4
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,11 @@ static_assert(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN < CHIP_DEVICE_
693693
* Time in seconds that a factory new device will advertise commissionable node discovery.
694694
*/
695695
#ifndef CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS
696+
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING == 0
696697
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS (15 * 60)
698+
#else
699+
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS (60 * 60 * 48)
700+
#endif
697701
#endif
698702

699703
/**

src/include/platform/ConnectivityManager.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ class ConnectivityManager
147147

148148
enum BLEAdvertisingMode
149149
{
150-
kFastAdvertising = 0,
151-
kSlowAdvertising = 1,
150+
kFastAdvertising = 0,
151+
kSlowAdvertising = 1,
152+
kExtendedAdvertising = 2,
152153
};
153154

154155
enum class SEDIntervalMode

src/platform/Zephyr/BLEManagerImpl.cpp

+64-14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ namespace {
6060
constexpr uint32_t kAdvertisingOptions = BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME;
6161
constexpr uint8_t kAdvertisingFlags = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR;
6262

63+
constexpr uint8_t kLowestRequestPriority = 7;
64+
6365
const bt_uuid_128 UUID128_CHIPoBLEChar_RX =
6466
BT_UUID_INIT_128(0x11, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18);
6567
const bt_uuid_128 UUID128_CHIPoBLEChar_TX =
@@ -161,7 +163,7 @@ BLEManagerImpl BLEManagerImpl::sInstance;
161163
CHIP_ERROR BLEManagerImpl::_Init()
162164
{
163165
int err = 0;
164-
int id = 0;
166+
int id = 0;
165167

166168
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
167169
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
@@ -245,7 +247,11 @@ void BLEManagerImpl::DriveBLEState()
245247
}
246248
else
247249
{
248-
if (mFlags.Has(Flags::kAdvertising))
250+
if (mFlags.Has(Flags::kAdvertising)
251+
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
252+
&& mFlags.Has(Flags::kExtendedAdvertisingEnabled)
253+
#endif
254+
)
249255
{
250256
err = StopAdvertising();
251257
SuccessOrExit(err);
@@ -293,18 +299,45 @@ inline CHIP_ERROR BLEManagerImpl::PrepareAdvertisingRequest()
293299
Encoding::LittleEndian::Put16(serviceData.uuid, UUID16_CHIPoBLEService.val);
294300
ReturnErrorOnFailure(ConfigurationMgr().GetBLEDeviceIdentificationInfo(serviceData.deviceIdInfo));
295301

302+
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
303+
if (mFlags.Has(Flags::kExtendedAdvertisingEnabled))
304+
{
305+
serviceData.deviceIdInfo.SetVendorId(DEVICE_HANDLE_NULL);
306+
serviceData.deviceIdInfo.SetProductId(DEVICE_HANDLE_NULL);
307+
serviceData.deviceIdInfo.SetExtendedAnnouncementFlag(true);
308+
}
309+
#endif
310+
296311
advertisingData[0] = BT_DATA(BT_DATA_FLAGS, &kAdvertisingFlags, sizeof(kAdvertisingFlags));
297312
advertisingData[1] = BT_DATA(BT_DATA_SVC_DATA16, &serviceData, sizeof(serviceData));
298313
scanResponseData[0] = BT_DATA(BT_DATA_NAME_COMPLETE, name, nameSize);
299314

300-
mAdvertisingRequest.priority = CHIP_DEVICE_BLE_ADVERTISING_PRIORITY;
301-
mAdvertisingRequest.options = kAdvertisingOptions;
302-
mAdvertisingRequest.minInterval = mFlags.Has(Flags::kFastAdvertisingEnabled)
303-
? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN
304-
: CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
305-
mAdvertisingRequest.maxInterval = mFlags.Has(Flags::kFastAdvertisingEnabled)
306-
? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX
307-
: CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
315+
mAdvertisingRequest.priority = CHIP_DEVICE_BLE_ADVERTISING_PRIORITY;
316+
mAdvertisingRequest.options = kAdvertisingOptions;
317+
318+
if (mFlags.Has(Flags::kFastAdvertisingEnabled))
319+
{
320+
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN;
321+
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX;
322+
}
323+
else
324+
{
325+
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
326+
if (mFlags.Has(Flags::kExtendedAdvertisingEnabled))
327+
{
328+
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN;
329+
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX;
330+
}
331+
else
332+
{
333+
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
334+
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
335+
}
336+
#else
337+
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
338+
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
339+
#endif
340+
}
308341
mAdvertisingRequest.advertisingData = Span<bt_data>(advertisingData);
309342
mAdvertisingRequest.scanResponseData = nameSize ? Span<bt_data>(scanResponseData) : Span<bt_data>{};
310343

@@ -363,10 +396,17 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising()
363396

364397
if (mFlags.Has(Flags::kFastAdvertisingEnabled))
365398
{
366-
// Start timer to change advertising interval.
399+
// Start timer to change advertising fast to slow interval.
367400
DeviceLayer::SystemLayer().StartTimer(
368401
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME),
369-
HandleBLEAdvertisementIntervalChange, this);
402+
HandleSlowBLEAdvertisementInterval, this);
403+
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
404+
405+
// Start timer to change advertising slow to extended interval.
406+
DeviceLayer::SystemLayer().StartTimer(
407+
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS),
408+
HandleExtendedBLEAdvertisementInterval, this);
409+
#endif
370410
}
371411
}
372412

@@ -394,7 +434,8 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising()
394434
}
395435

396436
// Cancel timer event changing CHIPoBLE advertisement interval
397-
DeviceLayer::SystemLayer().CancelTimer(HandleBLEAdvertisementIntervalChange, this);
437+
DeviceLayer::SystemLayer().CancelTimer(HandleSlowBLEAdvertisementInterval, this);
438+
DeviceLayer::SystemLayer().CancelTimer(HandleExtendedBLEAdvertisementInterval, this);
398439
}
399440

400441
return CHIP_NO_ERROR;
@@ -423,6 +464,9 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
423464
case BLEAdvertisingMode::kSlowAdvertising:
424465
mFlags.Set(Flags::kFastAdvertisingEnabled, false);
425466
break;
467+
case BLEAdvertisingMode::kExtendedAdvertising:
468+
mFlags.Set(Flags::kExtendedAdvertisingEnabled, true);
469+
break;
426470
default:
427471
return CHIP_ERROR_INVALID_ARGUMENT;
428472
}
@@ -607,12 +651,18 @@ CHIP_ERROR BLEManagerImpl::PrepareC3CharData()
607651
}
608652
#endif
609653

610-
void BLEManagerImpl::HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param)
654+
void BLEManagerImpl::HandleSlowBLEAdvertisementInterval(System::Layer * layer, void * param)
611655
{
612656
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
613657
ChipLogProgress(DeviceLayer, "CHIPoBLE advertising mode changed to slow");
614658
}
615659

660+
void BLEManagerImpl::HandleExtendedBLEAdvertisementInterval(System::Layer * layer, void * param)
661+
{
662+
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kExtendedAdvertising);
663+
ChipLogProgress(DeviceLayer, "CHIPoBLE advertising mode changed to extended");
664+
}
665+
616666
void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
617667
{
618668
CHIP_ERROR err = CHIP_NO_ERROR;

src/platform/Zephyr/BLEManagerImpl.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
9191
kAdvertisingRefreshNeeded =
9292
0x0010, /**< The advertising state/configuration has changed, but the SoftDevice has yet to be updated. */
9393
kChipoBleGattServiceRegister = 0x0020, /**< The system has currently CHIPoBLE GATT service registered. */
94+
kExtendedAdvertisingEnabled = 0x0040, /**< The appliaction enabled extended advertising*/
9495
};
9596

9697
struct ServiceData;
@@ -131,7 +132,8 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
131132
static void HandleTXIndicated(bt_conn * conn, bt_gatt_indicate_params * attr, uint8_t err);
132133
static void HandleConnect(bt_conn * conn, uint8_t err);
133134
static void HandleDisconnect(bt_conn * conn, uint8_t reason);
134-
static void HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param);
135+
static void HandleSlowBLEAdvertisementInterval(System::Layer * layer, void * param);
136+
static void HandleExtendedBLEAdvertisementInterval(System::Layer * layer, void * param);
135137

136138
// ===== Members for internal use by the following friends.
137139

0 commit comments

Comments
 (0)