Skip to content

Commit

Permalink
Implement #50
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinroger committed Mar 28, 2016
1 parent d7ea186 commit d9ec2d7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Homie/Boot/BootNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ void BootNormal::_mqttCallback(char* topic, char* payload) {
Logger.log(F("✴ OTA available (version "));
Logger.log(message);
Logger.logln(")");
strcpy(this->_otaVersion, payload);
this->_flaggedForOta = true;
Serial.println(F("Flagged for OTA"));
}
Expand Down Expand Up @@ -282,7 +283,7 @@ void BootNormal::loop() {

if (this->_flaggedForOta && this->_interface->reset.able) {
Logger.logln(F("Device is in a resettable state"));
Config.setOtaMode(true);
Config.setOtaMode(true, this->_otaVersion);

Logger.logln(F("↻ Rebooting in OTA mode"));
ESP.restart();
Expand Down
1 change: 1 addition & 0 deletions src/Homie/Boot/BootNormal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace HomieInternals {
bool _wifiDisconnectNotified;
bool _mqttConnectNotified;
bool _mqttDisconnectNotified;
char _otaVersion[MAX_FIRMWARE_VERSION_LENGTH];
bool _flaggedForOta;
bool _flaggedForReset;
Bounce _resetDebouncer;
Expand Down
4 changes: 3 additions & 1 deletion src/Homie/Boot/BootOta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ void BootOta::setup() {
Logger.logln(F("Starting OTA..."));


char dataToPass[8 + 1 + (MAX_FIRMWARE_NAME_LENGTH - 1) + 1 + (MAX_FIRMWARE_VERSION_LENGTH - 1) + 1];
char dataToPass[8 + 1 + (MAX_FIRMWARE_NAME_LENGTH - 1) + 1 + (MAX_FIRMWARE_VERSION_LENGTH - 1) + 2 + (MAX_FIRMWARE_VERSION_LENGTH - 1) + 1];
strcpy(dataToPass, Helpers.getDeviceId());
strcat(dataToPass, "=");
strcat(dataToPass, this->_interface->firmware.name);
strcat(dataToPass, "@");
strcat(dataToPass, this->_interface->firmware.version);
strcat(dataToPass, "->");
strcat(dataToPass, Config.getOtaVersion());
t_httpUpdate_return ret = ESPhttpUpdate.update(host, port, Config.get().ota.path, dataToPass, Config.get().ota.server.ssl.enabled, Config.get().ota.server.ssl.fingerprint, false);
switch(ret) {
case HTTP_UPDATE_FAILED:
Expand Down
18 changes: 16 additions & 2 deletions src/Homie/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bool ConfigClass::load() {

char buf[MAX_JSON_CONFIG_FILE_BUFFER_SIZE];
configFile.readBytes(buf, configSize);
configFile.close();

StaticJsonBuffer<MAX_JSON_CONFIG_ARDUINOJSON_BUFFER_SIZE> jsonBuffer;
JsonObject& parsedJson = jsonBuffer.parseObject(buf);
Expand All @@ -51,6 +52,15 @@ bool ConfigClass::load() {

if (SPIFFS.exists(CONFIG_OTA_PATH)) {
this->_bootMode = BOOT_OTA;

File otaFile = SPIFFS.open(CONFIG_OTA_PATH, "r");
if (otaFile) {
size_t otaSize = otaFile.size();
otaFile.readBytes(this->_otaVersion, otaSize);
otaFile.close();
} else {
Logger.logln(F("✖ Cannot open OTA file"));
}
} else {
this->_bootMode = BOOT_NORMAL;
}
Expand Down Expand Up @@ -177,7 +187,7 @@ void ConfigClass::write(const String& config) {
configFile.close();
}

void ConfigClass::setOtaMode(bool enabled) {
void ConfigClass::setOtaMode(bool enabled, const char* version) {
if (!this->_spiffsBegin()) { return; }

if (enabled) {
Expand All @@ -187,13 +197,17 @@ void ConfigClass::setOtaMode(bool enabled) {
return;
}

otaFile.print(1);
otaFile.print(version);
otaFile.close();
} else {
SPIFFS.remove(CONFIG_OTA_PATH);
}
}

const char* ConfigClass::getOtaVersion() {
return this->_otaVersion;
}

BootMode ConfigClass::getBootMode() {
return this->_bootMode;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Homie/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "FS.h"
#include "Datatypes/ConfigStruct.hpp"
#include "Helpers.hpp"
#include "Limits.hpp"
#include "Logger.hpp"

namespace HomieInternals {
Expand All @@ -16,13 +17,15 @@ namespace HomieInternals {
ConfigStruct& get();
void erase();
void write(const String& config);
void setOtaMode(bool enabled);
void setOtaMode(bool enabled, const char* version = "");
const char* getOtaVersion();
BootMode getBootMode();
void log(); // print the current config to log output

private:
BootMode _bootMode;
ConfigStruct _configStruct;
char _otaVersion[MAX_FIRMWARE_VERSION_LENGTH];
bool _spiffsBegan;

bool _spiffsBegin();
Expand Down

0 comments on commit d9ec2d7

Please sign in to comment.