Skip to content

Commit

Permalink
[Silabs]Rework KVS keymap implementation. (#25503)
Browse files Browse the repository at this point in the history
* Rework KVS keymap. Add a migration for the kvs changes and migrationManager to handle future migrations. Optimize baseApplication code

* Restyled by whitespace

* Restyled by clang-format

* fix build for SED options

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
jmartinez-silabs and restyled-commits authored Mar 7, 2023
1 parent a7cc2cf commit 3d4c314
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 50 deletions.
33 changes: 25 additions & 8 deletions examples/platform/silabs/efr32/BaseApplication.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ app::Clusters::NetworkCommissioning::Instance
sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::SlWiFiDriver::GetInstance()));
#endif /* SL_WIFI */

#if !(defined(CHIP_DEVICE_CONFIG_ENABLE_SED) && CHIP_DEVICE_CONFIG_ENABLE_SED)
bool sIsProvisioned = false;

bool sIsProvisioned = false;
#if !(defined(CHIP_DEVICE_CONFIG_ENABLE_SED) && CHIP_DEVICE_CONFIG_ENABLE_SED)
bool sIsEnabled = false;
bool sIsAttached = false;
bool sHaveBLEConnections = false;

#endif // CHIP_DEVICE_CONFIG_ENABLE_SED

EmberAfIdentifyEffectIdentifier sIdentifyEffect = EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_STOP_EFFECT;
Expand Down Expand Up @@ -236,6 +235,9 @@ CHIP_ERROR BaseApplication::Init(Identify * identifyObj)
SILABS_LOG("Getting QR code failed!");
}

PlatformMgr().AddEventHandler(OnPlatformEvent, 0);
sIsProvisioned = ConnectivityMgr().IsThreadProvisioned();

return err;
}

Expand Down Expand Up @@ -287,7 +289,7 @@ void BaseApplication::FunctionEventHandler(AppEvent * aEvent)
StopStatusLEDTimer();
#endif // CHIP_DEVICE_CONFIG_ENABLE_SED

chip::Server::GetInstance().ScheduleFactoryReset();
ScheduleFactoryReset();
}
}

Expand All @@ -307,9 +309,8 @@ void BaseApplication::LightEventHandler()
sIsAttached = ConnectivityMgr().IsWiFiStationConnected();
#endif /* SL_WIFI */
#if CHIP_ENABLE_OPENTHREAD
sIsProvisioned = ConnectivityMgr().IsThreadProvisioned();
sIsEnabled = ConnectivityMgr().IsThreadEnabled();
sIsAttached = ConnectivityMgr().IsThreadAttached();
sIsEnabled = ConnectivityMgr().IsThreadEnabled();
sIsAttached = ConnectivityMgr().IsThreadAttached();
#endif /* CHIP_ENABLE_OPENTHREAD */
sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0);
PlatformMgr().UnlockChipStack();
Expand Down Expand Up @@ -427,7 +428,7 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
#ifdef SL_WIFI
if (!ConnectivityMgr().IsWiFiStationProvisioned())
#else
if (!ConnectivityMgr().IsThreadProvisioned())
if (!sIsProvisioned)
#endif /* !SL_WIFI */
{
// Open Basic CommissioningWindow. Will start BLE advertisements
Expand Down Expand Up @@ -570,3 +571,19 @@ void BaseApplication::DispatchEvent(AppEvent * aEvent)
SILABS_LOG("Event received with no handler. Dropping event.");
}
}

void BaseApplication::ScheduleFactoryReset()
{
PlatformMgr().ScheduleWork([](intptr_t) {
PlatformMgr().HandleServerShuttingDown();
ConfigurationMgr().InitiateFactoryReset();
});
}

void BaseApplication::OnPlatformEvent(const ChipDeviceEvent * event, intptr_t)
{
if (event->Type == DeviceEventType::kServiceProvisioningChange)
{
sIsProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned;
}
}
11 changes: 11 additions & 0 deletions examples/platform/silabs/efr32/BaseApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <app/clusters/identify-server/identify-server.h>
#include <ble/BLEEndPoint.h>
#include <lib/core/CHIPError.h>
#include <platform/CHIPDeviceEvent.h>
#include <platform/CHIPDeviceLayer.h>

#ifdef DISPLAY_ENABLED
Expand Down Expand Up @@ -187,6 +188,16 @@ class BaseApplication
*/
static void LightEventHandler();

/**
* @brief Start the factory Reset process
* Almost identical to Server::ScheduleFactoryReset()
* but doesn't call GetFabricTable().DeleteAllFabrics(); which deletes Key per key.
* With our KVS platform implementation this is a lot slower than deleting the whole kvs section
* our silabs nvm3 driver which end up being doing in ConfigurationManagerImpl::DoFactoryReset(intptr_t arg).
*/
static void ScheduleFactoryReset();

static void OnPlatformEvent(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t);
/**********************************************************
* Protected Attributes declaration
*********************************************************/
Expand Down
6 changes: 4 additions & 2 deletions src/platform/silabs/KeyValueStoreManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ class KeyValueStoreManagerImpl final : public KeyValueStoreManager
CHIP_ERROR _Put(const char * key, const void * value, size_t value_size);
CHIP_ERROR _Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size = nullptr, size_t offset = 0) const;
CHIP_ERROR _Delete(const char * key);
CHIP_ERROR ErasePartition(void);
void ErasePartition(void);

static constexpr size_t kMaxEntries = KVS_MAX_ENTRIES;

static void ForceKeyMapSave();
static void KvsMapMigration();

private:
static void OnScheduledKeyMapSave(System::Layer * systemLayer, void * appState);

void ScheduleKeyMapSave(void);
bool IsValidKvsNvm3Key(const uint32_t nvm3Key) const;
CHIP_ERROR MapKvsKeyToNvm3(const char * key, uint32_t & nvm3Key, bool isSlotNeeded = false) const;
uint16_t hashKvsKeyString(const char * key) const;
CHIP_ERROR MapKvsKeyToNvm3(const char * key, uint16_t hash, uint32_t & nvm3Key, bool isSlotNeeded = false) const;

// ===== Members for internal use by the following friends.
friend KeyValueStoreManager & KeyValueStoreMgr();
Expand Down
6 changes: 3 additions & 3 deletions src/platform/silabs/SilabsConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
#include "nvm3.h"
#include "nvm3_default.h"
#include "nvm3_hal_flash.h"
#include <nvm3_lock.h>

// Substitute the GSDK weak nvm3_lockBegin and nvm3_lockEnd
// for an application controlled re-entrance protection
#define SILABS_SEM_TIMEOUT_ms 5
static SemaphoreHandle_t nvm3_Sem;
static StaticSemaphore_t nvm3_SemStruct;

void nvm3_lockBegin(void)
{
VerifyOrDie(nvm3_Sem != NULL);
xSemaphoreTake(nvm3_Sem, SILABS_SEM_TIMEOUT_ms);
xSemaphoreTake(nvm3_Sem, portMAX_DELAY);
}

void nvm3_lockEnd(void)
Expand All @@ -69,7 +69,7 @@ CHIP_ERROR SilabsConfig::Init()
{
return CHIP_ERROR_NO_MEMORY;
}

xSemaphoreGive(nvm3_Sem);
return MapNvm3Error(nvm3_open(nvm3_defaultHandle, nvm3_defaultInit));
}

Expand Down
7 changes: 4 additions & 3 deletions src/platform/silabs/SilabsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "nvm3_hal_flash.h"

#ifndef KVS_MAX_ENTRIES
#define KVS_MAX_ENTRIES 75 // Available key slot count for Kvs Key mapping.
#define KVS_MAX_ENTRIES 255 // Available key slot count for Kvs Key mapping.
#endif

// Delay before Key/Value is actually saved in NVM
Expand Down Expand Up @@ -145,6 +145,7 @@ class SilabsConfig
static constexpr Key kConfigKey_BootCount = SilabsConfigKey(kMatterCounter_KeyBase, 0x00);
static constexpr Key kConfigKey_TotalOperationalHours = SilabsConfigKey(kMatterCounter_KeyBase, 0x01);
static constexpr Key kConfigKey_LifeTimeCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x02);
static constexpr Key kConfigKey_MigrationCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x03);

// Matter KVS storage Keys
static constexpr Key kConfigKey_KvsStringKeyMap = SilabsConfigKey(kMatterKvs_KeyBase, 0x00);
Expand All @@ -161,8 +162,8 @@ class SilabsConfig
static constexpr Key kMinConfigKey_MatterCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x00);
static constexpr Key kMaxConfigKey_MatterCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x1F);

static constexpr Key kMinConfigKey_MatterKvs = SilabsConfigKey(kMatterKvs_KeyBase, 0x00);
static constexpr Key kMaxConfigKey_MatterKvs = SilabsConfigKey(kMatterKvs_KeyBase, 0xFF);
static constexpr Key kMinConfigKey_MatterKvs = kConfigKey_KvsStringKeyMap;
static constexpr Key kMaxConfigKey_MatterKvs = kConfigKey_KvsLastKeySlot;

static CHIP_ERROR Init(void);
static void DeInit(void);
Expand Down
1 change: 1 addition & 0 deletions src/platform/silabs/efr32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static_library("efr32") {
"../../SingletonConfigurationManager.cpp",
"ConfigurationManagerImpl.cpp",
"KeyValueStoreManagerImpl.cpp",
"MigrationManager.cpp",
"PlatformManagerImpl.cpp",
]

Expand Down
Loading

0 comments on commit 3d4c314

Please sign in to comment.