-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathESP_ConfigStorage.cpp
More file actions
175 lines (146 loc) · 4.35 KB
/
ESP_ConfigStorage.cpp
File metadata and controls
175 lines (146 loc) · 4.35 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "Arduino.h"
#include <EEPROM.h> // reuired for EEPROM opertaions
#include "ESP_ConfigStorage.h"
/*
ESP_ConfigStorage.cpp - EEPROM variable library
Copyright (c) 2016 Subhajit Das. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Data storage structure:
----------------------
| Size | Variable |
----------------------
| 10 | wifimode |
| 30 | ssid |
| 1 | protection |
| 70 | pass |
| 20 | host |
| 1 | configmode |
| 15 | ip |
| 15 | gateway |
| 15 | subnet |
----------------------
*/
#define WIFI_MODE_START (_startAddr)
#define WIFI_MODE_END (WIFI_MODE_START+9)
#define SSID_START (WIFI_MODE_END+1)
#define SSID_END (SSID_START+29)
#define PROTECTION (SSID_END+1)
#define PASSWORD_START (PROTECTION+1)
#define PASSWORD_END (PASSWORD_START+69)
#define HOST_START (PASSWORD_END+1)
#define HOST_END (HOST_START+19)
#define CONFIG_MODE (HOST_END+1)
#define IP_START (CONFIG_MODE+1)
#define IP_END (IP_START+14)
#define GATEWAY_START (IP_END+1)
#define GATEWAY_END (GATEWAY_START+14)
#define SUBNET_START (GATEWAY_END+1)
#define SUBNET_END (SUBNET_START+14)
Storage::Storage(uint16_t startAddress) {
EEPROM.begin(512); // initiate EEPROM storage
_startAddr = startAddress;
}
Storage::Storage(uint16_t startAddress, uint16_t size) {
EEPROM.begin(size); // initiate EEPROM storage
_startAddr = startAddress;
}
void Storage::clear() {
// clearing EEPROM
for (uint16_t i = 0; i < 512; ++i) {
EEPROM.write(i, 0);
}
}
String Storage::getEEPROM(uint16_t startAddr, uint16_t endAddr) {
String data = "";
byte b;
for (unsigned int i = startAddr; i <= endAddr; ++i) {
b = EEPROM.read(i);
if (b == 0 || b == '\n') break;
data += char(b);
}
return data;
}
boolean Storage::setEEPROM(uint16_t startAddr, uint16_t endAddr, String data) {
if (endAddr < startAddr + data.length() - 1) return false;
for (byte i = 0; i < data.length(); ++i) {
EEPROM.write(startAddr + i, data[i]);
}
return true;
}
// wifimode
String Storage::getWiFiMode() {
return getEEPROM(WIFI_MODE_START, WIFI_MODE_END);
}
boolean Storage::setWiFiMode(String data) {
return setEEPROM(WIFI_MODE_START, WIFI_MODE_END, data);
}
// ssid
String Storage::getSSID() {
return getEEPROM(SSID_START, SSID_END);
}
boolean Storage::setSSID(String data) {
return setEEPROM(SSID_START, SSID_END, data);
}
// encryption
boolean Storage::getProtection() {
return boolean(byte(EEPROM.read(PROTECTION)) != byte(0));
}
void Storage::setProtection(byte data) {
EEPROM.write(PROTECTION, data);
}
// pass
String Storage::getPassword() {
return getEEPROM(PASSWORD_START, PASSWORD_END);
}
boolean Storage::setPassword(String data) {
return setEEPROM(PASSWORD_START, PASSWORD_END, data);
}
// host
String Storage::getHostname() {
return getEEPROM(HOST_START, HOST_END);
}
boolean Storage::setHostname(String data) {
return setEEPROM(HOST_START, HOST_END, data);
}
// configmode
boolean Storage::getConfigMode() {
return boolean(byte(EEPROM.read(CONFIG_MODE)) != byte(0));
}
void Storage::setConfigMode(byte data) {
EEPROM.write(CONFIG_MODE, data);
}
// ip
String Storage::getIP() {
return getEEPROM(IP_START, IP_END);
}
boolean Storage::setIP(String data) {
return setEEPROM(IP_START, IP_END, data);
}
// gateway
String Storage::getGateway() {
return getEEPROM(GATEWAY_START, GATEWAY_END);
}
boolean Storage::setGateway(String data) {
return setEEPROM(GATEWAY_START, GATEWAY_END, data);
}
// subnet
String Storage::getSubnet() {
return getEEPROM(SUBNET_START, SUBNET_END);
}
boolean Storage::setSubnet(String data) {
return setEEPROM(SUBNET_START, SUBNET_END, data);
}
void Storage::finalize() {
EEPROM.commit(); // set to eeprom
}