diff --git a/Components/NVS_Lib/Inc/NVS.hpp b/Components/NVS_Lib/Inc/NVS.hpp index 4c731da..1a3980e 100644 --- a/Components/NVS_Lib/Inc/NVS.hpp +++ b/Components/NVS_Lib/Inc/NVS.hpp @@ -61,6 +61,8 @@ class NVS uint8_t *GetArray(const char *key, uint16_t *out_size = nullptr); + int8_t GetArray(const char *key, uint8_t *out_buf, uint16_t *out_size); + char *GetString(const char *key); uint32_t GetAvaliableSpaceInBytes(void); diff --git a/Components/NVS_Lib/Src/NVS.cpp b/Components/NVS_Lib/Src/NVS.cpp index 09589fa..e558b03 100644 --- a/Components/NVS_Lib/Src/NVS.cpp +++ b/Components/NVS_Lib/Src/NVS.cpp @@ -272,6 +272,8 @@ NVS_Cell *NVS::FindCellByKey(const char *key) uint32_t Bytes = Page->GetHeaderSize(); + bool status = false; + while ((!Cell->IsEmpty()) && (Bytes < CurrentPageUsedBytes)) { @@ -279,6 +281,7 @@ NVS_Cell *NVS::FindCellByKey(const char *key) { if (Cell->State == NVS_Cell::STATE_VALID) { + status = true; break; } } @@ -288,6 +291,11 @@ NVS_Cell *NVS::FindCellByKey(const char *key) Cell = Cell->GetNext(); } + if (!status) + { + Cell = nullptr; + } + return Cell; } @@ -296,6 +304,11 @@ NVS_Cell *NVS::FindCellByKey(const char *key) char *NVS::GetString(const char *key) { NVS_Cell *Cell = FindCellByKey(key); + + if ((!Cell) || (Cell->Header.Type != NVS_Cell::TYPE_ARRAY)) + { + return nullptr; + } return Cell->GetString(); } @@ -316,10 +329,42 @@ int8_t NVS::SetValue(const char *key, uint8_t *buf, uint16_t len) uint8_t *NVS::GetArray(const char *key, uint16_t *out_size) { NVS_Cell *Cell = FindCellByKey(key); + + + if ((!Cell) || (Cell->Header.Type != NVS_Cell::TYPE_ARRAY)) + { + return nullptr; + } + return Cell->GetArray(out_size); } + +int8_t NVS::GetArray(const char *key, uint8_t *out_buf, uint16_t *out_size) +{ + NVS_Cell *Cell = FindCellByKey(key); + uint16_t BlobSize = 0; + + if ((!Cell) || (Cell->Header.Type != NVS_Cell::TYPE_ARRAY)) + { + return -1; + } + + uint8_t *blob = Cell->GetArray(&BlobSize); + + if (out_size) + { + *out_size = BlobSize; + } + + memcpy(out_buf, blob, BlobSize); + + + return 0; +} + + uint32_t NVS::GetAvaliableSpaceInBytes(void) { uint32_t used = 0; diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index d532758..5b7d35b 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -5,8 +5,8 @@ -uint8_t PageBuffer1[128]; -uint8_t PageBuffer2[128]; +static uint8_t PageBuffer1[256]; +static uint8_t PageBuffer2[256]; class SettingsFlash_If : public NVS_IFlash { @@ -40,101 +40,137 @@ static const NVS::FlashDesc_t FlashDescriptor[] = static SettingsFlash_If FlashInterface; -NVS Storage(FlashInterface); -NVS CheckStorage(FlashInterface); +static NVS Storage(FlashInterface); +static NVS CheckStorage(FlashInterface); -uint16_t Check = 0; -uint16_t Writeble = 0; +static uint16_t ProbeU16 = 0x35; +static uint16_t CheckProbeU16 = 0; -char *buf = "hello, world"; -char *check_buf = nullptr; +char *StrProbe = (char *) "hello, world"; +char *CheckStrProbe = nullptr; -struct Test_t +struct ProbeStruct_t { uint32_t a1; uint32_t a2; }; +static ProbeStruct_t ProbeStruct = {35, 35}; -Test_t Probe = {35, 35}; +static ProbeStruct_t CheckProbeStruct; -Test_t *Read; +static ProbeStruct_t *CheckProbeStructPtr = nullptr; -uint16_t outsize = 0; +static uint16_t outsize = 0; -// Основная программа -int main(void) -{ - Storage.Init((NVS::FlashDesc_t *) FlashDescriptor, 2); - - Storage.SetValue("test", buf); - - Storage.RemoveValue("test"); - - printf("Avaliavble in bytes: %d\r\n", Storage.GetAvaliableSpaceInBytes()); - printf("Avaliavble in blocks: %d\r\n", Storage.GetAvaliableSpaceInBlocks()); - - Storage.SetValue("test", (uint8_t *) &Probe, sizeof(Probe)); - - Read = (Test_t *) Storage.GetArray("test", &outsize); - - check_buf = Storage.GetString("test"); - - Check = Storage.GetValue("test"); - - Storage.SetValue("test", Writeble++); +static void Write(void) +{ + if (!Storage.SetValue("test_string", StrProbe)) + { + printf("Write string success!\r\n"); + } + else + { + printf("Write string failed!\r\n"); + } - Check = Storage.GetValue("test"); - Storage.SetValue("test1", Writeble++); + if (!Storage.SetValue("test_struct", (uint8_t *) &ProbeStruct, sizeof(ProbeStruct))) + { + printf("Write struct success!\r\n"); + } + else + { + printf("Write struct failed!\r\n"); + } - Check = Storage.GetValue("test1"); - Storage.SetValue("test", Writeble++); + if (!Storage.SetValue("test_u16", ProbeU16)) + { + printf("Write u16 success!\r\n"); + } + else + { + printf("Write u16 failed!\r\n"); + } +} - Check = Storage.GetValue("test"); - Storage.SetValue("test1", Writeble++); - Check = Storage.GetValue("test1"); +static void Check(NVS &storage_for_check) +{ + CheckStrProbe = storage_for_check.GetString("test_string"); - Storage.SetValue("test", Writeble++); + if ((CheckStrProbe) && (!strcmp(CheckStrProbe, StrProbe))) + { + printf("String validation success!\r\n"); + } + else + { + printf("String validation failed!\r\n"); + } - Check = Storage.GetValue("test"); - Storage.SetValue("test1", Writeble++); + CheckProbeU16 = storage_for_check.GetValue("test_u16"); - Check = Storage.GetValue("test1"); + if (ProbeU16 == CheckProbeU16) + { + printf("U16 validation success!\r\n"); + } + else + { + printf("U16 validation failed!\r\n"); + } - Storage.SetValue("test", Writeble++); - Storage.SetValue("test1", Writeble++); + CheckProbeStructPtr = (ProbeStruct_t *)storage_for_check.GetArray("test_struct", &outsize); + if ((CheckProbeStructPtr) && (CheckProbeStructPtr->a1 == ProbeStruct.a1) && (CheckProbeStructPtr->a2 == ProbeStruct.a2) && (outsize == sizeof(ProbeStruct_t))) + { + printf("Struct validation 1 success!\r\n"); + } + else + { + printf("Struct validation 1 failed!\r\n"); + } - Check = Storage.GetValue("test1"); + if ((!storage_for_check.GetArray("test_struct", (uint8_t *) &CheckProbeStruct, &outsize)) && (CheckProbeStruct.a1 == ProbeStruct.a1) && (CheckProbeStruct.a2 == ProbeStruct.a2) && (outsize == sizeof(ProbeStruct_t))) + { + printf("Struct validation 2 success!\r\n"); + } + else + { + printf("Struct validation 2 failed!\r\n"); + } +} - Check = Storage.GetValue("test"); +// Основная программа +int main(void) +{ + Storage.Init((NVS::FlashDesc_t *) FlashDescriptor, 2); + for (uint8_t tries = 0; tries < 35; tries++) + { + Write(); + } + printf("\r\n\r\nTest immediatly...\r\n\r\n"); + Check(Storage); + printf("\r\n\r\n"); + printf("Initialization new storage in defined pages...\r\n"); CheckStorage.Init((NVS::FlashDesc_t *) FlashDescriptor, 2); - - Check = Storage.GetValue("test1"); - - Check = Storage.GetValue("test"); - - - - - + printf("\r\n\r\nTest after reset...\r\n\r\n"); + Check(CheckStorage); + printf("\r\n\r\n"); printf("End"); return 0;