Skip to content

Commit 175218b

Browse files
authored
ESP32: Rotating device ID for BLE transport (project-chip#13206)
* ESP32: Rotating device ID for BLE transport * Remove the ifdefs and fix the Zephyr static assert Also, set the default value of chip_enable_additional_data_advertising to false in src/platform/BUILD.gn. If this flag is set then Linux and ESP32 set the Additional Data flag and displayes the C3 characteristic. * Changed kServiceDataLenSize to kServiceDataTypeSize Added chip_enable_additional_data_advertising = true to tv-app and tv-cast-app Removed the unused characteristic definitions
1 parent b673e41 commit 175218b

File tree

11 files changed

+141
-18
lines changed

11 files changed

+141
-18
lines changed

config/esp32/components/chip/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ if (CONFIG_ENABLE_OTA_REQUESTOR)
107107
chip_gn_arg_append("chip_enable_ota_requestor" "true")
108108
endif()
109109

110+
if (CONFIG_ENABLE_ROTATING_DEVICE_ID)
111+
chip_gn_arg_append("chip_enable_additional_data_advertising" "true")
112+
chip_gn_arg_append("chip_enable_rotating_device_id" "true")
113+
endif()
114+
110115
set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in")
111116
file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}")
112117

config/esp32/components/chip/Kconfig

+5
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,11 @@ menu "CHIP Device Layer"
706706
Setting this to y will cause the commissioner to send commissioning commands to the
707707
various clusters after establishing a PASE session.
708708

709+
config ENABLE_ROTATING_DEVICE_ID
710+
depends on ENABLE_CHIPOBLE
711+
bool "Enable Rotating Device Identifier Support"
712+
default n
713+
709714
endmenu
710715

711716

examples/tv-app/linux/args.gni

+2
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]
2626

2727
chip_build_libshell = true
2828

29+
chip_enable_additional_data_advertising = true
30+
2931
chip_enable_rotating_device_id = true

examples/tv-casting-app/linux/args.gni

+2
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]
2626

2727
chip_build_libshell = true
2828

29+
chip_enable_additional_data_advertising = true
30+
2931
chip_enable_rotating_device_id = true

src/ble/CHIPBleServiceData.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ enum chipBLEServiceDataType
4545
*/
4646
struct ChipBLEDeviceIdentificationInfo
4747
{
48-
constexpr static uint16_t kDiscriminatorMask = 0xfff;
48+
constexpr static uint16_t kDiscriminatorMask = 0xfff;
49+
constexpr static uint8_t kAdditionalDataFlagMask = 0x1;
4950

5051
uint8_t OpCode;
5152
uint8_t DeviceDiscriminator[2];
5253
uint8_t DeviceVendorId[2];
5354
uint8_t DeviceProductId[2];
55+
uint8_t AdditionalDataFlag;
5456

5557
void Init() { memset(this, 0, sizeof(*this)); }
5658

@@ -74,6 +76,20 @@ struct ChipBLEDeviceIdentificationInfo
7476
deviceDiscriminator |= static_cast<uint16_t>(DeviceDiscriminator[1] << 8u & ~kDiscriminatorMask);
7577
chip::Encoding::LittleEndian::Put16(DeviceDiscriminator, deviceDiscriminator);
7678
}
79+
80+
uint8_t GetAdditionalDataFlag() const { return (AdditionalDataFlag & kAdditionalDataFlagMask); }
81+
82+
void SetAdditionalDataFlag(bool flag)
83+
{
84+
if (flag)
85+
{
86+
AdditionalDataFlag |= kAdditionalDataFlagMask;
87+
}
88+
else
89+
{
90+
AdditionalDataFlag &= static_cast<uint8_t>(~kAdditionalDataFlagMask);
91+
}
92+
}
7793
} __attribute__((packed));
7894

7995
} /* namespace Ble */

src/platform/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ if (chip_device_platform != "none") {
5454
chip_bypass_rendezvous = false
5555

5656
# Enable including the additional data in the advertisement packets
57-
chip_enable_additional_data_advertising = true
57+
chip_enable_additional_data_advertising = false
5858

5959
# Enable adding optional rotating device id to the additional data.
6060
chip_enable_rotating_device_id = false

src/platform/ESP32/BLEManagerImpl.h

+8
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ class BLEManagerImpl final : public BLEManager,
189189
uint16_t mServiceAttrHandle;
190190
uint16_t mRXCharAttrHandle;
191191
uint16_t mTXCharAttrHandle;
192+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
193+
uint16_t mC3CharAttrHandle;
194+
#endif
192195
uint16_t mTXCharCCCDAttrHandle;
193196
BitFlags<Flags> mFlags;
194197
char mDeviceName[kMaxDeviceNameLength + 1];
@@ -246,6 +249,11 @@ class BLEManagerImpl final : public BLEManager,
246249
static int ble_svr_gap_event(struct ble_gap_event * event, void * arg);
247250

248251
static int gatt_svr_chr_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt, void * arg);
252+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
253+
static int gatt_svr_chr_access_additional_data(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt,
254+
void * arg);
255+
void HandleC3CharRead(struct ble_gatt_char_context * param);
256+
#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */
249257
#endif
250258

251259
static void DriveBLEState(intptr_t arg);

src/platform/ESP32/BUILD.gn

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ static_library("ESP32") {
4848
"nimble/BLEManagerImpl.cpp",
4949
]
5050

51-
deps = [ "${chip_root}/src/lib/dnssd:platform_header" ]
51+
deps = [
52+
"${chip_root}/src/lib/dnssd:platform_header",
53+
"${chip_root}/src/setup_payload",
54+
]
5255

5356
public_deps = [
5457
"${chip_root}/src/crypto",

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+91-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <lib/support/CodeUtils.h>
3535
#include <lib/support/logging/CHIPLogging.h>
3636
#include <platform/internal/BLEManager.h>
37+
#include <setup_payload/AdditionalDataPayloadGenerator.h>
3738
#include <system/SystemTimer.h>
3839

3940
#include "esp_log.h"
@@ -67,29 +68,26 @@ struct ESP32ChipServiceData
6768
ChipBLEDeviceIdentificationInfo DeviceIdInfo;
6869
};
6970

70-
const ble_uuid128_t UUID_CHIPoBLEService = {
71-
BLE_UUID_TYPE_128, { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 }
72-
};
7371
const ble_uuid16_t ShortUUID_CHIPoBLEService = { BLE_UUID_TYPE_16, 0xFFF6 };
7472

7573
const ble_uuid128_t UUID128_CHIPoBLEChar_RX = {
7674
BLE_UUID_TYPE_128, { 0x11, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18 }
7775
};
7876
const ChipBleUUID chipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F,
7977
0x9D, 0x11 } };
80-
const ble_uuid128_t UUID_CHIPoBLEChar_RX = {
81-
{ BLE_UUID_TYPE_128 }, { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F, 0x9D, 0x11 }
82-
};
8378

84-
const ble_uuid128_t UUID128_CHIPoBLEChar_TX = {
85-
BLE_UUID_TYPE_128, { 0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18 }
86-
};
8779
const ChipBleUUID chipUUID_CHIPoBLEChar_TX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F,
8880
0x9D, 0x12 } };
8981
const ble_uuid128_t UUID_CHIPoBLEChar_TX = {
9082
{ BLE_UUID_TYPE_128 }, { 0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18 }
9183
};
9284

85+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
86+
const ble_uuid128_t UUID_CHIPoBLEChar_C3 = {
87+
{ BLE_UUID_TYPE_128 }, { 0x04, 0x8F, 0x21, 0x83, 0x8A, 0x74, 0x7D, 0xB8, 0xF2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64 }
88+
};
89+
#endif
90+
9391
SemaphoreHandle_t semaphoreHandle = NULL;
9492

9593
} // unnamed namespace
@@ -115,6 +113,14 @@ const struct ble_gatt_svc_def BLEManagerImpl::CHIPoBLEGATTAttrs[] = {
115113
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
116114
.val_handle = &sInstance.mTXCharCCCDAttrHandle,
117115
},
116+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
117+
{
118+
.uuid = (ble_uuid_t *) (&UUID_CHIPoBLEChar_C3),
119+
.access_cb = gatt_svr_chr_access_additional_data,
120+
.flags = BLE_GATT_CHR_F_READ,
121+
.val_handle = &sInstance.mC3CharAttrHandle,
122+
},
123+
#endif
118124
{
119125
0, /* No more characteristics in this service */
120126
},
@@ -133,7 +139,10 @@ CHIP_ERROR BLEManagerImpl::_Init()
133139
err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer());
134140
SuccessOrExit(err);
135141

136-
mRXCharAttrHandle = 0;
142+
mRXCharAttrHandle = 0;
143+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
144+
mC3CharAttrHandle = 0;
145+
#endif
137146
mTXCharCCCDAttrHandle = 0;
138147
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
139148
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
@@ -683,6 +692,10 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
683692
uint8_t advData[MAX_ADV_DATA_LEN];
684693
uint8_t index = 0;
685694

695+
constexpr uint8_t kServiceDataTypeSize = 1;
696+
697+
chip::Ble::ChipBLEDeviceIdentificationInfo deviceIdInfo;
698+
686699
// If a custom device name has not been specified, generate a CHIP-standard name based on the
687700
// bottom digits of the Chip device id.
688701
uint16_t discriminator;
@@ -706,19 +719,22 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
706719
advData[index++] = 0x02; // length
707720
advData[index++] = CHIP_ADV_DATA_TYPE_FLAGS; // AD type : flags
708721
advData[index++] = CHIP_ADV_DATA_FLAGS; // AD value
709-
advData[index++] = 0x0A; // length
722+
advData[index++] = kServiceDataTypeSize + sizeof(ESP32ChipServiceData); // length
710723
advData[index++] = CHIP_ADV_DATA_TYPE_SERVICE_DATA; // AD type: (Service Data - 16-bit UUID)
711724
advData[index++] = static_cast<uint8_t>(ShortUUID_CHIPoBLEService.value & 0xFF); // AD value
712725
advData[index++] = static_cast<uint8_t>((ShortUUID_CHIPoBLEService.value >> 8) & 0xFF); // AD value
713726

714-
chip::Ble::ChipBLEDeviceIdentificationInfo deviceIdInfo;
715727
err = ConfigurationMgr().GetBLEDeviceIdentificationInfo(deviceIdInfo);
716728
if (err != CHIP_NO_ERROR)
717729
{
718730
ChipLogError(DeviceLayer, "GetBLEDeviceIdentificationInfo(): %s", ErrorStr(err));
719731
ExitNow();
720732
}
721733

734+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
735+
deviceIdInfo.SetAdditionalDataFlag(true);
736+
#endif
737+
722738
VerifyOrExit(index + sizeof(deviceIdInfo) <= sizeof(advData), err = CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG);
723739
memcpy(&advData[index], &deviceIdInfo, sizeof(deviceIdInfo));
724740
index = static_cast<uint8_t>(index + sizeof(deviceIdInfo));
@@ -1046,6 +1062,69 @@ int BLEManagerImpl::ble_svr_gap_event(struct ble_gap_event * event, void * arg)
10461062
return err.AsInteger();
10471063
}
10481064

1065+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
1066+
void BLEManagerImpl::HandleC3CharRead(struct ble_gatt_char_context * param)
1067+
{
1068+
CHIP_ERROR err = CHIP_NO_ERROR;
1069+
chip::System::PacketBufferHandle bufferHandle;
1070+
1071+
char serialNumber[ConfigurationManager::kMaxSerialNumberLength + 1];
1072+
uint16_t lifetimeCounter = 0;
1073+
BitFlags<AdditionalDataFields> additionalDataFields;
1074+
1075+
#if CHIP_ENABLE_ROTATING_DEVICE_ID
1076+
err = ConfigurationMgr().GetSerialNumber(serialNumber, sizeof(serialNumber));
1077+
SuccessOrExit(err);
1078+
err = ConfigurationMgr().GetLifetimeCounter(lifetimeCounter);
1079+
SuccessOrExit(err);
1080+
1081+
additionalDataFields.Set(AdditionalDataFields::RotatingDeviceId);
1082+
#endif /* CHIP_ENABLE_ROTATING_DEVICE_ID */
1083+
1084+
err = AdditionalDataPayloadGenerator().generateAdditionalDataPayload(lifetimeCounter, serialNumber, strlen(serialNumber),
1085+
bufferHandle, additionalDataFields);
1086+
SuccessOrExit(err);
1087+
1088+
os_mbuf_append(param->ctxt->om, bufferHandle->Start(), bufferHandle->DataLength());
1089+
1090+
exit:
1091+
if (err != CHIP_NO_ERROR)
1092+
{
1093+
ChipLogError(DeviceLayer, "Failed to generate TLV encoded Additional Data (%s)", __func__);
1094+
}
1095+
return;
1096+
}
1097+
1098+
int BLEManagerImpl::gatt_svr_chr_access_additional_data(uint16_t conn_handle, uint16_t attr_handle,
1099+
struct ble_gatt_access_ctxt * ctxt, void * arg)
1100+
{
1101+
struct ble_gatt_char_context param;
1102+
int err = 0;
1103+
1104+
memset(&param, 0, sizeof(struct ble_gatt_char_context));
1105+
1106+
switch (ctxt->op)
1107+
{
1108+
case BLE_GATT_ACCESS_OP_READ_CHR:
1109+
1110+
param.conn_handle = conn_handle;
1111+
param.attr_handle = attr_handle;
1112+
param.ctxt = ctxt;
1113+
param.arg = arg;
1114+
sInstance.HandleC3CharRead(&param);
1115+
break;
1116+
1117+
default:
1118+
err = BLE_ATT_ERR_UNLIKELY;
1119+
break;
1120+
}
1121+
1122+
PlatformMgr().ScheduleWork(DriveBLEState, 0);
1123+
1124+
return err;
1125+
}
1126+
#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */
1127+
10491128
int BLEManagerImpl::gatt_svr_chr_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt, void * arg)
10501129
{
10511130
struct ble_gatt_char_context param;

src/platform/Linux/bluez/Helper.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@
4545

4646
/**
4747
* @file
48-
* Provides Bluez dbus implementatioon for BLE
48+
* Provides Bluez dbus implementation for BLE
4949
*/
50-
5150
#include <ble/BleUUID.h>
5251
#include <ble/CHIPBleServiceData.h>
5352
#include <lib/support/BitFlags.h>
@@ -1520,6 +1519,10 @@ CHIP_ERROR ConfigureBluezAdv(BLEAdvConfig & aBleAdvConfig, BluezEndpoint * apEnd
15201519
err = ConfigurationMgr().GetBLEDeviceIdentificationInfo(apEndpoint->mDeviceIdInfo);
15211520
SuccessOrExit(err);
15221521

1522+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
1523+
apEndpoint->mDeviceIdInfo.SetAdditionalDataFlag(true);
1524+
#endif
1525+
15231526
exit:
15241527
if (nullptr != msg)
15251528
{

src/platform/Zephyr/BLEManagerImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
269269
}
270270

271271
// Initialize service data
272-
static_assert(sizeof(serviceData) == 9, "Size of BLE advertisement data changed! Was that intentional?");
272+
static_assert(sizeof(serviceData) == 10, "Size of BLE advertisement data changed! Was that intentional?");
273273
chip::Encoding::LittleEndian::Put16(serviceData.uuid, UUID16_CHIPoBLEService.val);
274274
ReturnErrorOnFailure(ConfigurationMgr().GetBLEDeviceIdentificationInfo(serviceData.deviceIdInfo));
275275

0 commit comments

Comments
 (0)