Skip to content

Commit

Permalink
add tests, checks and new method for array
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyLadanov committed May 24, 2024
1 parent b4a6b85 commit c598208
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 57 deletions.
2 changes: 2 additions & 0 deletions Components/NVS_Lib/Inc/NVS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 45 additions & 0 deletions Components/NVS_Lib/Src/NVS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,16 @@ NVS_Cell *NVS::FindCellByKey(const char *key)

uint32_t Bytes = Page->GetHeaderSize();

bool status = false;


while ((!Cell->IsEmpty()) && (Bytes < CurrentPageUsedBytes))
{
if (Cell->IsKey(key))
{
if (Cell->State == NVS_Cell::STATE_VALID)
{
status = true;
break;
}
}
Expand All @@ -288,6 +291,11 @@ NVS_Cell *NVS::FindCellByKey(const char *key)
Cell = Cell->GetNext();
}

if (!status)
{
Cell = nullptr;
}

return Cell;
}

Expand All @@ -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();
}
Expand All @@ -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;
Expand Down
150 changes: 93 additions & 57 deletions Core/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<typeof(Check)>("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<typeof(Check)>("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<typeof(Check)>("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<typeof(Check)>("test");

Storage.SetValue("test1", Writeble++);

Check = Storage.GetValue<typeof(Check)>("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<typeof(Check)>("test");

Storage.SetValue("test1", Writeble++);
CheckProbeU16 = storage_for_check.GetValue<typeof(CheckProbeU16)>("test_u16");

Check = Storage.GetValue<typeof(Check)>("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<typeof(Check)>("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<typeof(Check)>("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<typeof(Check)>("test1");

Check = Storage.GetValue<typeof(Check)>("test");





printf("\r\n\r\nTest after reset...\r\n\r\n");
Check(CheckStorage);
printf("\r\n\r\n");

printf("End");
return 0;
Expand Down

0 comments on commit c598208

Please sign in to comment.