Repository navigation
Expand file tree
/
Copy pathload.cpp
More file actions
92 lines (75 loc) · 2.46 KB
/
Copy pathload.cpp
File metadata and controls
92 lines (75 loc) · 2.46 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
82
83
84
85
86
87
88
89
90
91
92
#include "serial/link.h"
#include "serial_log.h"
#include "settings/nvs.h"
#include "settings/settings.h"
#include "settings/values.h"
namespace {
// True when the networks came from the single-network keys of older firmware.
bool loadWifiNetworks() {
if (g_settingsPrefs.isKey(kKeyWifiNets)) {
uint8_t blob[sizeof(WifiNetwork) * SETTINGS_WIFI_NETWORK_MAX] = {};
const size_t length =
g_settingsPrefs.getBytes(kKeyWifiNets, blob, sizeof(blob));
g_wifiNetworks.load(blob, length);
return false;
}
if (!g_settingsPrefs.isKey(kKeyWifiSsid)) {
return false;
}
const String ssid = g_settingsPrefs.getString(kKeyWifiSsid, "");
const String password = g_settingsPrefs.getString(kKeyWifiPass, "");
g_wifiNetworks.add(ssid.c_str(), password.c_str());
return true;
}
void migrateLegacyWifi() {
if (!g_settingsPrefs.begin(kNs, false)) {
return;
}
// An invalid legacy SSID loads as an empty list; its keys go too.
const size_t length = g_wifiNetworks.count() * sizeof(WifiNetwork);
const bool moved =
length == 0 ||
g_settingsPrefs.putBytes(kKeyWifiNets, g_wifiNetworks.data(), length) ==
length;
if (moved) {
g_settingsPrefs.remove(kKeyWifiSsid);
g_settingsPrefs.remove(kKeyWifiPass);
}
g_settingsPrefs.end();
}
// Older firmware stored servo_hold here. Idle/sleep always limp now.
constexpr const char* kLegacyServoHold = "srv_hold";
void dropLegacyServoHold() {
if (g_settingsPrefs.isKey(kLegacyServoHold)) {
g_settingsPrefs.remove(kLegacyServoHold);
}
}
} // namespace
void initSettings() {
g_settings = SettingsValues::defaults();
g_wifiNetworks.clear();
bool legacyWifi = false;
// RW so a missing namespace is created without Preferences NOT_FOUND ERROR.
// Boot reads settings and drops the retired srv_hold key; saves use openForWrite.
if (g_settingsPrefs.begin(kNs, false)) {
g_settings.load();
legacyWifi = loadWifiNetworks();
dropLegacyServoHold();
g_settingsPrefs.end();
} else {
serialLogPrintln("Settings: NVS open failed; using defaults");
}
g_bootSettings = g_settings;
if (legacyWifi) {
migrateLegacyWifi();
}
// Older firmware had Wi-Fi nets but no link_mode; treat as Wi-Fi mode.
if (g_settings.linkMode[0] == '\0' && g_wifiNetworks.count() > 0) {
SettingsPatch patch;
patch.linkMode = "wifi";
saveSettings(patch);
}
// Framed serial API (and LOG frames) start after NVS is loaded.
serialLinkBegin();
g_settings.log("Settings:");
}