Skip to content

Commit fe1105b

Browse files
SeppoTakalorlubos
authored andcommitted
net: lwm2m_client_utils: Handle retried FOTA writes
When Block N=0 is retransmitted, our FOTA handler might see that as a completely new FOTA process, if previous one failed. We should instead return failure if the FOTA is retried as we already know that we failed the previous time. If we instead accept the first block, then retries are just skipped and handled normally. Signed-off-by: Seppo Takalo <[email protected]>
1 parent d8ca58d commit fe1105b

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

subsys/net/lib/lwm2m_client_utils/lwm2m/lwm2m_firmware.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <zephyr/settings/settings.h>
2424
/* Firmware update needs access to internal functions as well */
2525
#include <lwm2m_engine.h>
26-
26+
#include <zephyr/net/coap.h>
2727
#include <modem/modem_info.h>
2828
#include <pm_config.h>
2929
#include <zephyr/sys/reboot.h>
@@ -670,6 +670,34 @@ static void dfu_target_cb(enum dfu_target_evt_id evt)
670670
ARG_UNUSED(evt);
671671
}
672672

673+
static bool is_duplicate_token(void)
674+
{
675+
static uint8_t prev_token[COAP_TOKEN_MAX_LEN];
676+
static uint8_t prev_token_len;
677+
struct lwm2m_ctx *ctx;
678+
struct lwm2m_message *msg;
679+
680+
ctx = lwm2m_rd_client_ctx();
681+
if (!ctx) {
682+
return false;
683+
}
684+
685+
msg = (struct lwm2m_message *) ctx->processed_req;
686+
if (!msg) {
687+
return false;
688+
}
689+
if (msg->tkl == 0) {
690+
return false;
691+
}
692+
if (msg->tkl == prev_token_len &&
693+
memcmp(msg->token, prev_token, prev_token_len) == 0) {
694+
return true;
695+
}
696+
memcpy(prev_token, msg->token, msg->tkl);
697+
prev_token_len = msg->tkl;
698+
return false;
699+
}
700+
673701
static int firmware_block_received_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id,
674702
uint8_t *data, uint16_t data_len, bool last_block,
675703
size_t total_size, size_t offset)
@@ -691,6 +719,11 @@ static int firmware_block_received_cb(uint16_t obj_inst_id, uint16_t res_id, uin
691719
return -EAGAIN;
692720
}
693721

722+
if (is_duplicate_token()) {
723+
LOG_DBG("Duplicate token, don't restart DFU");
724+
return -EAGAIN;
725+
}
726+
694727
ongoing_obj_id = obj_inst_id;
695728
client_acknowledge();
696729

0 commit comments

Comments
 (0)