-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConfig.cpp
228 lines (189 loc) · 5.35 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "common.h"
#include "Config.h"
#include "net/HTTPClient.h"
#include "net/NTP.h"
#include "Log.h"
#include "Utils.h"
#include "LED.h"
#include "generic.h"
#include "net/TraceAccumulator.h"
#include <string.h>
std::string Config::macAddress = "";
double Config::lat = 0.0;
double Config::lon = 0.0;
uint32_t Config::syslogServer = 0;
bool Config::hasMACAddress() {
return !Config::macAddress.empty();
}
bool Config::hasPosition() {
return Config::lat != 0.0 && Config::lon != 0.0;
}
std::string Config::getMacAddress() {
return Config::macAddress;
}
void Config::setMacAddress(std::string macAddress) {
Config::macAddress = macAddress;
Config::save();
}
void Config::getMacAddressAsByte(byte mac[6]) {
for (int i = 0; i < 6; i++) {
char b[3] = {0, 0, 0};
b[0] = Config::macAddress[i * 2];
b[1] = Config::macAddress[(i * 2) + 1];
mac[i] = (byte) strtol(b, NULL, 16);
}
}
double Config::getLatitude() {
return Config::lat;
}
void Config::setLatitude(double lat) {
Config::lat = lat;
Config::save();
}
double Config::getLongitude() {
return Config::lon;
}
void Config::setLongitude(double lon) {
Config::lon = lon;
Config::save();
}
bool Config::readConfigFile(const char *filepath) {
Log::i("Reading config file %s", filepath);
FILE *fp = fopen(filepath, "r");
if (fp == NULL) {
Log::e("Error opening config file");
return false;
}
char buf[100 + 1];
char *arg;
while (fgets(buf, 100, fp) != NULL) {
arg = strchr(buf, ':');
if (arg == NULL) continue;
arg++;
std::string argument = std::string(arg);
argument = Utils::trim(argument, ' ');
argument = Utils::trim(argument, '\n');
argument = Utils::trim(argument, '\r');
if (strncmp("deviceid", buf, 8) == 0) {
if (argument.size() < 12) continue;
Config::macAddress = argument;
Log::d("Device ID: %s", Config::getMacAddress().c_str());
} else if (strncmp("lat", buf, 3) == 0) {
Config::lat = Utils::atofn(argument.c_str(), 8);
Log::d("Latitude: %lf", Config::getLatitude());
} else if (strncmp("lon", buf, 3) == 0) {
Config::lon = Utils::atofn(argument.c_str(), 8);
Log::d("Longitude: %lf", Config::getLongitude());
}
}
fclose(fp);
Log::i("Config file read OK");
return true;
}
void Config::save() {
FILE *fp = fopen(DEFAULT_CONFIG_PATH, "w");
if (fp == NULL) {
Log::e("Error opening config file for writing");
return;
}
char buf[200 + 1];
memset(buf, 0, 200 + 1);
snprintf(buf, 200, "deviceid:%s\n", Config::macAddress.c_str());
fwrite(buf, 1, strlen(buf), fp);
snprintf(buf, 200, "lat:%f\n", Config::lat);
fwrite(buf, 1, strlen(buf), fp);
snprintf(buf, 200, "lon:%f\n", Config::lon);
fwrite(buf, 1, strlen(buf), fp);
fclose(fp);
}
void Config::init() {
bool cfgOk = readConfigFile(DEFAULT_CONFIG_PATH);
if (!cfgOk) {
loadDefault();
}
}
void Config::loadDefault() {
lat = 0.0;
lon = 0.0;
macAddress = Utils::getInterfaceMAC();
}
bool Config::checkServerConfig() {
std::string cfg = HTTPClient::getConfig();
if (!cfg.empty()) {
std::map<std::string, std::string> params = configSplit(cfg, '|');
if (params.empty()) {
return false;
}
Seismometer *seismometer = Seismometer::getInstance();
std::string path = params["path"];
if (!path.empty()) {
// Firmware update
platformUpgrade(path);
}
// Workaround for old configuration
if (strcmp(params["server"].c_str(), "http://") == 0) {
HTTPClient::setBaseURL(params["server"]);
}
NTP::setNTPServer(params["ntpserver"]);
if (params.count("sigma") == 1) {
seismometer->setSigmaIter(atof(params["sigma"].c_str()));
} else {
seismometer->setSigmaIter(seismometer->getSigmaIter());
}
seismometer->resetLastPeriod();
if (params.count("trace") == 1) {
TraceAccumulator::setTrace(true);
}
return true;
} else {
return false;
}
}
std::map<std::string, std::string> &Config::configSplit(const std::string &s, char delim,
std::map<std::string, std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
Log::d("Splitting line: %s", item.c_str());
std::size_t dppos = item.find(':');
if (dppos == std::string::npos) {
Log::d("Find result in NPOS");
continue;
}
elems[item.substr(0, dppos)] = item.substr(dppos + 1, std::string::npos);
}
return elems;
}
std::map<std::string, std::string> Config::configSplit(const std::string &s, char delim) {
Log::d("Splitting %s", s.c_str());
std::map<std::string, std::string> elems;
try {
configSplit(s, delim, elems);
} catch (std::exception *e) {
Log::e("Config splitting exception: %s", e->what());
}
return elems;
}
void Config::file_put_contents(const char *path, std::string content) {
FILE *fp = fopen(path, "w");
if (fp != NULL) {
fwrite(content.c_str(), content.size(), 1, fp);
fclose(fp);
}
}
void Config::printConfig() {
Log::i("###################### Config ######################### ");
Log::i("Software version: %s", SOFTWARE_VERSION);
Log::i("Platform name: %s", PLATFORM_TAG);
Log::i("DeviceID: %s", Config::getMacAddress().c_str());
Log::i("Position (lat, lon): %lf %lf", Config::getLatitude(), Config::getLongitude());
Log::i("IP: %s", IPaddr::localIP().asString().c_str());
Log::i("Base URL: %s", HTTPClient::getBaseURL().c_str());
#ifdef DEBUG
Log::d("Build version: %s", BUILD_VERSION);
#endif
Log::i("##################### Config end ####################### ");
}
uint32_t Config::getSyslogServer() {
return syslogServer;
}