Skip to content

Commit 0f7800d

Browse files
Full integration of CTM credentials and migration.
1 parent feec059 commit 0f7800d

File tree

12 files changed

+376
-162
lines changed

12 files changed

+376
-162
lines changed

examples/platform/silabs/MatterConfig.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
268268
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
269269
#endif // SL_WIFI
270270

271-
ReturnErrorOnFailure(PlatformMgr().InitChipStack());
272-
273271
chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(appName);
274272

275273
// Provision Manager
@@ -279,6 +277,8 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
279277
SetCommissionableDataProvider(&provision.GetStorage());
280278
ChipLogProgress(DeviceLayer, "Provision mode %s", provision.IsProvisionRequired() ? "ENABLED" : "disabled");
281279

280+
ReturnErrorOnFailure(PlatformMgr().InitChipStack());
281+
282282
#if CHIP_ENABLE_OPENTHREAD
283283
ReturnErrorOnFailure(InitOpenThread());
284284
#endif

examples/platform/silabs/provision/ProvisionStorageDefault.cpp

Lines changed: 205 additions & 73 deletions
Large diffs are not rendered by default.

examples/platform/silabs/provision/ProvisionStorageFlash.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,12 @@ CHIP_ERROR Storage::GetTestEventTriggerKey(MutableByteSpan & keySpan)
817817
return CHIP_ERROR_NOT_IMPLEMENTED;
818818
}
819819

820+
CHIP_ERROR Storage::MigrateAttestationCredentialAPI()
821+
{
822+
// Not implemented for the ProvisionStorageFlash.
823+
return CHIP_NO_ERROR;
824+
}
825+
820826
} // namespace Provision
821827
} // namespace Silabs
822828
} // namespace DeviceLayer

src/platform/silabs/MigrationManager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717

1818
#include "MigrationManager.h"
19+
#include <headers/ProvisionManager.h>
20+
#include <headers/ProvisionStorage.h>
1921
#include <platform/CHIPDeviceLayer.h>
2022
#include <platform/silabs/SilabsConfig.h>
2123
#include <stdio.h>
@@ -41,6 +43,7 @@ static migrationData_t migrationTable[] = {
4143
{ .migrationGroup = 2, .migrationFunc = MigrateDacProvider },
4244
{ .migrationGroup = 3, .migrationFunc = MigrateCounterConfigs },
4345
{ .migrationGroup = 4, .migrationFunc = MigrateHardwareVersion },
46+
{ .migrationGroup = 5, .migrationFunc = MigrateCTM },
4447
// add any additional migration neccesary. migrationGroup should stay equal if done in the same commit or increment by 1 for
4548
// each new entry.
4649
};
@@ -132,6 +135,15 @@ void MigrateHardwareVersion(void)
132135
MigrationManager::MigrateUint16(kOldKey_HardwareVersion, SilabsConfig::kConfigKey_HardwareVersion);
133136
}
134137

138+
void MigrateCTM(void)
139+
{
140+
CHIP_ERROR err = Provision::Manager::GetInstance().GetStorage().MigrateAttestationCredentialAPI();
141+
if (err != CHIP_NO_ERROR)
142+
{
143+
ChipLogError(DeviceLayer, "Failed to migrate CTM: %s", ErrorStr(err));
144+
}
145+
}
146+
135147
} // namespace Silabs
136148
} // namespace DeviceLayer
137149
} // namespace chip

src/platform/silabs/MigrationManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void MigrateKvsMap(void);
4747
void MigrateDacProvider(void);
4848
void MigrateCounterConfigs(void);
4949
void MigrateHardwareVersion(void);
50+
void MigrateCTM(void);
5051

5152
} // namespace Silabs
5253
} // namespace DeviceLayer

src/platform/silabs/SiWx917/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ static_library("SiWx917") {
6363
"${silabs_platform_dir}/PlatformManagerImpl.h",
6464
"${silabs_platform_dir}/SilabsConfig.cpp",
6565
"${silabs_platform_dir}/SilabsConfig.h",
66+
"${silabs_platform_dir}/SilabsConfigUtils.cpp",
67+
"${silabs_platform_dir}/SilabsConfigUtils.h",
6668
"${silabs_platform_dir}/SystemPlatformConfig.h",
6769
"../../FreeRTOS/SystemTimeSupport.cpp",
6870
"../../SingletonConfigurationManager.cpp",

src/platform/silabs/SilabsConfig.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <lib/support/CodeUtils.h>
2828
#include <platform/internal/testing/ConfigUnitTest.h>
2929
#include <platform/silabs/CHIPDevicePlatformConfig.h>
30+
#include <platform/silabs/SilabsConfigUtils.h>
3031

3132
#include <nvm3.h>
3233
#include <nvm3_default.h>
@@ -63,25 +64,6 @@ namespace DeviceLayer {
6364
namespace Internal {
6465

6566
namespace {
66-
CHIP_ERROR MapNvm3Error(sl_status_t nvm3Res)
67-
{
68-
CHIP_ERROR err;
69-
70-
switch (nvm3Res)
71-
{
72-
case SL_STATUS_OK:
73-
err = CHIP_NO_ERROR;
74-
break;
75-
case SL_STATUS_NOT_FOUND:
76-
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
77-
break;
78-
default:
79-
err = CHIP_ERROR(ChipError::Range::kPlatform, (nvm3Res & 0xFF) + CHIP_DEVICE_CONFIG_SILABS_NVM3_ERROR_MIN);
80-
break;
81-
}
82-
83-
return err;
84-
}
8567

8668
template <typename T>
8769
CHIP_ERROR ReadConfigValueHelper(SilabsConfig::Key key, T & val)

src/platform/silabs/SilabsConfig.h

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
#include <functional>
2828
#include <platform/CHIPDeviceError.h>
2929

30-
#ifndef SL_COMMON_TOKEN_MANAGER_ENABLE_DYNAMIC_TOKENS
30+
#ifdef SL_TOKEN_MANAGER_DEFINES_H
31+
#include <sl_token_manager_defines.h>
32+
#else
3133
#include "nvm3.h"
3234
#include "nvm3_hal_flash.h"
33-
#else
34-
#include <sl_token_manager_defines.h>
3535
#endif
3636

3737
#ifndef KVS_MAX_ENTRIES
@@ -61,7 +61,22 @@ namespace Internal {
6161
* the template class (e.g. the ReadConfigValue() method).
6262
*/
6363

64-
#ifndef SL_COMMON_TOKEN_MANAGER_ENABLE_DYNAMIC_TOKENS
64+
#ifdef SL_TOKEN_MANAGER_DEFINES_H
65+
inline constexpr uint32_t kUserNvm3KeyDomainLoLimit = SL_TOKEN_NVM3_REGION_USER;
66+
inline constexpr uint32_t kUserNvm3KeyDomainHiLimit = SL_TOKEN_NVM3_REGION_ZIGBEE - 1;
67+
68+
// Only keep the MSBs of the Matter Region. The LSB of the region is determined by the keyBaseOffset.
69+
// with SilabsConfigKey Helper function.
70+
inline constexpr uint32_t kMatterNvm3KeyDomain = (SL_TOKEN_NVM3_REGION_MATTER & 0xFFFF000);
71+
// TODO: Determine what valuee to use for the static key domain to avoid collision between matter / Zigbee
72+
inline constexpr uint32_t kMatterStaticKeyDomain = (SL_TOKEN_NVM3_REGION_MATTER & 0xFFFF000);
73+
74+
constexpr inline uint32_t SilabsConfigKey(uint8_t keyBaseOffset, uint8_t id)
75+
{
76+
return SL_TOKEN_TYPE_NVM3 | kMatterNvm3KeyDomain | static_cast<uint32_t>(keyBaseOffset) << 8 | id;
77+
}
78+
79+
#else
6580
// Silabs NVM3 objects use a 20-bit number,
6681
// NVM3 Key 19:16 Stack region
6782
// NVM3 Key 15:0 Available NVM3 keys 0x0000 -> 0xFFFF.
@@ -77,24 +92,6 @@ constexpr inline uint32_t SilabsConfigKey(uint8_t keyBaseOffset, uint8_t id)
7792
{
7893
return kMatterNvm3KeyDomain | static_cast<uint32_t>(keyBaseOffset) << 8 | id;
7994
}
80-
#else
81-
82-
inline constexpr uint32_t kUserNvm3KeyDomainLoLimit = SL_TOKEN_NVM3_REGION_USER;
83-
inline constexpr uint32_t kUserNvm3KeyDomainHiLimit = SL_TOKEN_NVM3_REGION_ZIGBEE - 1;
84-
85-
// Only keep the MSBs of the Matter Region. The LSB of the region is determined by the keyBaseOffset.
86-
// with SilabsConfigKey Helper function.
87-
inline constexpr uint32_t kMatterNvm3KeyDomain = (SL_TOKEN_NVM3_REGION_MATTER & 0xFFFF000);
88-
89-
constexpr inline uint32_t SilabsConfigKey(uint8_t keyBaseOffset, uint8_t id)
90-
{
91-
return SL_TOKEN_TYPE_NVM3 | kMatterNvm3KeyDomain | static_cast<uint32_t>(keyBaseOffset) << 8 | id;
92-
}
93-
94-
constexpr inline uint32_t SilabsSecureTokenKey(uint8_t keyBaseOffset, uint8_t id)
95-
{
96-
return SL_TOKEN_TYPE_STATIC_SECURE | static_cast<uint32_t>(keyBaseOffset) << 8 | id;
97-
}
9895
#endif
9996

10097
inline constexpr uint32_t kMatterNvm3KeyLoLimit = 0x087200U; // Do not modify without Silabs GSDK team approval
@@ -151,16 +148,23 @@ class SilabsConfig
151148
// SHALL NOT be the same as the UniqueID attribute exposed in the Basic Information cluster.
152149
static constexpr Key kConfigKey_PersistentUniqueId = SilabsConfigKey(kMatterFactory_KeyBase, 0x1F);
153150
static constexpr Key kConfigKey_Creds_KeyId = SilabsConfigKey(kMatterFactory_KeyBase, 0x20);
154-
static constexpr Key kConfigKey_Creds_Base_Addr = SilabsConfigKey(kMatterFactory_KeyBase, 0x21);
155-
static constexpr Key kConfigKey_Creds_DAC_Offset = SilabsConfigKey(kMatterFactory_KeyBase, 0x22);
156-
static constexpr Key kConfigKey_Creds_DAC_Size = SilabsConfigKey(kMatterFactory_KeyBase, 0x23);
157-
static constexpr Key kConfigKey_Creds_PAI_Offset = SilabsConfigKey(kMatterFactory_KeyBase, 0x24);
158-
static constexpr Key kConfigKey_Creds_PAI_Size = SilabsConfigKey(kMatterFactory_KeyBase, 0x25);
159-
static constexpr Key kConfigKey_Creds_CD_Offset = SilabsConfigKey(kMatterFactory_KeyBase, 0x26);
160-
static constexpr Key kConfigKey_Creds_CD_Size = SilabsConfigKey(kMatterFactory_KeyBase, 0x27);
161-
static constexpr Key kConfigKey_Provision_Request = SilabsConfigKey(kMatterFactory_KeyBase, 0x28);
162-
static constexpr Key kConfigKey_Provision_Version = SilabsConfigKey(kMatterFactory_KeyBase, 0x29);
163-
static constexpr Key kOtaTlvEncryption_KeyId = SilabsConfigKey(kMatterFactory_KeyBase, 0x30);
151+
152+
// DEPRECATED KEYS : BEGIN
153+
static constexpr Key kConfigKey_Creds_Base_Addr = SilabsConfigKey(kMatterFactory_KeyBase, 0x21);
154+
static constexpr Key kConfigKey_Creds_DAC_Offset = SilabsConfigKey(kMatterFactory_KeyBase, 0x22);
155+
static constexpr Key kConfigKey_Creds_PAI_Offset = SilabsConfigKey(kMatterFactory_KeyBase, 0x24);
156+
static constexpr Key kConfigKey_Creds_CD_Offset = SilabsConfigKey(kMatterFactory_KeyBase, 0x26);
157+
// DEPRECATED KEYS : END
158+
159+
static constexpr Key kConfigKey_Creds_DAC_Size = SilabsConfigKey(kMatterFactory_KeyBase, 0x23);
160+
static constexpr Key kConfigKey_Creds_PAI_Size = SilabsConfigKey(kMatterFactory_KeyBase, 0x25);
161+
static constexpr Key kConfigKey_Creds_CD_Size = SilabsConfigKey(kMatterFactory_KeyBase, 0x27);
162+
static constexpr Key kConfigKey_Provision_Request = SilabsConfigKey(kMatterFactory_KeyBase, 0x28);
163+
static constexpr Key kConfigKey_Provision_Version = SilabsConfigKey(kMatterFactory_KeyBase, 0x29);
164+
static constexpr Key kOtaTlvEncryption_KeyId = SilabsConfigKey(kMatterFactory_KeyBase, 0x30);
165+
static constexpr Key kConfigKey_Creds_Dac = SilabsConfigKey(kMatterFactory_KeyBase, 0x31);
166+
static constexpr Key kConfigKey_Creds_Pai = SilabsConfigKey(kMatterFactory_KeyBase, 0x32);
167+
static constexpr Key kConfigKey_Creds_CD = SilabsConfigKey(kMatterFactory_KeyBase, 0x33);
164168

165169
// Matter Config Keys
166170
static constexpr Key kConfigKey_ServiceConfig = SilabsConfigKey(kMatterConfig_KeyBase, 0x01);

src/platform/silabs/SilabsConfigCTM.cpp

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <lib/support/CodeUtils.h>
2828
#include <platform/internal/testing/ConfigUnitTest.h>
2929
#include <platform/silabs/CHIPDevicePlatformConfig.h>
30+
#include <platform/silabs/SilabsConfigUtils.h>
3031

3132
#include "uart.h"
3233
#include <FreeRTOS.h>
@@ -35,7 +36,7 @@
3536
#include <nvm3_hal_flash.h>
3637
#include <nvm3_lock.h>
3738
#include <semphr.h>
38-
#include <sl_token_manager_interface.h>
39+
#include <sl_token_manager_api.h>
3940
// Substitute the GSDK weak nvm3_lockBegin and nvm3_lockEnd
4041
// for an application controlled re-entrance protection
4142
static SemaphoreHandle_t nvm3_Sem;
@@ -63,25 +64,6 @@ namespace chip {
6364
namespace DeviceLayer {
6465
namespace Internal {
6566
namespace {
66-
CHIP_ERROR MapNvm3Error(sl_status_t nvm3Res)
67-
{
68-
CHIP_ERROR err;
69-
70-
switch (nvm3Res)
71-
{
72-
case SL_STATUS_OK:
73-
err = CHIP_NO_ERROR;
74-
break;
75-
case SL_STATUS_NOT_FOUND:
76-
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
77-
break;
78-
default:
79-
err = CHIP_ERROR(ChipError::Range::kPlatform, (nvm3Res & 0xFF) + CHIP_DEVICE_CONFIG_SILABS_NVM3_ERROR_MIN);
80-
break;
81-
}
82-
83-
return err;
84-
}
8567

8668
template <typename T>
8769
CHIP_ERROR ReadConfigValueHelper(SilabsConfig::Key key, T & val)
@@ -98,9 +80,7 @@ CHIP_ERROR ReadConfigValueHelper(SilabsConfig::Key key, T & val)
9880
// Ensure the data size matches the expected size
9981
VerifyOrReturnError(dataLen == sizeof(T), CHIP_ERROR_INVALID_ARGUMENT);
10082

101-
// TODO: size_out from sl_token_manager_get_data is not used nor useful, remove once the API gets updated
102-
uint32_t unusedSizeOut;
103-
ReturnErrorOnFailure(MapNvm3Error(sl_token_manager_get_data(key, &tmpVal, dataLen, &unusedSizeOut)));
83+
ReturnErrorOnFailure(MapNvm3Error(sl_token_manager_get_data(key, &tmpVal, dataLen)));
10484

10585
val = tmpVal;
10686
return CHIP_NO_ERROR;
@@ -177,9 +157,7 @@ CHIP_ERROR SilabsConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize,
177157
// terminator char).
178158
VerifyOrExit((bufSize > dataLen), err = CHIP_ERROR_BUFFER_TOO_SMALL);
179159

180-
// TODO: size_out from sl_token_manager_get_data is not used nor useful, remove once the API gets updated
181-
uint32_t unusedSizeOut;
182-
err = MapNvm3Error(sl_token_manager_get_data(key, buf, dataLen, &unusedSizeOut));
160+
err = MapNvm3Error(sl_token_manager_get_data(key, buf, dataLen));
183161
SuccessOrExit(err);
184162

185163
outLen = ((dataLen == 1) && (buf[0] == 0)) ? 0 : dataLen;
@@ -195,9 +173,7 @@ CHIP_ERROR SilabsConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize,
195173
{
196174
// Read the first byte of the nvm3 string into a tmp var.
197175
char firstByte;
198-
// TODO: size_out from sl_token_manager_get_data is not used nor useful, remove once the API gets updated
199-
uint32_t unusedSizeOut;
200-
err = MapNvm3Error(sl_token_manager_get_data(key, &firstByte, 1, &unusedSizeOut));
176+
err = MapNvm3Error(sl_token_manager_get_data(key, &firstByte, 1));
201177
SuccessOrExit(err);
202178

203179
outLen = (firstByte == 0) ? 0 : dataLen;
@@ -225,14 +201,12 @@ CHIP_ERROR SilabsConfig::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSi
225201
size_t maxReadLength = dataLen - offset;
226202
if (bufSize >= maxReadLength)
227203
{
228-
// TODO: No API currently exist for partial reads in sl_token_manager_interface, replace this when available
229-
ReturnErrorOnFailure(MapNvm3Error(nvm3_readPartialData(nvm3_defaultHandle, key, buf, offset, maxReadLength)));
204+
ReturnErrorOnFailure(MapNvm3Error(sl_token_manager_get_partial_data(key, buf, offset, maxReadLength)));
230205
outLen = maxReadLength;
231206
}
232207
else
233208
{
234-
// TODO: No API currently exist for partial reads in sl_token_manager_interface, replace this when available
235-
ReturnErrorOnFailure(MapNvm3Error(nvm3_readPartialData(nvm3_defaultHandle, key, buf, offset, bufSize)));
209+
ReturnErrorOnFailure(MapNvm3Error(sl_token_manager_get_partial_data(key, buf, offset, bufSize)));
236210
// read was successful, but we did not read all the data from the object.
237211
outLen = bufSize;
238212
return CHIP_ERROR_BUFFER_TOO_SMALL;
@@ -248,9 +222,7 @@ CHIP_ERROR SilabsConfig::ReadConfigValueCounter(uint8_t counterIdx, uint32_t & v
248222

249223
VerifyOrReturnError(ValidConfigKey(key), CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
250224

251-
// TODO: size_out from sl_token_manager_get_data is not used nor useful, remove once the API gets updated
252-
uint32_t unusedSizeOut;
253-
ReturnErrorOnFailure(MapNvm3Error(sl_token_manager_get_data(key, &tmpVal, sizeof(tmpVal), &unusedSizeOut)));
225+
ReturnErrorOnFailure(MapNvm3Error(sl_token_manager_get_data(key, &tmpVal, sizeof(tmpVal))));
254226
val = tmpVal;
255227
return CHIP_NO_ERROR;
256228
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @file
20+
* Utility functions for Silicon Labs platform error handling
21+
* and common operations.
22+
*/
23+
24+
#include <platform/silabs/SilabsConfigUtils.h>
25+
26+
#include <lib/core/CHIPError.h>
27+
#include <lib/support/CodeUtils.h>
28+
#include <platform/CHIPDeviceError.h>
29+
30+
namespace chip {
31+
namespace DeviceLayer {
32+
namespace Internal {
33+
34+
CHIP_ERROR MapNvm3Error(sl_status_t nvm3Res)
35+
{
36+
CHIP_ERROR err;
37+
38+
switch (nvm3Res)
39+
{
40+
case SL_STATUS_OK:
41+
err = CHIP_NO_ERROR;
42+
break;
43+
case SL_STATUS_NOT_FOUND:
44+
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
45+
break;
46+
default:
47+
err = CHIP_ERROR(ChipError::Range::kPlatform, (nvm3Res & 0xFF) + CHIP_DEVICE_CONFIG_SILABS_NVM3_ERROR_MIN);
48+
break;
49+
}
50+
51+
return err;
52+
}
53+
54+
} // namespace Internal
55+
} // namespace DeviceLayer
56+
} // namespace chip

0 commit comments

Comments
 (0)