diff --git a/core/MyOTAFirmwareUpdate.cpp b/core/MyOTAFirmwareUpdate.cpp index ae7577d..1b2aca8 100644 --- a/core/MyOTAFirmwareUpdate.cpp +++ b/core/MyOTAFirmwareUpdate.cpp @@ -29,19 +29,20 @@ I2CEeprom _flash(MY_OTA_I2C_ADDR); #else SPIFlash _flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID); #endif -nodeFirmwareConfig_t _nodeFirmwareConfig; -bool _firmwareUpdateOngoing; -uint32_t _firmwareLastRequest; -uint16_t _firmwareBlock; -uint8_t _firmwareRetry; -void readFirmwareSettings(void) +LOCAL nodeFirmwareConfig_t _nodeFirmwareConfig; +LOCAL bool _firmwareUpdateOngoing = false; +LOCAL uint32_t _firmwareLastRequest; +LOCAL uint16_t _firmwareBlock; +LOCAL uint8_t _firmwareRetry; + +LOCAL void readFirmwareSettings(void) { hwReadConfigBlock((void*)&_nodeFirmwareConfig, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(nodeFirmwareConfig_t)); } -void firmwareOTAUpdateRequest(void) +LOCAL void firmwareOTAUpdateRequest(void) { const uint32_t enterMS = hwMillis(); if (_firmwareUpdateOngoing && (enterMS - _firmwareLastRequest > MY_OTA_RETRY_DELAY)) { @@ -67,9 +68,13 @@ void firmwareOTAUpdateRequest(void) } } -bool firmwareOTAUpdateProcess(void) +LOCAL bool firmwareOTAUpdateProcess(void) { if (_msg.type == ST_FIRMWARE_CONFIG_RESPONSE) { + if(_firmwareUpdateOngoing) { + OTA_DEBUG(PSTR("!OTA:FWP:UPDO\n")); // FW config response received, FW update already ongoing + return true; + } nodeFirmwareConfig_t *firmwareConfigResponse = (nodeFirmwareConfig_t *)_msg.data; // compare with current node configuration, if they differ, start FW fetch process if (memcmp(&_nodeFirmwareConfig, firmwareConfigResponse, sizeof(nodeFirmwareConfig_t))) { @@ -163,7 +168,7 @@ bool firmwareOTAUpdateProcess(void) return false; } -void presentBootloaderInformation(void) +LOCAL void presentBootloaderInformation(void) { requestFirmwareConfig_t *requestFirmwareConfig = (requestFirmwareConfig_t *)_msgTmp.data; mSetLength(_msgTmp, sizeof(requestFirmwareConfig_t)); @@ -177,12 +182,13 @@ void presentBootloaderInformation(void) (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, ST_FIRMWARE_CONFIG_REQUEST, false)); } -bool isFirmwareUpdateOngoing(void) + +LOCAL bool isFirmwareUpdateOngoing(void) { return _firmwareUpdateOngoing; } // do a crc16 on the whole received firmware -bool transportIsValidFirmware(void) +LOCAL bool transportIsValidFirmware(void) { // init crc uint16_t crc = ~0; diff --git a/core/MyOTAFirmwareUpdate.h b/core/MyOTAFirmwareUpdate.h index 0e6c111..3e011df 100644 --- a/core/MyOTAFirmwareUpdate.h +++ b/core/MyOTAFirmwareUpdate.h @@ -34,19 +34,20 @@ * * MyOTAFirmwareUpdate debug log messages: * -* |E| SYS | SUB | Message | Comment +* |E| SYS | SUB | Message | Comment * |-|-----|-----|-----------------------------|---------------------------------------------------------------------------- -* | | OTA | FWP | UPDATE | FW update initiated -* |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash -* | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available -* | | OTA | FWP | RECV B=%04X | Received FW block (B) -* |!| OTA | FWP | WRONG FWB | Wrong FW block received -* | | OTA | FWP | FW END | FW received, proceed to CRC verification -* | | OTA | FWP | CRC OK | FW CRC verification OK -* |!| OTA | FWP | CRC FAIL | FW CRC verification failed -* | | OTA | FRQ | FW REQ,T=%04X,V=%04X,B=%04X | Request FW update, FW type (T), version (V), block (B) -* |!| OTA | FRQ | FW UPD FAIL | FW update failed -* | | OTA | CRC | B=%04X,C=%04X,F=%04X | FW CRC verification. FW blocks (B), calculated CRC (C), FW CRC (F) +* | | OTA | FWP | UPDATE | FW update initiated +* |!| OTA | FWP | UPDO | FW config response received, FW update already ongoing +* |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash +* | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available +* | | OTA | FWP | RECV B=%04X | Received FW block (B) +* |!| OTA | FWP | WRONG FWB | Wrong FW block received +* | | OTA | FWP | FW END | FW received, proceed to CRC verification +* | | OTA | FWP | CRC OK | FW CRC verification OK +* |!| OTA | FWP | CRC FAIL | FW CRC verification failed +* | | OTA | FRQ | FW REQ,T=%04X,V=%04X,B=%04X | Request FW update, FW type (T), version (V), block (B) +* |!| OTA | FRQ | FW UPD FAIL | FW update failed +* | | OTA | CRC | B=%04X,C=%04X,F=%04X | FW CRC verification. FW blocks (B), calculated CRC (C), FW CRC (F) * * * @brief API declaration for MyOTAFirmwareUpdate @@ -58,6 +59,8 @@ #include "MySensorsCore.h" +#define LOCAL static //!< static + #define FIRMWARE_BLOCK_SIZE (16u) //!< Size of each firmware block #define FIRMWARE_MAX_REQUESTS (5u) //!< Number of times a firmware block should be requested before giving up #define MY_OTA_RETRY (5u) //!< Number of times to request a fw block before giving up @@ -120,27 +123,27 @@ typedef struct { * * Current firmware settings (type, version, crc, blocks) are read into _fc */ -void readFirmwareSettings(void); +LOCAL void readFirmwareSettings(void); /** * @brief Handle OTA FW update requests */ -void firmwareOTAUpdateRequest(void); +LOCAL void firmwareOTAUpdateRequest(void); /** * @brief Handle OTA FW update responses * * This function handles incoming OTA FW packets and stores them to external flash (Sensebender) */ -bool firmwareOTAUpdateProcess(void); +LOCAL bool firmwareOTAUpdateProcess(void); /** * @brief Validate uploaded FW CRC * * This function verifies if uploaded FW CRC is valid */ -bool transportIsValidFirmware(void); +LOCAL bool transportIsValidFirmware(void); /** * @brief Present bootloader/FW information upon startup */ -void presentBootloaderInformation(void); +LOCAL void presentBootloaderInformation(void); #endif diff --git a/core/MySensorsCore.cpp b/core/MySensorsCore.cpp index 678b66e..76c36c1 100644 --- a/core/MySensorsCore.cpp +++ b/core/MySensorsCore.cpp @@ -542,14 +542,6 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in CORE_DEBUG(PSTR("MCO:SLP:MS=%" PRIu32 ",SMS=%" PRIu8 ",I1=%" PRIu8 ",M1=%" PRIu8 ",I2=%" PRIu8 ",M2=%" PRIu8 "\n"), sleepingMS, smartSleep, interrupt1, mode1, interrupt2, mode2); - // OTA FW feature: do not sleep if FW update ongoing -#if defined(MY_OTA_FIRMWARE_FEATURE) - if (isFirmwareUpdateOngoing()) { - CORE_DEBUG(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing - wait(sleepingMS); - return MY_SLEEP_NOT_POSSIBLE; - } -#endif // repeater feature: sleeping not possible #if defined(MY_REPEATER_FEATURE) (void)smartSleep; @@ -583,15 +575,36 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in return MY_SLEEP_NOT_POSSIBLE; } } + // OTA FW feature: do not sleep if FW update ongoing +#if defined(MY_OTA_FIRMWARE_FEATURE) + while (isFirmwareUpdateOngoing() && sleepingTimeMS) { + CORE_DEBUG(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing + wait(1000ul); + sleepingTimeMS = sleepingTimeMS >= 1000ul ? sleepingTimeMS - 1000ul : 1000ul; + } +#endif // MY_OTA_FIRMWARE_FEATURE if (smartSleep) { + // sleeping time left? + if (sleepingTimeMS < ((uint32_t)MY_SMART_SLEEP_WAIT_DURATION_MS)) { + wait(sleepingMS); + CORE_DEBUG(PSTR("!MCO:SLP:NTL\n")); // sleeping not possible, no time left + return MY_SLEEP_NOT_POSSIBLE; + } // notify controller about going to sleep, payload indicates smartsleep waiting time in MS (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_PRE_SLEEP_NOTIFICATION).set((uint32_t)MY_SMART_SLEEP_WAIT_DURATION_MS)); wait(MY_SMART_SLEEP_WAIT_DURATION_MS); // listen for incoming messages +#if defined(MY_OTA_FIRMWARE_FEATURE) + // check if during smart sleep waiting period a FOTA request was received + if (isFirmwareUpdateOngoing()) { + CORE_DEBUG(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing + return MY_SLEEP_NOT_POSSIBLE; + } +#endif // MY_OTA_FIRMWARE_FEATURE } #else (void)smartSleep; -#endif +#endif // MY_SENSOR_NETWORK #if defined(MY_SENSOR_NETWORK) transportDisable(); diff --git a/core/MySensorsCore.h b/core/MySensorsCore.h index 3008e28..70e1913 100644 --- a/core/MySensorsCore.h +++ b/core/MySensorsCore.h @@ -52,6 +52,7 @@ * | | MCO | PIM | NODE REG=%%d | Registration response received, registration status (REG) * | | MCO | SLP | MS=%%lu,SMS=%%d,I1=%%d,M1=%%d,I2=%%d,M2=%%d | Sleep node, time (MS), smartSleep (SMS), Int1/M1, Int2/M2 * | | MCO | SLP | WUP=%%d | Node woke-up, reason/IRQ (WUP) +* |!| MCO | SLP | NTL | Sleeping not possible, no time left * |!| MCO | SLP | FWUPD | Sleeping not possible, FW update ongoing * |!| MCO | SLP | REP | Sleeping not possible, repeater feature enabled * |!| MCO | SLP | TNR | Transport not ready, attempt to reconnect until timeout (MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS)