-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path991_HTTPUpdate.ino
320 lines (291 loc) · 10.3 KB
/
991_HTTPUpdate.ino
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
void upIn()
{
// const uint8_t fingerprint[20] = {0xcc, 0xaa, 0x48, 0x48, 0x66, 0x46, 0x0e, 0x91, 0x53, 0x2c, 0x9c, 0x7c, 0x23, 0x2a, 0xb1, 0x74, 0x4d, 0x29, 0x9d, 0x33};
std::unique_ptr<BearSSL::WiFiClientSecure> clientup(new BearSSL::WiFiClientSecure);
//clientup->setFingerprint(fingerprint);
clientup->setInsecure();
HTTPClient https;
if (https.begin(*clientup, "https://guest:guest:x-oauth-basic@raw.githubusercontent.com/InnuendoPi/MQTTDevice2/master/data/index.html"))
{
int httpCode = https.GET();
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
// Serial.printf("*** SYSINFO: [HTTPS] GET index.html Antwort: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK)
{
// get lenght of document (is -1 when Server sends no Content-Length header)
int len = https.getSize();
// create buffer for read
static uint8_t buff[128] = {0};
// Open file for write
fsUploadFile = LittleFS.open("/index.html", "w");
if (!fsUploadFile)
{
//Serial.printf( F("file open failed"));
Serial.println("Abbruch!");
Serial.println("*** SYSINFO: Fehler beim Speichern index.html");
https.end();
return;
}
// read all data from server
while (https.connected() && (len > 0 || len == -1))
{
// get available data size
size_t size = clientup->available();
//Serial.printf("*** SYSINFO: [HTTPS] index size avail: %d\n", size);
if (size)
{
//read up to 128 byte
int c = clientup->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
// write it to file
fsUploadFile.write(buff, c);
if (len > 0)
{
len -= c;
}
}
delay(1);
}
Serial.println("*** SYSINFO: Index Update abgeschlossen.");
fsUploadFile.close();
LittleFS.remove("/update.txt");
fsUploadFile = LittleFS.open("/update2.txt", "w");
int bytesWritten = fsUploadFile.print("0");
fsUploadFile.close();
}
else
return;
}
else
{
Serial.println("Abbruch!");
Serial.printf("*** SYSINFO: Update index.html Fehler: %s\n", https.errorToString(httpCode).c_str());
https.end();
LittleFS.end(); // unmount LittleFS
ESP.restart();
return;
}
https.end();
return;
}
return;
}
void upCerts()
{
// const uint8_t fingerprint[20] = {0xcc, 0xaa, 0x48, 0x48, 0x66, 0x46, 0x0e, 0x91, 0x53, 0x2c, 0x9c, 0x7c, 0x23, 0x2a, 0xb1, 0x74, 0x4d, 0x29, 0x9d, 0x33};
std::unique_ptr<BearSSL::WiFiClientSecure> clientup(new BearSSL::WiFiClientSecure);
//clientup->setFingerprint(fingerprint);
clientup->setInsecure();
HTTPClient https;
if (https.begin(*clientup, "https://guest:guest:x-oauth-basic@raw.githubusercontent.com/InnuendoPi/MQTTDevice2/master/Info/ce.rts"))
{
int httpCode = https.GET();
if (httpCode > 0)
{
// Serial.printf("*** SYSINFO: [HTTPS] GET certs.ar Antwort: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK)
{
int len = https.getSize();
static uint8_t buff[128] = {0};
fsUploadFile = LittleFS.open("/certs.ar", "w");
if (!fsUploadFile)
{
Serial.println("Abbruch!");
Serial.println("*** SYSINFO: Fehler beim Speichern certs.ar");
https.end();
return;
}
while (https.connected() && (len > 0 || len == -1))
{
size_t size = clientup->available();
if (size)
{
int c = clientup->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
fsUploadFile.write(buff, c);
if (len > 0)
{
len -= c;
}
}
delay(1);
}
fsUploadFile.close();
Serial.println("*** SYSINFO: Certs Update abgeschlossen.");
LittleFS.remove("/update2.txt");
fsUploadFile = LittleFS.open("/update3.txt", "w");
int bytesWritten = fsUploadFile.print("0");
fsUploadFile.close();
}
else
return;
}
else
{
Serial.println("Abbruch!");
Serial.printf("*** SYSINFO: Update certs Fehler: %s\n", https.errorToString(httpCode).c_str());
https.end();
LittleFS.end(); // unmount LittleFS
ESP.restart();
return;
}
https.end();
return;
}
return;
}
void upFirm()
{
BearSSL::CertStore certStore;
int numCerts = certStore.initCertStore(LittleFS, PSTR("/certs.idx"), PSTR("/certs.ar"));
Serial.print(F("Number of CA certs read: "));
Serial.println(numCerts);
if (numCerts == 0)
{
Serial.println(F("*** SYSINFO: No certs found. Did you run certs-from-mozill.py and upload the LittleFS directory before running?"));
return; // Can't connect to anything w/o certs!
}
BearSSL::WiFiClientSecure clientFirm;
clientFirm.setCertStore(&certStore);
clientFirm.setInsecure();
ESPhttpUpdate.onStart(update_started);
ESPhttpUpdate.onEnd(update_finished);
//ESPhttpUpdate.onProgress(update_progress);
ESPhttpUpdate.onError(update_error);
t_httpUpdate_return ret = ESPhttpUpdate.update(clientFirm, "https://raw.githubusercontent.com/InnuendoPi/MQTTDevice2/master/build/MQTTDevice2.ino.bin");
switch (ret)
{
case HTTP_UPDATE_FAILED:
Serial.printf("*** SYSINFO: HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("*** SYSINFO: HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("*** SYSINFO: HTTP_UPDATE_OK");
break;
}
return;
}
// // Stack dump
// // https://github.com/esp8266/Arduino/blob/master/doc/Troubleshooting/stack_dump.md
void updateSys()
{
if (LittleFS.exists("/update.txt"))
{
fsUploadFile = LittleFS.open("/update.txt", "r");
String line;
while (fsUploadFile.available())
{
line = char(fsUploadFile.read());
}
fsUploadFile.close();
int i = line.toInt();
if (i > 3)
{
LittleFS.remove("/update.txt");
Serial.println("*** SYSINFO: ERROR Index Update");
return;
}
fsUploadFile = LittleFS.open("/update.txt", "w");
i++;
int bytesWritten = fsUploadFile.print(i);
fsUploadFile.close();
fsUploadFile = LittleFS.open("/log1.txt", "w");
bytesWritten = fsUploadFile.print((i));
fsUploadFile.close();
Serial.print("*** SYSINFO Starte Index Update Free Heap: ");
Serial.println(ESP.getFreeHeap());
upIn();
}
if (LittleFS.exists("/update2.txt"))
{
fsUploadFile = LittleFS.open("/update2.txt", "r");
String line;
while (fsUploadFile.available())
{
line = char(fsUploadFile.read());
}
fsUploadFile.close();
int i = line.toInt();
if (i > 3)
{
LittleFS.remove("/update2.txt");
Serial.println("*** SYSINFO: ERROR Cert Update");
return;
}
fsUploadFile = LittleFS.open("/update2.txt", "w");
i++;
int bytesWritten = fsUploadFile.print(i);
fsUploadFile.close();
fsUploadFile = LittleFS.open("/log2.txt", "w");
bytesWritten = fsUploadFile.print((i));
fsUploadFile.close();
Serial.print("*** SYSINFO Starte Cert Update Free Heap: ");
Serial.println(ESP.getFreeHeap());
upCerts();
}
if (LittleFS.exists("/update3.txt"))
{
fsUploadFile = LittleFS.open("/update3.txt", "r");
String line;
while (fsUploadFile.available())
{
line = char(fsUploadFile.read());
}
fsUploadFile.close();
int i = line.toInt();
if (i > 3)
{
LittleFS.remove("/update3.txt");
Serial.println("*** SYSINFO: ERROR Firmware Update");
return;
}
fsUploadFile = LittleFS.open("/update3.txt", "w");
i++;
int bytesWritten = fsUploadFile.print(i);
fsUploadFile.close();
fsUploadFile = LittleFS.open("/log3.txt", "w");
bytesWritten = fsUploadFile.print((i));
fsUploadFile.close();
Serial.print("*** SYSINFO Starte Firmware Update Free Heap: ");
Serial.println(ESP.getFreeHeap());
upFirm();
}
}
void startHTTPUpdate()
{
// Starte Updates
fsUploadFile = LittleFS.open("/update.txt", "w");
if (!fsUploadFile)
{
DEBUG_MSG("%s\n", "*** Fehler WebUpdate Datei erstellen auf LittleFS nicht möglich");
return;
}
else
{
int bytesWritten = fsUploadFile.print("0");
fsUploadFile.close();
}
cbpiEventSystem(EM_REBOOT);
}
void update_progress(int cur, int total)
{
Serial.printf("*** SYSINFO: Firmware Update %d von %d Bytes\n", cur, total);
}
void update_started()
{
Serial.println("*** SYSINFO: Firmware Update gestartet");
}
void update_finished()
{
Serial.println("*** SYSINFO: Firmware Update beendet");
LittleFS.remove("/update3.txt");
}
void update_error(int err)
{
Serial.printf("*** SYSINFO: Firmware Update Fehler error code %d\n", err);
LittleFS.end(); // unmount LittleFS
ESP.restart();
}