Skip to content

Commit

Permalink
Simplify adv name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Feb 1, 2024
1 parent 08ac1ce commit 797e4e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
25 changes: 11 additions & 14 deletions src/platform/Linux/bluez/BluezAdvertisement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ BluezLEAdvertisement1 * BluezAdvertisement::CreateLEAdvertisement()
GVariantBuilder serviceDataBuilder;
GVariantBuilder serviceUUIDsBuilder;
GAutoPtr<char> debugStr;
const char * localNamePtr;
char localName[32];

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

localNamePtr = mpAdvName;
if (localNamePtr == nullptr)
{
g_snprintf(localName, sizeof(localName), "%s%04x", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, getpid() & 0xffff);
localNamePtr = localName;
}

serviceData = g_variant_builder_end(&serviceDataBuilder);
serviceUUID = g_variant_builder_end(&serviceUUIDsBuilder);

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

// advertising name corresponding to the PID and object path, for debug purposes
bluez_leadvertisement1_set_local_name(adv, localNamePtr);
bluez_leadvertisement1_set_local_name(adv, mAdvName);
bluez_leadvertisement1_set_service_uuids(adv, serviceUUID);

// 0xffff means no appearance
Expand Down Expand Up @@ -148,10 +138,19 @@ CHIP_ERROR BluezAdvertisement::Init(const BluezEndpoint & aEndpoint, ChipAdvType
g_object_get(G_OBJECT(mpRoot), "object-path", &MakeUniquePointerReceiver(rootPath).Get(), nullptr);
mpAdvPath = g_strdup_printf("%s/advertising", rootPath.get());
mAdvType = aAdvType;
mpAdvName = g_strdup(aAdvName);
mpAdvUUID = g_strdup(aAdvUUID);
mAdvDurationMs = aAdvDurationMs;

if (aAdvName != nullptr)
{
g_snprintf(mAdvName, sizeof(mAdvName), "%s", aAdvName);
}
else
{
// Advertising name corresponding to the PID, for debug purposes.
g_snprintf(mAdvName, sizeof(mAdvName), "%s%04x", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, getpid() & 0xffff);
}

err = ConfigurationMgr().GetBLEDeviceIdentificationInfo(mDeviceIdInfo);
ReturnErrorOnFailure(err);

Expand Down Expand Up @@ -209,8 +208,6 @@ void BluezAdvertisement::Shutdown()

g_free(mpAdvPath);
mpAdvPath = nullptr;
g_free(mpAdvName);
mpAdvName = nullptr;
g_free(mpAdvUUID);
mpAdvUUID = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/bluez/BluezAdvertisement.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ class BluezAdvertisement

Ble::ChipBLEDeviceIdentificationInfo mDeviceIdInfo;
char * mpAdvPath = nullptr;
char * mpAdvName = nullptr;
char * mpAdvUUID = nullptr;
ChipAdvType mAdvType;
uint16_t mAdvDurationMs = 0;
char mAdvName[32] = "";
};

} // namespace Internal
Expand Down

0 comments on commit 797e4e9

Please sign in to comment.