From d75ba1b5666f517f88b5e43ec9f2d4ad008d92e2 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 25 Apr 2024 22:18:18 +0200 Subject: [PATCH] code cleanup --- src/app.cpp | 28 ++++++++-------------------- src/app.h | 14 +++++--------- src/config/config.h | 5 +++++ src/config/settings.h | 12 +++--------- src/hms/CmtRadio.h | 24 +++++++++++++++++++++--- 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index d00a95c2..30942fec 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -33,23 +33,16 @@ void app::setup() { resetSystem(); esp_task_wdt_reset(); - mSettings.setup(); - mSettings.getPtr(mConfig); + mSettings.setup(mConfig); ah::Scheduler::setup(mConfig->inst.startWithoutTime); DPRINT(DBG_INFO, F("Settings valid: ")); - DSERIAL.flush(); - if (mSettings.getValid()) - DBGPRINTLN(F("true")); - else - DBGPRINTLN(F("false")); + DBGPRINTLN(mConfig->valid ? F("true") : F("false")); esp_task_wdt_reset(); mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf); #if defined(ESP32) - if(mConfig->cmt.enabled) { - mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, mConfig->cmt.pinSclk, mConfig->cmt.pinSdio, mConfig->cmt.pinCsb, mConfig->cmt.pinFcsb, mConfig->sys.region); - } + mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->cmt, mConfig->sys.region); #endif #ifdef ETHERNET @@ -57,16 +50,16 @@ void app::setup() { mNetwork = static_cast(new AhoyEthernet()); #else mNetwork = static_cast(new AhoyWifi()); - #endif // ETHERNET + #endif mNetwork->setup(mConfig, &mTimestamp, [this](bool gotIp) { this->onNetwork(gotIp); }, [this](bool gotTime) { this->onNtpUpdate(gotTime); }); mNetwork->begin(); esp_task_wdt_reset(); mCommunication.setup(&mTimestamp, &mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace); - mCommunication.addPayloadListener(std::bind(&app::payloadEventListener, this, std::placeholders::_1, std::placeholders::_2)); + mCommunication.addPayloadListener([this] (uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); }); #if defined(ENABLE_MQTT) - mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); }); + mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); }); #endif mSys.setup(&mTimestamp, &mConfig->inst, this); for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { @@ -81,7 +74,6 @@ void app::setup() { esp_task_wdt_reset(); // when WiFi is in client mode, then enable mqtt broker - #if !defined(AP_ONLY) #if defined(ENABLE_MQTT) mMqttEnabled = (mConfig->mqtt.broker[0] > 0); if (mMqttEnabled) { @@ -90,7 +82,6 @@ void app::setup() { mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); }); } #endif - #endif setupLed(); esp_task_wdt_reset(); @@ -126,9 +117,7 @@ void app::setup() { #if defined(ENABLE_SIMULATOR) mSimulator.setup(&mSys, &mTimestamp, 0); - mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { - payloadEventListener(cmd, iv); - }); + mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); }); #endif /*ENABLE_SIMULATOR*/ esp_task_wdt_reset(); @@ -142,8 +131,7 @@ void app::loop(void) { mNrfRadio.loop(); #if defined(ESP32) - if(mConfig->cmt.enabled) - mCmtRadio.loop(); + mCmtRadio.loop(); #endif ah::Scheduler::loop(); diff --git a/src/app.h b/src/app.h index a7be0a55..4f07adc6 100644 --- a/src/app.h +++ b/src/app.h @@ -217,7 +217,7 @@ class app : public IApp, public ah::Scheduler { } bool getSettingsValid() override { - return mSettings.getValid(); + return mConfig->valid; } bool getRebootRequestState() override { @@ -354,15 +354,13 @@ class app : public IApp, public ah::Scheduler { void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true); void payloadEventListener(uint8_t cmd, Inverter<> *iv) { - #if !defined(AP_ONLY) #if defined(ENABLE_MQTT) if (mMqttEnabled) mMqtt.payloadEventListener(cmd, iv); - #endif /*ENABLE_MQTT*/ #endif #if defined(PLUGIN_DISPLAY) - if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type) - mDisplay.payloadEventListener(cmd); + if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type) + mDisplay.payloadEventListener(cmd); #endif updateLed(); } @@ -425,8 +423,7 @@ class app : public IApp, public ah::Scheduler { #ifdef ENABLE_SYSLOG DbgSyslog mDbgSyslog; #endif - //PayloadType mPayload; - //MiPayloadType mMiPayload; + PubSerialType mPubSerial; #if !defined(ETHERNET) //Improv mImprov; @@ -447,10 +444,9 @@ class app : public IApp, public ah::Scheduler { bool mNetworkConnected = false; - // mqtt #if defined(ENABLE_MQTT) PubMqttType mMqtt; - #endif /*ENABLE_MQTT*/ + #endif bool mTickerInstallOnce = false; bool mMqttEnabled = false; diff --git a/src/config/config.h b/src/config/config.h index 3dfac72e..49705eb6 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -28,6 +28,11 @@ // If the next line is uncommented, Ahoy will stay in access point mode all the time //#define AP_ONLY +#if defined(AP_ONLY) + #if defined(ENABLE_MQTT) + #undef ENABLE_MQTT + #endif +#endif // timeout for automatic logoff (20 minutes) #define LOGOUT_TIMEOUT (20 * 60) diff --git a/src/config/settings.h b/src/config/settings.h index e97519a1..4f5ba4c0 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -239,8 +239,9 @@ class settings { std::fill(reinterpret_cast(&mCfg), reinterpret_cast(&mCfg) + sizeof(mCfg), 0); } - void setup() { + void setup(settings_t *&cfg) { DPRINTLN(DBG_INFO, F("Initializing FS ..")); + cfg = &mCfg; mCfg.valid = false; #if !defined(ESP32) @@ -276,14 +277,6 @@ class settings { DPRINTLN(DBG_INFO, F("FS stopped")); } - void getPtr(settings_t *&cfg) { - cfg = &mCfg; - } - - bool getValid(void) { - return mCfg.valid; - } - inline bool getLastSaveSucceed() { return mLastSaveSucceed; } @@ -915,6 +908,7 @@ class settings { } #endif + private: settings_t mCfg; bool mLastSaveSucceed = 0; }; diff --git a/src/hms/CmtRadio.h b/src/hms/CmtRadio.h index 4b043ae2..b6405329 100644 --- a/src/hms/CmtRadio.h +++ b/src/hms/CmtRadio.h @@ -15,16 +15,25 @@ template class CmtRadio : public Radio { typedef Cmt2300a CmtType; public: - void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, uint8_t pinSclk, uint8_t pinSdio, uint8_t pinCsb, uint8_t pinFcsb, uint8_t region = 0, bool genDtuSn = true) { - mCmt.setup(pinSclk, pinSdio, pinCsb, pinFcsb); - reset(genDtuSn, static_cast(region)); + void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, cfgCmt_t *cfg, uint8_t region = 0, bool genDtuSn = true) { + mCfg = cfg; + + if(!cfg->enabled) + return; + mPrivacyMode = privacyMode; mSerialDebug = serialDebug; mPrintWholeTrace = printWholeTrace; mTxBuf.fill(0); + + mCmt.setup(cfg->pinSclk, cfg->pinSdio, cfg->pinCsb, cfg->pinFcsb); + reset(genDtuSn, static_cast(region)); } void loop() override { + if(!mCfg->enabled) + return; + mCmt.loop(); if((!mIrqRcvd) && (!mRqstGetRx)) return; @@ -41,6 +50,9 @@ class CmtRadio : public Radio { } void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override { + if(!mCfg->enabled) + return; + DPRINT(DBG_INFO, F("sendControlPacket cmd: ")); DBGHEXLN(cmd); initPacket(iv->radioId.u64, TX_REQ_DEVCONTROL, SINGLE_FRAME); @@ -59,6 +71,9 @@ class CmtRadio : public Radio { } bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) override { + if(!isChipConnected()) + return false; + uint8_t fromCh = mCmt.freq2Chan(fromkHz); uint8_t toCh = mCmt.freq2Chan(tokHz); @@ -68,6 +83,8 @@ class CmtRadio : public Radio { bool switchFrequencyCh(Inverter<> *iv, uint8_t fromCh, uint8_t toCh) override { if((0xff == fromCh) || (0xff == toCh)) return false; + if(!isChipConnected()) + return false; mCmt.switchChannel(fromCh); sendSwitchChCmd(iv, toCh); @@ -188,6 +205,7 @@ class CmtRadio : public Radio { } CmtType mCmt; + cfgCmt_t *mCfg = nullptr; bool mCmtAvail = false; bool mRqstGetRx = false; uint32_t mMillis = 0;