From c732630191eab9b918c924deea4928372f0ea766 Mon Sep 17 00:00:00 2001 From: jmartinez-silabs <67972863+jmartinez-silabs@users.noreply.github.com> Date: Tue, 19 Jul 2022 19:30:35 -0400 Subject: [PATCH] Add support for Additional data advertising and rotating ID. Add build preset in efr32 build script (#20960) --- scripts/examples/gn_efr32_example.sh | 6 ++ src/platform/EFR32/BLEManagerImpl.cpp | 69 ++++++++++++++++++- src/platform/EFR32/BLEManagerImpl.h | 12 +++- src/platform/EFR32/CHIPPlatformConfig.h | 4 +- src/platform/EFR32/EFR32Config.h | 1 + .../{gatt.xml => gatt_configuration.btconf} | 27 +++----- src/platform/EFR32/gatt_db.c | 29 +++++--- src/platform/EFR32/gatt_db.h | 5 +- 8 files changed, 120 insertions(+), 33 deletions(-) rename src/platform/EFR32/{gatt.xml => gatt_configuration.btconf} (90%) diff --git a/scripts/examples/gn_efr32_example.sh b/scripts/examples/gn_efr32_example.sh index 59185701c237db..a8e200b028ae56 100755 --- a/scripts/examples/gn_efr32_example.sh +++ b/scripts/examples/gn_efr32_example.sh @@ -98,6 +98,8 @@ if [ "$#" == "0" ]; then For minimum consumption, disable openthread cli and qr code --wifi build wifi example variant for given exansion board + --additional_data_advertising + enable Addition data advertissing and rotating device ID " elif [ "$#" -lt "2" ]; then echo "Invalid number of arguments @@ -141,6 +143,10 @@ else optArgs+="chip_enable_wifi_ipv4=true " shift ;; + --additional_data_advertising) + optArgs+="chip_enable_additional_data_advertising=true chip_enable_rotating_device_id=true " + shift + ;; *) if [ "$1" =~ *"use_rs911x=true"* ] || [ "$1" =~ *"use_wf200=true"* ]; then USE_WIFI=true diff --git a/src/platform/EFR32/BLEManagerImpl.cpp b/src/platform/EFR32/BLEManagerImpl.cpp index e79d403235ffdf..72d55817042c16 100644 --- a/src/platform/EFR32/BLEManagerImpl.cpp +++ b/src/platform/EFR32/BLEManagerImpl.cpp @@ -40,8 +40,13 @@ #include #include #include +#include #include +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +#include +#endif + using namespace ::chip; using namespace ::chip::Ble; @@ -282,6 +287,17 @@ void BLEManagerImpl::bluetoothStackEventHandler(void * p_arg) } break; + case sl_bt_evt_gatt_server_user_read_request_id: { + ChipLogProgress(DeviceLayer, "GATT server user_read_request"); +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + if (bluetooth_evt->data.evt_gatt_server_user_read_request.characteristic == gattdb_CHIPoBLEChar_C3) + { + HandleC3ReadRequest(bluetooth_evt); + } +#endif // CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + } + break; + case sl_bt_evt_connection_remote_used_features_id: { // ChipLogProgress(DeviceLayer, "link layer features supported by the remote device"); } @@ -619,6 +635,10 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void) memcpy(&advData[index], (void *) &mDeviceIdInfo, mDeviceIdInfoLength); // AD value index += mDeviceIdInfoLength; +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + ReturnErrorOnFailure(EncodeAdditionalDataTlv()); +#endif + if (0xff != advertising_set_handle) { sl_bt_advertiser_delete_set(advertising_set_handle); @@ -929,7 +949,7 @@ void BLEManagerImpl::HandleRXCharWrite(volatile sl_bt_msg_t * evt) void BLEManagerImpl::HandleTxConfirmationEvent(BLE_CONNECTION_OBJECT conId) { ChipDeviceEvent event; - uint8_t timerHandle = sInstance.GetTimerHandle(conId); + uint8_t timerHandle = sInstance.GetTimerHandle(conId, false); ChipLogProgress(DeviceLayer, "Tx Confirmation received"); @@ -1021,6 +1041,53 @@ BLEManagerImpl::CHIPoBLEConState * BLEManagerImpl::GetConnectionState(uint8_t co return NULL; } +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +CHIP_ERROR BLEManagerImpl::EncodeAdditionalDataTlv() +{ + CHIP_ERROR err = CHIP_NO_ERROR; + BitFlags additionalDataFields; + AdditionalDataPayloadGeneratorParams additionalDataPayloadParams; + +#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) + uint8_t rotatingDeviceIdUniqueId[ConfigurationManager::kRotatingDeviceIDUniqueIDLength] = {}; + MutableByteSpan rotatingDeviceIdUniqueIdSpan(rotatingDeviceIdUniqueId); + + err = DeviceLayer::GetDeviceInstanceInfoProvider()->GetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdSpan); + SuccessOrExit(err); + err = ConfigurationMgr().GetLifetimeCounter(additionalDataPayloadParams.rotatingDeviceIdLifetimeCounter); + SuccessOrExit(err); + additionalDataPayloadParams.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueIdSpan; + additionalDataFields.Set(AdditionalDataFields::RotatingDeviceId); +#endif /* CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) */ + + err = AdditionalDataPayloadGenerator().generateAdditionalDataPayload(additionalDataPayloadParams, c3AdditionalDataBufferHandle, + additionalDataFields); + +exit: + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to generate TLV encoded Additional Data (%s)", __func__); + } + + return err; +} + +void BLEManagerImpl::HandleC3ReadRequest(volatile sl_bt_msg_t * evt) +{ + sl_bt_evt_gatt_server_user_read_request_t * readReq = + (sl_bt_evt_gatt_server_user_read_request_t *) &(evt->data.evt_gatt_server_user_read_request); + ChipLogDetail(DeviceLayer, "Read request received for CHIPoBLEChar_C3 - opcode:%d", readReq->att_opcode); + sl_status_t ret = sl_bt_gatt_server_send_user_read_response(readReq->connection, readReq->characteristic, 0, + sInstance.c3AdditionalDataBufferHandle->DataLength(), + sInstance.c3AdditionalDataBufferHandle->Start(), nullptr); + + if (ret != SL_STATUS_OK) + { + ChipLogDetail(DeviceLayer, "Failed to send read response, err:%ld", ret); + } +} +#endif // CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + uint8_t BLEManagerImpl::GetTimerHandle(uint8_t connectionHandle, bool allocate) { uint8_t freeIndex = kMaxConnections; diff --git a/src/platform/EFR32/BLEManagerImpl.h b/src/platform/EFR32/BLEManagerImpl.h index 561e553acd0ca2..1d557d134f252b 100644 --- a/src/platform/EFR32/BLEManagerImpl.h +++ b/src/platform/EFR32/BLEManagerImpl.h @@ -125,12 +125,18 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla char mDeviceName[kMaxDeviceNameLength + 1]; // The advertising set handle allocated from Bluetooth stack. uint8_t advertising_set_handle = 0xff; +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + PacketBufferHandle c3AdditionalDataBufferHandle; +#endif CHIP_ERROR MapBLEError(int bleErr); void DriveBLEState(void); CHIP_ERROR ConfigureAdvertisingData(void); CHIP_ERROR StartAdvertising(void); CHIP_ERROR StopAdvertising(void); +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + CHIP_ERROR EncodeAdditionalDataTlv(); +#endif void UpdateMtu(volatile sl_bt_msg_t * evt); void HandleBootEvent(void); void HandleConnectEvent(volatile sl_bt_msg_t * evt); @@ -145,10 +151,14 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs); void CancelBleAdvTimeoutTimer(void); CHIPoBLEConState * GetConnectionState(uint8_t conId, bool allocate = false); - uint8_t GetTimerHandle(uint8_t connectionHandle, bool allocate = false); static void DriveBLEState(intptr_t arg); static void bluetoothStackEventHandler(void * p_arg); static void BleAdvTimeoutHandler(TimerHandle_t xTimer); + uint8_t GetTimerHandle(uint8_t connectionHandle, bool allocate); + +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + static void HandleC3ReadRequest(volatile sl_bt_msg_t * evt); +#endif }; /** diff --git a/src/platform/EFR32/CHIPPlatformConfig.h b/src/platform/EFR32/CHIPPlatformConfig.h index 534b0b92595d91..f150127bbed5a9 100644 --- a/src/platform/EFR32/CHIPPlatformConfig.h +++ b/src/platform/EFR32/CHIPPlatformConfig.h @@ -30,11 +30,11 @@ #define CHIP_CONFIG_ABORT() abort() -#define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE uint8_t +#define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE uint32_t #define CHIP_CONFIG_PERSISTED_STORAGE_ENC_MSG_CNTR_ID 1 #define CHIP_CONFIG_PERSISTED_STORAGE_MAX_KEY_LENGTH 2 -#define CHIP_CONFIG_LIFETIIME_PERSISTED_COUNTER_KEY 0x01 +#define CHIP_CONFIG_LIFETIIME_PERSISTED_COUNTER_KEY EFR32Config::kConfigKey_LifeTimeCounter // ==================== Security Adaptations ==================== diff --git a/src/platform/EFR32/EFR32Config.h b/src/platform/EFR32/EFR32Config.h index 09b07f6b240104..2017bcf2ce38b8 100644 --- a/src/platform/EFR32/EFR32Config.h +++ b/src/platform/EFR32/EFR32Config.h @@ -133,6 +133,7 @@ class EFR32Config // Matter Counter Keys static constexpr Key kConfigKey_BootCount = EFR32ConfigKey(kMatterCounter_KeyBase, 0x00); static constexpr Key kConfigKey_TotalOperationalHours = EFR32ConfigKey(kMatterCounter_KeyBase, 0x01); + static constexpr Key kConfigKey_LifeTimeCounter = EFR32ConfigKey(kMatterCounter_KeyBase, 0x02); // Matter KVS storage Keys static constexpr Key kConfigKey_KvsStringKeyMap = EFR32ConfigKey(kMatterKvs_KeyBase, 0x00); diff --git a/src/platform/EFR32/gatt.xml b/src/platform/EFR32/gatt_configuration.btconf similarity index 90% rename from src/platform/EFR32/gatt.xml rename to src/platform/EFR32/gatt_configuration.btconf index f4cb14332347ff..b6e4af590a8ae3 100644 --- a/src/platform/EFR32/gatt.xml +++ b/src/platform/EFR32/gatt_configuration.btconf @@ -1,22 +1,4 @@ - - @@ -102,5 +84,14 @@ + + + + Custom characteristic (Additional commissioning- related data) + + + + + diff --git a/src/platform/EFR32/gatt_db.c b/src/platform/EFR32/gatt_db.c index 1a173698a9be9e..aa10ec56fa920d 100644 --- a/src/platform/EFR32/gatt_db.c +++ b/src/platform/EFR32/gatt_db.c @@ -32,9 +32,10 @@ GATT_DATA(const uint16_t gattdb_uuidtable_16_map[]) = { GATT_DATA(const uint8_t gattdb_uuidtable_128_map[]) = { 0x11, 0x9d, 0x9f, 0x42, 0x9c, 0x4f, 0x9f, 0x95, 0x59, 0x45, 0x3d, 0x26, 0xf5, 0x2e, 0xee, 0x18, 0x12, 0x9d, 0x9f, 0x42, 0x9c, 0x4f, 0x9f, 0x95, 0x59, 0x45, 0x3d, 0x26, 0xf5, 0x2e, 0xee, 0x18, + 0x04, 0x8f, 0x21, 0x83, 0x8a, 0x74, 0x7d, 0xb8, 0xf2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64, 0x63, 0x60, 0x32, 0xe0, 0x37, 0x5e, 0xa4, 0x88, 0x53, 0x4e, 0x6d, 0xfb, 0x64, 0x35, 0xbf, 0xf7, }; -GATT_DATA(const sli_bt_gattdb_value_t gattdb_attribute_field_26) = { .len = 16, +GATT_DATA(const sli_bt_gattdb_value_t gattdb_attribute_field_28) = { .len = 16, .data = { 0xf0, 0x19, @@ -367,33 +368,41 @@ GATT_DATA(const sli_bt_gattdb_attribute_t gattdb_attributes_map[]) = { .state = 0x00, .datatype = 0x03, .configdata = { .flags = 0x03, .clientconfig_index = 0x01 } }, - { .handle = 0x1b, + { .handle = 0x1b, + .uuid = 0x0002, + .permissions = 0x801, + .caps = 0xffff, + .state = 0x00, + .datatype = 0x05, + .characteristic = { .properties = 0x02, .char_uuid = 0x8002 } }, + { .handle = 0x1c, .uuid = 0x8002, .permissions = 0x801, .caps = 0xffff, .state = 0x00, .datatype = 0x07, .dynamicdata = NULL }, + { .handle = 0x1d, .uuid = 0x0000, .permissions = 0x801, .caps = 0xffff, .state = 0x00, .datatype = 0x00, - .constdata = &gattdb_attribute_field_26 }, - { .handle = 0x1c, + .constdata = &gattdb_attribute_field_28 }, + { .handle = 0x1e, .uuid = 0x0002, .permissions = 0x801, .caps = 0xffff, .state = 0x00, .datatype = 0x05, - .characteristic = { .properties = 0x08, .char_uuid = 0x8002 } }, - { .handle = 0x1d, .uuid = 0x8002, .permissions = 0x802, .caps = 0xffff, .state = 0x00, .datatype = 0x07, .dynamicdata = NULL }, + .characteristic = { .properties = 0x08, .char_uuid = 0x8003 } }, + { .handle = 0x1f, .uuid = 0x8003, .permissions = 0x802, .caps = 0xffff, .state = 0x00, .datatype = 0x07, .dynamicdata = NULL }, }; GATT_HEADER(const sli_bt_gattdb_t gattdb) = { .attributes = gattdb_attributes_map, - .attribute_table_size = 29, - .attribute_num = 29, + .attribute_table_size = 31, + .attribute_num = 31, .uuid16 = gattdb_uuidtable_16_map, .uuid16_table_size = 12, .uuid16_num = 12, .uuid128 = gattdb_uuidtable_128_map, - .uuid128_table_size = 3, - .uuid128_num = 3, + .uuid128_table_size = 4, + .uuid128_num = 4, .num_ccfg = 2, .caps_mask = 0xffff, .enabled_caps = 0xffff, diff --git a/src/platform/EFR32/gatt_db.h b/src/platform/EFR32/gatt_db.h index 209434ff795fac..e059feda15f0ed 100644 --- a/src/platform/EFR32/gatt_db.h +++ b/src/platform/EFR32/gatt_db.h @@ -31,13 +31,16 @@ extern "C" { extern const sli_bt_gattdb_t gattdb; +#define gattdb_generic_attribute 1 #define gattdb_service_changed_char 3 #define gattdb_database_hash 6 #define gattdb_client_support_features 8 #define gattdb_device_name 11 #define gattdb_CHIPoBLEChar_Rx 23 #define gattdb_CHIPoBLEChar_Tx 25 -#define gattdb_ota_control 29 +#define gattdb_CHIPoBLEChar_C3 28 +#define gattdb_ota 29 +#define gattdb_ota_control 31 #if __cplusplus }