Repository navigation
Expand file tree
/
Copy pathEEPROMHandler.cpp
More file actions
168 lines (147 loc) · 4.4 KB
/
Copy pathEEPROMHandler.cpp
File metadata and controls
168 lines (147 loc) · 4.4 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
#include "EEPROMHandler.h"
EEPROMHandler::EEPROMHandler() {
this->config = new SonofPowCofig();
size_t size = sizeof(SonofPowCofig);
EEPROM.begin(size);
this->load();
uint8_t ecsrc = this->ecsrc();
if(ecsrc!=this->config->ecsrc) {
Serial.write("ecsrc not equal!");
this->byDefault();
this->loaded = false;
} else {
if(ecsrc>0){
this->loaded = true;
} else {
this->loaded = false;
}
}
}
EEPROMHandler::~EEPROMHandler(void) {
delete this->config;
}
void EEPROMHandler::save() {
this->config->ecsrc = this->ecsrc();
uint8_t* p = (uint8_t*) this->config;
size_t sz1 = sizeof(SonofPowCofig);
for( size_t i=0; i<sz1; i++) {
EEPROM.write(i, *(p + i));
}
EEPROM.commit();
}
uint8_t EEPROMHandler::isLoaded() {
return this->loaded;
}
//======================================================================================
uint8_t* EEPROMHandler::getWifiAPSsid(void) {
return this->config->wifiApSsid;
}
uint8_t* EEPROMHandler::getWifiAPPass(void) {
return this->config->wifiApPass;
}
EEPROMHandler* EEPROMHandler::setWifiApSsid(char* ssid, uint8_t length) {
uint8_t* dest = this->config->wifiApSsid;
memcpy(dest, ssid, length);
dest[length] = 0;
return this;
}
EEPROMHandler* EEPROMHandler::setWifiApPass(char* pass, uint8_t length) {
uint8_t* dest = this->config->wifiApPass;
memcpy(dest, pass, length);
dest[length] = 0;
return this;
}
uint8_t EEPROMHandler::isWifiStValid() {
return *(this->getWifiStSsid()) != 0;
}
uint8_t* EEPROMHandler::getWifiStSsid() {
return this->config->wifiStSsid;
}
uint8_t* EEPROMHandler::getWifiStPass() {
return this->config->wifiStPass;
}
EEPROMHandler* EEPROMHandler::setWifiStSsid(char* ssid, uint8_t length) {
uint8_t* dest = this->config->wifiStSsid;
memcpy(dest, ssid, length);
dest[length] = 0;
return this;
}
EEPROMHandler* EEPROMHandler::setWifiStPass(char* pass, uint8_t length) {
uint8_t* dest = this->config->wifiStPass;
memcpy(dest, pass, length);
dest[length] = 0;
return this;
}
//======================================================================================
uint8_t* EEPROMHandler::getServerUser() {
return this->config->httpServerUser;
}
uint8_t* EEPROMHandler::getServerPass() {
return this->config->httpServerPass;
}
EEPROMHandler* EEPROMHandler::setServerUser(char* user, uint8_t length) {
uint8_t* dest = this->config->httpServerUser;
memcpy(dest, user, length);
dest[length] = 0;
return this;
}
EEPROMHandler* EEPROMHandler::setServerPass(char* pass, uint8_t length) {
uint8_t* dest = this->config->httpServerPass;
memcpy(dest, pass, length);
dest[length] = 0;
return this;
}
//======================================================================================
uint8_t EEPROMHandler::isClientValid() {
return *(this->getClientUser()) != 0;
}
uint8_t* EEPROMHandler::getClientUser() {
return this->config->httpClientUser;
}
uint8_t* EEPROMHandler::getClientPass() {
return this->config->httpClientPass;
}
EEPROMHandler* EEPROMHandler::setClientUser(char* user, uint8_t length) {
uint8_t* dest = this->config->httpClientUser;
memcpy(dest, user, length);
dest[length] = 0;
return this;
}
EEPROMHandler* EEPROMHandler::setClientPass(char* pass, uint8_t length) {
uint8_t* dest = this->config->httpClientPass;
memcpy(dest, pass, length);
dest[length] = 0;
return this;
}
//======================================================================================
void EEPROMHandler::load() {
size_t sz1 = sizeof(SonofPowCofig);
uint8_t* p = (uint8_t*) this->config;
for( size_t i=0; i<sz1; i++ ) {
uint8_t c = EEPROM.read(i);
*(p + i) = c;
}
}
void EEPROMHandler::byDefault() {
size_t sz1 = sizeof(SonofPowCofig);
uint8_t* p = (uint8_t*) this->config;
for( size_t i=0; i<sz1; i++ ) {
*(p + i) = 0;
}
this->setWifiApSsid("esp8266_ssid", 12);
this->setWifiApPass("esp8266_pass", 12);
this->setServerUser("admin", 5);
this->setServerPass("admin", 5);
}
uint16_t EEPROMHandler::ecsrc(void) {
uint16_t src = 0;
size_t sz1 = sizeof(SonofPowCofig);
uint8_t* p = (uint8_t*)this->config;
uint16_t ecsrc = this->config->ecsrc;
this->config->ecsrc = 0;
for( size_t i=0; i<sz1; i++){
src +=*(p++);
}
this->config->ecsrc = ecsrc;
return src;
}