-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOtaManager.cpp
More file actions
147 lines (124 loc) · 3.53 KB
/
OtaManager.cpp
File metadata and controls
147 lines (124 loc) · 3.53 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
#include "OtaManager.h"
#include <WiFi.h>
#include <ArduinoOTA.h>
#include "DebugLog.h"
#include "ui/UiManager.h"
#define Serial0 DebugSerial0
static constexpr uint32_t kOtaRetryIntervalMs = 5000;
bool OtaManager::begin(SettingsV1* settings, UiManager* ui) {
_s = settings;
_ui = ui;
_started = false;
_inProgress = false;
_rebootPending = false;
_lastStartAttemptMs = 0;
_rebootAtMs = 0;
_lastLoggedProgressPct = 255;
_lastUiProgressPct = 255;
if (!_s) {
Serial0.println("[OTA] settings=null");
return false;
}
if (!_s->otaEnabled) {
Serial0.println("[OTA] disabled");
return false;
}
if (!WiFi.isConnected()) {
Serial0.println("[OTA] WiFi not connected yet, OTA pending");
return true;
}
return tryStart();
}
bool OtaManager::tryStart() {
if (!_s) {
Serial0.println("[OTA] settings=null");
return false;
}
if (_started) return true;
if (!_s->otaEnabled) {
return false;
}
if (!WiFi.isConnected()) {
return false;
}
WiFi.setSleep(false);
ArduinoOTA.setHostname(_s->otaHostname);
ArduinoOTA.setTimeout(15000);
ArduinoOTA.setRebootOnSuccess(false);
if (_s->otaPort > 0) {
ArduinoOTA.setPort(_s->otaPort);
}
if (strlen(_s->otaPassword) > 0) {
ArduinoOTA.setPassword(_s->otaPassword);
}
ArduinoOTA
.onStart([this]() {
_inProgress = true;
_rebootPending = false;
_rebootAtMs = 0;
_lastLoggedProgressPct = 255;
_lastUiProgressPct = 255;
if (_ui) {
_ui->showOtaStatusScreen("Mise a jour en cours", "Reception du firmware via OTA locale...", 0, false);
}
Serial0.println("[OTA] Start");
})
.onEnd([this]() {
if (_ui) {
_ui->showOtaStatusScreen("Mise a jour terminee", "Verification terminee. Redemarrage en cours...", 100, true);
}
_rebootPending = true;
_rebootAtMs = millis() + kRebootDelayAfterSuccessMs;
Serial0.println("\n[OTA] End");
})
.onProgress([this](unsigned int progress, unsigned int total) {
const uint8_t pct = (total == 0) ? 0 : (uint8_t)((progress * 100U) / total);
if (pct == _lastLoggedProgressPct) return;
_lastLoggedProgressPct = pct;
const uint8_t uiPct = (pct >= 100) ? 100 : (uint8_t)((pct / 5U) * 5U);
if (_ui && (uiPct != _lastUiProgressPct || pct == 100)) {
_lastUiProgressPct = uiPct;
_ui->showOtaStatusScreen("Mise a jour en cours", "Ecriture du firmware en flash...", pct, false);
}
Serial0.printf("[OTA] Progress: %u%%\n", (unsigned)pct);
})
.onError([this](ota_error_t error) {
_inProgress = false;
_rebootPending = false;
_rebootAtMs = 0;
if (_ui) {
_ui->hideOtaStatusScreen();
}
Serial0.printf("[OTA] Error[%u]\n", (unsigned)error);
});
ArduinoOTA.begin();
_started = true;
_lastStartAttemptMs = millis();
Serial0.printf("[OTA] ready on %s:%u hostname=%s\n",
WiFi.localIP().toString().c_str(),
(unsigned)_s->otaPort,
_s->otaHostname);
return true;
}
void OtaManager::loop() {
if (!_s || !_s->otaEnabled) return;
if (_rebootPending) {
if ((int32_t)(millis() - _rebootAtMs) >= 0) {
Serial0.println("[OTA] Reboot");
delay(20);
ESP.restart();
}
return;
}
if (!_started) {
uint32_t now = millis();
if (_lastStartAttemptMs == 0 || (now - _lastStartAttemptMs) >= kOtaRetryIntervalMs) {
_lastStartAttemptMs = now;
if (WiFi.isConnected()) {
tryStart();
}
}
return;
}
ArduinoOTA.handle();
}