-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathapp.c
More file actions
599 lines (479 loc) · 13.4 KB
/
app.c
File metadata and controls
599 lines (479 loc) · 13.4 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/zbus/zbus.h>
#include <zephyr/task_wdt/task_wdt.h>
#include <zephyr/smf.h>
#include "modules_common.h"
#include "message_channel.h"
#include "button.h"
#include "network.h"
#include "cloud_module.h"
#include "fota.h"
#include "location.h"
#if defined(CONFIG_APP_LED)
#include "led.h"
#endif /* CONFIG_APP_LED */
#if defined(CONFIG_APP_ENVIRONMENTAL)
#include "environmental.h"
#endif /* CONFIG_APP_ENVIRONMENTAL */
#if defined(CONFIG_APP_BATTERY)
#include "battery.h"
#endif /* CONFIG_APP_BATTERY */
/* Register log module */
LOG_MODULE_REGISTER(app, CONFIG_APP_LOG_LEVEL);
/* Define a ZBUS listener for this module */
static void app_callback(const struct zbus_channel *chan);
ZBUS_LISTENER_DEFINE(app_listener, app_callback);
/* Observe channels */
ZBUS_CHAN_ADD_OBS(CONFIG_CHAN, app_listener, 0);
ZBUS_CHAN_ADD_OBS(CLOUD_CHAN, app_listener, 0);
ZBUS_CHAN_ADD_OBS(BUTTON_CHAN, app_listener, 0);
ZBUS_CHAN_ADD_OBS(FOTA_CHAN, app_listener, 0);
ZBUS_CHAN_ADD_OBS(NETWORK_CHAN, app_listener, 0);
/* Forward declarations */
static void trigger_work_fn(struct k_work *work);
/* Delayable work used to schedule triggers */
static K_WORK_DELAYABLE_DEFINE(trigger_work, trigger_work_fn);
/* Forward declarations of state handlers */
static void running_entry(void *o);
static void running_run(void *o);
static void periodic_triggering_entry(void *o);
static void periodic_triggering_run(void *o);
static void idle_entry(void *o);
static void idle_run(void *o);
static void fota_entry(void *o);
static void fota_run(void *o);
static void fota_downloading_run(void *o);
static void fota_network_disconnect_entry(void *o);
static void fota_network_disconnect_run(void *o);
static void fota_applying_image_entry(void *o);
static void fota_applying_image_run(void *o);
static void fota_rebooting_entry(void *o);
enum state {
/* Normal operation */
STATE_RUNNING,
/* Triggers are periodically sent at a configured interval */
STATE_PERIODIC_TRIGGERING,
/* Disconnected from the network, no triggers are sent */
STATE_IDLE,
/* Ongoing FOTA process, triggers are blocked */
STATE_FOTA,
/* FOTA image is being downloaded */
STATE_FOTA_DOWNLOADING,
/* Disconnecting from the network */
STATE_FOTA_NETWORK_DISCONNECT,
/* Applying the image */
STATE_FOTA_APPLYING_IMAGE,
/* Rebooting */
STATE_FOTA_REBOOTING,
};
/* State object for the app module.
* Used to transfer data between state changes.
*/
struct app_state_object {
/* This must be first */
struct smf_ctx ctx;
/* Last channel type that a message was received on */
const struct zbus_channel *chan;
/* Trigger interval */
uint64_t interval_sec;
/* Button number */
uint8_t button_number;
/* Time available */
enum time_status time_status;
/* Cloud status */
enum cloud_msg_type status;
/* FOTA status */
enum fota_msg_type fota_status;
/* Network status */
enum network_msg_type network_status;
};
static struct app_state_object app_state;
/* Construct state table */
static const struct smf_state states[] = {
[STATE_RUNNING] = SMF_CREATE_STATE(
running_entry,
running_run,
NULL,
NULL,
NULL
),
[STATE_PERIODIC_TRIGGERING] = SMF_CREATE_STATE(
periodic_triggering_entry,
periodic_triggering_run,
NULL,
&states[STATE_RUNNING],
NULL
),
[STATE_IDLE] = SMF_CREATE_STATE(
idle_entry,
idle_run,
NULL,
&states[STATE_RUNNING],
NULL
),
[STATE_FOTA] = SMF_CREATE_STATE(
fota_entry,
fota_run,
NULL,
NULL,
&states[STATE_FOTA_DOWNLOADING]
),
[STATE_FOTA_DOWNLOADING] = SMF_CREATE_STATE(
NULL,
fota_downloading_run,
NULL,
&states[STATE_FOTA],
NULL
),
[STATE_FOTA_NETWORK_DISCONNECT] = SMF_CREATE_STATE(
fota_network_disconnect_entry,
fota_network_disconnect_run,
NULL,
&states[STATE_FOTA],
NULL
),
[STATE_FOTA_APPLYING_IMAGE] = SMF_CREATE_STATE(
fota_applying_image_entry,
fota_applying_image_run,
NULL,
&states[STATE_FOTA],
NULL
),
[STATE_FOTA_REBOOTING] = SMF_CREATE_STATE(
fota_rebooting_entry,
NULL,
NULL,
&states[STATE_FOTA],
NULL
),
};
static void triggers_send(void)
{
int err;
#if defined(CONFIG_APP_REQUEST_NETWORK_QUALITY)
struct network_msg network_msg = {
.type = NETWORK_QUALITY_SAMPLE_REQUEST,
};
err = zbus_chan_pub(&NETWORK_CHAN, &network_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
#endif /* CONFIG_APP_REQUEST_NETWORK_QUALITY */
#if defined(CONFIG_APP_BATTERY)
struct battery_msg battery_msg = {
.type = BATTERY_PERCENTAGE_SAMPLE_REQUEST,
};
err = zbus_chan_pub(&BATTERY_CHAN, &battery_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
#endif /* CONFIG_APP_BATTERY */
#if defined(CONFIG_APP_ENVIRONMENTAL)
struct battery_msg environmental_msg = {
.type = ENVIRONMENTAL_SENSOR_SAMPLE_REQUEST,
};
err = zbus_chan_pub(&ENVIRONMENTAL_CHAN, &environmental_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
#endif /* CONFIG_APP_ENVIRONMENTAL */
/* Send FOTA poll trigger */
enum fota_msg_type fota_msg = FOTA_POLL_REQUEST;
err = zbus_chan_pub(&FOTA_CHAN, &fota_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub FOTA trigger, error: %d", err);
SEND_FATAL_ERROR();
return;
}
/* Send trigger for shadow polling */
struct cloud_msg cloud_msg = {
.type = CLOUD_POLL_SHADOW
};
err = zbus_chan_pub(&CLOUD_CHAN, &cloud_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub shadow trigger, error: %d", err);
SEND_FATAL_ERROR();
return;
}
/* Trigger location search and environmental data sample */
enum location_msg_type location_msg = LOCATION_SEARCH_TRIGGER;
err = zbus_chan_pub(&LOCATION_CHAN, &location_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub data sample trigger, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
/* Delayable work used to send triggers to the rest of the system */
static void trigger_work_fn(struct k_work *work)
{
ARG_UNUSED(work);
LOG_DBG("Sending data sample trigger");
triggers_send();
LOG_DBG("Next trigger in %lld seconds", app_state.interval_sec);
k_work_reschedule(&trigger_work, K_SECONDS(app_state.interval_sec));
}
/* Zephyr State Machine framework handlers */
/* STATE_RUNNING */
static void running_entry(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
LOG_DBG("%s", __func__);
if (state_object->status == CLOUD_CONNECTED_READY_TO_SEND ||
state_object->status == CLOUD_PAYLOAD_JSON ||
state_object->status == CLOUD_POLL_SHADOW) {
STATE_SET(app_state, STATE_PERIODIC_TRIGGERING);
return;
}
STATE_SET(app_state, STATE_IDLE);
}
static void running_run(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
if (state_object->chan == &FOTA_CHAN &&
state_object->fota_status == FOTA_DOWNLOADING_UPDATE) {
STATE_SET(app_state, STATE_FOTA);
return;
}
}
/* STATE_IDLE */
static void idle_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
#if defined(CONFIG_APP_LED)
/* Blink Yellow */
struct led_msg led_msg = {
.type = LED_RGB_SET,
.red = 255,
.green = 255,
.blue = 0,
.duration_on_msec = 250,
.duration_off_msec = 2000,
.repetitions = 10,
};
int err = zbus_chan_pub(&LED_CHAN, &led_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
#endif /* CONFIG_APP_LED */
k_work_cancel_delayable(&trigger_work);
}
static void idle_run(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
if ((state_object->chan == &CLOUD_CHAN) &&
(state_object->status == CLOUD_CONNECTED_READY_TO_SEND)) {
LOG_DBG("Cloud connected and ready, going into periodic triggering state");
STATE_SET(app_state, STATE_PERIODIC_TRIGGERING);
return;
}
}
/* STATE_CONNECTED */
static void periodic_triggering_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
#if defined(CONFIG_APP_LED)
/* Blink Green */
struct led_msg led_msg = {
.type = LED_RGB_SET,
.red = 0,
.green = 255,
.blue = 0,
.duration_on_msec = 250,
.duration_off_msec = 2000,
.repetitions = 10,
};
int err = zbus_chan_pub(&LED_CHAN, &led_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
#endif /* CONFIG_APP_LED */
k_work_reschedule(&trigger_work, K_NO_WAIT);
}
static void periodic_triggering_run(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
if ((state_object->chan == &CLOUD_CHAN) &&
((state_object->status == CLOUD_CONNECTED_PAUSED) ||
(state_object->status == CLOUD_DISCONNECTED))) {
LOG_DBG("Cloud disconnected/paused, going into idle state");
STATE_SET(app_state, STATE_IDLE);
return;
}
if (state_object->chan == &BUTTON_CHAN) {
k_work_reschedule(&trigger_work, K_NO_WAIT);
return;
}
if (state_object->chan == &CONFIG_CHAN) {
LOG_DBG("Configuration update, new interval: %lld", state_object->interval_sec);
k_work_reschedule(&trigger_work, K_SECONDS(state_object->interval_sec));
return;
}
}
/* STATE_FOTA */
static void fota_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
k_work_cancel_delayable(&trigger_work);
}
static void fota_run(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
if (state_object->chan == &FOTA_CHAN) {
switch (state_object->fota_status) {
case FOTA_CANCELED:
__fallthrough;
case FOTA_DOWNLOAD_TIMED_OUT:
__fallthrough;
case FOTA_DOWNLOAD_FAILED:
STATE_SET(app_state, STATE_RUNNING);
return;
default:
/* Don't care */
break;
}
}
}
/* STATE_FOTA_DOWNLOADING */
static void fota_downloading_run(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
if (state_object->chan == &FOTA_CHAN) {
switch (state_object->fota_status) {
case FOTA_SUCCESS_REBOOT_NEEDED:
STATE_SET(app_state, STATE_FOTA_NETWORK_DISCONNECT);
return;
case FOTA_IMAGE_APPLY_NEEDED:
STATE_SET(app_state, STATE_FOTA_APPLYING_IMAGE);
return;
default:
/* Don't care */
break;
}
}
}
/* STATE_FOTA_NETWORK_DISCONNECT */
static void fota_network_disconnect_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
int err;
struct network_msg msg = {
.type = NETWORK_DISCONNECT
};
err = zbus_chan_pub(&NETWORK_CHAN, &msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
}
static void fota_network_disconnect_run(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
if (state_object->chan == &NETWORK_CHAN &&
state_object->network_status == NETWORK_DISCONNECTED) {
STATE_SET(app_state, STATE_FOTA_REBOOTING);
return;
}
}
/* STATE_FOTA_APPLYING_IMAGE, */
static void fota_applying_image_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
int err;
struct network_msg msg = {
.type = NETWORK_DISCONNECT
};
err = zbus_chan_pub(&NETWORK_CHAN, &msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
}
static void fota_applying_image_run(void *o)
{
const struct app_state_object *state_object = (const struct app_state_object *)o;
if (state_object->chan == &NETWORK_CHAN &&
state_object->network_status == NETWORK_DISCONNECTED) {
int err;
enum fota_msg_type msg = FOTA_IMAGE_APPLY;
err = zbus_chan_pub(&FOTA_CHAN, &msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
} else if (state_object->chan == &FOTA_CHAN &&
state_object->fota_status == FOTA_SUCCESS_REBOOT_NEEDED) {
STATE_SET(app_state, STATE_FOTA_REBOOTING);
return;
}
}
/* STATE_FOTA_REBOOTING */
static void fota_rebooting_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
/* Reboot the device */
LOG_WRN("Rebooting the device to apply the FOTA update");
/* Flush log buffer */
LOG_PANIC();
k_sleep(K_SECONDS(5));
sys_reboot(SYS_REBOOT_COLD);
}
/* Function called when there is a message received on a channel that the module listens to */
static void app_callback(const struct zbus_channel *chan)
{
int err;
/* Update the state object with the channel that the message was received on */
app_state.chan = chan;
/* Copy corresponding data to the state object depending on the incoming channel */
if (&CONFIG_CHAN == chan) {
const struct configuration *config = zbus_chan_const_msg(chan);
if (config->update_interval_present) {
app_state.interval_sec = config->update_interval;
}
} else if (&CLOUD_CHAN == chan) {
const struct cloud_msg *cloud_msg = zbus_chan_const_msg(chan);
app_state.status = cloud_msg->type;
} else if (&FOTA_CHAN == chan) {
const enum fota_msg_type *fota_status = zbus_chan_const_msg(chan);
app_state.fota_status = *fota_status;
} else if (&NETWORK_CHAN == chan) {
const struct network_msg *network_msg = zbus_chan_const_msg(chan);
app_state.network_status = network_msg->type;
}
/* State object updated, run SMF */
err = STATE_RUN(app_state);
if (err) {
LOG_ERR("smf_run_state, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static int app_init(void)
{
app_state.interval_sec = CONFIG_APP_MODULE_TRIGGER_TIMEOUT_SECONDS;
STATE_SET_INITIAL(app_state, STATE_RUNNING);
return 0;
}
SYS_INIT(app_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);