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

Restyle ESP32: Rotating device ID for BLE transport #13207

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ if (CONFIG_ENABLE_OTA_REQUESTOR)
chip_gn_arg_append("chip_enable_ota_requestor" "true")
endif()

if (CONFIG_ENABLE_EXTENDED_DATA)
chip_gn_arg_append("chip_enable_additional_data_advertising" "true")
if (CONFIG_ENABLE_ROTATING_DEVICE_ID)
chip_gn_arg_append("chip_enable_rotating_device_id" "true")
endif()
endif()

set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in")
file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}")

Expand Down
10 changes: 10 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,16 @@ menu "CHIP Device Layer"
Setting this to y will cause the commissioner to send commissioning commands to the
various clusters after establishing a PASE session.

config ENABLE_EXTENDED_DATA
depends on ENABLE_CHIPOBLE
bool "Include extended data during commissioning"
default n

config ENABLE_ROTATING_DEVICE_ID
depends on ENABLE_EXTENDED_DATA
bool "Enable Rotating Device Identifier Support"
default n

endmenu


Expand Down
2 changes: 1 addition & 1 deletion src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi
#if CHIP_ENABLE_ROTATING_DEVICE_ID
char rotatingDeviceIdHexBuffer[RotatingDeviceId::kHexMaxLength];
ReturnErrorOnFailure(GenerateRotatingDeviceId(rotatingDeviceIdHexBuffer, ArraySize(rotatingDeviceIdHexBuffer)));
advertiseParameters.SetRotatingId(chip::Optional<const char *>::Value(rotatingDeviceIdHexBuffer));
advertiseParameters.SetRotatingDeviceId(chip::Optional<const char *>::Value(rotatingDeviceIdHexBuffer));
#endif

advertiseParameters.SetMRPConfig(gDefaultMRPConfig).SetTcpSupported(Optional<bool>(INET_CONFIG_ENABLE_TCP_ENDPOINT));
Expand Down
22 changes: 22 additions & 0 deletions src/ble/CHIPBleServiceData.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ enum chipBLEServiceDataType
struct ChipBLEDeviceIdentificationInfo
{
constexpr static uint16_t kDiscriminatorMask = 0xfff;
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
constexpr static uint8_t kAdditionalDataFlagMask = 0x1;
#endif

uint8_t OpCode;
uint8_t DeviceDiscriminator[2];
uint8_t DeviceVendorId[2];
uint8_t DeviceProductId[2];
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
uint8_t AdditionalDataFlag;
#endif

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

Expand All @@ -74,6 +80,22 @@ struct ChipBLEDeviceIdentificationInfo
deviceDiscriminator |= static_cast<uint16_t>(DeviceDiscriminator[1] << 8u & ~kDiscriminatorMask);
chip::Encoding::LittleEndian::Put16(DeviceDiscriminator, deviceDiscriminator);
}

#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
uint8_t GetAdditionalDataFlag() const { return (AdditionalDataFlag & kAdditionalDataFlagMask); }

void SetAdditionalDataFlag(bool flag)
{
if (flag)
{
AdditionalDataFlag |= kAdditionalDataFlagMask;
}
else
{
AdditionalDataFlag &= static_cast<uint8_t>(~kAdditionalDataFlagMask);
}
}
#endif
} __attribute__((packed));

} /* namespace Ble */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ GenericConfigurationManagerImpl<ConfigClass>::GetBLEDeviceIdentificationInfo(Ble
SuccessOrExit(err);
deviceIdInfo.SetDeviceDiscriminator(discriminator);

#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
deviceIdInfo.SetAdditionalDataFlag(true);
#endif

exit:
return err;
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if (chip_device_platform != "none") {
chip_bypass_rendezvous = false

# Enable including the additional data in the advertisement packets
chip_enable_additional_data_advertising = true
chip_enable_additional_data_advertising = false

# Enable adding optional rotating device id to the additional data.
chip_enable_rotating_device_id = false
Expand Down
8 changes: 8 additions & 0 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ class BLEManagerImpl final : public BLEManager,
uint16_t mServiceAttrHandle;
uint16_t mRXCharAttrHandle;
uint16_t mTXCharAttrHandle;
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
uint16_t mC3CharAttrHandle;
#endif
uint16_t mTXCharCCCDAttrHandle;
BitFlags<Flags> mFlags;
char mDeviceName[kMaxDeviceNameLength + 1];
Expand Down Expand Up @@ -246,6 +249,11 @@ class BLEManagerImpl final : public BLEManager,
static int ble_svr_gap_event(struct ble_gap_event * event, void * arg);

static int gatt_svr_chr_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt, void * arg);
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
static int gatt_svr_chr_access_additional_data(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt,
void * arg);
void HandleC3CharRead(struct ble_gatt_char_context * param);
#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */
#endif

static void DriveBLEState(intptr_t arg);
Expand Down
5 changes: 4 additions & 1 deletion src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ static_library("ESP32") {
"nimble/BLEManagerImpl.cpp",
]

deps = [ "${chip_root}/src/lib/dnssd:platform_header" ]
deps = [
"${chip_root}/src/lib/dnssd:platform_header",
"${chip_root}/src/setup_payload",
]

public_deps = [
"${chip_root}/src/crypto",
Expand Down
96 changes: 93 additions & 3 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/internal/BLEManager.h>
#include <setup_payload/AdditionalDataPayloadGenerator.h>
#include <system/SystemTimer.h>

#include "esp_log.h"
Expand Down Expand Up @@ -90,6 +91,17 @@ const ble_uuid128_t UUID_CHIPoBLEChar_TX = {
{ BLE_UUID_TYPE_128 }, { 0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18 }
};

#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
const ble_uuid128_t UUID128_CHIPoBLEChar_C3 = {
BLE_UUID_TYPE_128, { 0x04, 0x8F, 0x21, 0x83, 0x8A, 0x74, 0x7D, 0xB8, 0xF2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64 }
};
const ChipBleUUID chipUUID_CHIPoBLEChar_C3 = { { 0x64, 0x63, 0x02, 0x38, 0x87, 0x72, 0x45, 0xF2, 0xB8, 0x7D, 0x74, 0x8A, 0x83, 0x21,
0x8F, 0x04 } };
const ble_uuid128_t UUID_CHIPoBLEChar_C3 = {
{ BLE_UUID_TYPE_128 }, { 0x04, 0x8F, 0x21, 0x83, 0x8A, 0x74, 0x7D, 0xB8, 0xF2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64 }
};
#endif

SemaphoreHandle_t semaphoreHandle = NULL;

} // unnamed namespace
Expand All @@ -115,6 +127,14 @@ const struct ble_gatt_svc_def BLEManagerImpl::CHIPoBLEGATTAttrs[] = {
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
.val_handle = &sInstance.mTXCharCCCDAttrHandle,
},
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
{
.uuid = (ble_uuid_t *) (&UUID_CHIPoBLEChar_C3),
.access_cb = gatt_svr_chr_access_additional_data,
.flags = BLE_GATT_CHR_F_READ,
.val_handle = &sInstance.mC3CharAttrHandle,
},
#endif
{
0, /* No more characteristics in this service */
},
Expand All @@ -133,7 +153,10 @@ CHIP_ERROR BLEManagerImpl::_Init()
err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer());
SuccessOrExit(err);

mRXCharAttrHandle = 0;
mRXCharAttrHandle = 0;
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
mC3CharAttrHandle = 0;
#endif
mTXCharCCCDAttrHandle = 0;
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
Expand Down Expand Up @@ -683,6 +706,11 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
uint8_t advData[MAX_ADV_DATA_LEN];
uint8_t index = 0;

constexpr uint8_t kServiceDataLenSize = 1;
constexpr uint8_t kServiceDataUUIDSize = 2;

chip::Ble::ChipBLEDeviceIdentificationInfo deviceIdInfo;

// If a custom device name has not been specified, generate a CHIP-standard name based on the
// bottom digits of the Chip device id.
uint16_t discriminator;
Expand All @@ -706,12 +734,11 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
advData[index++] = 0x02; // length
advData[index++] = CHIP_ADV_DATA_TYPE_FLAGS; // AD type : flags
advData[index++] = CHIP_ADV_DATA_FLAGS; // AD value
advData[index++] = 0x0A; // length
advData[index++] = kServiceDataLenSize + kServiceDataUUIDSize + sizeof(deviceIdInfo); // length
advData[index++] = CHIP_ADV_DATA_TYPE_SERVICE_DATA; // AD type: (Service Data - 16-bit UUID)
advData[index++] = static_cast<uint8_t>(ShortUUID_CHIPoBLEService.value & 0xFF); // AD value
advData[index++] = static_cast<uint8_t>((ShortUUID_CHIPoBLEService.value >> 8) & 0xFF); // AD value

chip::Ble::ChipBLEDeviceIdentificationInfo deviceIdInfo;
err = ConfigurationMgr().GetBLEDeviceIdentificationInfo(deviceIdInfo);
if (err != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -1046,6 +1073,69 @@ int BLEManagerImpl::ble_svr_gap_event(struct ble_gap_event * event, void * arg)
return err.AsInteger();
}

#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
void BLEManagerImpl::HandleC3CharRead(struct ble_gatt_char_context * param)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::System::PacketBufferHandle bufferHandle;

char serialNumber[ConfigurationManager::kMaxSerialNumberLength + 1];
uint16_t lifetimeCounter = 0;
BitFlags<AdditionalDataFields> additionalDataFields;

#if CHIP_ENABLE_ROTATING_DEVICE_ID
err = ConfigurationMgr().GetSerialNumber(serialNumber, sizeof(serialNumber));
SuccessOrExit(err);
err = ConfigurationMgr().GetLifetimeCounter(lifetimeCounter);
SuccessOrExit(err);

additionalDataFields.Set(AdditionalDataFields::RotatingDeviceId);
#endif /* CHIP_ENABLE_ROTATING_DEVICE_ID */

err = AdditionalDataPayloadGenerator().generateAdditionalDataPayload(lifetimeCounter, serialNumber, strlen(serialNumber),
bufferHandle, additionalDataFields);
SuccessOrExit(err);

os_mbuf_append(param->ctxt->om, bufferHandle->Start(), bufferHandle->DataLength());

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to generate TLV encoded Additional Data (%s)", __func__);
}
return;
}

int BLEManagerImpl::gatt_svr_chr_access_additional_data(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt * ctxt, void * arg)
{
struct ble_gatt_char_context param;
int err = 0;

memset(&param, 0, sizeof(struct ble_gatt_char_context));

switch (ctxt->op)
{
case BLE_GATT_ACCESS_OP_READ_CHR:

param.conn_handle = conn_handle;
param.attr_handle = attr_handle;
param.ctxt = ctxt;
param.arg = arg;
sInstance.HandleC3CharRead(&param);
break;

default:
err = BLE_ATT_ERR_UNLIKELY;
break;
}

PlatformMgr().ScheduleWork(DriveBLEState, 0);

return err;
}
#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */

int BLEManagerImpl::gatt_svr_chr_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt, void * arg)
{
struct ble_gatt_char_context param;
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/bluez/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* @file
* Provides Bluez dbus implementatioon for BLE
*/
#include <platform/CHIPDeviceConfig.h>

#include <ble/BleUUID.h>
#include <ble/CHIPBleServiceData.h>
Expand Down