Skip to content

Commit

Permalink
[nrf noup] BLE Extended Advertisment 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 <[email protected]>
  • Loading branch information
LipinskiPNordicSemi committed Feb 13, 2024
1 parent 854bbf3 commit 0a7f833
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/include/platform/CHIPDeviceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,11 @@ static_assert(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN < CHIP_DEVICE_
* Time in seconds that a factory new device will advertise commissionable node discovery.
*/
#ifndef CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING == 0
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS (15 * 60)
#else
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS (60 * 60 * 48)
#endif
#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
78 changes: 64 additions & 14 deletions src/platform/Zephyr/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace {
constexpr uint32_t kAdvertisingOptions = BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME;
constexpr uint8_t kAdvertisingFlags = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR;

constexpr uint8_t kLowestRequestPriority = 7;

const bt_uuid_128 UUID128_CHIPoBLEChar_RX =
BT_UUID_INIT_128(0x11, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18);
const bt_uuid_128 UUID128_CHIPoBLEChar_TX =
Expand Down Expand Up @@ -161,7 +163,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 @@ -245,7 +247,11 @@ void BLEManagerImpl::DriveBLEState()
}
else
{
if (mFlags.Has(Flags::kAdvertising))
if (mFlags.Has(Flags::kAdvertising)
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
&& mFlags.Has(Flags::kExtendedAdvertisingEnabled)
#endif
)
{
err = StopAdvertising();
SuccessOrExit(err);
Expand Down Expand Up @@ -293,18 +299,45 @@ 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;
}
else
{
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
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;
}
else
{
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
}
#else
mAdvertisingRequest.minInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
mAdvertisingRequest.maxInterval = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
#endif
}
mAdvertisingRequest.advertisingData = Span<bt_data>(advertisingData);
mAdvertisingRequest.scanResponseData = nameSize ? Span<bt_data>(scanResponseData) : Span<bt_data>{};

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

if (mFlags.Has(Flags::kFastAdvertisingEnabled))
{
// Start timer to change advertising interval.
// Start timer to change advertising fast to slow interval.
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 slow to extended interval.
DeviceLayer::SystemLayer().StartTimer(
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS),
HandleExtendedBLEAdvertisementInterval, this);
#endif
}
}

Expand Down Expand Up @@ -394,7 +434,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 Down Expand Up @@ -423,6 +464,9 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
case BLEAdvertisingMode::kSlowAdvertising:
mFlags.Set(Flags::kFastAdvertisingEnabled, false);
break;
case BLEAdvertisingMode::kExtendedAdvertising:
mFlags.Set(Flags::kExtendedAdvertisingEnabled, true);
break;
default:
return CHIP_ERROR_INVALID_ARGUMENT;
}
Expand Down Expand Up @@ -607,12 +651,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

0 comments on commit 0a7f833

Please sign in to comment.