ota update

This commit is contained in:
2026-08-16 14:52:23 +02:00
parent fb09b2b7a1
commit a02e280f42
4 changed files with 69 additions and 23 deletions

View File

@@ -13,12 +13,11 @@ namespace OtaUpdate {
namespace {
constexpr const char* RELEASES_API_URL =
"https://api.github.com/repos/Eiswolf-BG/eiswolfs-flightradar-CYD/releases/latest";
// GitHub requires a meaningful User-Agent for API requests
// (otherwise HTTP 403) - same principle as Config::NOMINATIM_USER_AGENT
// for address search.
constexpr const char* USER_AGENT =
"EiswolfsFlightradarCYD-OTA/1.0 (+https://github.com/Eiswolf-BG/eiswolfs-flightradar-CYD)";
"https://git.rasmusbendtsen.dk/api/v1/repos/rasmus/flyradar/releases/latest";
// Gitea API uses Authorization: token header instead of
// User-Agent. The token is set in Config::GITEA_API_TOKEN.
// Gitea serves downloads directly (no redirect), so
// setFollowRedirects is not needed.
bool parseVersion(const char* s, int& major, int& minor, int& patch) {
major = minor = patch = 0;
@@ -56,7 +55,12 @@ CheckInfo checkForUpdate() {
Serial.println("[OTA] Check failed: http.begin() returned false.");
return info;
}
http.addHeader("User-Agent", USER_AGENT);
http.addHeader("User-Agent", "EiswolfsFlightradarCYD-OTA/1.0");
if (Config::GITEA_API_TOKEN[0]) {
String auth = "token ";
auth += Config::GITEA_API_TOKEN;
http.addHeader("Authorization", auth);
}
int code = http.GET();
if (code != HTTP_CODE_OK) {
@@ -121,21 +125,25 @@ bool performUpdate(const char* url, void (*onProgress)(uint8_t percent)) {
Serial.printf("[OTA] Starting: url=%s freeHeap=%u RSSI=%ddBm\n", url,
(unsigned)ESP.getFreeHeap(), WiFi.RSSI());
WiFiClientSecure client;
WiFiClientSecure client;
client.setInsecure();
client.setTimeout(15000);
// IMPORTANT: GitHub's "browser_download_url" for release assets is NOT
// a direct download link, but first returns an HTTP 301/302 redirect
// to a signed objects.githubusercontent.com URL. HTTPUpdate does NOT
// follow redirects by default (HTTPC_DISABLE_FOLLOW_REDIRECTS is the
// default) - without this line the download aborts with HTTP_UPDATE_FAILED
// because only the redirect response arrives instead of the .bin file. See
// e.g. espressif/arduino-esp32#3020. HTTPC_STRICT_FOLLOW_REDIRECTS
// suffices since we only use GET.
httpUpdate.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
// Gitea serves downloads directly (no redirect like GitHub's S3),
// but private repos still need the Authorization header. Use the
// HTTPClient overload of httpUpdate.update() so we can set it.
HTTPClient http;
if (!http.begin(client, url)) {
Serial.println("[OTA] Failed: http.begin() returned false for download URL.");
return false;
}
if (Config::GITEA_API_TOKEN[0]) {
String auth = "token ";
auth += Config::GITEA_API_TOKEN;
http.addHeader("Authorization", auth);
}
// We show a brief success message ourselves after successful installation,
// We show a brief success message ourselves after successful installation,
// before the device reboots - see
// menu_screen.cpp::runOtaUpdateScreen().
httpUpdate.rebootOnUpdate(false);
@@ -156,7 +164,9 @@ bool performUpdate(const char* url, void (*onProgress)(uint8_t percent)) {
}
});
t_httpUpdate_return result = httpUpdate.update(client, url);
t_httpUpdate_return result = httpUpdate.update(http);
http.end();
if (result != HTTP_UPDATE_OK) {
Serial.printf("[OTA] Failed: result=%d error=%d (%s) freeHeap=%u\n", (int)result,