spejsiot-sonoff_pow/soft/src/spejsiot/endpoints/ImplementationEndpoint.cpp

79 lines
2.0 KiB
C++

#include <endpoints/ImplementationEndpoint.h>
#include <rboot-api.h>
EndpointResult ImplementationEndpoint::onValue(String property, String value) {
if (property == "ota" and value == "true") {
startOTA();
return 200;
}
return 400;
}
void ImplementationEndpoint::startOTA() {
uint8_t slot;
rboot_config bootconf;
#ifdef RBOOT_TWO_ROMS
String romURL = OTA_URL + parent->deviceID + (parent->currentSlot ? "/rom1.bin" : "/rom0.bin");
#else
String romURL = OTA_URL + parent->deviceID + "/rom0.bin";
#endif
String spiffsURL = OTA_URL + parent->deviceID + "/spiff_rom.bin";
debugf("Updating...");
// need a clean object, otherwise if run before and failed will not run again
if (otaUpdater) delete otaUpdater;
otaUpdater = new rBootHttpUpdate();
bootconf = rboot_get_config();
slot = !parent->currentSlot;
debugf("Updating to rom %d.", slot);
// flash rom to position indicated in the rBoot config rom table
otaUpdater->addItem(bootconf.roms[slot], romURL);
#ifndef DISABLE_SPIFFS
// use user supplied values (defaults for 4mb flash in makefile)
if (slot == 0) {
otaUpdater->addItem(RBOOT_SPIFFS_0, spiffsURL);
} else {
otaUpdater->addItem(RBOOT_SPIFFS_1, spiffsURL);
}
#endif
otaUpdater->setCallback(OtaUpdateDelegate(&ImplementationEndpoint::otaUpdateCallback, this));
otaUpdater->start();
notify("ota", "started");
parent->statusLED.high();
}
void ImplementationEndpoint::otaUpdateCallback(rBootHttpUpdate& updater, bool result) {
parent->statusLED.idle();
if(result == true) {
// success
notify("ota", "finished");
uint8 slot;
if (parent->currentSlot == 0)
slot = 1;
else
slot = 0;
// set to boot new rom and then reboot
debugf("Firmware updated, rebooting to rom %d...", slot);
rboot_set_temp_rom(slot);
System.restart();
} else {
notify("ota", "failed");
}
}