-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.cpp
274 lines (231 loc) · 7.76 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// **********************************************************************************
// Remora Configuration source file
// **********************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend this library but please abide with the CC-BY-SA license:
// Attribution-NonCommercial-ShareAlike 4.0 International License
// http://creativecommons.org/licenses/by-nc-sa/4.0/
//
// This program works with the Remora board
// see schematic here https://github.com/thibdct/programmateur-fil-pilote-wifi
//
// Written by Charles-Henri Hallard (http://hallard.me)
//
// History 2016-02-04 - First release
//
// All text above must be included in any redistribution.
//
// **********************************************************************************
#include "./config.h"
#ifdef ESP8266
// Configuration structure for whole program
_Config config;
uint16_t crc16Update(uint16_t crc, uint8_t a)
{
int i;
crc ^= a;
for (i = 0; i < 8; ++i) {
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
/* ======================================================================
Function: eeprom_dump
Purpose : dump eeprom value to serial
Input : -
Output : -
Comments: -
====================================================================== */
void eepromDump(uint8_t bytesPerRow)
{
uint16_t i,b;
char buf[10];
uint16_t j=0 ;
// default to 16 bytes per row
if (bytesPerRow==0)
bytesPerRow=16;
Debugln();
// loop thru EEP address
for (i = 0; i < sizeof(_Config); i++) {
// First byte of the row ?
if (j==0) {
// Display Address
Debugf("%04X : ", i);
}
// write byte in hex form
Debugf("%02X ", EEPROM.read(i));
// Last byte of the row ?
// start a new line
if (++j >= bytesPerRow) {
j=0;
Debugln();
}
}
}
/* ======================================================================
Function: readConfig
Purpose : fill config structure with data located into eeprom
Input : true if we need to clear actual struc in case of error
Output : true if config found and crc ok, false otherwise
Comments: -
====================================================================== */
bool readConfig (bool clear_on_error)
{
uint16_t crc = ~0;
uint8_t * pconfig = (uint8_t *) &config ;
uint8_t data ;
// For whole size of config structure
for (uint16_t i = 0; i < sizeof(_Config); ++i) {
// read data
data = EEPROM.read(i);
// save into struct
*pconfig++ = data ;
// calc CRC
crc = crc16Update(crc, data);
}
// CRC Error ?
if (crc != 0) {
// Clear config if wanted
if (clear_on_error)
memset(&config, 0, sizeof( _Config ));
return false;
}
return true ;
}
/* ======================================================================
Function: saveConfig
Purpose : save config structure values into eeprom
Input : -
Output : true if saved and readback ok
Comments: once saved, config is read again to check the CRC
====================================================================== */
bool saveConfig (void)
{
uint8_t * pconfig ;
bool ret_code;
//eepromDump(32);
// Init pointer
pconfig = (uint8_t *) &config ;
// Init CRC
config.crc = ~0;
// For whole size of config structure, pre-calculate CRC
for (uint16_t i = 0; i < sizeof (_Config) - 2; ++i)
config.crc = crc16Update(config.crc, *pconfig++);
// Re init pointer
pconfig = (uint8_t *) &config ;
// For whole size of config structure, write to EEP
for (uint16_t i = 0; i < sizeof(_Config); ++i)
EEPROM.write(i, *pconfig++);
// Physically save
EEPROM.commit();
// Read Again to see if saved ok, but do
// not clear if error this avoid clearing
// default config and breaks OTA
ret_code = readConfig(false);
DebugF("Write config ");
if (ret_code) {
Debugln(F("OK!"));
} else {
Debugln(F("Error!"));
}
//eepromDump(32);
// return result
return (ret_code);
}
/* ======================================================================
Function: showConfig
Purpose : display configuration
Input : -
Output : -
Comments: -
====================================================================== */
void showConfig()
{
DebuglnF("===== Wifi");
DebugF("ssid :"); Debugln(config.ssid);
DebugF("psk :"); Debugln(config.psk);
DebugF("host :"); Debugln(config.host);
DebugF("ap_psk :"); Debugln(config.ap_psk);
DebugF("OTA auth :"); Debugln(config.ota_auth);
DebugF("OTA port :"); Debugln(config.ota_port);
DebugF("Config :");
if (config.config & CFG_RGB_LED) DebugF(" RGB");
if (config.config & CFG_DEBUG) DebugF(" DEBUG");
if (config.config & CFG_LCD) DebugF(" LCD");
_wdt_feed();
DebuglnF("\r\n===== Emoncms");
DebugF("host :"); Debugln(config.emoncms.host);
DebugF("port :"); Debugln(config.emoncms.port);
DebugF("url :"); Debugln(config.emoncms.url);
DebugF("key :"); Debugln(config.emoncms.apikey);
DebugF("node :"); Debugln(config.emoncms.node);
DebugF("freq :"); Debugln(config.emoncms.freq);
_wdt_feed();
DebuglnF("\r\n===== Jeedom");
DebugF("host :"); Debugln(config.jeedom.host);
DebugF("port :"); Debugln(config.jeedom.port);
DebugF("url :"); Debugln(config.jeedom.url);
DebugF("key :"); Debugln(config.jeedom.apikey);
DebugF("finger :");
for (int i=0; i < CFG_JDOM_FINGER_PRINT_SIZE; i++) {
DEBUG_SERIAL.print(config.jeedom.fingerprint[i], HEX);
DebugF(" ");
}
Debugln();
DebugF("compteur :"); Debugln(config.jeedom.adco);
DebugF("freq :"); Debugln(config.jeedom.freq);
_wdt_feed();
DebugF("LED Bright: "); Debugln(config.led_bright);
}
/* ======================================================================
Function: ResetConfig
Purpose : Set configuration to default values
Input : -
Output : -
Comments: -
====================================================================== */
void resetConfig(void)
{
// enable default configuration
memset(&config, 0, sizeof(_Config));
// Set default Hostname
sprintf_P(config.host, PSTR("Remora_%06X"), ESP.getChipId());
strcpy_P(config.ota_auth, PSTR(DEFAULT_OTA_AUTH));
config.ota_port = DEFAULT_OTA_PORT ;
// Add other init default config here
// Emoncms
strcpy_P(config.emoncms.host, CFG_EMON_DEFAULT_HOST);
config.emoncms.port = CFG_EMON_DEFAULT_PORT;
strcpy_P(config.emoncms.url, CFG_EMON_DEFAULT_URL);
config.emoncms.apikey[0] = '\0';
config.emoncms.node = 0;
config.emoncms.freq = 0;
// Jeedom
strcpy_P(config.jeedom.host, CFG_JDOM_DEFAULT_HOST);
config.jeedom.port = CFG_JDOM_DEFAULT_PORT;
strcpy_P(config.jeedom.url, CFG_JDOM_DEFAULT_URL);
strcpy_P(config.jeedom.adco, CFG_JDOM_DEFAULT_ADCO);
config.jeedom.apikey[0] = '\0';
for (int i=0; i < CFG_JDOM_FINGER_PRINT_SIZE; i++) {
config.jeedom.fingerprint[i] = 0;
}
config.jeedom.freq = 0;
config.led_bright = DEFAULT_LED_BRIGHTNESS;
config.config |= CFG_RGB_LED;
// save back
saveConfig();
}
String getFingerPrint(void) {
char buffer[61] = { 0 };
sprintf(buffer, "%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X"
, config.jeedom.fingerprint[0], config.jeedom.fingerprint[1], config.jeedom.fingerprint[2], config.jeedom.fingerprint[3]
, config.jeedom.fingerprint[4], config.jeedom.fingerprint[5], config.jeedom.fingerprint[6], config.jeedom.fingerprint[7]
, config.jeedom.fingerprint[8], config.jeedom.fingerprint[9], config.jeedom.fingerprint[10], config.jeedom.fingerprint[11]
, config.jeedom.fingerprint[12], config.jeedom.fingerprint[13], config.jeedom.fingerprint[14], config.jeedom.fingerprint[15]
, config.jeedom.fingerprint[16], config.jeedom.fingerprint[17], config.jeedom.fingerprint[18], config.jeedom.fingerprint[19]);
return String(buffer);
}
#endif // ESP8266