ESP32-PaxCounter/src/OTA.cpp

218 lines
6.8 KiB
C++
Raw Normal View History

2018-09-15 16:29:52 +02:00
/*
Parts of this code:
Copyright (c) 2014-present PlatformIO <contact@platformio.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <WiFiClientSecure.h>
#include <Update.h>
#include <BintrayClient.h>
#include "ota.h"
2018-08-12 15:42:58 +02:00
const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE);
2018-09-15 16:29:52 +02:00
// Connection port (HTTPS)
const int port = 443;
2018-09-15 16:29:52 +02:00
// Connection timeout
const uint32_t RESPONSE_TIMEOUT_MS = 5000;
2018-08-12 15:42:58 +02:00
2018-09-15 16:29:52 +02:00
// Variables to validate firmware content
volatile int contentLength = 0;
volatile bool isValidContentType = false;
2018-08-16 21:10:13 +02:00
2018-09-15 16:29:52 +02:00
// Local logging tag
static const char TAG[] = "main";
2018-09-15 16:29:52 +02:00
void start_ota_update() {
ESP_LOGI(TAG, "Starting Wifi OTA update");
2018-09-15 16:29:52 +02:00
WiFi.begin(WIFI_SSID, WIFI_PASS);
2018-09-15 16:29:52 +02:00
while (WiFi.status() != WL_CONNECTED) {
delay(2000);
ESP_LOGI(TAG, "trying to connect to %s", WIFI_SSID);
}
2018-08-12 15:42:58 +02:00
2018-09-15 16:29:52 +02:00
ESP_LOGI(TAG, "connected to %s", WIFI_SSID);
2018-08-12 15:42:58 +02:00
2018-09-15 18:59:20 +02:00
checkFirmwareUpdates(); // gets and flashes new firmware and restarts
2018-09-15 21:10:11 +02:00
ESP.restart(); // reached only if update was not successful
2018-09-15 16:29:52 +02:00
} // start_ota_update
void checkFirmwareUpdates() {
// Fetch the latest firmware version
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "OTA mode, checking latest firmware version on server...");
2018-09-15 16:29:52 +02:00
const String latest = bintray.getLatestVersion();
if (latest.length() == 0) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(
TAG,
"Could not load info about the latest firmware. Rebooting to runmode.");
2018-09-15 16:29:52 +02:00
return;
} else if (atoi(latest.c_str()) <= VERSION) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Current firmware is up to date. Rebooting to runmode.");
2018-09-15 16:29:52 +02:00
return;
}
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "New firmware version v%s available. Downloading...",
2018-09-15 16:29:52 +02:00
latest.c_str());
processOTAUpdate(latest);
}
2018-08-12 15:42:58 +02:00
2018-09-15 16:29:52 +02:00
// A helper function to extract header value from header
inline String getHeaderValue(String header, String headerName) {
return header.substring(strlen(headerName.c_str()));
}
2018-08-12 15:42:58 +02:00
2018-09-15 16:29:52 +02:00
/**
* OTA update processing
*/
void processOTAUpdate(const String &version) {
String firmwarePath = bintray.getBinaryPath(version);
if (!firmwarePath.endsWith(".bin")) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Unsupported binary format, OTA update cancelled.");
2018-09-15 16:29:52 +02:00
return;
}
2018-08-12 15:42:58 +02:00
2018-09-15 16:29:52 +02:00
String currentHost = bintray.getStorageHost();
String prevHost = currentHost;
WiFiClientSecure client;
client.setCACert(bintray.getCertificate(currentHost));
if (!client.connect(currentHost.c_str(), port)) {
ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str());
return;
}
bool redirect = true;
while (redirect) {
if (currentHost != prevHost) {
client.stop();
client.setCACert(bintray.getCertificate(currentHost));
if (!client.connect(currentHost.c_str(), port)) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s",
2018-09-15 16:29:52 +02:00
currentHost.c_str());
return;
}
}
// ESP_LOGI(TAG, "Requesting: " + firmwarePath);
client.print(String("GET ") + firmwarePath + " HTTP/1.1\r\n");
client.print(String("Host: ") + currentHost + "\r\n");
client.print("Cache-Control: no-cache\r\n");
client.print("Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > RESPONSE_TIMEOUT_MS) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Client Timeout.");
2018-09-15 16:29:52 +02:00
client.stop();
return;
}
}
while (client.available()) {
String line = client.readStringUntil('\n');
// Check if the line is end of headers by removing space symbol
line.trim();
// if the the line is empty, this is the end of the headers
if (!line.length()) {
break; // proceed to OTA update
}
// Check allowed HTTP responses
if (line.startsWith("HTTP/1.1")) {
if (line.indexOf("200") > 0) {
ESP_LOGI(TAG, "Got 200 status code from server. Proceeding to "
"firmware flashing");
redirect = false;
} else if (line.indexOf("302") > 0) {
ESP_LOGI(TAG, "Got 302 status code from server. Redirecting to the "
"new address");
redirect = true;
} else {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Could not get a valid firmware url.");
2018-09-15 16:29:52 +02:00
// Unexptected HTTP response. Retry or skip update?
redirect = false;
}
}
// Extracting new redirect location
if (line.startsWith("Location: ")) {
String newUrl = getHeaderValue(line, "Location: ");
ESP_LOGI(TAG, "Got new url: %s", newUrl.c_str());
newUrl.remove(0, newUrl.indexOf("//") + 2);
currentHost = newUrl.substring(0, newUrl.indexOf('/'));
newUrl.remove(newUrl.indexOf(currentHost), currentHost.length());
firmwarePath = newUrl;
ESP_LOGI(TAG, "firmwarePath: %s", firmwarePath.c_str());
continue;
}
// Checking headers
if (line.startsWith("Content-Length: ")) {
contentLength =
atoi((getHeaderValue(line, "Content-Length: ")).c_str());
ESP_LOGI(TAG, "Got %d bytes from server", contentLength);
}
if (line.startsWith("Content-Type: ")) {
String contentType = getHeaderValue(line, "Content-Type: ");
ESP_LOGI(TAG, "Got %s payload", contentType.c_str());
if (contentType == "application/octet-stream") {
isValidContentType = true;
}
}
}
}
// check whether we have everything for OTA update
if (contentLength && isValidContentType) {
if (Update.begin(contentLength)) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Starting OTA update. This will take some time to "
"complete...");
2018-09-15 16:29:52 +02:00
size_t written = Update.writeStream(client);
if (written == contentLength) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Written %d bytes successfully", written);
2018-09-15 16:29:52 +02:00
} else {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "Written only %d of %d bytes, OTA update cancelled.", written, contentLength);
2018-09-15 16:29:52 +02:00
// Retry??
}
if (Update.end()) {
if (Update.isFinished()) {
2018-09-15 21:10:11 +02:00
ESP_LOGI(TAG, "OTA update completed. Rebooting to runmode.");
2018-09-15 16:29:52 +02:00
ESP.restart();
} else {
ESP_LOGI(TAG, "Something went wrong! OTA update hasn't been finished "
"properly.");
}
} else {
ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError());
}
} else {
ESP_LOGI(TAG, "There isn't enough space to start OTA update");
client.flush();
}
} else {
ESP_LOGI(TAG,
"There was no valid content in the response from the OTA server!");
client.flush();
}
2018-08-12 15:42:58 +02:00
}