-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathweb_update.cpp
More file actions
355 lines (315 loc) · 8.6 KB
/
Copy pathweb_update.cpp
File metadata and controls
355 lines (315 loc) · 8.6 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "web_update.h"
#include <HTTPClient.h>
#include <Update.h>
#include <WiFi.h>
web_update::web_update(bool debugger, uint8_t timeout_seconds, uint16_t read_buffer, bool https)
{
if (buffer < 64)
buffer = 64;
if (timeout_seconds < 1)
timeout_seconds = 1;
this->buffer = read_buffer;
this->debug = debugger;
this->time_out = timeout_seconds;
this->Https = https;
}
void web_update::host(char *host)
{
this->HostC = host;
}
void web_update::host(IPAddress host)
{
this->ip = host;
}
void web_update::hostPort(uint16_t port)
{
this->hostPort_ = port;
}
void web_update::directory(char *Dir)
{
this->dirC = Dir;
}
void web_update::debugger(bool debugger)
{
this->debug = debugger;
}
void web_update::debuggerProd(bool debugger)
{
this->debugProd = debugger;
}
void web_update::https(bool https)
{
this->Https = https;
}
void web_update::buffer_size(uint16_t Buffer)
{
if (buffer < 64)
buffer = 64;
this->buffer = Buffer;
}
void web_update::timeout(uint8_t timeout)
{
if (timeout < 1)
timeout = 1;
this->time_out = timeout;
}
#ifndef UpdateOverEthernet
uint8_t web_update::update_wifi()
{
vPortEnterCritical;
if (buffer == 1)
buffer = 16384;
if (buffer > esp_get_free_heap_size())
{
if (debug)
Serial.println("Buffer size is bigger than free heap size");
updatingFirmware = false;
vPortExitCritical;
return 4;
}
char *host = new char[strlen(HostC) + strlen(dirC) + 20];
if (Https)
strcpy(host, "https://");
else
strcpy(host, "http://");
strcat(host, HostC);
strcat(host, dirC);
if (WiFi.status() != WL_CONNECTED)
{
if (debug)
Serial.println("WiFi not connected");
updatingFirmware = false;
delete[] host;
vPortExitCritical;
return 1;
}
if (debug)
{
Serial.print("Conecting to HOST: ");
Serial.println(host);
}
wifi_client.begin(host);
uint16_t response = wifi_client.GET();
if (debug)
{
Serial.print("Response: ");
Serial.println(response);
}
if (response != 200)
{
if (debug)
Serial.println("Check host destination and internet conection.");
wifi_client.end();
updatingFirmware = false;
delete[] host;
vPortExitCritical;
return 2;
}
totalLength = wifi_client.getSize();
uint32_t len = totalLength;
Update.begin(UPDATE_SIZE_UNKNOWN);
if (debug)
{
Serial.print("Update size: ");
Serial.println(totalLength);
}
uint8_t *buff = new uint8_t[buffer];
WiFiClient *stream = wifi_client.getStreamPtr();
if (debug)
Serial.println("Starting Update");
time_out *= 1000;
while (wifi_client.connected() && (len > 0 || len == -1))
{
updatingFirmware = true;
if (updateAsyncDelay.isExpired())
{
if (debug)
Serial.println("Update timeout");
Update.end();
wifi_client.end();
updatingFirmware = false;
updatingFirmware = false;
delete[] host;
delete[] buff;
vPortExitCritical;
return 3;
}
size_t size = stream->available();
if (size)
{
uint16_t c = stream->readBytes(buff, ((size > buffer) ? buffer : size));
Update.write(buff, c);
currentLength += c;
if (debug)
{
Serial.print(currentLength);
Serial.print("/");
Serial.println(totalLength);
}
if (len > 0)
len -= c;
if (currentLength >= totalLength)
{
bool ok = Update.end(true);
if (ok)
ESP.restart();
else
{
if (debug)
Serial.println("Update failed");
delete[] buff;
delete[] host;
vPortExitCritical;
return 5;
}
}
}
}
return 0; // to dont show warning on vscode using platformio
}
#endif
bool web_update::isUpdating()
{
return updatingFirmware;
}
#ifdef UpdateOverEthernet
uint8_t web_update::update_ethernet()
{
vPortEnterCritical;
if (buffer == 1)
buffer = 16384;
if (buffer > esp_get_free_heap_size())
{
if (debug)
Serial.println("Buffer size is bigger than free heap size");
updatingFirmware = false;
vPortExitCritical;
return 4;
}
updatingFirmware = true;
if (!Ethernet.hardwareStatus)
{
if (debug)
Serial.println("No ethernet hardware found!");
if (debugProd)
Serial.println("Error on update");
Serial.println();
updatingFirmware = false;
vPortExitCritical;
return 1;
}
if (debug)
{
Serial.print("Conecting to Host: ");
if (ip[0] == 0)
Serial.print(HostC);
else
Serial.print(ip);
}
uint32_t connectionSuccess;
if (ip[0] == 0)
connectionSuccess = ethernet_client.connect(HostC, hostPort_);
else
connectionSuccess = ethernet_client.connect(ip, hostPort_);
if (connectionSuccess) // starts ethernet_client connection, checks for connection
{
if (debug)
{
Serial.print("Connected to Host, now getting firmware, on: ");
if (ip[0] == 0)
Serial.print(HostC);
else
Serial.print(ip);
Serial.print(" ");
Serial.println(dirC);
Serial.println();
}
ethernet_client.print("GET ");
ethernet_client.print(dirC);
ethernet_client.println(" HTTP/1.1");
ethernet_client.print("Host: ");
if (ip[0] == 0)
ethernet_client.println(HostC);
else
ethernet_client.println(ip);
ethernet_client.println("Connection: close");
ethernet_client.println();
}
else
{
if (debug)
Serial.println("\r\nCheck host destination and internet conection.");
updatingFirmware = false;
vPortExitCritical;
return 2;
}
uint8_t *buff = new uint8_t[buffer];
uint32_t bb = 0;
uint32_t resp_header = 0; // get the response header out of the payload
Update.begin(UPDATE_SIZE_UNKNOWN);
time_out *= 1000;
updateAsyncDelay.start(time_out, AsyncDelay::MILLIS);
char lastFourChars[4] = {0, 1, 2, 3};
bool headerEnded = false;
while (ethernet_client.connected() && !ethernet_client.available())
vTaskDelay(1); // waits for data
while (ethernet_client.connected() || ethernet_client.available())
{
if (updateAsyncDelay.isExpired())
{
if (debug)
Serial.println("Update timeout");
if (debugProd)
Serial.println("Update failed, check internet connection.");
Serial.println();
Update.end();
ethernet_client.stop();
updatingFirmware = false;
delete[] buff;
vPortExitCritical;
return 3;
}
uint32_t size = ethernet_client.available();
if (!headerEnded)
{
char c = ethernet_client.read();
if (debug)
Serial.print(c); // get the response header out of the payload
lastFourChars[0] = lastFourChars[1];
lastFourChars[1] = lastFourChars[2];
lastFourChars[2] = lastFourChars[3];
lastFourChars[3] = c;
if (lastFourChars[0] == '\r' && lastFourChars[1] == '\n' && lastFourChars[2] == '\r' && lastFourChars[3] == '\n')
{
headerEnded = true;
if (debug)
Serial.println("----- Header End -----");
continue;
}
if (!headerEnded)
continue;
}
memset(buff, 0, buffer);
uint16_t c = ethernet_client.readBytes(buff, ((size > buffer) ? buffer : size));
Update.write(buff, c);
bb += c;
if (debug)
Serial.println(bb);
delay(10);
}
ethernet_client.stop(); // stop ethernet_client
bool ok = Update.end(true);
if (ok)
ESP.restart();
else
{
if (debug || debugProd)
Serial.println("Update failed.");
Serial.println();
delete[] buff;
vPortExitCritical;
return 5;
}
return 0;
}
#endif