Skip to content

Commit

Permalink
[nrf noup] BLE Extended Advertisement option
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
LipinskiPNordicSemi committed Feb 14, 2024
1 parent 854bbf3 commit 9688a76
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 24 deletions.
17 changes: 17 additions & 0 deletions config/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,21 @@ config CHIP_OTA_IMAGE_EXTRA_ARGS

endif

config CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
bool "Allow to extend advertising time longer than 15min"
help
Enable BLE extended advertising.
If this config is true,
CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS can be set up to 48h.

config CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_MINUTES
int "Configures commissioning tiemout"
range 15 2880 if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
range 0 15
default 15
help
Specify how long the device will be in the commisioning modes in seconds.
If CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING is set false,
maximal commisioning time is 15 minutes, if true can be extended up to 48h.

endif
4 changes: 2 additions & 2 deletions src/app/server/CommissioningWindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ CHIP_ERROR CommissioningWindowManager::AdvertiseAndListenForPASE()
return CHIP_NO_ERROR;
}

CHIP_ERROR CommissioningWindowManager::OpenBasicCommissioningWindow(Seconds16 commissioningTimeout,
CHIP_ERROR CommissioningWindowManager::OpenBasicCommissioningWindow(Seconds32 commissioningTimeout,
CommissioningWindowAdvertisement advertisementMode)
{
RestoreDiscriminator();
Expand Down Expand Up @@ -304,7 +304,7 @@ CHIP_ERROR CommissioningWindowManager::OpenBasicCommissioningWindow(Seconds16 co

CHIP_ERROR
CommissioningWindowManager::OpenBasicCommissioningWindowForAdministratorCommissioningCluster(
System::Clock::Seconds16 commissioningTimeout, FabricIndex fabricIndex, VendorId vendorId)
System::Clock::Seconds32 commissioningTimeout, FabricIndex fabricIndex, VendorId vendorId)
{
ReturnErrorOnFailure(OpenBasicCommissioningWindow(commissioningTimeout, CommissioningWindowAdvertisement::kDnssdOnly));

Expand Down
10 changes: 5 additions & 5 deletions src/app/server/CommissioningWindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ class CommissioningWindowManager : public Messaging::UnsolicitedMessageHandler,
return CHIP_NO_ERROR;
}

static constexpr System::Clock::Seconds16 MaxCommissioningTimeout()
static constexpr System::Clock::Seconds32 MaxCommissioningTimeout()
{
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
// Specification section 2.3.1 - Extended Announcement Duration up to 48h
return System::Clock::Seconds16(60 * 60 * 48);
return System::Clock::Seconds32(60 * 60 * 48);
#else
// Specification section 5.4.2.3. Announcement Duration says 15 minutes.
return System::Clock::Seconds16(15 * 60);
return System::Clock::Seconds32(15 * 60);
#endif
}

Expand All @@ -81,15 +81,15 @@ class CommissioningWindowManager : public Messaging::UnsolicitedMessageHandler,
*/
CHIP_ERROR
OpenBasicCommissioningWindow(
System::Clock::Seconds16 commissioningTimeout = System::Clock::Seconds16(CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS),
System::Clock::Seconds32 commissioningTimeout = System::Clock::Seconds16(CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS),
CommissioningWindowAdvertisement advertisementMode = chip::CommissioningWindowAdvertisement::kAllSupported);

/**
* Open the pairing window using default configured parameters, triggered by
* the Administrator Commmissioning cluster implementation.
*/
CHIP_ERROR
OpenBasicCommissioningWindowForAdministratorCommissioningCluster(System::Clock::Seconds16 commissioningTimeout,
OpenBasicCommissioningWindowForAdministratorCommissioningCluster(System::Clock::Seconds32 commissioningTimeout,
FabricIndex fabricIndex, VendorId vendorId);

CHIP_ERROR OpenEnhancedCommissioningWindow(System::Clock::Seconds16 commissioningTimeout, uint16_t discriminator,
Expand Down
2 changes: 1 addition & 1 deletion src/include/platform/CHIPDeviceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@
*
*/
#ifndef CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART
#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART 1
#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART 0
#endif

/**
Expand Down
5 changes: 3 additions & 2 deletions src/include/platform/ConnectivityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ class ConnectivityManager

enum BLEAdvertisingMode
{
kFastAdvertising = 0,
kSlowAdvertising = 1,
kFastAdvertising = 0,
kSlowAdvertising = 1,
kExtendedAdvertising = 2,
};

enum class SEDIntervalMode
Expand Down
71 changes: 58 additions & 13 deletions src/platform/Zephyr/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ BLEManagerImpl BLEManagerImpl::sInstance;
CHIP_ERROR BLEManagerImpl::_Init()
{
int err = 0;
int id = 0;
int id = 0;

mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
Expand Down Expand Up @@ -293,18 +293,39 @@ inline CHIP_ERROR BLEManagerImpl::PrepareAdvertisingRequest()
Encoding::LittleEndian::Put16(serviceData.uuid, UUID16_CHIPoBLEService.val);
ReturnErrorOnFailure(ConfigurationMgr().GetBLEDeviceIdentificationInfo(serviceData.deviceIdInfo));

#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
if (mFlags.Has(Flags::kExtendedAdvertisingEnabled))
{
serviceData.deviceIdInfo.SetVendorId(DEVICE_HANDLE_NULL);
serviceData.deviceIdInfo.SetProductId(DEVICE_HANDLE_NULL);
serviceData.deviceIdInfo.SetExtendedAnnouncementFlag(true);
}
#endif

advertisingData[0] = BT_DATA(BT_DATA_FLAGS, &kAdvertisingFlags, sizeof(kAdvertisingFlags));
advertisingData[1] = BT_DATA(BT_DATA_SVC_DATA16, &serviceData, sizeof(serviceData));
scanResponseData[0] = BT_DATA(BT_DATA_NAME_COMPLETE, name, nameSize);

mAdvertisingRequest.priority = CHIP_DEVICE_BLE_ADVERTISING_PRIORITY;
mAdvertisingRequest.options = kAdvertisingOptions;
mAdvertisingRequest.minInterval = mFlags.Has(Flags::kFastAdvertisingEnabled)
? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN
: CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
mAdvertisingRequest.maxInterval = mFlags.Has(Flags::kFastAdvertisingEnabled)
? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX
: CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
mAdvertisingRequest.priority = CHIP_DEVICE_BLE_ADVERTISING_PRIORITY;
mAdvertisingRequest.options = kAdvertisingOptions;

if (mFlags.Has(Flags::kFastAdvertisingEnabled))
{
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN;
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX;
}
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
else if (mFlags.Has(Flags::kExtendedAdvertisingEnabled))
{
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN;
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX;
}
#endif
else
{
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
}
mAdvertisingRequest.advertisingData = Span<bt_data>(advertisingData);
mAdvertisingRequest.scanResponseData = nameSize ? Span<bt_data>(scanResponseData) : Span<bt_data>{};

Expand Down Expand Up @@ -363,10 +384,17 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising()

if (mFlags.Has(Flags::kFastAdvertisingEnabled))
{
// Start timer to change advertising interval.
// Start timer to change advertising interval from fast to slow.
DeviceLayer::SystemLayer().StartTimer(
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME),
HandleBLEAdvertisementIntervalChange, this);
HandleSlowBLEAdvertisementInterval, this);

#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
// Start timer to change advertising interval from slow to extended.
DeviceLayer::SystemLayer().StartTimer(
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS),
HandleExtendedBLEAdvertisementInterval, this);
#endif
}
}

Expand All @@ -383,6 +411,10 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising()
mFlags.Clear(Flags::kAdvertising);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);

#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
mFlags.Clear(Flags::kExtendedAdvertisingEnabled);
#endif

ChipLogProgress(DeviceLayer, "CHIPoBLE advertising stopped");

// Post a CHIPoBLEAdvertisingChange(Stopped) event.
Expand All @@ -394,7 +426,8 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising()
}

// Cancel timer event changing CHIPoBLE advertisement interval
DeviceLayer::SystemLayer().CancelTimer(HandleBLEAdvertisementIntervalChange, this);
DeviceLayer::SystemLayer().CancelTimer(HandleSlowBLEAdvertisementInterval, this);
DeviceLayer::SystemLayer().CancelTimer(HandleExtendedBLEAdvertisementInterval, this);
}

return CHIP_NO_ERROR;
Expand All @@ -419,8 +452,14 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
{
case BLEAdvertisingMode::kFastAdvertising:
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
mFlags.Set(Flags::kExtendedAdvertisingEnabled, false);
break;
case BLEAdvertisingMode::kSlowAdvertising:
mFlags.Set(Flags::kFastAdvertisingEnabled, false);
mFlags.Set(Flags::kExtendedAdvertisingEnabled, false);
break;
case BLEAdvertisingMode::kExtendedAdvertising:
mFlags.Set(Flags::kExtendedAdvertisingEnabled, true);
mFlags.Set(Flags::kFastAdvertisingEnabled, false);
break;
default:
Expand Down Expand Up @@ -607,12 +646,18 @@ CHIP_ERROR BLEManagerImpl::PrepareC3CharData()
}
#endif

void BLEManagerImpl::HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param)
void BLEManagerImpl::HandleSlowBLEAdvertisementInterval(System::Layer * layer, void * param)
{
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
ChipLogProgress(DeviceLayer, "CHIPoBLE advertising mode changed to slow");
}

void BLEManagerImpl::HandleExtendedBLEAdvertisementInterval(System::Layer * layer, void * param)
{
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kExtendedAdvertising);
ChipLogProgress(DeviceLayer, "CHIPoBLE advertising mode changed to extended");
}

void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down
4 changes: 3 additions & 1 deletion src/platform/Zephyr/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
kAdvertisingRefreshNeeded =
0x0010, /**< The advertising state/configuration has changed, but the SoftDevice has yet to be updated. */
kChipoBleGattServiceRegister = 0x0020, /**< The system has currently CHIPoBLE GATT service registered. */
kExtendedAdvertisingEnabled = 0x0040, /**< The appliaction enabled extended advertising*/
};

struct ServiceData;
Expand Down Expand Up @@ -131,7 +132,8 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
static void HandleTXIndicated(bt_conn * conn, bt_gatt_indicate_params * attr, uint8_t err);
static void HandleConnect(bt_conn * conn, uint8_t err);
static void HandleDisconnect(bt_conn * conn, uint8_t reason);
static void HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param);
static void HandleSlowBLEAdvertisementInterval(System::Layer * layer, void * param);
static void HandleExtendedBLEAdvertisementInterval(System::Layer * layer, void * param);

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

Expand Down
8 changes: 8 additions & 0 deletions src/platform/Zephyr/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@
#ifdef CONFIG_CHIP_EXTENDED_DISCOVERY
#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1
#endif // CONFIG_CHIP_EXTENDED_DISCOVERY

#ifdef CONFIG_CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING 1
#endif

#ifdef CONFIG_CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_MINUTES
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS CONFIG_CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_MINUTES * 60
#endif
8 changes: 8 additions & 0 deletions src/platform/nrfconnect/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@
#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1
#endif // CONFIG_CHIP_EXTENDED_DISCOVERY

#ifdef CONFIG_CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING 1
#endif // CONFIG_CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING

#ifdef CONFIG_CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_MINUTES
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS CONFIG_CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_MINUTES * 60
#endif // CONFIG_CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_MINUTES

#ifndef CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH
#ifdef CONFIG_CHIP_FACTORY_DATA
// UID will be copied from the externally programmed factory data, so we don't know the actual length and we need to assume some max
Expand Down

0 comments on commit 9688a76

Please sign in to comment.