Skip to content

Commit a80879e

Browse files
authored
Fix FOTA updates with smart sleep (#999)
1 parent a9c49f0 commit a80879e

File tree

4 files changed

+60
-37
lines changed

4 files changed

+60
-37
lines changed

core/MyOTAFirmwareUpdate.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ I2CEeprom _flash(MY_OTA_I2C_ADDR);
2929
#else
3030
SPIFlash _flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID);
3131
#endif
32-
nodeFirmwareConfig_t _nodeFirmwareConfig;
33-
bool _firmwareUpdateOngoing;
34-
uint32_t _firmwareLastRequest;
35-
uint16_t _firmwareBlock;
36-
uint8_t _firmwareRetry;
3732

38-
void readFirmwareSettings(void)
33+
LOCAL nodeFirmwareConfig_t _nodeFirmwareConfig;
34+
LOCAL bool _firmwareUpdateOngoing = false;
35+
LOCAL uint32_t _firmwareLastRequest;
36+
LOCAL uint16_t _firmwareBlock;
37+
LOCAL uint8_t _firmwareRetry;
38+
39+
LOCAL void readFirmwareSettings(void)
3940
{
4041
hwReadConfigBlock((void*)&_nodeFirmwareConfig, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS,
4142
sizeof(nodeFirmwareConfig_t));
4243
}
4344

44-
void firmwareOTAUpdateRequest(void)
45+
LOCAL void firmwareOTAUpdateRequest(void)
4546
{
4647
const uint32_t enterMS = hwMillis();
4748
if (_firmwareUpdateOngoing && (enterMS - _firmwareLastRequest > MY_OTA_RETRY_DELAY)) {
@@ -67,9 +68,13 @@ void firmwareOTAUpdateRequest(void)
6768
}
6869
}
6970

70-
bool firmwareOTAUpdateProcess(void)
71+
LOCAL bool firmwareOTAUpdateProcess(void)
7172
{
7273
if (_msg.type == ST_FIRMWARE_CONFIG_RESPONSE) {
74+
if(_firmwareUpdateOngoing) {
75+
OTA_DEBUG(PSTR("!OTA:FWP:UPDO\n")); // FW config response received, FW update already ongoing
76+
return true;
77+
}
7378
nodeFirmwareConfig_t *firmwareConfigResponse = (nodeFirmwareConfig_t *)_msg.data;
7479
// compare with current node configuration, if they differ, start FW fetch process
7580
if (memcmp(&_nodeFirmwareConfig, firmwareConfigResponse, sizeof(nodeFirmwareConfig_t))) {
@@ -163,7 +168,7 @@ bool firmwareOTAUpdateProcess(void)
163168
return false;
164169
}
165170

166-
void presentBootloaderInformation(void)
171+
LOCAL void presentBootloaderInformation(void)
167172
{
168173
requestFirmwareConfig_t *requestFirmwareConfig = (requestFirmwareConfig_t *)_msgTmp.data;
169174
mSetLength(_msgTmp, sizeof(requestFirmwareConfig_t));
@@ -177,12 +182,13 @@ void presentBootloaderInformation(void)
177182
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM,
178183
ST_FIRMWARE_CONFIG_REQUEST, false));
179184
}
180-
bool isFirmwareUpdateOngoing(void)
185+
186+
LOCAL bool isFirmwareUpdateOngoing(void)
181187
{
182188
return _firmwareUpdateOngoing;
183189
}
184190
// do a crc16 on the whole received firmware
185-
bool transportIsValidFirmware(void)
191+
LOCAL bool transportIsValidFirmware(void)
186192
{
187193
// init crc
188194
uint16_t crc = ~0;

core/MyOTAFirmwareUpdate.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@
3434
*
3535
* MyOTAFirmwareUpdate debug log messages:
3636
*
37-
* |E| SYS | SUB | Message | Comment
37+
* |E| SYS | SUB | Message | Comment
3838
* |-|-----|-----|-----------------------------|----------------------------------------------------------------------------
39-
* | | OTA | FWP | UPDATE | FW update initiated
40-
* |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash
41-
* | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available
42-
* | | OTA | FWP | RECV B=%04X | Received FW block (B)
43-
* |!| OTA | FWP | WRONG FWB | Wrong FW block received
44-
* | | OTA | FWP | FW END | FW received, proceed to CRC verification
45-
* | | OTA | FWP | CRC OK | FW CRC verification OK
46-
* |!| OTA | FWP | CRC FAIL | FW CRC verification failed
47-
* | | OTA | FRQ | FW REQ,T=%04X,V=%04X,B=%04X | Request FW update, FW type (T), version (V), block (B)
48-
* |!| OTA | FRQ | FW UPD FAIL | FW update failed
49-
* | | OTA | CRC | B=%04X,C=%04X,F=%04X | FW CRC verification. FW blocks (B), calculated CRC (C), FW CRC (F)
39+
* | | OTA | FWP | UPDATE | FW update initiated
40+
* |!| OTA | FWP | UPDO | FW config response received, FW update already ongoing
41+
* |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash
42+
* | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available
43+
* | | OTA | FWP | RECV B=%04X | Received FW block (B)
44+
* |!| OTA | FWP | WRONG FWB | Wrong FW block received
45+
* | | OTA | FWP | FW END | FW received, proceed to CRC verification
46+
* | | OTA | FWP | CRC OK | FW CRC verification OK
47+
* |!| OTA | FWP | CRC FAIL | FW CRC verification failed
48+
* | | OTA | FRQ | FW REQ,T=%04X,V=%04X,B=%04X | Request FW update, FW type (T), version (V), block (B)
49+
* |!| OTA | FRQ | FW UPD FAIL | FW update failed
50+
* | | OTA | CRC | B=%04X,C=%04X,F=%04X | FW CRC verification. FW blocks (B), calculated CRC (C), FW CRC (F)
5051
*
5152
*
5253
* @brief API declaration for MyOTAFirmwareUpdate
@@ -58,6 +59,8 @@
5859

5960
#include "MySensorsCore.h"
6061

62+
#define LOCAL static //!< static
63+
6164
#define FIRMWARE_BLOCK_SIZE (16u) //!< Size of each firmware block
6265
#define FIRMWARE_MAX_REQUESTS (5u) //!< Number of times a firmware block should be requested before giving up
6366
#define MY_OTA_RETRY (5u) //!< Number of times to request a fw block before giving up
@@ -120,27 +123,27 @@ typedef struct {
120123
*
121124
* Current firmware settings (type, version, crc, blocks) are read into _fc
122125
*/
123-
void readFirmwareSettings(void);
126+
LOCAL void readFirmwareSettings(void);
124127
/**
125128
* @brief Handle OTA FW update requests
126129
*/
127-
void firmwareOTAUpdateRequest(void);
130+
LOCAL void firmwareOTAUpdateRequest(void);
128131
/**
129132
* @brief Handle OTA FW update responses
130133
*
131134
* This function handles incoming OTA FW packets and stores them to external flash (Sensebender)
132135
*/
133-
bool firmwareOTAUpdateProcess(void);
136+
LOCAL bool firmwareOTAUpdateProcess(void);
134137
/**
135138
* @brief Validate uploaded FW CRC
136139
*
137140
* This function verifies if uploaded FW CRC is valid
138141
*/
139-
bool transportIsValidFirmware(void);
142+
LOCAL bool transportIsValidFirmware(void);
140143
/**
141144
* @brief Present bootloader/FW information upon startup
142145
*/
143-
void presentBootloaderInformation(void);
146+
LOCAL void presentBootloaderInformation(void);
144147

145148
#endif
146149

core/MySensorsCore.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,6 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in
542542
CORE_DEBUG(PSTR("MCO:SLP:MS=%" PRIu32 ",SMS=%" PRIu8 ",I1=%" PRIu8 ",M1=%" PRIu8 ",I2=%" PRIu8
543543
",M2=%" PRIu8 "\n"), sleepingMS, smartSleep,
544544
interrupt1, mode1, interrupt2, mode2);
545-
// OTA FW feature: do not sleep if FW update ongoing
546-
#if defined(MY_OTA_FIRMWARE_FEATURE)
547-
if (isFirmwareUpdateOngoing()) {
548-
CORE_DEBUG(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing
549-
wait(sleepingMS);
550-
return MY_SLEEP_NOT_POSSIBLE;
551-
}
552-
#endif
553545
// repeater feature: sleeping not possible
554546
#if defined(MY_REPEATER_FEATURE)
555547
(void)smartSleep;
@@ -583,15 +575,36 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in
583575
return MY_SLEEP_NOT_POSSIBLE;
584576
}
585577
}
578+
// OTA FW feature: do not sleep if FW update ongoing
579+
#if defined(MY_OTA_FIRMWARE_FEATURE)
580+
while (isFirmwareUpdateOngoing() && sleepingTimeMS) {
581+
CORE_DEBUG(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing
582+
wait(1000ul);
583+
sleepingTimeMS = sleepingTimeMS >= 1000ul ? sleepingTimeMS - 1000ul : 1000ul;
584+
}
585+
#endif // MY_OTA_FIRMWARE_FEATURE
586586
if (smartSleep) {
587+
// sleeping time left?
588+
if (sleepingTimeMS < ((uint32_t)MY_SMART_SLEEP_WAIT_DURATION_MS)) {
589+
wait(sleepingMS);
590+
CORE_DEBUG(PSTR("!MCO:SLP:NTL\n")); // sleeping not possible, no time left
591+
return MY_SLEEP_NOT_POSSIBLE;
592+
}
587593
// notify controller about going to sleep, payload indicates smartsleep waiting time in MS
588594
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL,
589595
I_PRE_SLEEP_NOTIFICATION).set((uint32_t)MY_SMART_SLEEP_WAIT_DURATION_MS));
590596
wait(MY_SMART_SLEEP_WAIT_DURATION_MS); // listen for incoming messages
597+
#if defined(MY_OTA_FIRMWARE_FEATURE)
598+
// check if during smart sleep waiting period a FOTA request was received
599+
if (isFirmwareUpdateOngoing()) {
600+
CORE_DEBUG(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing
601+
return MY_SLEEP_NOT_POSSIBLE;
602+
}
603+
#endif // MY_OTA_FIRMWARE_FEATURE
591604
}
592605
#else
593606
(void)smartSleep;
594-
#endif
607+
#endif // MY_SENSOR_NETWORK
595608

596609
#if defined(MY_SENSOR_NETWORK)
597610
transportDisable();

core/MySensorsCore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* | | MCO | PIM | NODE REG=%%d | Registration response received, registration status (REG)
5353
* | | MCO | SLP | MS=%%lu,SMS=%%d,I1=%%d,M1=%%d,I2=%%d,M2=%%d | Sleep node, time (MS), smartSleep (SMS), Int1/M1, Int2/M2
5454
* | | MCO | SLP | WUP=%%d | Node woke-up, reason/IRQ (WUP)
55+
* |!| MCO | SLP | NTL | Sleeping not possible, no time left
5556
* |!| MCO | SLP | FWUPD | Sleeping not possible, FW update ongoing
5657
* |!| MCO | SLP | REP | Sleeping not possible, repeater feature enabled
5758
* |!| MCO | SLP | TNR | Transport not ready, attempt to reconnect until timeout (MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS)

0 commit comments

Comments
 (0)