Skip to content

Commit e75f229

Browse files
authored
[Linux] Pass advertising name directly to BluezAdvertisement (project-chip#31824)
* [Linux] Pass advertising name directly to BluezAdvertisement * Simplify adv name handling
1 parent 2576913 commit e75f229

File tree

7 files changed

+24
-46
lines changed

7 files changed

+24
-46
lines changed

src/app/tests/suites/certification/Test_TC_DD_2_2.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ tests:
6363
[1657221603.350739][368108:368108] CHIP:SVR: https://dhrishi.github.io/connectedhomeip/qrcode.html?data=MT%3A-24J048N01KA0648G00
6464
[1657221603.350773][368108:368108] CHIP:SVR: Manual pairing code: [749701123365521327694]
6565
[1657221603.350846][368108:368108] CHIP:DMG: Endpoint 0, Cluster 0x0000_001D update version to 4dd11284
66-
[1657221603.353048][368108:368113] CHIP:DL: TRACE: Bluez mainloop starting Thread
67-
[1657221603.353167][368108:368111] CHIP:DL: TRACE: Bus acquired for name MATTER-3841
6866
disabled: true
6967

7068
- label:

src/app/tests/suites/certification/Test_TC_DD_3_11.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ tests:
8585
[1667888323.967841][3151:3151] CHIP:SVR: https://project-chip.github.io/connectedhomeip/qrcode.html?data=MT%3A-24J042C00KA0648G00
8686
[1667888323.967940][3151:3151] CHIP:SVR: Manual pairing code: [34970112332]
8787
[1667888323.968031][3151:3151] CHIP:DMG: Endpoint 0, Cluster 0x0000_001D update version to ea7ea7b8
88-
[1667888323.981832][3151:3153] CHIP:DL: TRACE: Bus acquired for name MATTER-3840
8988
disabled: true
9089

9190
- label: "Step 1b: Scan the QR code from the previous step using the DUT."

src/platform/Linux/BLEManagerImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ void BLEManagerImpl::DriveBLEState()
571571
// Initializes the Bluez BLE layer if needed.
572572
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kBluezBLELayerInitialized))
573573
{
574-
SuccessOrExit(err = mEndpoint.Init(mAdapterId, mIsCentral, nullptr, mDeviceName));
574+
SuccessOrExit(err = mEndpoint.Init(mAdapterId, mIsCentral, nullptr));
575575
mFlags.Set(Flags::kBluezBLELayerInitialized);
576576
}
577577

@@ -595,7 +595,7 @@ void BLEManagerImpl::DriveBLEState()
595595
// Configure advertising data if it hasn't been done yet.
596596
if (!mFlags.Has(Flags::kAdvertisingConfigured))
597597
{
598-
SuccessOrExit(err = mBLEAdvertisement.Init(mEndpoint, mBLEAdvType, mpBLEAdvUUID, mBLEAdvDurationMs));
598+
SuccessOrExit(err = mBLEAdvertisement.Init(mEndpoint, mBLEAdvType, mpBLEAdvUUID, mBLEAdvDurationMs, mDeviceName));
599599
mFlags.Set(Flags::kAdvertisingConfigured);
600600
}
601601

src/platform/Linux/bluez/BluezAdvertisement.cpp

+14-17
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ BluezLEAdvertisement1 * BluezAdvertisement::CreateLEAdvertisement()
4747
GVariantBuilder serviceDataBuilder;
4848
GVariantBuilder serviceUUIDsBuilder;
4949
GAutoPtr<char> debugStr;
50-
const char * localNamePtr;
51-
char localName[32];
5250

5351
ChipLogDetail(DeviceLayer, "Create BLE adv object at %s", mpAdvPath);
5452
object = bluez_object_skeleton_new(mpAdvPath);
@@ -62,13 +60,6 @@ BluezLEAdvertisement1 * BluezAdvertisement::CreateLEAdvertisement()
6260
g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, &mDeviceIdInfo, sizeof(mDeviceIdInfo), sizeof(uint8_t)));
6361
g_variant_builder_add(&serviceUUIDsBuilder, "s", mpAdvUUID);
6462

65-
localNamePtr = mpAdapterName;
66-
if (localNamePtr == nullptr)
67-
{
68-
g_snprintf(localName, sizeof(localName), "%s%04x", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, getpid() & 0xffff);
69-
localNamePtr = localName;
70-
}
71-
7263
serviceData = g_variant_builder_end(&serviceDataBuilder);
7364
serviceUUID = g_variant_builder_end(&serviceUUIDsBuilder);
7465

@@ -88,8 +79,7 @@ BluezLEAdvertisement1 * BluezAdvertisement::CreateLEAdvertisement()
8879
if (mAdvType & BLUEZ_ADV_TYPE_SCANNABLE)
8980
bluez_leadvertisement1_set_discoverable_timeout(adv, UINT16_MAX);
9081

91-
// advertising name corresponding to the PID and object path, for debug purposes
92-
bluez_leadvertisement1_set_local_name(adv, localNamePtr);
82+
bluez_leadvertisement1_set_local_name(adv, mAdvName);
9383
bluez_leadvertisement1_set_service_uuids(adv, serviceUUID);
9484

9585
// 0xffff means no appearance
@@ -134,24 +124,33 @@ CHIP_ERROR BluezAdvertisement::InitImpl()
134124
}
135125

136126
CHIP_ERROR BluezAdvertisement::Init(const BluezEndpoint & aEndpoint, ChipAdvType aAdvType, const char * aAdvUUID,
137-
uint32_t aAdvDurationMs)
127+
uint32_t aAdvDurationMs, const char * aAdvName)
138128
{
139129
GAutoPtr<char> rootPath;
140130
CHIP_ERROR err;
141131

142132
VerifyOrExit(mpAdv == nullptr, err = CHIP_ERROR_INCORRECT_STATE;
143133
ChipLogError(DeviceLayer, "FAIL: BLE advertisement already initialized in %s", __func__));
144134

145-
mpRoot = reinterpret_cast<GDBusObjectManagerServer *>(g_object_ref(aEndpoint.GetGattApplicationObjectManager()));
146-
mpAdapter = reinterpret_cast<BluezAdapter1 *>(g_object_ref(aEndpoint.GetAdapter()));
147-
mpAdapterName = g_strdup(aEndpoint.GetAdapterName());
135+
mpRoot = reinterpret_cast<GDBusObjectManagerServer *>(g_object_ref(aEndpoint.GetGattApplicationObjectManager()));
136+
mpAdapter = reinterpret_cast<BluezAdapter1 *>(g_object_ref(aEndpoint.GetAdapter()));
148137

149138
g_object_get(G_OBJECT(mpRoot), "object-path", &MakeUniquePointerReceiver(rootPath).Get(), nullptr);
150139
mpAdvPath = g_strdup_printf("%s/advertising", rootPath.get());
151140
mAdvType = aAdvType;
152141
mpAdvUUID = g_strdup(aAdvUUID);
153142
mAdvDurationMs = aAdvDurationMs;
154143

144+
if (aAdvName != nullptr)
145+
{
146+
g_snprintf(mAdvName, sizeof(mAdvName), "%s", aAdvName);
147+
}
148+
else
149+
{
150+
// Advertising name corresponding to the PID, for debug purposes.
151+
g_snprintf(mAdvName, sizeof(mAdvName), "%s%04x", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, getpid() & 0xffff);
152+
}
153+
155154
err = ConfigurationMgr().GetBLEDeviceIdentificationInfo(mDeviceIdInfo);
156155
ReturnErrorOnFailure(err);
157156

@@ -209,8 +208,6 @@ void BluezAdvertisement::Shutdown()
209208

210209
g_free(mpAdvPath);
211210
mpAdvPath = nullptr;
212-
g_free(mpAdapterName);
213-
mpAdapterName = nullptr;
214211
g_free(mpAdvUUID);
215212
mpAdvUUID = nullptr;
216213

src/platform/Linux/bluez/BluezAdvertisement.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class BluezAdvertisement
4141
BluezAdvertisement() = default;
4242
~BluezAdvertisement() { Shutdown(); }
4343

44-
CHIP_ERROR Init(const BluezEndpoint & aEndpoint, ChipAdvType aAdvType, const char * aAdvUUID, uint32_t aAdvDurationMs);
44+
CHIP_ERROR Init(const BluezEndpoint & aEndpoint, ChipAdvType aAdvType, const char * aAdvUUID, uint32_t aAdvDurationMs,
45+
const char * aAdvName);
4546
void Shutdown();
4647

4748
/// Start BLE advertising.
@@ -77,11 +78,11 @@ class BluezAdvertisement
7778
bool mIsAdvertising = false;
7879

7980
Ble::ChipBLEDeviceIdentificationInfo mDeviceIdInfo;
80-
char * mpAdvPath = nullptr;
81-
char * mpAdapterName = nullptr;
82-
char * mpAdvUUID = nullptr;
81+
char * mpAdvPath = nullptr;
82+
char * mpAdvUUID = nullptr;
8383
ChipAdvType mAdvType;
8484
uint16_t mAdvDurationMs = 0;
85+
char mAdvName[32] = "";
8586
};
8687

8788
} // namespace Internal

src/platform/Linux/bluez/BluezEndpoint.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -617,12 +617,6 @@ CHIP_ERROR BluezEndpoint::StartupEndpointBindings()
617617
VerifyOrReturnError(conn != nullptr, CHIP_ERROR_INTERNAL,
618618
ChipLogError(DeviceLayer, "FAIL: get bus sync in %s, error: %s", __func__, err->message));
619619

620-
if (mpAdapterName != nullptr)
621-
mpOwningName = g_strdup_printf("%s", mpAdapterName);
622-
else
623-
mpOwningName = g_strdup_printf("C-%04x", getpid() & 0xffff);
624-
ChipLogDetail(DeviceLayer, "TRACE: Bus acquired for name %s", mpOwningName);
625-
626620
SetupGattServer(conn.get());
627621

628622
mpObjMgr = g_dbus_object_manager_client_new_sync(
@@ -662,7 +656,7 @@ CHIP_ERROR BluezEndpoint::RegisterGattApplication()
662656
return err;
663657
}
664658

665-
CHIP_ERROR BluezEndpoint::Init(uint32_t aAdapterId, bool aIsCentral, const char * apBleAddr, const char * apBleName)
659+
CHIP_ERROR BluezEndpoint::Init(uint32_t aAdapterId, bool aIsCentral, const char * apBleAddr)
666660
{
667661
CHIP_ERROR err;
668662

@@ -672,11 +666,7 @@ CHIP_ERROR BluezEndpoint::Init(uint32_t aAdapterId, bool aIsCentral, const char
672666
if (apBleAddr != nullptr)
673667
mpAdapterAddr = g_strdup(apBleAddr);
674668

675-
if (!aIsCentral)
676-
{
677-
mpAdapterName = g_strdup(apBleName);
678-
}
679-
else
669+
if (aIsCentral)
680670
{
681671
mpConnectCancellable = g_cancellable_new();
682672
}
@@ -723,8 +713,6 @@ void BluezEndpoint::Shutdown()
723713
},
724714
this);
725715

726-
g_free(mpOwningName);
727-
g_free(mpAdapterName);
728716
g_free(mpAdapterAddr);
729717
g_free(mpRootPath);
730718
g_free(mpServicePath);

src/platform/Linux/bluez/BluezEndpoint.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ class BluezEndpoint
6969
BluezEndpoint() = default;
7070
~BluezEndpoint() = default;
7171

72-
CHIP_ERROR Init(uint32_t aAdapterId, bool aIsCentral, const char * apBleAddr, const char * apBleName);
72+
CHIP_ERROR Init(uint32_t aAdapterId, bool aIsCentral, const char * apBleAddr);
7373
void Shutdown();
7474

7575
BluezAdapter1 * GetAdapter() const { return mpAdapter; }
76-
const char * GetAdapterName() const { return mpAdapterName; }
7776

7877
CHIP_ERROR RegisterGattApplication();
7978
GDBusObjectManagerServer * GetGattApplicationObjectManager() const { return mpRoot; }
@@ -127,12 +126,8 @@ class BluezEndpoint
127126
bool mIsCentral = false;
128127
bool mIsInitialized = false;
129128

130-
// Bus owning name
131-
char * mpOwningName = nullptr;
132-
133129
// Adapter properties
134130
uint32_t mAdapterId = 0;
135-
char * mpAdapterName = nullptr;
136131
char * mpAdapterAddr = nullptr;
137132

138133
// Paths for objects published by this service

0 commit comments

Comments
 (0)