-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcloud_module.c
More file actions
785 lines (623 loc) · 19.8 KB
/
cloud_module.c
File metadata and controls
785 lines (623 loc) · 19.8 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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
* 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/smf.h>
#include <zephyr/task_wdt/task_wdt.h>
#include <net/nrf_cloud.h>
#include <net/nrf_cloud_coap.h>
#include <nrf_cloud_coap_transport.h>
#include <zephyr/net/coap.h>
#include <app_version.h>
#include "cloud_module.h"
#include "app_common.h"
#include "network.h"
#if defined(CONFIG_APP_POWER)
#include "power.h"
#endif /* CONFIG_APP_POWER */
#if defined(CONFIG_APP_ENVIRONMENTAL)
#include "environmental.h"
#endif /* CONFIG_APP_ENVIRONMENTAL */
/* Register log module */
LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
#define CUSTOM_JSON_APPID_VAL_CONEVAL "CONEVAL"
#define CUSTOM_JSON_APPID_VAL_BATTERY "BATTERY"
#if defined(CONFIG_APP_POWER)
#define BAT_MSG_SIZE sizeof(struct power_msg)
#else
#define BAT_MSG_SIZE 0
#endif /* CONFIG_APP_POWER */
#if defined(CONFIG_APP_ENVIRONMENTAL)
#define ENV_MSG_SIZE sizeof(struct environmental_msg)
#else
#define ENV_MSG_SIZE 0
#endif /* CONFIG_APP_ENVIRONMENTAL) */
#define MAX_MSG_SIZE (MAX(sizeof(struct cloud_msg), \
MAX(sizeof(struct network_msg), \
MAX(BAT_MSG_SIZE, ENV_MSG_SIZE))))
BUILD_ASSERT(CONFIG_APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS >
CONFIG_APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS,
"Watchdog timeout must be greater than maximum message processing time");
/* Register subscriber */
ZBUS_MSG_SUBSCRIBER_DEFINE(cloud);
/* Observe channels */
ZBUS_CHAN_ADD_OBS(NETWORK_CHAN, cloud, 0);
ZBUS_CHAN_ADD_OBS(CLOUD_CHAN, cloud, 0);
#if defined(CONFIG_APP_ENVIRONMENTAL)
ZBUS_CHAN_ADD_OBS(ENVIRONMENTAL_CHAN, cloud, 0);
#endif /* CONFIG_APP_ENVIRONMENTAL */
#if defined(CONFIG_APP_POWER)
ZBUS_CHAN_ADD_OBS(POWER_CHAN, cloud, 0);
#endif /* CONFIG_APP_POWER */
/* Define channels provided by this module */
ZBUS_CHAN_DEFINE(CLOUD_CHAN,
struct cloud_msg,
NULL,
NULL,
ZBUS_OBSERVERS_EMPTY,
ZBUS_MSG_INIT(.type = CLOUD_DISCONNECTED)
);
/* Enumerator to be used in privat cloud channel */
enum priv_cloud_msg {
CLOUD_BACKOFF_EXPIRED,
};
/* Create private cloud channel for internal messaging that is not intended for external use.
* The channel is needed to communicate from asynchronous callbacks to the state machine and
* ensure state transitions only happen from the cloud module thread where the state machine
* is running.
*/
ZBUS_CHAN_DEFINE(PRIV_CLOUD_CHAN,
enum priv_cloud_msg,
NULL,
NULL,
ZBUS_OBSERVERS(cloud),
CLOUD_BACKOFF_EXPIRED
);
/* Connection attempt backoff timer is run as a delayable work on the system workqueue */
static void backoff_timer_work_fn(struct k_work *work);
static K_WORK_DELAYABLE_DEFINE(backoff_timer_work, backoff_timer_work_fn);
/* State machine definitions */
static const struct smf_state states[];
/* Forward declarations of state handlers */
static void state_running_entry(void *o);
static void state_running_run(void *o);
static void state_disconnected_entry(void *o);
static void state_disconnected_run(void *o);
static void state_connecting_entry(void *o);
static void state_connecting_attempt_entry(void *o);
static void state_connecting_backoff_entry(void *o);
static void state_connecting_backoff_run(void *o);
static void state_connecting_backoff_exit(void *o);
static void state_connected_entry(void *o);
static void state_connected_exit(void *o);
static void state_connected_ready_entry(void *o);
static void state_connected_ready_run(void *o);
static void state_connected_paused_entry(void *o);
static void state_connected_paused_run(void *o);
/* Defining the hierarchical cloud module states:
*
* STATE_RUNNING: The cloud module has started and is running
* - STATE_DISCONNECTED: Cloud connection is not established
* - STATE_CONNECTING: The module is connecting to cloud
* - STATE_CONNECTING_ATTEMPT: The module is trying to connect to cloud
* - STATE_CONNECTING_BACKOFF: The module is waiting before trying to connect again
* - STATE_CONNECTED: Cloud connection has been established. Note that because of
* connection ID being used, the connection is valid even though
* network connection is intermittently lost (and socket is closed)
* - STATE_CONNECTED_READY: Connected to cloud and network connection, ready to send
* - STATE_CONNECTED_PAUSED: Connected to cloud, but not network connection
*/
enum cloud_module_state {
STATE_RUNNING,
STATE_DISCONNECTED,
STATE_CONNECTING,
STATE_CONNECTING_ATTEMPT,
STATE_CONNECTING_BACKOFF,
STATE_CONNECTED,
STATE_CONNECTED_READY,
STATE_CONNECTED_PAUSED,
};
/* Construct state table */
static const struct smf_state states[] = {
[STATE_RUNNING] =
SMF_CREATE_STATE(state_running_entry, state_running_run, NULL,
NULL, /* No parent state */
&states[STATE_DISCONNECTED]), /* Initial transition */
[STATE_DISCONNECTED] =
SMF_CREATE_STATE(state_disconnected_entry, state_disconnected_run, NULL,
&states[STATE_RUNNING],
NULL),
[STATE_CONNECTING] = SMF_CREATE_STATE(
state_connecting_entry, NULL, NULL,
&states[STATE_RUNNING],
&states[STATE_CONNECTING_ATTEMPT]),
[STATE_CONNECTING_ATTEMPT] = SMF_CREATE_STATE(
state_connecting_attempt_entry, NULL, NULL,
&states[STATE_CONNECTING],
NULL),
[STATE_CONNECTING_BACKOFF] = SMF_CREATE_STATE(
state_connecting_backoff_entry, state_connecting_backoff_run,
state_connecting_backoff_exit,
&states[STATE_CONNECTING],
NULL),
[STATE_CONNECTED] =
SMF_CREATE_STATE(state_connected_entry, NULL, state_connected_exit,
&states[STATE_RUNNING],
&states[STATE_CONNECTED_READY]),
[STATE_CONNECTED_READY] =
SMF_CREATE_STATE(state_connected_ready_entry, state_connected_ready_run, NULL,
&states[STATE_CONNECTED],
NULL),
[STATE_CONNECTED_PAUSED] =
SMF_CREATE_STATE(state_connected_paused_entry, state_connected_paused_run, NULL,
&states[STATE_CONNECTED],
NULL),
};
/* Cloud module state object.
* Used to transfer data between state changes.
*/
struct cloud_state {
/* This must be first */
struct smf_ctx ctx;
/* Last channel type that a message was received on */
const struct zbus_channel *chan;
/* Last received message */
uint8_t msg_buf[MAX_MSG_SIZE];
/* Network status */
enum network_msg_type nw_status;
/* Connection attempt counter. Reset when entering STATE_CONNECTING */
uint32_t connection_attempts;
/* Connection backoff time */
uint32_t backoff_time;
};
/* Static helper function */
static void task_wdt_callback(int channel_id, void *user_data)
{
LOG_ERR("Watchdog expired, Channel: %d, Thread: %s",
channel_id, k_thread_name_get((k_tid_t)user_data));
SEND_FATAL_ERROR_WATCHDOG_TIMEOUT();
}
static void connect_to_cloud(const struct cloud_state *state_object)
{
int err;
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN];
err = nrf_cloud_client_id_get(buf, sizeof(buf));
if (err == 0) {
LOG_INF("Connecting to nRF Cloud CoAP with client ID: %s", buf);
} else {
LOG_ERR("nrf_cloud_client_id_get, error: %d, cannot continue", err);
SEND_FATAL_ERROR();
return;
}
err = nrf_cloud_coap_connect(APP_VERSION_STRING);
if (err == 0) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTED]);
return;
}
/* Connection failed, retry */
LOG_ERR("nrf_cloud_coap_connect, error: %d", err);
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTING_BACKOFF]);
}
static uint32_t calculate_backoff_time(uint32_t attempts)
{
uint32_t backoff_time = CONFIG_APP_CLOUD_BACKOFF_INITIAL_SECONDS;
/* Calculate backoff time */
if (IS_ENABLED(CONFIG_APP_CLOUD_BACKOFF_TYPE_EXPONENTIAL)) {
backoff_time = CONFIG_APP_CLOUD_BACKOFF_INITIAL_SECONDS << (attempts - 1);
} else if (IS_ENABLED(CONFIG_APP_CLOUD_BACKOFF_TYPE_LINEAR)) {
backoff_time = CONFIG_APP_CLOUD_BACKOFF_INITIAL_SECONDS +
((attempts - 1) * CONFIG_APP_CLOUD_BACKOFF_LINEAR_INCREMENT_SECONDS);
}
/* Limit backoff time */
if (backoff_time > CONFIG_APP_CLOUD_BACKOFF_MAX_SECONDS) {
backoff_time = CONFIG_APP_CLOUD_BACKOFF_MAX_SECONDS;
}
LOG_DBG("Backoff time: %u seconds", backoff_time);
return backoff_time;
}
static void backoff_timer_work_fn(struct k_work *work)
{
int err;
enum priv_cloud_msg msg = CLOUD_BACKOFF_EXPIRED;
ARG_UNUSED(work);
err = zbus_chan_pub(&PRIV_CLOUD_CHAN, &msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
}
/* Zephyr State Machine Framework handlers */
/* Handler for STATE_RUNNING */
static void state_running_entry(void *o)
{
int err;
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
err = nrf_cloud_coap_init();
if (err) {
LOG_ERR("nrf_cloud_coap_init, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static void state_running_run(void *o)
{
const struct cloud_state *state_object = (const struct cloud_state *)o;
if (state_object->chan == &NETWORK_CHAN) {
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
if (msg.type == NETWORK_DISCONNECTED) {
smf_set_state(SMF_CTX(state_object), &states[STATE_DISCONNECTED]);
return;
}
}
}
/* Handlers for STATE_CLOUD_DISCONNECTED. */
static void state_disconnected_entry(void *o)
{
int err;
struct cloud_msg cloud_msg = {
.type = CLOUD_DISCONNECTED,
};
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
err = zbus_chan_pub(&CLOUD_CHAN, &cloud_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static void state_disconnected_run(void *o)
{
const struct cloud_state *state_object = (const struct cloud_state *)o;
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
if ((state_object->chan == &NETWORK_CHAN) && (msg.type == NETWORK_CONNECTED)) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTING]);
return;
}
}
/* Handlers for STATE_CONNECTING */
static void state_connecting_entry(void *o)
{
/* Reset connection attempts counter */
struct cloud_state *state_object = o;
LOG_DBG("%s", __func__);
state_object->connection_attempts = 0;
}
/* Handler for STATE_CONNECTING_ATTEMPT */
static void state_connecting_attempt_entry(void *o)
{
struct cloud_state *state_object = o;
LOG_DBG("%s", __func__);
state_object->connection_attempts++;
connect_to_cloud(state_object);
}
/* Handler for STATE_CONNECTING_BACKOFF */
static void state_connecting_backoff_entry(void *o)
{
int err;
struct cloud_state *state_object = o;
LOG_DBG("%s", __func__);
state_object->backoff_time = calculate_backoff_time(state_object->connection_attempts);
err = k_work_schedule(&backoff_timer_work, K_SECONDS(state_object->backoff_time));
if (err < 0) {
LOG_ERR("k_work_schedule, error: %d", err);
SEND_FATAL_ERROR();
}
}
static void state_connecting_backoff_run(void *o)
{
const struct cloud_state *state_object = (const struct cloud_state *)o;
if (state_object->chan == &PRIV_CLOUD_CHAN) {
const enum priv_cloud_msg msg = *(const enum priv_cloud_msg *)state_object->msg_buf;
if (msg == CLOUD_BACKOFF_EXPIRED) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTING_ATTEMPT]);
return;
}
}
}
static void state_connecting_backoff_exit(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
(void)k_work_cancel_delayable(&backoff_timer_work);
}
/* Handler for STATE_CLOUD_CONNECTED. */
static void state_connected_entry(void *o)
{
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
LOG_INF("Connected to Cloud");
}
static void state_connected_exit(void *o)
{
int err;
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
err = nrf_cloud_coap_disconnect();
if (err && (err != -ENOTCONN && err != -EPERM)) {
LOG_ERR("nrf_cloud_coap_disconnect, error: %d", err);
SEND_FATAL_ERROR();
}
}
/* Handlers for STATE_CONNECTED_READY */
static void shadow_get(bool delta_only)
{
int err;
struct cloud_msg msg = {
.type = CLOUD_SHADOW_RESPONSE,
.response = {
.buffer_data_len = sizeof(msg.response.buffer),
},
};
LOG_DBG("Requesting device shadow from the device");
err = nrf_cloud_coap_shadow_get(msg.response.buffer,
&msg.response.buffer_data_len,
delta_only,
COAP_CONTENT_FORMAT_APP_CBOR);
if (err == -EACCES) {
LOG_WRN("Not connected, error: %d", err);
return;
} else if (err == -ETIMEDOUT) {
LOG_WRN("Request timed out, error: %d", err);
return;
} else if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err > 0) {
LOG_WRN("Cloud error: %d", err);
return;
} else if (err == -E2BIG) {
LOG_WRN("The provided buffer is not large enough, error: %d", err);
return;
} else if (err) {
LOG_ERR("Failed to request shadow delta: %d", err);
return;
}
if (msg.response.buffer_data_len == 0) {
LOG_DBG("No shadow delta changes available");
return;
}
/* Workaroud: Sometimes nrf_cloud_coap_shadow_get() returns 0 even though obtaining
* the shadow failed. Ignore the payload if the first 10 bytes are zero.
*/
if (!memcmp(msg.response.buffer, "\0\0\0\0\0\0\0\0\0\0", 10)) {
LOG_WRN("Returned buffeør is empty, ignore");
return;
}
err = zbus_chan_pub(&CLOUD_CHAN, &msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
/* Clear the shadow delta by reporting the same data back to the shadow reported state */
err = nrf_cloud_coap_patch("state/reported", NULL,
msg.response.buffer,
msg.response.buffer_data_len,
COAP_CONTENT_FORMAT_APP_CBOR,
true,
NULL,
NULL);
if (err) {
LOG_ERR("Failed to patch the device shadow, error: %d", err);
return;
}
}
static void state_connected_ready_entry(void *o)
{
int err;
struct cloud_msg cloud_msg = {
.type = CLOUD_CONNECTED_READY_TO_SEND,
};
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
err = zbus_chan_pub(&CLOUD_CHAN, &cloud_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
shadow_get(false);
}
static void state_connected_ready_run(void *o)
{
int err;
const struct cloud_state *state_object = (const struct cloud_state *)o;
bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
if (state_object->chan == &NETWORK_CHAN) {
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
switch (msg.type) {
case NETWORK_DISCONNECTED:
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTED_PAUSED]);
break;
case NETWORK_CONNECTED:
smf_set_handled(SMF_CTX(state_object));
break;
case NETWORK_QUALITY_SAMPLE_RESPONSE:
err = nrf_cloud_coap_sensor_send(CUSTOM_JSON_APPID_VAL_CONEVAL,
msg.conn_eval_params.energy_estimate,
NRF_CLOUD_NO_TIMESTAMP,
confirmable);
if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_RSRP,
msg.conn_eval_params.rsrp,
NRF_CLOUD_NO_TIMESTAMP,
confirmable);
if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
SEND_FATAL_ERROR();
return;
}
break;
default:
break;
}
}
#if defined(CONFIG_APP_POWER)
if (state_object->chan == &POWER_CHAN) {
struct power_msg msg = MSG_TO_POWER_MSG(state_object->msg_buf);
if (msg.type == POWER_BATTERY_PERCENTAGE_SAMPLE_RESPONSE) {
err = nrf_cloud_coap_sensor_send(CUSTOM_JSON_APPID_VAL_BATTERY,
msg.percentage,
NRF_CLOUD_NO_TIMESTAMP,
confirmable);
if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
SEND_FATAL_ERROR();
return;
}
return;
}
}
#endif /* CONFIG_APP_POWER */
#if defined(CONFIG_APP_ENVIRONMENTAL)
if (state_object->chan == &ENVIRONMENTAL_CHAN) {
struct environmental_msg msg = MSG_TO_ENVIRONMENTAL_MSG(state_object->msg_buf);
if (msg.type == ENVIRONMENTAL_SENSOR_SAMPLE_RESPONSE) {
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_TEMP,
msg.temperature,
NRF_CLOUD_NO_TIMESTAMP,
confirmable);
if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_AIR_PRESS,
msg.pressure,
NRF_CLOUD_NO_TIMESTAMP,
confirmable);
if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_HUMID,
msg.humidity,
NRF_CLOUD_NO_TIMESTAMP,
confirmable);
if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
SEND_FATAL_ERROR();
return;
}
return;
}
}
#endif /* CONFIG_APP_ENVIRONMENTAL */
if (state_object->chan == &CLOUD_CHAN) {
const struct cloud_msg msg = MSG_TO_CLOUD_MSG(state_object->msg_buf);
if (msg.type == CLOUD_PAYLOAD_JSON) {
err = nrf_cloud_coap_json_message_send(msg.payload.buffer,
false, confirmable);
if (err == -ENETUNREACH) {
LOG_WRN("Network is unreachable, error: %d", err);
return;
} else if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
}
if (state_object->chan == &CLOUD_CHAN) {
const enum cloud_msg_type msg_type = MSG_TO_CLOUD_MSG(state_object->msg_buf).type;
if (msg_type == CLOUD_POLL_SHADOW) {
LOG_DBG("Poll shadow trigger received");
shadow_get(true);
}
}
}
/* Handlers for STATE_CONNECTED_PAUSED */
static void state_connected_paused_entry(void *o)
{
int err;
struct cloud_msg cloud_msg = {
.type = CLOUD_CONNECTED_PAUSED,
};
ARG_UNUSED(o);
LOG_DBG("%s", __func__);
err = zbus_chan_pub(&CLOUD_CHAN, &cloud_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static void state_connected_paused_run(void *o)
{
const struct cloud_state *state_object = (const struct cloud_state *)o;
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
if ((state_object->chan == &NETWORK_CHAN) && (msg.type == NETWORK_CONNECTED)) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTED_READY]);
return;
}
}
/* End of state handlers */
static void cloud_module_thread(void)
{
int err;
int task_wdt_id;
const uint32_t wdt_timeout_ms = (CONFIG_APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
const uint32_t execution_time_ms =
(CONFIG_APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC);
const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms);
struct cloud_state cloud_state = { 0 };
LOG_DBG("cloud module task started");
task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get());
/* Initialize the state machine to STATE_RUNNING, which will also run its entry function */
smf_set_initial(SMF_CTX(&cloud_state), &states[STATE_RUNNING]);
while (true) {
err = task_wdt_feed(task_wdt_id);
if (err) {
LOG_ERR("task_wdt_feed, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = zbus_sub_wait_msg(&cloud, &cloud_state.chan, cloud_state.msg_buf,
zbus_wait_ms);
if (err == -ENOMSG) {
continue;
} else if (err) {
LOG_ERR("zbus_sub_wait_msg, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = smf_run_state(SMF_CTX(&cloud_state));
if (err) {
LOG_ERR("smf_run_state(), error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
}
K_THREAD_DEFINE(cloud_module_thread_id,
CONFIG_APP_CLOUD_THREAD_STACK_SIZE,
cloud_module_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);