From f547c8fc07af4c1d771b16c03873628a73f0b6f9 Mon Sep 17 00:00:00 2001 From: anschrammh Date: Tue, 27 Sep 2022 07:26:12 +0200 Subject: [PATCH] Added a new OTAManager class which wraps the OTAUpdater class and configures it using parameters read from a cfg file --- src/app/OTAManager.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++ src/app/OTAManager.h | 27 +++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/app/OTAManager.cpp create mode 100644 src/app/OTAManager.h diff --git a/src/app/OTAManager.cpp b/src/app/OTAManager.cpp new file mode 100644 index 0000000..c68929d --- /dev/null +++ b/src/app/OTAManager.cpp @@ -0,0 +1,89 @@ +#include "OTAManager.h" +#include "definition.h" + +#define DEBUG_OTA_MANAGER(...) do {} while(0) + +//#define DEBUG_OTA_MANAGER(...) do { Serial.printf(__VA_ARGS__); Serial.println();} while(0) + +OTAManager::OTAManager(SDCardManager &sdCardManager, const BoardConfig &boardConfig) : _sdCardManager(&sdCardManager), _boardConfig(&boardConfig) +{ } + +OTAManager::~OTAManager() +{ } + +boolean OTAManager::init(void) +{ + //If the SDCardManager is not available, then we disable the OTA service + if(!_sdCardManager) + { + DEBUG_OTA_MANAGER("SDCardMng is NULL !"); + return true; + } + + //If the SDCardManager is available, then we try to read the config file + CFGDictionary *otaCfg = _sdCardManager->getCFGFile(OTA_CFG_FILE); + boolean toReturn(true); + //If we did not find the file + if(!otaCfg) + { + DEBUG_OTA_MANAGER("otaCfg is NULL !"); + return false; + } + + if((*otaCfg)("ENABLED") != nullptr) + { + if((*otaCfg)("ENABLED")->booleanValue()) + { + DEBUG_OTA_MANAGER("ENABLED : %s", (*otaCfg)("ENABLED")->stringValue()); + if((*otaCfg)("OTA_SERVER_ADDRESS") != nullptr) //This is the only required parameter + { + _isServiceEnabled = true; + _otaUpdater.setLedPin(_boardConfig->getOnBoard_LED(), LOW); + + _otaUpdater.setServerAddress((*otaCfg)("OTA_SERVER_ADDRESS")->stringValue()); + + if((*otaCfg)("OTA_SERVER_PORT") != nullptr) + { + DEBUG_OTA_MANAGER("OTA_SERVER_PORT : %s", (*otaCfg)("OTA_SERVER_PORT")->stringValue()); + _otaUpdater.setPort((*otaCfg)("OTA_SERVER_PORT")->uintValue()); + } + + if((*otaCfg)("OTA_SERVICE_PATH") != nullptr) + { + DEBUG_OTA_MANAGER("OTA_SERVICE_PATH : %s", (*otaCfg)("OTA_SERVICE_PATH")->stringValue()); + _otaUpdater.setPath((*otaCfg)("OTA_SERVICE_PATH")->stringValue()); + } + + if((*otaCfg)("OTA_SERVICE_AUTH_KEY") != nullptr) + { + DEBUG_OTA_MANAGER("OTA_SERVICE_AUTH_KEY : %s", (*otaCfg)("OTA_SERVICE_AUTH_KEY")->stringValue()); + _otaUpdater.setOtaAuthKey((*otaCfg)("OTA_SERVICE_AUTH_KEY")->stringValue()); + } + } + else + { + DEBUG_OTA_MANAGER("OTA_SERVER_ADDRESS is NULL !"); + toReturn = false; + } + } + } + else + { + DEBUG_OTA_MANAGER("ENABLED is NULL !"); + toReturn = false; + } + + delete otaCfg; + + return toReturn; +} + +boolean OTAManager::isEnabled(void) const +{ + return _isServiceEnabled; +} + +OTAUpdater& OTAManager::getOTAUpdater(void) +{ + return _otaUpdater; +} \ No newline at end of file diff --git a/src/app/OTAManager.h b/src/app/OTAManager.h new file mode 100644 index 0000000..6def4b0 --- /dev/null +++ b/src/app/OTAManager.h @@ -0,0 +1,27 @@ +#ifndef OTAMANAGER_H +#define OTAMANAGER_H + +#include "OTAUpdater.h" +#include "SDCardManager.h" +#include "BoardConfig.h" + +class OTAManager +{ + friend class SAB; + public: + ~OTAManager(); + + boolean init(void); + boolean isEnabled(void) const; + OTAUpdater& getOTAUpdater(void); + protected: + OTAManager(SDCardManager &sdCardManager, const BoardConfig &boardConfig); + OTAManager(); + private: + OTAUpdater _otaUpdater; + SDCardManager *_sdCardManager = nullptr; + const BoardConfig * _boardConfig = nullptr; + boolean _isServiceEnabled = false; +}; + +#endif //OTAMANAGER_H \ No newline at end of file