Skip to content

Commit d159284

Browse files
committed
modules: fota: Rename events and add state transition
* Make events from the FOTA module more descriptive. * Add state transition from STATE_DOWNLOADING_UPDATE to STATE_WAITING_FOR_POLL_REQUEST if the download is canceled from the cloud. * Update tests accordingly. Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent b20f015 commit d159284

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

app/src/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ static void periodic_triggering_entry(void *o)
377377
}
378378
#endif /* CONFIG_APP_LED */
379379

380-
k_work_reschedule(&trigger_work, K_NO_WAIT);
380+
k_work_reschedule(&trigger_work, K_SECONDS(10));
381381
}
382382

383383
static void periodic_triggering_run(void *o)
@@ -421,7 +421,7 @@ static void fota_run(void *o)
421421

422422
if (state_object->chan == &FOTA_CHAN) {
423423
switch (state_object->fota_status) {
424-
case FOTA_CANCELED:
424+
case FOTA_DOWNLOAD_CANCELED:
425425
__fallthrough;
426426
case FOTA_DOWNLOAD_TIMED_OUT:
427427
__fallthrough;

app/src/modules/fota/fota.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void fota_status(enum nrf_cloud_fota_status status, const char *const sta
193193
case NRF_CLOUD_FOTA_CANCELED:
194194
LOG_WRN("Firmware download canceled");
195195

196-
evt = FOTA_CANCELED;
196+
evt = FOTA_DOWNLOAD_CANCELED;
197197
break;
198198
case NRF_CLOUD_FOTA_TIMED_OUT:
199199
LOG_WRN("Firmware download timed out");
@@ -265,7 +265,7 @@ static void state_running_run(void *o)
265265
if (&FOTA_CHAN == state_object->chan) {
266266
const enum fota_msg_type msg_type = MSG_TO_FOTA_TYPE(state_object->msg_buf);
267267

268-
if (msg_type == FOTA_CANCEL) {
268+
if (msg_type == FOTA_DOWNLOAD_CANCEL) {
269269
STATE_SET(fota_state, STATE_CANCELED);
270270
}
271271
}
@@ -287,7 +287,7 @@ static void state_waiting_for_poll_request_run(void *o)
287287

288288
if (msg_type == FOTA_POLL_REQUEST) {
289289
STATE_SET(fota_state, STATE_POLLING_FOR_UPDATE);
290-
} else if (msg_type == FOTA_CANCEL) {
290+
} else if (msg_type == FOTA_DOWNLOAD_CANCEL) {
291291
LOG_DBG("No ongoing FOTA update, nothing to cancel");
292292

293293
STATE_EVENT_HANDLED(fota_state);
@@ -343,7 +343,7 @@ static void state_polling_for_update_run(void *o)
343343
case FOTA_NO_AVAILABLE_UPDATE:
344344
STATE_SET(fota_state, STATE_WAITING_FOR_POLL_REQUEST);
345345
break;
346-
case FOTA_CANCEL:
346+
case FOTA_DOWNLOAD_CANCEL:
347347
LOG_DBG("No ongoing FOTA update, nothing to cancel");
348348

349349
STATE_EVENT_HANDLED(fota_state);
@@ -373,6 +373,10 @@ static void state_downloading_update_run(void *o)
373373
case FOTA_IMAGE_APPLY_NEEDED:
374374
STATE_SET(fota_state, STATE_WAITING_FOR_IMAGE_APPLY);
375375
break;
376+
case FOTA_DOWNLOAD_CANCELED:
377+
__fallthrough;
378+
case FOTA_DOWNLOAD_TIMED_OUT:
379+
__fallthrough;
376380
case FOTA_DOWNLOAD_FAILED:
377381
STATE_SET(fota_state, STATE_WAITING_FOR_POLL_REQUEST);
378382
break;
@@ -450,7 +454,7 @@ static void state_canceling_run(void *o)
450454
if (&FOTA_CHAN == state_object->chan) {
451455
const enum fota_msg_type msg = MSG_TO_FOTA_TYPE(state_object->msg_buf);
452456

453-
if (msg == FOTA_CANCELED) {
457+
if (msg == FOTA_DOWNLOAD_CANCELED) {
454458
STATE_SET(fota_state, STATE_WAITING_FOR_POLL_REQUEST);
455459
}
456460
}

app/src/modules/fota/fota.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ enum fota_msg_type {
4141
*/
4242
FOTA_IMAGE_APPLY_NEEDED,
4343

44-
/* Event notified when the FOTA update has been canceled. */
45-
FOTA_CANCELED,
44+
/* Event notified when the FOTA download has been canceled. */
45+
FOTA_DOWNLOAD_CANCELED,
4646

4747
/* Input message types */
4848

@@ -52,8 +52,8 @@ enum fota_msg_type {
5252
/* Request to apply the downloaded firmware image. */
5353
FOTA_IMAGE_APPLY,
5454

55-
/* Cancel the FOTA process. */
56-
FOTA_CANCEL,
55+
/* Cancel the FOTA download. */
56+
FOTA_DOWNLOAD_CANCEL,
5757
};
5858

5959
#define MSG_TO_FOTA_TYPE(_msg) (*(const enum fota_msg_type *)_msg)

tests/module/fota/src/fota_module_test.c

+38-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ void tearDown(void)
6767
no_events_expect(3600);
6868

6969
/* Reset DuTs internal state between each test case */
70-
event_send(FOTA_CANCEL);
71-
event_expect(FOTA_CANCEL);
70+
event_send(FOTA_DOWNLOAD_CANCEL);
71+
event_expect(FOTA_DOWNLOAD_CANCEL);
7272
}
7373

7474
static void event_expect(enum fota_msg_type expected_fota_type)
@@ -187,6 +187,42 @@ void test_fota_module_should_fail_on_timeout(void)
187187
event_expect(FOTA_DOWNLOAD_TIMED_OUT);
188188
}
189189

190+
void test_fota_module_should_fail_on_fail(void)
191+
{
192+
/* Given */
193+
nrf_cloud_fota_poll_process_fake.return_val = 0;
194+
195+
/* 1. Poll for update */
196+
event_send(FOTA_POLL_REQUEST);
197+
event_expect(FOTA_POLL_REQUEST);
198+
199+
/* 2. Downloading update */
200+
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_DOWNLOADING);
201+
event_expect(FOTA_DOWNLOADING_UPDATE);
202+
203+
/* 3. Download failed */
204+
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_FAILED);
205+
event_expect(FOTA_DOWNLOAD_FAILED);
206+
}
207+
208+
void test_fota_module_should_fail_on_cancelation(void)
209+
{
210+
/* Given */
211+
nrf_cloud_fota_poll_process_fake.return_val = 0;
212+
213+
/* 1. Poll for update */
214+
event_send(FOTA_POLL_REQUEST);
215+
event_expect(FOTA_POLL_REQUEST);
216+
217+
/* 2. Downloading update */
218+
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_DOWNLOADING);
219+
event_expect(FOTA_DOWNLOADING_UPDATE);
220+
221+
/* 3. Download canceled */
222+
invoke_nrf_cloud_fota_callback_stub_status(NRF_CLOUD_FOTA_CANCELED);
223+
event_expect(FOTA_DOWNLOAD_CANCELED);
224+
}
225+
190226
/* This is required to be added to each test. That is because unity's
191227
* main may return nonzero, while zephyr's main currently must
192228
* return 0 in all cases (other values are reserved).

0 commit comments

Comments
 (0)