-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathStorage.cpp
More file actions
81 lines (71 loc) · 2.23 KB
/
Storage.cpp
File metadata and controls
81 lines (71 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <Arduino.h>
#include "Storage.h"
#include <MsgPack.h>
#include <ExtFlashLoader.h>
static auto FlashStartAddress = reinterpret_cast<const uint8_t* const>(0x04000000);
Storage::Storage(ExtFlashLoader::QSPIFlash& flash) :
Flash_(flash)
{
Flash_.initialize();
Flash_.reset();
Flash_.enterToMemoryMode();
}
void Storage::Load()
{
if (memcmp(&FlashStartAddress[0], "AZ01", 4) != 0)
{
WiFiSSID.clear();
WiFiPassword.clear();
IdScope.clear();
RegistrationId.clear();
SymmetricKey.clear();
EdgeImpulseHmacKey.clear();
}
else
{
MsgPack::Unpacker unpacker;
unpacker.feed(&FlashStartAddress[8], *(const uint32_t*)&FlashStartAddress[4]);
MsgPack::str_t str[6]; // Increased size to 6
// Attempt to deserialize 6 strings. If old data has 5, this might be an issue.
// A robust solution would check unpacker.parsed() or use versioning.
// For now, we assume it either works or EdgeImpulseHmacKey remains empty/default.
unpacker.deserialize(str[0], str[1], str[2], str[3], str[4], str[5]);
WiFiSSID = str[0].c_str();
WiFiPassword = str[1].c_str();
IdScope = str[2].c_str();
RegistrationId = str[3].c_str();
SymmetricKey = str[4].c_str();
if (str[5].size() > 0) { // Basic check if the 6th string was populated
EdgeImpulseHmacKey = str[5].c_str();
} else {
EdgeImpulseHmacKey = ""; // Default if not found or empty
}
}
}
void Storage::Save()
{
MsgPack::Packer packer;
{
MsgPack::str_t str[6]; // Increased size to 6
str[0] = WiFiSSID.c_str();
str[1] = WiFiPassword.c_str();
str[2] = IdScope.c_str();
str[3] = RegistrationId.c_str();
str[4] = SymmetricKey.c_str();
str[5] = EdgeImpulseHmacKey.c_str(); // Add HMAC key
packer.serialize(str[0], str[1], str[2], str[3], str[4], str[5]); // Serialize 6 strings
}
std::vector<uint8_t> buf(4 + 4 + packer.size());
memcpy(&buf[0], "AZ01", 4);
*(uint32_t*)&buf[4] = packer.size();
memcpy(&buf[8], packer.data(), packer.size());
ExtFlashLoader::writeExternalFlash(Flash_, 0, &buf[0], buf.size(), [](std::size_t bytes_processed, std::size_t bytes_total, bool verifying) { return true; });
}
void Storage::Erase()
{
Flash_.exitFromMemoryMode();
Flash_.writeEnable();
Flash_.eraseSector(0);
Flash_.waitProgram(0);
Flash_.enterToMemoryMode();
}