-
Notifications
You must be signed in to change notification settings - Fork 7
/
EOTAUpdate.cpp
229 lines (195 loc) · 5.68 KB
/
EOTAUpdate.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
#include <HTTPClient.h>
#include <MD5Builder.h>
#include <StreamString.h>
#include <Update.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <EOTAUpdate.h>
EOTAUpdate::EOTAUpdate(
const String &url,
const unsigned currentVersion,
const unsigned long updateIntervalMs)
:
_url(url),
_forceSSL(url.startsWith("https://")),
_currentVersion(currentVersion),
_updateIntervalMs(updateIntervalMs),
_lastUpdateMs(0)
{
}
bool EOTAUpdate::CheckAndUpdate(bool force)
{
const bool hasEverChecked = _lastUpdateMs != 0;
const bool lastCheckIsRecent = (millis() - _lastUpdateMs < _updateIntervalMs);
if (!force && hasEverChecked && lastCheckIsRecent)
{
return false;
}
if (WiFi.status() != WL_CONNECTED)
{
log_e("Wifi not connected");
return false;
}
log_i("Checking for updates");
_lastUpdateMs = millis();
String binURL;
String binMD5;
if (GetUpdateFWURL(binURL, binMD5))
{
log_i("Update found. Performing update");
return PerformOTA(binURL, binMD5);
}
return false;
}
bool EOTAUpdate::GetUpdateFWURL(String &binURL, String &binMD5)
{
return GetUpdateFWURL(binURL, binMD5, _url);
}
bool EOTAUpdate::GetUpdateFWURL(String &binURL, String &binMD5, const String &url, const uint16_t retries)
{
log_d("Fetching OTA config from: %s", url.c_str());
if (retries == 0)
{
log_e("Too many retries/redirections");
return false;
}
bool isSSL = url.startsWith("https");
if (_forceSSL && !isSSL)
{
log_e("Trying to access a non-ssl URL on a secure update checker");
return false;
}
HTTPClient httpClient;
auto client = WiFiClient();
if (!httpClient.begin(url))
{
log_e("Error initializing client");
return false;
}
const char *headerKeys[] = {"Location"};
httpClient.collectHeaders(headerKeys, 1);
int httpCode = httpClient.GET();
switch (httpCode)
{
case HTTP_CODE_OK:
break;
case HTTP_CODE_MOVED_PERMANENTLY:
if (httpClient.hasHeader("Location"))
{
return GetUpdateFWURL(binURL, binMD5, httpClient.header("Location"), retries - 1);
}
// Do not break here
default:
log_e("[HTTP] [ERROR] [%d] %s",
httpCode,
httpClient.errorToString(httpCode).c_str());
log_d("Response:\n%s",
httpClient.getString().c_str());
return false;
}
auto & payloadStream = httpClient.getStream();
binURL = payloadStream.readStringUntil('\n');
const unsigned newVersionNumber = payloadStream.readStringUntil('\n').toInt();
binMD5 = payloadStream.readStringUntil('\n');
const String newVersionString = payloadStream.readStringUntil('\n');
httpClient.end();
if (binURL.length() == 0)
{
log_e("Error parsing remote path of new binary");
return false;
}
if (newVersionNumber == 0)
{
log_e("Error parsing version number");
return false;
}
if (binMD5.length() > 0 && binMD5.length() != 32)
{
log_e("The MD5 is not 32 characters long. Aborting update");
return false;
}
log_d("Fetched update information:");
log_d("File url: %s", binURL.c_str());
log_d("File MD5: %s", binMD5.c_str());
log_d("Current version: %u", _currentVersion);
log_d("Published version: [%u] %s", newVersionNumber, newVersionString.c_str());
log_d("Update available: %s", (newVersionNumber > _currentVersion) ? "YES" : "NO");
return newVersionNumber > _currentVersion;
}
bool EOTAUpdate::PerformOTA(String &binURL, String &binMD5)
{
log_d("Fetching OTA from: %s", binURL.c_str());
if (binURL.length() == 0)
{
return false;
}
bool isSSL = binURL.startsWith("https");
if (_forceSSL && !isSSL)
{
log_e("Trying to access a non-ssl URL on a secure update checker");
return false;
}
if (WiFi.status() != WL_CONNECTED)
{
log_d("Wifi not connected");
return false;
}
HTTPClient httpClient;
if (!httpClient.begin(binURL))
{
log_e("Error initializing client");
return false;
}
const auto httpCode = httpClient.GET();
if (httpCode != HTTP_CODE_OK)
{
log_e("[HTTP] [ERROR] [%d] %s",
httpCode,
httpClient.errorToString(httpCode).c_str());
log_d("Response:\n%s",
httpClient.getString().c_str());
return false;
}
const auto payloadSize = httpClient.getSize();
auto & payloadStream = httpClient.getStream();
if (binMD5.length() > 0 &&
!Update.setMD5(binMD5.c_str()))
{
log_e("Failed to set the expected MD5");
return false;
}
const bool canBegin = Update.begin(payloadSize);
if (payloadSize <= 0)
{
log_e("Fetched binary has 0 size");
return false;
}
if (!canBegin)
{
log_e("Not enough space to begin OTA");
return false;
}
const auto written = Update.writeStream(payloadStream);
if (written != payloadSize)
{
log_e("Error. Written %lu out of %lu", written, payloadSize);
return false;
}
if (!Update.end())
{
StreamString errorMsg;
Update.printError(errorMsg);
log_e("Error Occurred: %s", errorMsg.c_str());
return false;
}
if (!Update.isFinished())
{
StreamString errorMsg;
Update.printError(errorMsg);
log_e("Undefined OTA update error: %s", errorMsg.c_str());
return false;
}
log_i("Update completed. Rebooting");
ESP.restart();
return true;
}