forked from ardyesp/ESPWebDAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
192 lines (165 loc) · 4.28 KB
/
config.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
189
190
191
192
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <SdFat.h>
#include <EEPROM.h>
#include "pins.h"
#include "config.h"
#include "serial.h"
#include "sdControl.h"
int Config::loadSD() {
SdFat sdfat;
SERIAL_ECHOLN("Going to load config from INI file");
if(!sdcontrol.canWeTakeBus()) {
SERIAL_ECHOLN("Marlin is controling the bus");
return -1;
}
sdcontrol.takeBusControl();
if(!sdfat.begin(SD_CS, SPI_FULL_SPEED)) {
SERIAL_ECHOLN("Initial SD failed");
sdcontrol.relinquishBusControl();
return -2;
}
File file = sdfat.open("SETUP.INI", FILE_READ);
if (!file) {
SERIAL_ECHOLN("Open INI file failed");
sdcontrol.relinquishBusControl();
return -3;
}
// Get SSID and PASSWORD from file
int rst = 0,step = 0;
String buffer,sKEY,sValue;
while (file.available()) { // check for EOF
buffer = file.readStringUntil('\n');
if(buffer.length() == 0) continue; // Empty line
buffer.replace(" ", ""); // Delete all blank
buffer.replace("\r", ""); // Delete all CR
int iS = buffer.indexOf('='); // Get the seperator
if(iS < 0) continue; // Bad line
sKEY = buffer.substring(0,iS);
sValue = buffer.substring(iS+1);
if(sKEY == "SSID") {
SERIAL_ECHOLN("INI file : SSID found");
if(sValue.length()>0) {
memset(data.ssid,'\0',WIFI_SSID_LEN);
sValue.toCharArray(data.ssid,WIFI_SSID_LEN);
step++;
}
else {
rst = -4;
goto FAIL;
}
}
else if(sKEY == "PASSWORD") {
SERIAL_ECHOLN("INI file : PASSWORD found");
if(sValue.length()>0) {
memset(data.psw,'\0',WIFI_PASSWD_LEN);
sValue.toCharArray(data.psw,WIFI_PASSWD_LEN);
step++;
}
else {
rst = -5;
goto FAIL;
}
}
else continue; // Bad line
}
if(step != 2) { // We miss ssid or password
//memset(data,) // TODO: do we need to empty the data?
SERIAL_ECHOLN("Please check your SSDI or PASSWORD in ini file");
rst = -6;
goto FAIL;
}
FAIL:
file.close();
sdcontrol.relinquishBusControl();
return rst;
}
unsigned char Config::load() {
// Try to get the config from ini file
if(0 == loadSD())
{
return 1; // Return as connected before
}
SERIAL_ECHOLN("Going to load config from EEPROM");
EEPROM.begin(EEPROM_SIZE);
uint8_t *p = (uint8_t*)(&data);
for (int i = 0; i < sizeof(data); i++)
{
*(p + i) = EEPROM.read(i);
}
EEPROM.commit();
if(data.flag) {
SERIAL_ECHOLN("Going to use the old config to connect the network");
}
SERIAL_ECHOLN("We didn't connect the network before");
return data.flag;
}
char* Config::ssid() {
return data.ssid;
}
void Config::ssid(char* ssid) {
if(ssid == NULL) return;
strncpy(data.ssid,ssid,WIFI_SSID_LEN);
}
char* Config::password() {
return data.psw;
}
void Config::password(char* password) {
if(password == NULL) return;
strncpy(data.psw,password,WIFI_PASSWD_LEN);
}
void Config::save(const char*ssid,const char*password) {
if(ssid ==NULL || password==NULL)
return;
EEPROM.begin(EEPROM_SIZE);
data.flag = 1;
strncpy(data.ssid, ssid, WIFI_SSID_LEN);
strncpy(data.psw, password, WIFI_PASSWD_LEN);
uint8_t *p = (uint8_t*)(&data);
for (int i = 0; i < sizeof(data); i++)
{
EEPROM.write(i, *(p + i));
}
EEPROM.commit();
}
void Config::save() {
if(data.ssid == NULL || data.psw == NULL)
return;
EEPROM.begin(EEPROM_SIZE);
data.flag = 1;
uint8_t *p = (uint8_t*)(&data);
for (int i = 0; i < sizeof(data); i++)
{
EEPROM.write(i, *(p + i));
}
EEPROM.commit();
}
// Save to ip address to sdcard
int Config::save_ip(const char *ip) {
SdFat sdfat;
SERIAL_ECHOLN("Going to save config to ip.gcode file");
if(!sdcontrol.canWeTakeBus()) {
SERIAL_ECHOLN("Marlin is controling the bus");
return -1;
}
sdcontrol.takeBusControl();
if(!sdfat.begin(SD_CS, SPI_FULL_SPEED)) {
SERIAL_ECHOLN("Initial SD failed");
sdcontrol.relinquishBusControl();
return -2;
}
// Remove the old file
sdfat.remove("ip.gcode");
File file = sdfat.open("ip.gcode", FILE_WRITE);
if (!file) {
SERIAL_ECHOLN("Open ip file failed");
sdcontrol.relinquishBusControl();
return -3;
}
// Get SSID and PASSWORD from file
char buf[21] = "M117 ";
strncat(buf,ip,15);
file.write(buf, 21);
file.close();
}
Config config;