Repository navigation
Expand file tree
/
Copy pathdroid_persistence.cpp
More file actions
60 lines (48 loc) · 1.61 KB
/
Copy pathdroid_persistence.cpp
File metadata and controls
60 lines (48 loc) · 1.61 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
#include "droid_persistence.h"
#include <cstdio>
#include <Preferences.h>
namespace {
constexpr const char *kNamespace = "snips_droids";
} // namespace
DroidStore DroidPersistence::load() {
DroidStore store;
Preferences prefs;
// Not readOnly: avoids a misleading "nvs_open failed: NOT_FOUND" log
// on a device that's never saved this namespace yet — see
// calibration_store.cpp's load() for the full explanation.
if (!prefs.begin(kNamespace, /*readOnly=*/false)) {
return store;
}
const int count = prefs.getInt("count", 0);
char nameKey[8];
char panKey[8];
char name[DroidEntry::kMaxNameLength + 1];
char panId[DroidEntry::kMaxPanIdLength + 1];
for (int i = 0; i < count && i < static_cast<int>(DroidStore::kMaxDroids);
++i) {
std::snprintf(nameKey, sizeof(nameKey), "name%d", i);
std::snprintf(panKey, sizeof(panKey), "pan%d", i);
prefs.getString(nameKey, name, sizeof(name));
prefs.getString(panKey, panId, sizeof(panId));
store.add(name, panId);
}
prefs.end();
return store;
}
void DroidPersistence::save(const DroidStore &store) {
Preferences prefs;
if (!prefs.begin(kNamespace, /*readOnly=*/false)) {
return;
}
prefs.clear(); // drop any entries from a previously-longer list
prefs.putInt("count", static_cast<int>(store.count()));
char nameKey[8];
char panKey[8];
for (size_t i = 0; i < store.count(); ++i) {
std::snprintf(nameKey, sizeof(nameKey), "name%zu", i);
std::snprintf(panKey, sizeof(panKey), "pan%zu", i);
prefs.putString(nameKey, store.at(i).name);
prefs.putString(panKey, store.at(i).panId);
}
prefs.end();
}