Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linux] Extract BLE advertisement to separate class #30310

Merged
merged 11 commits into from
Nov 10, 2023
10 changes: 10 additions & 0 deletions scripts/helpers/platforms/iwyu.imp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
{ symbol: [ 'declval', private, '<utility>', public ] },
{ symbol: [ 'tm', private, '<sys/time.h>', public ] },

## GLib/GIO
{ include: [ '"gio/gdbusinterface.h"', private, '<gio/gio.h>', public ] },
{ include: [ '"gio/gdbusinterfaceskeleton.h"', private, '<gio/gio.h>', public ] },
{ include: [ '"gio/gdbusobject.h"', private, '<gio/gio.h>', public ] },
{ include: [ '"gio/gdbusobjectmanager.h"', private, '<gio/gio.h>', public ] },
{ include: [ '"gio/gdbusobjectmanagerclient.h"', private, '<gio/gio.h>', public ] },
{ include: [ '"gio/gdbusobjectmanagerserver.h"', private, '<gio/gio.h>', public ] },
{ include: [ '"gio/gdbusobjectskeleton.h"', private, '<gio/gio.h>', public ] },
{ include: [ '"gobject/gclosure.h"', private, '<glib-object.h>', public ] },

## ble/*
{ include: [ '"ble/CHIPBleServiceData.h"', private, '<ble/CHIPBleServiceData.h>', public ] },
{ include: [ '@"ble/Ble.*.h"', private, '<ble/Ble.h>', public ] },
Expand Down
67 changes: 18 additions & 49 deletions src/platform/Linux/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ CHIP_ERROR BLEManagerImpl::_Init()

void BLEManagerImpl::_Shutdown()
{
// ensure scan resources are cleared (e.g. timeout timers)
// Ensure scan resources are cleared (e.g. timeout timers).
mDeviceScanner.Shutdown();
// Stop advertising and free resources.
mBLEAdvertisement.Shutdown();
// Release BLE connection resources (unregister from BlueZ).
ShutdownBluezBleLayer(mpEndpoint);
mFlags.Clear(Flags::kBluezBLELayerInitialized);
Expand Down Expand Up @@ -184,32 +186,15 @@ uint16_t BLEManagerImpl::_NumConnections()

CHIP_ERROR BLEManagerImpl::ConfigureBle(uint32_t aAdapterId, bool aIsCentral)
{
CHIP_ERROR err = CHIP_NO_ERROR;
mBLEAdvConfig.mpBleName = mDeviceName;
mBLEAdvConfig.mAdapterId = aAdapterId;
mBLEAdvConfig.mMajor = 1;
mBLEAdvConfig.mMinor = 1;
mBLEAdvConfig.mVendorId = 1;
mBLEAdvConfig.mProductId = 1;
mBLEAdvConfig.mDeviceId = 1;
mBLEAdvConfig.mDuration = 2;
mBLEAdvConfig.mPairingStatus = 0;
mBLEAdvConfig.mType = ChipAdvType::BLUEZ_ADV_TYPE_UNDIRECTED_CONNECTABLE_SCANNABLE;
mBLEAdvConfig.mpAdvertisingUUID = "0xFFF6";

mAdapterId = aAdapterId;
mIsCentral = aIsCentral;

return err;
}

CHIP_ERROR BLEManagerImpl::StartBLEAdvertising()
{
return StartBluezAdv(mpEndpoint);
}
mBLEAdvType = ChipAdvType::BLUEZ_ADV_TYPE_UNDIRECTED_CONNECTABLE_SCANNABLE;
mBLEAdvDurationMs = 2;
mpBLEAdvUUID = "0xFFF6";

CHIP_ERROR BLEManagerImpl::StopBLEAdvertising()
{
return StopBluezAdv(mpEndpoint);
return CHIP_NO_ERROR;
}

void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
Expand Down Expand Up @@ -290,12 +275,6 @@ void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEv
HandleIndicationReceived(apEvent->Platform.BLEIndicationReceived.mConnection, &CHIP_BLE_SVC_ID, &ChipUUID_CHIPoBLEChar_TX,
PacketBufferHandle::Adopt(apEvent->Platform.BLEIndicationReceived.mData));
break;
case DeviceEventType::kPlatformLinuxBLEPeripheralAdvConfiguredComplete:
VerifyOrExit(apEvent->Platform.BLEPeripheralAdvConfiguredComplete.mIsSuccess, err = CHIP_ERROR_INCORRECT_STATE);
sInstance.mFlags.Set(Flags::kAdvertisingConfigured).Clear(Flags::kControlOpInProgress);
controlOpComplete = true;
ChipLogProgress(DeviceLayer, "CHIPoBLE advertising config complete");
break;
case DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete:
VerifyOrExit(apEvent->Platform.BLEPeripheralAdvStartComplete.mIsSuccess, err = CHIP_ERROR_INCORRECT_STATE);
sInstance.mFlags.Clear(Flags::kControlOpInProgress).Clear(Flags::kAdvertisingRefreshNeeded);
Expand Down Expand Up @@ -592,7 +571,7 @@ void BLEManagerImpl::DriveBLEState()
// Initializes the Bluez BLE layer if needed.
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kBluezBLELayerInitialized))
{
err = InitBluezBleLayer(mIsCentral, nullptr, mBLEAdvConfig, mpEndpoint);
err = InitBluezBleLayer(mAdapterId, mIsCentral, nullptr, mDeviceName, mpEndpoint);
SuccessOrExit(err);
mFlags.Set(Flags::kBluezBLELayerInitialized);
}
Expand All @@ -615,20 +594,19 @@ void BLEManagerImpl::DriveBLEState()
{
mFlags.Clear(Flags::kAdvertisingRefreshNeeded);

// Configure advertising data if it hasn't been done yet. This is an asynchronous step which
// must complete before advertising can be started. When that happens, this method will
// be called again, and execution will proceed to the code below.
// Configure advertising data if it hasn't been done yet.
if (!mFlags.Has(Flags::kAdvertisingConfigured))
{
err = BluezAdvertisementSetup(mpEndpoint);
ExitNow();
err = mBLEAdvertisement.Init(mpEndpoint, mBLEAdvType, mpBLEAdvUUID, mBLEAdvDurationMs);
SuccessOrExit(err);
mFlags.Set(Flags::kAdvertisingConfigured);
}

// Start advertising. This is also an asynchronous step.
err = StartBLEAdvertising();
// Start advertising. This is an asynchronous step. BLE manager will be notified of
// advertising start completion via a call to NotifyBLEPeripheralAdvStartComplete.
err = mBLEAdvertisement.Start();
SuccessOrExit(err);

sInstance.mFlags.Set(Flags::kAdvertising);
mFlags.Set(Flags::kControlOpInProgress);
ExitNow();
}
}
Expand All @@ -638,7 +616,7 @@ void BLEManagerImpl::DriveBLEState()
{
if (mFlags.Has(Flags::kAdvertising))
{
err = StopBLEAdvertising();
err = mBLEAdvertisement.Stop();
SuccessOrExit(err);
mFlags.Set(Flags::kControlOpInProgress);

Expand Down Expand Up @@ -751,15 +729,6 @@ void BLEManagerImpl::NotifyBLEPeripheralRegisterAppComplete(bool aIsSuccess, voi
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEPeripheralAdvConfiguredComplete(bool aIsSuccess, void * apAppstate)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvConfiguredComplete;
event.Platform.BLEPeripheralAdvConfiguredComplete.mIsSuccess = aIsSuccess;
event.Platform.BLEPeripheralAdvConfiguredComplete.mpAppstate = apAppstate;
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(bool aIsSuccess, void * apAppstate)
{
ChipDeviceEvent event;
Expand Down
32 changes: 11 additions & 21 deletions src/platform/Linux/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@

#pragma once

#include <cstdint>
#include <string>

#include <ble/BleLayer.h>
#include <platform/internal/BLEManager.h>

#include "bluez/BluezAdvertisement.h"
#include "bluez/BluezEndpoint.h"
#include "bluez/ChipDeviceScanner.h"
#include "bluez/Types.h"
Expand All @@ -38,21 +40,6 @@ namespace Internal {

void HandleIncomingBleConnection(Ble::BLEEndPoint * bleEP);

struct BLEAdvConfig
{
char * mpBleName;
uint32_t mAdapterId;
uint8_t mMajor;
uint8_t mMinor;
uint16_t mVendorId;
uint16_t mProductId;
uint64_t mDeviceId;
uint8_t mPairingStatus;
ChipAdvType mType;
uint16_t mDuration;
const char * mpAdvertisingUUID;
};

enum class BleScanState : uint8_t
{
kNotScanning,
Expand Down Expand Up @@ -106,7 +93,6 @@ class BLEManagerImpl final : public BLEManager,
static void HandleTXComplete(BLE_CONNECTION_OBJECT user_data);

static void NotifyBLEPeripheralRegisterAppComplete(bool aIsSuccess, void * apAppstate);
static void NotifyBLEPeripheralAdvConfiguredComplete(bool aIsSuccess, void * apAppstate);
static void NotifyBLEPeripheralAdvStartComplete(bool aIsSuccess, void * apAppstate);
static void NotifyBLEPeripheralAdvStopComplete(bool aIsSuccess, void * apAppstate);

Expand Down Expand Up @@ -187,9 +173,6 @@ class BLEManagerImpl final : public BLEManager,
kMaxAdvertisementDataSetSize = 31 // TODO: verify this
};

CHIP_ERROR StartBLEAdvertising();
CHIP_ERROR StopBLEAdvertising();

void DriveBLEState();
static void DriveBLEState(intptr_t arg);

Expand All @@ -198,13 +181,20 @@ class BLEManagerImpl final : public BLEManager,
void CleanScanConfig();

CHIPoBLEServiceMode mServiceMode;
BLEAdvConfig mBLEAdvConfig;
BLEScanConfig mBLEScanConfig;
BitFlags<Flags> mFlags;

uint32_t mAdapterId = 0;
char mDeviceName[kMaxDeviceNameLength + 1];
bool mIsCentral = false;
BluezEndpoint * mpEndpoint = nullptr;

BluezAdvertisement mBLEAdvertisement;
ChipAdvType mBLEAdvType = ChipAdvType::BLUEZ_ADV_TYPE_UNDIRECTED_CONNECTABLE_SCANNABLE;
uint16_t mBLEAdvDurationMs = 20;
const char * mpBLEAdvUUID = nullptr;

ChipDeviceScanner mDeviceScanner;
BLEScanConfig mBLEScanConfig;
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ static_library("Linux") {
"BlePlatformConfig.h",
"bluez/AdapterIterator.cpp",
"bluez/AdapterIterator.h",
"bluez/BluezAdvertisement.cpp",
"bluez/BluezAdvertisement.h",
"bluez/BluezConnection.cpp",
"bluez/BluezConnection.h",
"bluez/BluezEndpoint.cpp",
Expand Down
1 change: 0 additions & 1 deletion src/platform/Linux/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ enum InternalPlatformSpecificEventTypes
kPlatformLinuxBLEC1WriteEvent,
kPlatformLinuxBLEOutOfBuffersEvent,
kPlatformLinuxBLEPeripheralRegisterAppComplete,
kPlatformLinuxBLEPeripheralAdvConfiguredComplete,
kPlatformLinuxBLEPeripheralAdvStartComplete,
kPlatformLinuxBLEPeripheralAdvStopComplete
};
Expand Down
Loading
Loading