Skip to content

Commit

Permalink
Restyled by clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits authored and jmartinez-silabs committed Mar 6, 2023
1 parent 290d23d commit 3abbbf6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 53 deletions.
5 changes: 2 additions & 3 deletions examples/platform/silabs/efr32/BaseApplication.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ CHIP_ERROR BaseApplication::Init(Identify * identifyObj)

ConfigurationMgr().LogDeviceConfig();


// Create buffer for QR code that can fit max size and null terminator.
char qrCodeBuffer[chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
chip::MutableCharSpan QRCode(qrCodeBuffer);
Expand Down Expand Up @@ -311,8 +310,8 @@ void BaseApplication::LightEventHandler()
sIsAttached = ConnectivityMgr().IsWiFiStationConnected();
#endif /* SL_WIFI */
#if CHIP_ENABLE_OPENTHREAD
sIsEnabled = ConnectivityMgr().IsThreadEnabled();
sIsAttached = ConnectivityMgr().IsThreadAttached();
sIsEnabled = ConnectivityMgr().IsThreadEnabled();
sIsAttached = ConnectivityMgr().IsThreadAttached();
#endif /* CHIP_ENABLE_OPENTHREAD */
sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0);
PlatformMgr().UnlockChipStack();
Expand Down
2 changes: 1 addition & 1 deletion examples/platform/silabs/efr32/BaseApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
#include <app/clusters/identify-server/identify-server.h>
#include <ble/BLEEndPoint.h>
#include <lib/core/CHIPError.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/CHIPDeviceEvent.h>
#include <platform/CHIPDeviceLayer.h>

#ifdef DISPLAY_ENABLED
#include "demo-ui.h"
Expand Down
2 changes: 1 addition & 1 deletion src/platform/silabs/SilabsConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
#include <platform/internal/testing/ConfigUnitTest.h>

#include "FreeRTOS.h"
#include <nvm3_lock.h>
#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
Expand Down
70 changes: 36 additions & 34 deletions src/platform/silabs/efr32/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* Platform-specific key value storage implementation for SILABS
*/

#include "MigrationManager.h"
#include <crypto/CHIPCryptoPAL.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/KeyValueStoreManager.h>
#include <platform/silabs/SilabsConfig.h>
#include "MigrationManager.h"
#include <stdio.h>
#include <string.h>

Expand All @@ -41,7 +41,7 @@ namespace DeviceLayer {
namespace PersistedStorage {

KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
uint16_t mKvsKeyMap[KeyValueStoreManagerImpl::kMaxEntries] = {0};
uint16_t mKvsKeyMap[KeyValueStoreManagerImpl::kMaxEntries] = { 0 };

CHIP_ERROR KeyValueStoreManagerImpl::Init(void)
{
Expand All @@ -53,8 +53,8 @@ CHIP_ERROR KeyValueStoreManagerImpl::Init(void)

memset(mKvsKeyMap, 0, sizeof(mKvsKeyMap));
size_t outLen;
err = SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_KvsStringKeyMap,
reinterpret_cast<uint8_t *>(mKvsKeyMap), sizeof(mKvsKeyMap), outLen);
err = SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_KvsStringKeyMap, reinterpret_cast<uint8_t *>(mKvsKeyMap),
sizeof(mKvsKeyMap), outLen);

if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND) // Initial boot
{
Expand All @@ -73,13 +73,13 @@ bool KeyValueStoreManagerImpl::IsValidKvsNvm3Key(uint32_t nvm3Key) const
uint16_t KeyValueStoreManagerImpl::hashKvsKeyString(const char * key) const
{
uint8_t hash256[Crypto::kSHA256_Hash_Length] = { 0 };
Crypto::Hash_SHA256(reinterpret_cast<const uint8_t*>(key), strlen(key), hash256);
Crypto::Hash_SHA256(reinterpret_cast<const uint8_t *>(key), strlen(key), hash256);

uint16_t hash16 ,i = 0;
uint16_t hash16, i = 0;

while (!hash16 && (i < (Crypto::kSHA256_Hash_Length-1)))
while (!hash16 && (i < (Crypto::kSHA256_Hash_Length - 1)))
{
hash16 = (hash256[i] | (hash256[i+1] <<8));
hash16 = (hash256[i] | (hash256[i + 1] << 8));
i++;
}
return hash16;
Expand All @@ -88,7 +88,7 @@ uint16_t KeyValueStoreManagerImpl::hashKvsKeyString(const char * key) const
CHIP_ERROR KeyValueStoreManagerImpl::MapKvsKeyToNvm3(const char * key, uint16_t hash, uint32_t & nvm3Key, bool isSlotNeeded) const
{
CHIP_ERROR err;
char * strPrefix = nullptr;
char * strPrefix = nullptr;
uint8_t firstEmptyKeySlot = kMaxEntries;
for (uint8_t keyIndex = 0; keyIndex < kMaxEntries; keyIndex++)
{
Expand All @@ -102,14 +102,14 @@ CHIP_ERROR KeyValueStoreManagerImpl::MapKvsKeyToNvm3(const char * key, uint16_t
if (strPrefix == nullptr)
{
// Use a calloc to initialize all bits to 0. alloc +1 for a null char
strPrefix = static_cast<char *>(Platform::MemoryCalloc(1,length+1));
strPrefix = static_cast<char *>(Platform::MemoryCalloc(1, length + 1));
VerifyOrDie(strPrefix != nullptr);
}

// Collision prevention
// Read the data from NVM3 it should be prefixed by the kvsString
// else we will look for another matching hash in the map
SilabsConfig::ReadConfigValueBin(tempNvm3key, reinterpret_cast<uint8_t*>(strPrefix), length, readCount, 0);
SilabsConfig::ReadConfigValueBin(tempNvm3key, reinterpret_cast<uint8_t *>(strPrefix), length, readCount, 0);
if (strcmp(key, strPrefix) == 0)
{
// String matches we have confirmed the hash pointed us the right key data
Expand Down Expand Up @@ -154,8 +154,8 @@ void KeyValueStoreManagerImpl::ForceKeyMapSave()

void KeyValueStoreManagerImpl::OnScheduledKeyMapSave(System::Layer * systemLayer, void * appState)
{
SilabsConfig::WriteConfigValueBin(SilabsConfig::kConfigKey_KvsStringKeyMap,
reinterpret_cast<const uint8_t *>(mKvsKeyMap), sizeof(mKvsKeyMap));
SilabsConfig::WriteConfigValueBin(SilabsConfig::kConfigKey_KvsStringKeyMap, reinterpret_cast<const uint8_t *>(mKvsKeyMap),
sizeof(mKvsKeyMap));
}

void KeyValueStoreManagerImpl::ScheduleKeyMapSave(void)
Expand All @@ -175,14 +175,15 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t
VerifyOrReturnError(key != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

uint32_t nvm3Key;
uint16_t hash = hashKvsKeyString(key);
uint16_t hash = hashKvsKeyString(key);
CHIP_ERROR err = MapKvsKeyToNvm3(key, hash, nvm3Key);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);

size_t outLen;
// The user doesn't need the KeyString prefix, Read data after it
size_t KeyStringLen = strlen(key);
err = SilabsConfig::ReadConfigValueBin(nvm3Key, reinterpret_cast<uint8_t *>(value), value_size, outLen, (offset_bytes + KeyStringLen));
err = SilabsConfig::ReadConfigValueBin(nvm3Key, reinterpret_cast<uint8_t *>(value), value_size, outLen,
(offset_bytes + KeyStringLen));
if (read_bytes_size)
{
*read_bytes_size = outLen;
Expand All @@ -201,21 +202,21 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value,
VerifyOrReturnError(key != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

uint32_t nvm3Key;
uint16_t hash = hashKvsKeyString(key);
uint16_t hash = hashKvsKeyString(key);
CHIP_ERROR err = MapKvsKeyToNvm3(key, hash, nvm3Key, /* isSlotNeeded */ true);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);

// add the string Key as prefix to the stored data as a collision prevention mechanism.
size_t keyStringLen = strlen(key);
uint8_t* prefixedData = static_cast<uint8_t *>(Platform::MemoryAlloc(keyStringLen+value_size));
size_t keyStringLen = strlen(key);
uint8_t * prefixedData = static_cast<uint8_t *>(Platform::MemoryAlloc(keyStringLen + value_size));
VerifyOrDie(prefixedData != nullptr);
memcpy(prefixedData, key, keyStringLen);
memcpy(prefixedData+keyStringLen, value, value_size);
memcpy(prefixedData + keyStringLen, value, value_size);

err = SilabsConfig::WriteConfigValueBin(nvm3Key, prefixedData, keyStringLen+value_size);
err = SilabsConfig::WriteConfigValueBin(nvm3Key, prefixedData, keyStringLen + value_size);
if (err == CHIP_NO_ERROR)
{
uint32_t keyIndex = CONVERT_NVM3KEY_TO_KEYMAP_INDEX(nvm3Key);
uint32_t keyIndex = CONVERT_NVM3KEY_TO_KEYMAP_INDEX(nvm3Key);
mKvsKeyMap[keyIndex] = hash;
ScheduleKeyMapSave();
}
Expand All @@ -228,14 +229,14 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
VerifyOrReturnError(key != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

uint32_t nvm3Key;
uint16_t hash = hashKvsKeyString(key);
uint16_t hash = hashKvsKeyString(key);
CHIP_ERROR err = MapKvsKeyToNvm3(key, hash, nvm3Key);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);

err = SilabsConfig::ClearConfigValue(nvm3Key);
if (err == CHIP_NO_ERROR)
{
uint32_t keyIndex = CONVERT_NVM3KEY_TO_KEYMAP_INDEX(nvm3Key);
uint32_t keyIndex = CONVERT_NVM3KEY_TO_KEYMAP_INDEX(nvm3Key);
mKvsKeyMap[keyIndex] = 0;
ScheduleKeyMapSave();
}
Expand All @@ -256,31 +257,32 @@ void KeyValueStoreManagerImpl::ErasePartition(void)

void KeyValueStoreManagerImpl::KvsMapMigration(void)
{
size_t readlen = 0;
constexpr uint8_t oldMaxEntires = 120;
char mKvsStoredKeyString[oldMaxEntires][PersistentStorageDelegate::kKeyLengthMax + 1] = {0};
CHIP_ERROR err = SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_KvsStringKeyMap,
reinterpret_cast<uint8_t *>(mKvsStoredKeyString), sizeof(mKvsStoredKeyString), readlen);
size_t readlen = 0;
constexpr uint8_t oldMaxEntires = 120;
char mKvsStoredKeyString[oldMaxEntires][PersistentStorageDelegate::kKeyLengthMax + 1] = { 0 };
CHIP_ERROR err =
SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_KvsStringKeyMap, reinterpret_cast<uint8_t *>(mKvsStoredKeyString),
sizeof(mKvsStoredKeyString), readlen);

if (err == CHIP_NO_ERROR)
{
for (uint8_t i=0; i < oldMaxEntires; i++)
for (uint8_t i = 0; i < oldMaxEntires; i++)
{
if (mKvsStoredKeyString[i][0] != 0)
{
size_t dataLen = 0;
size_t dataLen = 0;
uint32_t nvm3Key = CONVERT_KEYMAP_INDEX_TO_NVM3KEY(i);

if (SilabsConfig::ConfigValueExists(nvm3Key, dataLen))
{
// Read old data and prefix it with the string Key for the collision prevention mechanism.
size_t keyStringLen = strlen(mKvsStoredKeyString[i]);
uint8_t* prefixedData = static_cast<uint8_t *>(Platform::MemoryAlloc(keyStringLen+dataLen));
size_t keyStringLen = strlen(mKvsStoredKeyString[i]);
uint8_t * prefixedData = static_cast<uint8_t *>(Platform::MemoryAlloc(keyStringLen + dataLen));
VerifyOrDie(prefixedData != nullptr);
memcpy(prefixedData, mKvsStoredKeyString[i], keyStringLen);

SilabsConfig::ReadConfigValueBin(nvm3Key, prefixedData+keyStringLen, dataLen, readlen);
SilabsConfig::WriteConfigValueBin(nvm3Key, prefixedData, keyStringLen+dataLen);
SilabsConfig::ReadConfigValueBin(nvm3Key, prefixedData + keyStringLen, dataLen, readlen);
SilabsConfig::WriteConfigValueBin(nvm3Key, prefixedData, keyStringLen + dataLen);
mKvsKeyMap[i] = KeyValueStoreMgrImpl().hashKvsKeyString(mKvsStoredKeyString[i]);
Platform::MemoryFree(prefixedData);
}
Expand Down
20 changes: 10 additions & 10 deletions src/platform/silabs/efr32/MigrationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ typedef struct
{
uint32_t migrationGroup;
func_ptr migrationFunc;
}migrationData_t;
} migrationData_t;

#define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
static migrationData_t migrationTable[] = {
{ .migrationGroup = 1 , .migrationFunc = &KeyValueStoreMgrImpl().KvsMapMigration },
// add any additional migration neccesary. migrationGroup should stay equal if done in the same commit or increment by 1 for each new entry.
{ .migrationGroup = 1, .migrationFunc = &KeyValueStoreMgrImpl().KvsMapMigration },
// add any additional migration neccesary. migrationGroup should stay equal if done in the same commit or increment by 1 for
// each new entry.
};

} //namespace
} // namespace

void EFR32Migration::applyMigrations()
{
Expand All @@ -55,19 +56,18 @@ void EFR32Migration::applyMigrations()
if (lastMigationGroupDone < migrationTable[i].migrationGroup)
{
(*migrationTable[i].migrationFunc)();
completedMigrationGroup = max(migrationTable[i].migrationGroup,completedMigrationGroup);
completedMigrationGroup = max(migrationTable[i].migrationGroup, completedMigrationGroup);
}
}
SilabsConfig::WriteConfigValue(SilabsConfig::kConfigKey_MigrationCounter, completedMigrationGroup);
}


EFR32Migration& EFR32Migration::GetMigrationManager()
EFR32Migration & EFR32Migration::GetMigrationManager()
{
static EFR32Migration sMigrationManager;
return sMigrationManager;
}

} // nameplace EFR32
} // nameplace DeviceLayer
} // nameplace chip
} // namespace EFR32
} // namespace DeviceLayer
} // namespace chip
10 changes: 6 additions & 4 deletions src/platform/silabs/efr32/MigrationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*/

#include <stdio.h>
#include <stdint.h>
#include <stdio.h>

namespace chip {
namespace DeviceLayer {
Expand All @@ -25,16 +25,18 @@ namespace EFR32 {
class EFR32Migration
{
friend class KeyValueStoreManagerImpl;

public:
/**
* The EFR32 migration manager is implemented as a singleton
* User should get the object from this getter.
*/
static EFR32Migration& GetMigrationManager();
static EFR32Migration & GetMigrationManager();
static void applyMigrations();

private:
EFR32Migration() {};
~EFR32Migration() {};
EFR32Migration(){};
~EFR32Migration(){};
};

} // namespace EFR32
Expand Down

0 comments on commit 3abbbf6

Please sign in to comment.