Fix FOTA updates with smart sleep (#999)

This commit is contained in:
tekka
2017-12-06 23:02:57 +01:00
committed by GitHub
parent a9c49f0210
commit a80879e056
4 changed files with 60 additions and 37 deletions
+17 -11
View File
@@ -29,19 +29,20 @@ I2CEeprom _flash(MY_OTA_I2C_ADDR);
#else #else
SPIFlash _flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID); SPIFlash _flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID);
#endif #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, hwReadConfigBlock((void*)&_nodeFirmwareConfig, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS,
sizeof(nodeFirmwareConfig_t)); sizeof(nodeFirmwareConfig_t));
} }
void firmwareOTAUpdateRequest(void) LOCAL void firmwareOTAUpdateRequest(void)
{ {
const uint32_t enterMS = hwMillis(); const uint32_t enterMS = hwMillis();
if (_firmwareUpdateOngoing && (enterMS - _firmwareLastRequest > MY_OTA_RETRY_DELAY)) { 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 (_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; nodeFirmwareConfig_t *firmwareConfigResponse = (nodeFirmwareConfig_t *)_msg.data;
// compare with current node configuration, if they differ, start FW fetch process // compare with current node configuration, if they differ, start FW fetch process
if (memcmp(&_nodeFirmwareConfig, firmwareConfigResponse, sizeof(nodeFirmwareConfig_t))) { if (memcmp(&_nodeFirmwareConfig, firmwareConfigResponse, sizeof(nodeFirmwareConfig_t))) {
@@ -163,7 +168,7 @@ bool firmwareOTAUpdateProcess(void)
return false; return false;
} }
void presentBootloaderInformation(void) LOCAL void presentBootloaderInformation(void)
{ {
requestFirmwareConfig_t *requestFirmwareConfig = (requestFirmwareConfig_t *)_msgTmp.data; requestFirmwareConfig_t *requestFirmwareConfig = (requestFirmwareConfig_t *)_msgTmp.data;
mSetLength(_msgTmp, sizeof(requestFirmwareConfig_t)); mSetLength(_msgTmp, sizeof(requestFirmwareConfig_t));
@@ -177,12 +182,13 @@ void presentBootloaderInformation(void)
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM,
ST_FIRMWARE_CONFIG_REQUEST, false)); ST_FIRMWARE_CONFIG_REQUEST, false));
} }
bool isFirmwareUpdateOngoing(void)
LOCAL bool isFirmwareUpdateOngoing(void)
{ {
return _firmwareUpdateOngoing; return _firmwareUpdateOngoing;
} }
// do a crc16 on the whole received firmware // do a crc16 on the whole received firmware
bool transportIsValidFirmware(void) LOCAL bool transportIsValidFirmware(void)
{ {
// init crc // init crc
uint16_t crc = ~0; uint16_t crc = ~0;
+20 -17
View File
@@ -34,19 +34,20 @@
* *
* MyOTAFirmwareUpdate debug log messages: * MyOTAFirmwareUpdate debug log messages:
* *
* |E| SYS | SUB | Message | Comment * |E| SYS | SUB | Message | Comment
* |-|-----|-----|-----------------------------|---------------------------------------------------------------------------- * |-|-----|-----|-----------------------------|----------------------------------------------------------------------------
* | | OTA | FWP | UPDATE | FW update initiated * | | OTA | FWP | UPDATE | FW update initiated
* |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash * |!| OTA | FWP | UPDO | FW config response received, FW update already ongoing
* | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available * |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash
* | | OTA | FWP | RECV B=%04X | Received FW block (B) * | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available
* |!| OTA | FWP | WRONG FWB | Wrong FW block received * | | OTA | FWP | RECV B=%04X | Received FW block (B)
* | | OTA | FWP | FW END | FW received, proceed to CRC verification * |!| OTA | FWP | WRONG FWB | Wrong FW block received
* | | OTA | FWP | CRC OK | FW CRC verification OK * | | OTA | FWP | FW END | FW received, proceed to CRC verification
* |!| OTA | FWP | CRC FAIL | FW CRC verification failed * | | OTA | FWP | CRC OK | FW CRC verification OK
* | | OTA | FRQ | FW REQ,T=%04X,V=%04X,B=%04X | Request FW update, FW type (T), version (V), block (B) * |!| OTA | FWP | CRC FAIL | FW CRC verification failed
* |!| OTA | FRQ | FW UPD FAIL | FW update failed * | | OTA | FRQ | FW REQ,T=%04X,V=%04X,B=%04X | Request FW update, FW type (T), version (V), block (B)
* | | OTA | CRC | B=%04X,C=%04X,F=%04X | FW CRC verification. FW blocks (B), calculated CRC (C), FW CRC (F) * |!| 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 * @brief API declaration for MyOTAFirmwareUpdate
@@ -58,6 +59,8 @@
#include "MySensorsCore.h" #include "MySensorsCore.h"
#define LOCAL static //!< static
#define FIRMWARE_BLOCK_SIZE (16u) //!< Size of each firmware block #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 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 #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 * Current firmware settings (type, version, crc, blocks) are read into _fc
*/ */
void readFirmwareSettings(void); LOCAL void readFirmwareSettings(void);
/** /**
* @brief Handle OTA FW update requests * @brief Handle OTA FW update requests
*/ */
void firmwareOTAUpdateRequest(void); LOCAL void firmwareOTAUpdateRequest(void);
/** /**
* @brief Handle OTA FW update responses * @brief Handle OTA FW update responses
* *
* This function handles incoming OTA FW packets and stores them to external flash (Sensebender) * 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 * @brief Validate uploaded FW CRC
* *
* This function verifies if uploaded FW CRC is valid * This function verifies if uploaded FW CRC is valid
*/ */
bool transportIsValidFirmware(void); LOCAL bool transportIsValidFirmware(void);
/** /**
* @brief Present bootloader/FW information upon startup * @brief Present bootloader/FW information upon startup
*/ */
void presentBootloaderInformation(void); LOCAL void presentBootloaderInformation(void);
#endif #endif
+22 -9
View File
@@ -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 CORE_DEBUG(PSTR("MCO:SLP:MS=%" PRIu32 ",SMS=%" PRIu8 ",I1=%" PRIu8 ",M1=%" PRIu8 ",I2=%" PRIu8
",M2=%" PRIu8 "\n"), sleepingMS, smartSleep, ",M2=%" PRIu8 "\n"), sleepingMS, smartSleep,
interrupt1, mode1, interrupt2, mode2); 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 // repeater feature: sleeping not possible
#if defined(MY_REPEATER_FEATURE) #if defined(MY_REPEATER_FEATURE)
(void)smartSleep; (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; 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) { 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 // notify controller about going to sleep, payload indicates smartsleep waiting time in MS
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL,
I_PRE_SLEEP_NOTIFICATION).set((uint32_t)MY_SMART_SLEEP_WAIT_DURATION_MS)); I_PRE_SLEEP_NOTIFICATION).set((uint32_t)MY_SMART_SLEEP_WAIT_DURATION_MS));
wait(MY_SMART_SLEEP_WAIT_DURATION_MS); // listen for incoming messages 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 #else
(void)smartSleep; (void)smartSleep;
#endif #endif // MY_SENSOR_NETWORK
#if defined(MY_SENSOR_NETWORK) #if defined(MY_SENSOR_NETWORK)
transportDisable(); transportDisable();
+1
View File
@@ -52,6 +52,7 @@
* | | MCO | PIM | NODE REG=%%d | Registration response received, registration status (REG) * | | 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 | 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 | 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 | FWUPD | Sleeping not possible, FW update ongoing
* |!| MCO | SLP | REP | Sleeping not possible, repeater feature enabled * |!| 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) * |!| MCO | SLP | TNR | Transport not ready, attempt to reconnect until timeout (MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS)