128 lines
3.1 KiB
C++
128 lines
3.1 KiB
C++
|
#include <Arduino.h>
|
||
|
#include <Update.h>
|
||
|
#include "config.h"
|
||
|
#include "updater.h"
|
||
|
#include "http_client_wrapper.h"
|
||
|
|
||
|
void Updater::run() {
|
||
|
DEBUG("Updater is running...\n");
|
||
|
HTTPClientWrapper* http = new HTTPClientWrapper();
|
||
|
DEBUG("Requesting update info...\n");
|
||
|
bool result = http->get(OTA_UPDATE_URL);
|
||
|
if (!result) {
|
||
|
ERROR("Updater failed requesting %s.\n", OTA_UPDATE_URL);
|
||
|
return;
|
||
|
}
|
||
|
String line = http->readUntil("\n");
|
||
|
if (!line.startsWith("VERSION=")) {
|
||
|
ERROR("Expected first line to be VERSION.\n");
|
||
|
return;
|
||
|
}
|
||
|
uint16_t version = line.substring(8).toInt();
|
||
|
if (version==0) {
|
||
|
ERROR("Could not parse version number.\n");
|
||
|
return;
|
||
|
}
|
||
|
DEBUG("Found version %d. My version is %d.\n", version, OTA_VERSION);
|
||
|
if (version <= OTA_VERSION) {
|
||
|
return;
|
||
|
}
|
||
|
line = http->readUntil("\n");
|
||
|
if (!line.startsWith("IMAGE_PATH=")) {
|
||
|
ERROR("Expected second line to be IMAGE_PATH.\n");
|
||
|
return;
|
||
|
}
|
||
|
String image_path = line.substring(11);
|
||
|
image_path.trim();
|
||
|
line = http->readUntil("\n");
|
||
|
if (!line.startsWith("SPIFFS_PATH=")) {
|
||
|
ERROR("Expected third line to be SPIFFS_PATH.\n");
|
||
|
return;
|
||
|
}
|
||
|
String spiffs_path = line.substring(12);
|
||
|
spiffs_path.trim();
|
||
|
|
||
|
http->close();
|
||
|
delete http;
|
||
|
|
||
|
result = do_update(U_FLASH, image_path);
|
||
|
if (result) {
|
||
|
do_update(U_SPIFFS, spiffs_path);
|
||
|
}
|
||
|
DEBUG("Done. Rebooting...\n");
|
||
|
ESP.restart();
|
||
|
}
|
||
|
|
||
|
bool Updater::do_update(int command, String url) {
|
||
|
HTTPClientWrapper* http = new HTTPClientWrapper();
|
||
|
bool result = http->get(url);
|
||
|
if (!result) {
|
||
|
ERROR("Updater failed requesting %s.\n", url.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
result = Update.begin(http->getSize(), command);
|
||
|
if (!result) {
|
||
|
ERROR("Update could not be started.\n");
|
||
|
return false;
|
||
|
}
|
||
|
uint8_t buf[512];
|
||
|
uint16_t len;
|
||
|
while((len = http->read(buf, 512))) {
|
||
|
Update.write(buf, 512);
|
||
|
}
|
||
|
|
||
|
http->close();
|
||
|
delete http;
|
||
|
|
||
|
result = Update.end();
|
||
|
if (!result) {
|
||
|
ERROR("Writing the update failed somewhere. Aborted.\n");
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void Updater::update_spiffs() {
|
||
|
HTTPClientWrapper* http = new HTTPClientWrapper();
|
||
|
DEBUG("Requesting update info...\n");
|
||
|
bool result = http->get(OTA_UPDATE_URL);
|
||
|
if (!result) {
|
||
|
ERROR("Updater failed requesting %s.\n", OTA_UPDATE_URL);
|
||
|
return;
|
||
|
}
|
||
|
String line = http->readUntil("\n");
|
||
|
if (!line.startsWith("VERSION=")) {
|
||
|
ERROR("Expected first line to be VERSION.\n");
|
||
|
return;
|
||
|
}
|
||
|
uint16_t version = line.substring(8).toInt();
|
||
|
if (version==0) {
|
||
|
ERROR("Could not parse version number.\n");
|
||
|
return;
|
||
|
} else if (version > OTA_VERSION) {
|
||
|
DEBUG("Found newer version %d. My version is %d. Starting full update!\n", version, OTA_VERSION);
|
||
|
run();
|
||
|
} else {
|
||
|
DEBUG("Loading SPIFFS image.\n");
|
||
|
line = http->readUntil("\n");
|
||
|
if (!line.startsWith("IMAGE_PATH=")) {
|
||
|
ERROR("Expected second line to be IMAGE_PATH.\n");
|
||
|
return;
|
||
|
}
|
||
|
line = http->readUntil("\n");
|
||
|
if (!line.startsWith("SPIFFS_PATH=")) {
|
||
|
ERROR("Expected third line to be SPIFFS_PATH.\n");
|
||
|
return;
|
||
|
}
|
||
|
String spiffs_path = line.substring(12);
|
||
|
spiffs_path.trim();
|
||
|
do_update(U_SPIFFS, spiffs_path);
|
||
|
DEBUG("Done. Rebooting...\n");
|
||
|
delay(500);
|
||
|
ESP.restart();
|
||
|
}
|
||
|
http->close();
|
||
|
delete http;
|
||
|
}
|