-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.cpp
188 lines (168 loc) · 4.53 KB
/
storage.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "storage.h"
#include <EEPROM.h>
#define SSID_LENGTH 64
#define PASSWORD_LENGTH 32
#define HOSTNAME_LENGTH 16
struct Config {
// WIFI
char SSID[SSID_LENGTH];
char WifiPassword[PASSWORD_LENGTH];
char Hostname[HOSTNAME_LENGTH];
char OTAPassword[PASSWORD_LENGTH];
// MQTT
char MQTTHostname[HOSTNAME_LENGTH];
char MQTTUsername[PASSWORD_LENGTH];
char MQTTPassword[PASSWORD_LENGTH];
} currentConfig;
const size_t ConfigLength = sizeof(currentConfig);
void ReadConfig() {
char *c = (char *)(¤tConfig);
for (int i=0; i<ConfigLength;i++) {
c[i] = EEPROM.read(i);
}
// Pad all strings to be null terminated
currentConfig.SSID[SSID_LENGTH-1] = 0x00;
currentConfig.WifiPassword[PASSWORD_LENGTH-1] = 0x00;
currentConfig.OTAPassword[PASSWORD_LENGTH-1] = 0x00;
currentConfig.Hostname[HOSTNAME_LENGTH-1] = 0x00;
currentConfig.MQTTHostname[HOSTNAME_LENGTH-1] = 0x00;
currentConfig.MQTTUsername[PASSWORD_LENGTH-1] = 0x00;
currentConfig.MQTTPassword[PASSWORD_LENGTH-1] = 0x00;
}
void SaveConfig() {
currentConfig.SSID[SSID_LENGTH-1] = 0x00;
currentConfig.WifiPassword[PASSWORD_LENGTH-1] = 0x00;
currentConfig.OTAPassword[PASSWORD_LENGTH-1] = 0x00;
currentConfig.Hostname[HOSTNAME_LENGTH-1] = 0x00;
currentConfig.MQTTHostname[HOSTNAME_LENGTH-1] = 0x00;
currentConfig.MQTTUsername[PASSWORD_LENGTH-1] = 0x00;
currentConfig.MQTTPassword[PASSWORD_LENGTH-1] = 0x00;
char *c = (char *)(¤tConfig);
for (int i=0; i<ConfigLength;i++) {
EEPROM.write(i, c[i]);
}
EEPROM.commit();
}
String GetWifiSSID() {
return String(currentConfig.SSID);
}
String GetWifiPassword() {
return String(currentConfig.WifiPassword);
}
String GetOTAPassword() {
return String(currentConfig.OTAPassword);
}
String GetHostname() {
return String(currentConfig.Hostname);
}
String GetMQTTHost() {
return String(currentConfig.MQTTHostname);
}
String GetMQTTUser() {
return String(currentConfig.MQTTUsername);
}
String GetMQTTPass() {
return String(currentConfig.MQTTPassword);
}
void SaveWifiSSID(String ssid) {
int maxLen = SSID_LENGTH-1;
if (ssid.length() < maxLen) {
maxLen = ssid.length();
}
for (int i = 0; i < SSID_LENGTH; i++) {
if (i < maxLen) {
currentConfig.SSID[i] = ssid[i];
} else {
currentConfig.SSID[i] = 0x00;
}
}
SaveConfig();
}
void SaveWifiPassword(String pass) {
int maxLen = PASSWORD_LENGTH-1;
if (pass.length() < maxLen) {
maxLen = pass.length();
}
for (int i = 0; i < PASSWORD_LENGTH; i++) {
if (i < maxLen) {
currentConfig.WifiPassword[i] = pass[i];
} else {
currentConfig.WifiPassword[i] = 0x00;
}
}
SaveConfig();
}
void SaveOTAPassword(String pass) {
int maxLen = PASSWORD_LENGTH-1;
if (pass.length() < maxLen) {
maxLen = pass.length();
}
for (int i = 0; i < PASSWORD_LENGTH; i++) {
if (i < maxLen) {
currentConfig.OTAPassword[i] = pass[i];
} else {
currentConfig.OTAPassword[i] = 0x00;
}
}
SaveConfig();
}
void SaveHostname(String hostname) {
int maxLen = HOSTNAME_LENGTH-1;
if (hostname.length() < maxLen) {
maxLen = hostname.length();
}
for (int i = 0; i < HOSTNAME_LENGTH; i++) {
if (i < maxLen) {
currentConfig.Hostname[i] = hostname[i];
} else {
currentConfig.Hostname[i] = 0x00;
}
}
SaveConfig();
}
void InitStorage() {
EEPROM.begin(ConfigLength);
ReadConfig();
}
void SaveMQTTHost(String hostname) {
int maxLen = HOSTNAME_LENGTH-1;
if (hostname.length() < maxLen) {
maxLen = hostname.length();
}
for (int i = 0; i < HOSTNAME_LENGTH; i++) {
if (i < maxLen) {
currentConfig.MQTTHostname[i] = hostname[i];
} else {
currentConfig.MQTTHostname[i] = 0x00;
}
}
SaveConfig();
}
void SaveMQTTUser(String username) {
int maxLen = PASSWORD_LENGTH-1;
if (username.length() < maxLen) {
maxLen = username.length();
}
for (int i = 0; i < PASSWORD_LENGTH; i++) {
if (i < maxLen) {
currentConfig.MQTTUsername[i] = username[i];
} else {
currentConfig.MQTTUsername[i] = 0x00;
}
}
SaveConfig();
}
void SaveMQTTPass(String password) {
int maxLen = PASSWORD_LENGTH-1;
if (password.length() < maxLen) {
maxLen = password.length();
}
for (int i = 0; i < PASSWORD_LENGTH; i++) {
if (i < maxLen) {
currentConfig.MQTTPassword[i] = password[i];
} else {
currentConfig.MQTTPassword[i] = 0x00;
}
}
SaveConfig();
}