-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsettings.cpp
More file actions
159 lines (120 loc) · 3.04 KB
/
Copy pathsettings.cpp
File metadata and controls
159 lines (120 loc) · 3.04 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "settings.h"
#include "settings_internal.h"
#include <cstring>
uint32_t settingsSleepTimeoutMin() {
return g_sleepTimeoutMin;
}
uint32_t settingsSleepTimeoutMs() {
return g_sleepTimeoutMin * 60UL * 1000UL;
}
const char* settingsHostname() {
return g_hostname;
}
const char* settingsBootHostname() {
return g_bootHostname;
}
uint8_t settingsVolume() {
return g_volume;
}
bool settingsWelcomeEnabled() {
return g_welcome;
}
bool settingsSerialLogEnabled() {
return g_serialLog;
}
uint32_t settingsContinuousTimeoutMin() {
return g_continuousTimeoutMin;
}
const char* settingsLoading() {
return g_loading;
}
const char* settingsAccessToken() {
return g_accessToken;
}
bool settingsAccessTokenSet() {
return g_accessToken[0] != '\0';
}
const char* settingsWifiSsid() {
return g_wifiSsid;
}
const char* settingsWifiPassword() {
return g_wifiPassword;
}
bool settingsWifiConfigured() {
return g_wifiSsid[0] != '\0';
}
bool settingsWifiPasswordSet() {
return g_wifiPassword[0] != '\0';
}
bool settingsValidateSleepTimeout(uint32_t sleepTimeoutMin) {
return sleepTimeoutMin >= SETTINGS_SLEEP_TIMEOUT_MIN_MIN &&
sleepTimeoutMin <= SETTINGS_SLEEP_TIMEOUT_MAX_MIN;
}
bool settingsValidateHostname(const char* hostname) {
if (hostname == nullptr) {
return false;
}
const size_t len = strlen(hostname);
if (len < 1 || len > SETTINGS_HOSTNAME_MAX_LEN) {
return false;
}
if (hostname[0] == '-' || hostname[len - 1] == '-') {
return false;
}
for (size_t i = 0; i < len; i++) {
const char c = hostname[i];
const bool ok =
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-';
if (!ok) {
return false;
}
}
return true;
}
bool settingsValidateVolume(uint8_t volume) {
return volume <= SETTINGS_VOLUME_MAX;
}
bool settingsValidateContinuousTimeout(uint32_t continuousTimeoutMin) {
return continuousTimeoutMin >= SETTINGS_CONTINUOUS_TIMEOUT_MIN_MIN &&
continuousTimeoutMin <= SETTINGS_CONTINUOUS_TIMEOUT_MAX_MIN;
}
bool settingsValidateLoading(const char* loading) {
if (loading == nullptr) {
return false;
}
return strcmp(loading, "progress") == 0 ||
strcmp(loading, "sleep_inertia") == 0;
}
bool settingsValidateAccessToken(const char* accessToken) {
if (accessToken == nullptr) {
return false;
}
const size_t len = strlen(accessToken);
if (len > SETTINGS_ACCESS_TOKEN_MAX_LEN) {
return false;
}
for (size_t i = 0; i < len; i++) {
const unsigned char c =
static_cast<unsigned char>(accessToken[i]);
if (c < 0x20 || c > 0x7E) {
return false;
}
}
return true;
}
bool settingsValidateWifiSsid(const char* wifiSsid) {
if (wifiSsid == nullptr) {
return false;
}
const size_t len = strlen(wifiSsid);
return len >= 1 && len <= SETTINGS_WIFI_SSID_MAX_LEN;
}
bool settingsValidateWifiPassword(const char* wifiPassword) {
if (wifiPassword == nullptr) {
return false;
}
return strlen(wifiPassword) <= SETTINGS_WIFI_PASSWORD_MAX_LEN;
}