Skip to content

Commit

Permalink
Add support for Additional data advertising and rotating ID. Add buil…
Browse files Browse the repository at this point in the history
…d preset in efr32 build script (#20960)
  • Loading branch information
jmartinez-silabs authored Jul 19, 2022
1 parent 5eb84bc commit c732630
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 33 deletions.
6 changes: 6 additions & 0 deletions scripts/examples/gn_efr32_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ if [ "$#" == "0" ]; then
For minimum consumption, disable openthread cli and qr code
--wifi <wf200 | rs911x>
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
Expand Down Expand Up @@ -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
Expand Down
69 changes: 68 additions & 1 deletion src/platform/EFR32/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CommissionableDataProvider.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <platform/EFR32/freertos_bluetooth.h>

#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
#include <setup_payload/AdditionalDataPayloadGenerator.h>
#endif

using namespace ::chip;
using namespace ::chip::Ble;

Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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> 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;
Expand Down
12 changes: 11 additions & 1 deletion src/platform/EFR32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/platform/EFR32/CHIPPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ====================

Expand Down
1 change: 1 addition & 0 deletions src/platform/EFR32/EFR32Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->

<!--Custom BLE GATT-->
<gatt gatt_caching="true" generic_attribute_service="true" header="gatt_db.h" name="Custom BLE GATT" out="gatt_db.c" prefix="gattdb_">

Expand Down Expand Up @@ -102,5 +84,14 @@
<notify authenticated="false" bonded="false" encrypted="false"/>
</properties>
</characteristic>

<!--CHIPoBLEChar_C3-->
<characteristic const="false" id="CHIPoBLEChar_C3" name="CHIPoBLEChar_C3" sourceId="custom.type" uuid="64630238-8772-45F2-B87D-748A83218F04">
<informativeText>Custom characteristic (Additional commissioning- related data) </informativeText>
<value length="0" type="user" variable_length="false"/>
<properties>
<read authenticated="false" bonded="false" encrypted="false"/>
</properties>
</characteristic>
</service>
</gatt>
29 changes: 19 additions & 10 deletions src/platform/EFR32/gatt_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/platform/EFR32/gatt_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c732630

Please sign in to comment.