Repository navigation
Expand file tree
/
Copy pathconfig.cpp
More file actions
148 lines (122 loc) · 3.15 KB
/
Copy pathconfig.cpp
File metadata and controls
148 lines (122 loc) · 3.15 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
#include "pins.h"
#include "config.h"
#include "serial.h"
#include "SD.h"
#include "sdcontrol.h"
#include "FSWebServer.h"
int Config::readINI(bool bSD) {
SERIAL_ECHOLN("Going to load config from SDCard SETUP.INI file");
File file;
if(bSD)
{
sdcontrol.takeControl();
file = SD.open(CONFIG_FILE, "r");
}
else
{
file = INTERNAL_FS.open(CONFIG_FILE, "r");
}
if (!file) {
SERIAL_ECHOLN("Failed to open config file");
if(bSD) sdcontrol.relinquishControl();
return 1;
}
// 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("\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 = -2;
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 = -3;
goto FAIL;
}
}
else continue; // Bad line
}
if(step != 2) { // We miss ssid or password
SERIAL_ECHOLN("Please check your SSDI or PASSWORD in ini file");
rst = -4;
goto FAIL;
}
FAIL:
file.close();
if(bSD) sdcontrol.relinquishControl();
return rst;
}
unsigned char Config::load() {
INTERNAL_FS.begin(true);
// Try to get the config from ini file (user override)
if(0 == readINI(true)) // SDCard
{
return 1; // Return as connected before
}
if(0 == readINI(false)) // SPIFFS/INTERNAL_FS
{
return 1; // Return as connected before
}
SERIAL_ECHOLN("Going to load config from Preferences");
prefs.begin("sdwifi", false);
prefs.getBytes("Creds", &data, sizeof(data) );
if(data.flag) {
SERIAL_ECHOLN("Going to use the old network config");
}
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;
data.flag = 1;
strncpy(data.ssid, ssid, sizeof(data.ssid) );
strncpy(data.psw, password, sizeof(data.psw) );
prefs.putBytes("Creds", &data, sizeof(data) );
}
void Config::save() {
if(data.ssid == NULL || data.psw == NULL)
return;
data.flag = 1;
prefs.putBytes("Creds", &data, sizeof(data) );
}
void Config::clear() {
memset(&data, 0, sizeof(data));
prefs.putBytes("Creds", &data, sizeof(data) );
}
Config config;