spejsiot/spejsiot/endpoints/ImplementationEndpoint.cpp

72 lines
1.8 KiB
C++
Raw Normal View History

2017-03-06 16:56:37 +00:00
#include <endpoints/ImplementationEndpoint.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;
String romURL = OTA_URL + parent->deviceID + "/rom0.bin";
String spiffsURL = OTA_URL + parent->deviceID + "/spiff_rom.bin";
Serial.println("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();
if (parent->currentSlot == 0)
slot = 1;
else
slot = 0;
Serial.printf("Updating to rom %d.\r\n", 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
2017-06-12 15:40:23 +00:00
otaUpdater->setCallback(OtaUpdateDelegate(&ImplementationEndpoint::otaUpdateCallback, this));
2017-03-06 16:56:37 +00:00
otaUpdater->start();
notify("ota", "started");
}
void ImplementationEndpoint::otaUpdateCallback(rBootHttpUpdate& updater, bool result) {
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
Serial.printf("Firmware updated, rebooting to rom %d...\r\n", slot);
rboot_set_temp_rom(slot);
System.restart();
} else {
notify("ota", "failed");
}
}