forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathota.cpp
More file actions
408 lines (337 loc) · 12.1 KB
/
Copy pathota.cpp
File metadata and controls
408 lines (337 loc) · 12.1 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "ota.h"
#include "config.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <Update.h>
#include <BLE2902.h>
// OTA State
static uint8_t otaStatus = OTA_STATUS_IDLE;
static uint8_t otaProgress = 0;
static bool otaCancelled = false;
// WiFi credentials (stored temporarily)
static char wifiSSID[WIFI_MAX_SSID_LEN + 1] = {0};
static char wifiPassword[WIFI_MAX_PASS_LEN + 1] = {0};
static bool wifiCredentialsSet = false;
// Firmware URL
static char firmwareURL[OTA_MAX_URL_LEN + 1] = {0};
static bool firmwareURLSet = false;
// OTA task state
static bool otaTaskRunning = false;
static TaskHandle_t otaTaskHandle = NULL;
// BLE Characteristics
static BLECharacteristic *otaControlCharacteristic = NULL;
static BLECharacteristic *otaDataCharacteristic = NULL;
// Forward declarations
static void ota_task(void *parameter);
static bool connect_wifi();
static bool download_and_install_firmware();
// Callback for OTA Control characteristic
class OTAControlCallback : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) override {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
ota_handle_command((uint8_t *)value.data(), value.length());
}
}
void onRead(BLECharacteristic *pCharacteristic) override {
// Return current status when read
uint8_t status[2] = {otaStatus, otaProgress};
pCharacteristic->setValue(status, 2);
}
};
void ota_set_characteristics(BLECharacteristic *controlChar, BLECharacteristic *dataChar) {
otaControlCharacteristic = controlChar;
otaDataCharacteristic = dataChar;
}
void ota_handle_command(uint8_t *data, size_t length) {
if (length < 1) return;
uint8_t command = data[0];
Serial.printf("OTA: Received command 0x%02X, length %d\n", command, length);
switch (command) {
case OTA_CMD_SET_WIFI: {
// Format: [cmd, ssid_len, ssid..., pass_len, pass...]
if (length < 3) {
Serial.println("OTA: Invalid WiFi command length");
ota_notify_status(OTA_STATUS_ERROR);
return;
}
uint8_t ssidLen = data[1];
if (ssidLen > WIFI_MAX_SSID_LEN || length < 3 + ssidLen) {
Serial.println("OTA: Invalid SSID length");
ota_notify_status(OTA_STATUS_ERROR);
return;
}
memcpy(wifiSSID, &data[2], ssidLen);
wifiSSID[ssidLen] = '\0';
uint8_t passLen = data[2 + ssidLen];
if (passLen > WIFI_MAX_PASS_LEN || length < 3 + ssidLen + passLen) {
Serial.println("OTA: Invalid password length");
ota_notify_status(OTA_STATUS_ERROR);
return;
}
memcpy(wifiPassword, &data[3 + ssidLen], passLen);
wifiPassword[passLen] = '\0';
wifiCredentialsSet = true;
Serial.printf("OTA: WiFi credentials set - SSID: %s\n", wifiSSID);
ota_notify_status(OTA_STATUS_IDLE);
break;
}
case OTA_CMD_SET_URL: {
// Format: [cmd, url_len (2 bytes big-endian), url...]
if (length < 4) {
Serial.println("OTA: Invalid URL command length");
ota_notify_status(OTA_STATUS_ERROR);
return;
}
uint16_t urlLen = (data[1] << 8) | data[2];
if (urlLen > OTA_MAX_URL_LEN || length < 3 + urlLen) {
Serial.println("OTA: Invalid URL length");
ota_notify_status(OTA_STATUS_ERROR);
return;
}
memcpy(firmwareURL, &data[3], urlLen);
firmwareURL[urlLen] = '\0';
firmwareURLSet = true;
Serial.printf("OTA: Firmware URL set: %s\n", firmwareURL);
ota_notify_status(OTA_STATUS_IDLE);
break;
}
case OTA_CMD_START_OTA: {
if (!wifiCredentialsSet) {
Serial.println("OTA: WiFi credentials not set");
ota_notify_status(OTA_STATUS_ERROR);
return;
}
if (!firmwareURLSet) {
Serial.println("OTA: Firmware URL not set");
ota_notify_status(OTA_STATUS_ERROR);
return;
}
if (otaTaskRunning) {
Serial.println("OTA: Update already in progress");
return;
}
// Start OTA task
otaCancelled = false;
otaTaskRunning = true;
xTaskCreate(ota_task, "ota_task", 8192, NULL, 5, &otaTaskHandle);
break;
}
case OTA_CMD_CANCEL_OTA: {
ota_cancel();
break;
}
case OTA_CMD_GET_STATUS: {
ota_notify_status(otaStatus, otaProgress);
break;
}
default:
Serial.printf("OTA: Unknown command 0x%02X\n", command);
ota_notify_status(OTA_STATUS_ERROR);
break;
}
}
void ota_notify_status(uint8_t status, uint8_t progress) {
otaStatus = status;
otaProgress = progress;
if (otaDataCharacteristic != NULL) {
uint8_t notification[2] = {status, progress};
otaDataCharacteristic->setValue(notification, 2);
otaDataCharacteristic->notify();
}
Serial.printf("OTA: Status 0x%02X, Progress %d%%\n", status, progress);
}
static void ota_task(void *parameter) {
Serial.println("OTA: Task started");
// Step 1: Connect to WiFi
if (!connect_wifi()) {
ota_notify_status(OTA_STATUS_WIFI_FAILED);
otaTaskRunning = false;
vTaskDelete(NULL);
return;
}
if (otaCancelled) {
WiFi.disconnect(true);
ota_notify_status(OTA_STATUS_IDLE);
otaTaskRunning = false;
vTaskDelete(NULL);
return;
}
// Step 2: Download and install firmware
if (!download_and_install_firmware()) {
WiFi.disconnect(true);
otaTaskRunning = false;
vTaskDelete(NULL);
return;
}
// Step 3: Reboot
Serial.println("OTA: Preparing to reboot...");
ota_notify_status(OTA_STATUS_REBOOTING);
delay(2000); // Give time for BLE notification to be sent
Serial.println("OTA: Disconnecting WiFi...");
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
delay(500);
Serial.println("OTA: Rebooting now!");
ESP.restart();
// Should never reach here
vTaskDelete(NULL);
}
static bool connect_wifi() {
Serial.printf("OTA: Connecting to WiFi: %s\n", wifiSSID);
ota_notify_status(OTA_STATUS_WIFI_CONNECTING);
WiFi.mode(WIFI_STA);
WiFi.begin(wifiSSID, wifiPassword);
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED) {
if (otaCancelled) {
Serial.println("OTA: WiFi connection cancelled");
return false;
}
if (millis() - startTime > WIFI_CONNECT_TIMEOUT_MS) {
Serial.println("OTA: WiFi connection timeout");
return false;
}
delay(500);
Serial.print(".");
}
Serial.printf("\nOTA: WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str());
ota_notify_status(OTA_STATUS_WIFI_CONNECTED);
return true;
}
static bool download_and_install_firmware() {
Serial.printf("OTA: Downloading firmware from: %s\n", firmwareURL);
ota_notify_status(OTA_STATUS_DOWNLOADING, 0);
// Determine if URL is HTTPS
bool isHttps = strncmp(firmwareURL, "https://", 8) == 0;
Serial.printf("OTA: Using %s\n", isHttps ? "HTTPS" : "HTTP");
WiFiClient *client = nullptr;
WiFiClientSecure *secureClient = nullptr;
if (isHttps) {
secureClient = new WiFiClientSecure;
if (!secureClient) {
Serial.println("OTA: Failed to create WiFiClientSecure");
ota_notify_status(OTA_STATUS_DOWNLOAD_FAILED);
return false;
}
// Skip certificate validation for testing (TODO: add proper certs for production)
secureClient->setInsecure();
client = secureClient;
} else {
client = new WiFiClient;
if (!client) {
Serial.println("OTA: Failed to create WiFiClient");
ota_notify_status(OTA_STATUS_DOWNLOAD_FAILED);
return false;
}
}
HTTPClient http;
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.begin(*client, firmwareURL);
http.setTimeout(30000); // 30 second timeout
http.addHeader("User-Agent", "ESP32-OTA/1.0");
Serial.printf("OTA: Starting HTTP GET request...\n");
int httpCode = http.GET();
Serial.printf("OTA: HTTP response code: %d\n", httpCode);
if (httpCode != HTTP_CODE_OK) {
Serial.printf("OTA: HTTP GET failed, code: %d\n", httpCode);
ota_notify_status(OTA_STATUS_DOWNLOAD_FAILED);
http.end();
if (secureClient) delete secureClient; else delete client;
return false;
}
int contentLength = http.getSize();
if (contentLength <= 0) {
Serial.println("OTA: Invalid content length");
ota_notify_status(OTA_STATUS_DOWNLOAD_FAILED);
http.end();
if (secureClient) delete secureClient; else delete client;
return false;
}
Serial.printf("OTA: Firmware size: %d bytes\n", contentLength);
// Check if there's enough space
if (!Update.begin(contentLength)) {
Serial.println("OTA: Not enough space for update");
ota_notify_status(OTA_STATUS_INSTALL_FAILED);
http.end();
if (secureClient) delete secureClient; else delete client;
return false;
}
WiFiClient *stream = http.getStreamPtr();
uint8_t buffer[1024];
int totalRead = 0;
int lastProgress = -1;
ota_notify_status(OTA_STATUS_INSTALLING, 0);
while (http.connected() && totalRead < contentLength) {
if (otaCancelled) {
Serial.println("OTA: Download cancelled");
Update.abort();
http.end();
if (secureClient) delete secureClient; else delete client;
ota_notify_status(OTA_STATUS_IDLE);
return false;
}
size_t available = stream->available();
if (available > 0) {
size_t toRead = min(available, sizeof(buffer));
int bytesRead = stream->readBytes(buffer, toRead);
if (bytesRead > 0) {
if (Update.write(buffer, bytesRead) != bytesRead) {
Serial.println("OTA: Write failed");
Update.abort();
http.end();
if (secureClient) delete secureClient; else delete client;
ota_notify_status(OTA_STATUS_INSTALL_FAILED);
return false;
}
totalRead += bytesRead;
int progress = (totalRead * 100) / contentLength;
// Notify progress every 5%
if (progress != lastProgress && progress % 5 == 0) {
ota_notify_status(OTA_STATUS_INSTALLING, progress);
lastProgress = progress;
}
}
} else {
delay(10);
}
}
http.end();
// Properly delete the client based on type
if (secureClient) {
delete secureClient;
} else {
delete client;
}
if (totalRead != contentLength) {
Serial.printf("OTA: Incomplete download: %d/%d\n", totalRead, contentLength);
Update.abort();
ota_notify_status(OTA_STATUS_DOWNLOAD_FAILED);
return false;
}
if (!Update.end(true)) {
Serial.printf("OTA: Update failed: %s\n", Update.errorString());
ota_notify_status(OTA_STATUS_INSTALL_FAILED);
return false;
}
Serial.println("OTA: Update complete!");
ota_notify_status(OTA_STATUS_INSTALL_COMPLETE, 100);
delay(500); // Give BLE time to send notification
return true;
}
void ota_loop() {
// Currently nothing needed in loop - OTA runs in separate task
}
uint8_t ota_get_status() {
return otaStatus;
}
bool ota_is_busy() {
return otaTaskRunning;
}
void ota_cancel() {
if (otaTaskRunning) {
Serial.println("OTA: Cancelling...");
otaCancelled = true;
}
}