-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfota.c
More file actions
472 lines (383 loc) · 11.5 KB
/
fota.c
File metadata and controls
472 lines (383 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/zbus/zbus.h>
#include <net/nrf_cloud_coap.h>
#include <net/nrf_cloud_fota_poll.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/task_wdt/task_wdt.h>
#include <nrf_cloud_fota.h>
#include <zephyr/smf.h>
#include <net/fota_download.h>
#include "message_channel.h"
#include "modules_common.h"
#include "fota.h"
/* Register log module */
LOG_MODULE_REGISTER(fota, CONFIG_APP_FOTA_LOG_LEVEL);
/* Register message subscriber - will be called everytime a channel that the module listens on
* receives a new message.
*/
ZBUS_MSG_SUBSCRIBER_DEFINE(fota);
/* Define FOTA channel */
ZBUS_CHAN_DEFINE(FOTA_CHAN,
enum fota_msg_type,
NULL,
NULL,
ZBUS_OBSERVERS_EMPTY,
ZBUS_MSG_INIT(0)
);
/* Observe channels */
ZBUS_CHAN_ADD_OBS(FOTA_CHAN, fota, 0);
#define MAX_MSG_SIZE sizeof(enum fota_msg_type)
/* FOTA support context */
static void fota_reboot(enum nrf_cloud_fota_reboot_status status);
static void fota_status(enum nrf_cloud_fota_status status, const char *const status_details);
enum fota_module_state {
/* The module is initialized and running */
STATE_RUNNING,
/* The module is waiting for a poll request */
STATE_WAITING_FOR_POLL_REQUEST,
/* The module is polling for an update */
STATE_POLLING_FOR_UPDATE,
/* The module is downloading an update */
STATE_DOWNLOADING_UPDATE,
/* The module is waiting for the event FOTA_IMAGE_APPLY to apply the image */
STATE_WAITING_FOR_IMAGE_APPLY,
/* The FOTA module is waiting for a reboot */
STATE_REBOOT_NEEDED,
/* The FOTA module has been canceled, cleaning up */
STATE_CANCELED,
};
/* User defined state object.
* Used to transfer data between state changes.
*/
struct fota_state {
/* This must be first */
struct smf_ctx ctx;
/* Last channel type that a message was received on */
const struct zbus_channel *chan;
/* Buffer for last zbus message */
uint8_t msg_buf[MAX_MSG_SIZE];
/* FOTA context */
struct nrf_cloud_fota_poll_ctx fota_ctx;
};
/* Forward declarations */
static void state_running_entry(void *o);
static void state_running_run(void *o);
static void state_waiting_for_poll_request_entry(void *o);
static void state_waiting_for_poll_request_run(void *o);
static void state_polling_for_update_entry(void *o);
static void state_polling_for_update_run(void *o);
static void state_downloading_update_entry(void *o);
static void state_downloading_update_run(void *o);
static void state_waiting_for_image_apply_entry(void *o);
static void state_waiting_for_image_apply_run(void *o);
static void state_reboot_needed_entry(void *o);
static void state_canceled_entry(void *o);
static struct fota_state fota_state = {
.fota_ctx.reboot_fn = fota_reboot,
.fota_ctx.status_fn = fota_status,
};
static const struct smf_state states[] = {
[STATE_RUNNING] =
SMF_CREATE_STATE(state_running_entry,
state_running_run,
NULL,
NULL, /* No parent state */
&states[STATE_WAITING_FOR_POLL_REQUEST]),
[STATE_WAITING_FOR_POLL_REQUEST] =
SMF_CREATE_STATE(state_waiting_for_poll_request_entry,
state_waiting_for_poll_request_run,
NULL,
&states[STATE_RUNNING],
NULL), /* No initial transition */
[STATE_POLLING_FOR_UPDATE] =
SMF_CREATE_STATE(state_polling_for_update_entry,
state_polling_for_update_run,
NULL,
&states[STATE_RUNNING],
NULL),
[STATE_DOWNLOADING_UPDATE] =
SMF_CREATE_STATE(state_downloading_update_entry,
state_downloading_update_run,
NULL,
&states[STATE_RUNNING],
NULL),
[STATE_WAITING_FOR_IMAGE_APPLY] =
SMF_CREATE_STATE(state_waiting_for_image_apply_entry,
state_waiting_for_image_apply_run,
NULL,
&states[STATE_RUNNING],
NULL),
[STATE_REBOOT_NEEDED] =
SMF_CREATE_STATE(state_reboot_needed_entry,
NULL,
NULL,
&states[STATE_RUNNING],
NULL),
[STATE_CANCELED] =
SMF_CREATE_STATE(state_canceled_entry,
NULL,
NULL,
&states[STATE_RUNNING],
NULL),
};
/* Private functions */
static void fota_reboot(enum nrf_cloud_fota_reboot_status status)
{
int err;
enum fota_msg_type evt = FOTA_SUCCESS_REBOOT_NEEDED;
LOG_DBG("Reboot requested with FOTA status %d", status);
err = zbus_chan_pub(&FOTA_CHAN, &evt, K_SECONDS(1));
if (err) {
LOG_DBG("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
}
static void fota_status(enum nrf_cloud_fota_status status, const char *const status_details)
{
int err;
enum fota_msg_type evt = { 0 };
LOG_DBG("FOTA status: %d, details: %s", status, status_details ? status_details : "None");
switch (status) {
case NRF_CLOUD_FOTA_DOWNLOADING:
LOG_DBG("Downloading firmware update");
evt = FOTA_DOWNLOADING_UPDATE;
break;
case NRF_CLOUD_FOTA_FAILED:
LOG_WRN("Firmware download failed");
evt = FOTA_DOWNLOAD_FAILED;
break;
case NRF_CLOUD_FOTA_TIMED_OUT:
LOG_WRN("Firmware download timed out");
evt = FOTA_DOWNLOAD_TIMED_OUT;
break;
case NRF_CLOUD_FOTA_SUCCEEDED:
LOG_DBG("Firmware update succeeded");
LOG_DBG("Waiting for reboot request from the nRF Cloud FOTA Poll library");
/* Don't send any event in case of success, the nRF Cloud FOTA poll library will
* notify when a reboot is needed via the fota_reboot() callback.
*/
return;
case NRF_CLOUD_FOTA_FMFU_VALIDATION_NEEDED:
LOG_DBG("Full Modem FOTA Update validation needed, network disconnect required");
evt = FOTA_IMAGE_APPLY_NEEDED;
break;
default:
LOG_DBG("Unknown FOTA status: %d", status);
return;
}
err = zbus_chan_pub(&FOTA_CHAN, &evt, K_SECONDS(1));
if (err) {
LOG_DBG("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
}
static void task_wdt_callback(int channel_id, void *user_data)
{
LOG_DBG("Watchdog expired, Channel: %d, Thread: %s",
channel_id, k_thread_name_get((k_tid_t)user_data));
SEND_FATAL_ERROR_WATCHDOG_TIMEOUT();
}
/* State handlers */
static void state_running_entry(void *o)
{
int err;
struct fota_state *state_object = o;
LOG_DBG("%s", __func__);
/* Initialize the FOTA context */
err = nrf_cloud_fota_poll_init(&state_object->fota_ctx);
if (err) {
LOG_DBG("nrf_cloud_fota_poll_init failed: %d", err);
SEND_FATAL_ERROR();
}
/* Process pending FOTA job, the FOTA type is returned */
err = nrf_cloud_fota_poll_process_pending(&state_object->fota_ctx);
if (err < 0) {
LOG_DBG("nrf_cloud_fota_poll_process_pending failed: %d", err);
} else if (err != NRF_CLOUD_FOTA_TYPE__INVALID) {
LOG_DBG("Processed pending FOTA job type: %d", err);
}
}
static void state_running_run(void *o)
{
const struct fota_state *state_object = (const struct fota_state *)o;
if (&FOTA_CHAN == state_object->chan) {
const enum fota_msg_type msg_type = MSG_TO_FOTA_TYPE(state_object->msg_buf);
if (msg_type == FOTA_CANCEL) {
STATE_SET(fota_state, STATE_CANCELED);
}
}
}
static void state_waiting_for_poll_request_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
}
static void state_waiting_for_poll_request_run(void *o)
{
const struct fota_state *state_object = (const struct fota_state *)o;
if (&FOTA_CHAN == state_object->chan) {
const enum fota_msg_type msg_type = MSG_TO_FOTA_TYPE(state_object->msg_buf);
if (msg_type == FOTA_POLL_REQUEST) {
STATE_SET(fota_state, STATE_POLLING_FOR_UPDATE);
} else if (msg_type == FOTA_CANCEL) {
LOG_DBG("No ongoing FOTA update, nothing to cancel");
STATE_EVENT_HANDLED(fota_state);
}
}
}
static void state_polling_for_update_entry(void *o)
{
struct fota_state *state_object = o;
LOG_DBG("%s", __func__);
/* Start the FOTA processing */
int err = nrf_cloud_fota_poll_process(&state_object->fota_ctx);
switch (err) {
case -EAGAIN:
LOG_DBG("No FOTA job available");
enum fota_msg_type evt = FOTA_NO_AVAILABLE_UPDATE;
err = zbus_chan_pub(&FOTA_CHAN, &evt, K_SECONDS(1));
if (err) {
LOG_DBG("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
break;
case -ENOENT:
LOG_DBG("FOTA job finished, status reported to nRF Cloud");
break;
case 0:
LOG_DBG("Job available, FOTA processing started");
break;
default:
LOG_DBG("nrf_cloud_fota_poll_process, error: %d", err);
SEND_FATAL_ERROR();
break;
}
}
static void state_polling_for_update_run(void *o)
{
const struct fota_state *state_object = (const struct fota_state *)o;
if (&FOTA_CHAN == state_object->chan) {
const enum fota_msg_type evt = MSG_TO_FOTA_TYPE(state_object->msg_buf);
switch (evt) {
case FOTA_DOWNLOADING_UPDATE:
STATE_SET(fota_state, STATE_DOWNLOADING_UPDATE);
break;
case FOTA_NO_AVAILABLE_UPDATE:
STATE_SET(fota_state, STATE_WAITING_FOR_POLL_REQUEST);
break;
default:
/* Don't care */
break;
}
}
}
static void state_downloading_update_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
}
static void state_downloading_update_run(void *o)
{
const struct fota_state *state_object = (const struct fota_state *)o;
if (&FOTA_CHAN == state_object->chan) {
const enum fota_msg_type evt = MSG_TO_FOTA_TYPE(state_object->msg_buf);
switch (evt) {
case FOTA_IMAGE_APPLY_NEEDED:
STATE_SET(fota_state, STATE_WAITING_FOR_IMAGE_APPLY);
break;
case FOTA_DOWNLOAD_FAILED:
STATE_SET(fota_state, STATE_WAITING_FOR_POLL_REQUEST);
break;
default:
/* Don't care */
break;
}
}
}
static void state_waiting_for_image_apply_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
}
static void state_waiting_for_image_apply_run(void *o)
{
struct fota_state *state_object = o;
if (&FOTA_CHAN == state_object->chan) {
const enum fota_msg_type evt = MSG_TO_FOTA_TYPE(state_object->msg_buf);
switch (evt) {
case FOTA_IMAGE_APPLY:
LOG_DBG("Applying downloaded firmware image");
/* Apply the downloaded firmware image */
int err = nrf_cloud_fota_poll_update_apply(&state_object->fota_ctx);
if (err) {
LOG_DBG("nrf_cloud_fota_poll_update_apply, error: %d", err);
SEND_FATAL_ERROR();
}
break;
case FOTA_SUCCESS_REBOOT_NEEDED:
STATE_SET(fota_state, STATE_REBOOT_NEEDED);
break;
default:
/* Don't care */
break;
}
}
}
static void state_reboot_needed_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("Waiting for the application to reboot in order to apply the update");
}
static void state_canceled_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
LOG_WRN("Canceling download");
(void)fota_download_cancel();
STATE_SET(fota_state, STATE_WAITING_FOR_POLL_REQUEST);
}
/* End of state handlers */
static void fota_task(void)
{
int err;
int task_wdt_id;
const uint32_t wdt_timeout_ms = (CONFIG_APP_FOTA_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
const uint32_t execution_time_ms = (CONFIG_APP_FOTA_EXEC_TIME_SECONDS_MAX * MSEC_PER_SEC);
const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms);
LOG_DBG("FOTA module task started");
task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get());
STATE_SET_INITIAL(fota_state, STATE_RUNNING);
while (true) {
err = task_wdt_feed(task_wdt_id);
if (err) {
LOG_DBG("task_wdt_feed, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = zbus_sub_wait_msg(&fota, &fota_state.chan, fota_state.msg_buf, zbus_wait_ms);
if (err == -ENOMSG) {
continue;
} else if (err) {
LOG_DBG("zbus_sub_wait_msg, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = STATE_RUN(fota_state);
if (err) {
LOG_DBG("handle_message, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
}
K_THREAD_DEFINE(fota_task_id,
CONFIG_APP_FOTA_THREAD_STACK_SIZE,
fota_task, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);