-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcloud.c
More file actions
1231 lines (974 loc) · 32.6 KB
/
cloud.c
File metadata and controls
1231 lines (974 loc) · 32.6 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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <net/nrf_cloud_rest.h>
#include <nrf_cloud_coap_transport.h>
#include <zephyr/net/coap.h>
#include <app_version.h>
#include <date_time.h>
#if defined(CONFIG_MEMFAULT)
#include <memfault/ports/zephyr/http.h>
#include <memfault/metrics/metrics.h>
#include <memfault/panics/coredump.h>
#endif /* CONFIG_MEMFAULT */
#include "cloud.h"
#include "cloud_internal.h"
#include "cloud_provisioning.h"
#include "cloud_location.h"
#ifdef CONFIG_APP_ENVIRONMENTAL
#include "cloud_environmental.h"
#endif /* CONFIG_APP_ENVIRONMENTAL */
#include "app_common.h"
#include "network.h"
#include "storage.h"
/* 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"
#define AGNSS_MAX_DATA_SIZE 3800
/* Prevent nRF Provisioning Shell from being used to trigger provisioning.
* The cloud state machine does not support out of order provisioning via nRF Provisioning shell.
*/
BUILD_ASSERT(!IS_ENABLED(CONFIG_NRF_PROVISIONING_SHELL),
"nRF Provisioning Shell not supported, use att_cloud_provision shell command instead");
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 zbus subscriber */
ZBUS_MSG_SUBSCRIBER_DEFINE(cloud_subscriber);
/* Define the channels that the module subscribes to, their associated message types
* and the subscriber that will receive the messages on the channel.
* ENVIRONMENTAL_CHAN, POWER_CHAN, and LOCATION_CHAN are optional and are only included if the
* corresponding module is enabled.
*/
#define CHANNEL_LIST(X) \
X(NETWORK_CHAN, struct network_msg) \
X(CLOUD_CHAN, struct cloud_msg) \
X(STORAGE_CHAN, struct storage_msg) \
X(LOCATION_CHAN, struct location_msg) \
X(STORAGE_DATA_CHAN, struct storage_msg)
/* Calculate the maximum message size from the list of channels */
#define MAX_MSG_SIZE MAX_MSG_SIZE_FROM_LIST(CHANNEL_LIST)
/* Add the cloud_subscriber as observer to all the channels in the list. */
#define ADD_OBSERVERS(_chan, _type) ZBUS_CHAN_ADD_OBS(_chan, cloud_subscriber, 0);
/*
* Expand to a call to ZBUS_CHAN_ADD_OBS for each channel in the list.
* Example: ZBUS_CHAN_ADD_OBS(NETWORK_CHAN, cloud_subscriber, 0);
*/
CHANNEL_LIST(ADD_OBSERVERS)
ZBUS_CHAN_DEFINE(CLOUD_CHAN,
struct cloud_msg,
NULL,
NULL,
ZBUS_OBSERVERS_EMPTY,
ZBUS_MSG_INIT(.type = CLOUD_DISCONNECTED)
);
/* 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_subscriber),
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 */
/* Cloud module states */
enum cloud_module_state {
/* The cloud module has started and is running */
STATE_RUNNING,
/* Cloud connection is not established */
STATE_DISCONNECTED,
/* The module is connecting to cloud */
STATE_CONNECTING,
/* The module is trying to connect to cloud */
STATE_CONNECTING_ATTEMPT,
/* Module is provisioned to nRF Cloud CoAP */
STATE_PROVISIONED,
/* The module is trying to provision to nRF Cloud CoAP using
* nRF Cloud Provisioning Service
*/
STATE_PROVISIONING,
/* The module is waiting before trying to connect again */
STATE_CONNECTING_BACKOFF,
/* 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,
/* Connected to cloud and network connection, ready to send data */
STATE_CONNECTED_READY,
/* Connected to cloud, but not network connection */
STATE_CONNECTED_PAUSED,
};
/* Types of sections of the shadow document to poll for. */
enum shadow_poll_type {
SHADOW_POLL_DELTA,
SHADOW_POLL_DESIRED,
};
/* State object.
* Used to transfer context data between state changes.
*/
struct cloud_state_object {
/* 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];
/* Last network connection status */
bool network_connected;
/* Provisioning ongoing flag */
bool provisioning_ongoing;
/* Connection attempt counter. Reset when entering STATE_CONNECTING */
uint32_t connection_attempts;
/* Connection backoff time */
uint32_t backoff_time;
};
/* Forward declarations of state handlers */
static void state_running_entry(void *obj);
static void state_disconnected_entry(void *obj);
static enum smf_state_result state_disconnected_run(void *obj);
static void state_connecting_entry(void *obj);
static enum smf_state_result state_connecting_run(void *obj);
static void state_connecting_attempt_entry(void *obj);
static void state_connecting_provisioned_entry(void *obj);
static enum smf_state_result state_connecting_provisioned_run(void *obj);
static void state_connecting_provisioning_entry(void *obj);
static enum smf_state_result state_connecting_provisioning_run(void *obj);
static void state_connecting_backoff_entry(void *obj);
static enum smf_state_result state_connecting_backoff_run(void *obj);
static void state_connecting_backoff_exit(void *obj);
static void state_connected_entry(void *obj);
static void state_connected_exit(void *obj);
static void state_connected_ready_entry(void *obj);
static enum smf_state_result state_connected_ready_run(void *obj);
static void state_connected_paused_entry(void *obj);
static enum smf_state_result state_connected_paused_run(void *obj);
/* State machine definition */
static const struct smf_state states[] = {
[STATE_RUNNING] =
SMF_CREATE_STATE(state_running_entry, NULL, 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, state_connecting_run, NULL,
&states[STATE_RUNNING],
&states[STATE_CONNECTING_ATTEMPT]),
[STATE_CONNECTING_ATTEMPT] =
SMF_CREATE_STATE(state_connecting_attempt_entry, NULL, NULL,
&states[STATE_CONNECTING],
&states[STATE_PROVISIONED]),
[STATE_PROVISIONED] =
SMF_CREATE_STATE(state_connecting_provisioned_entry,
state_connecting_provisioned_run,
NULL,
&states[STATE_CONNECTING_ATTEMPT],
NULL),
[STATE_PROVISIONING] =
SMF_CREATE_STATE(state_connecting_provisioning_entry,
state_connecting_provisioning_run, NULL,
&states[STATE_CONNECTING_ATTEMPT],
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),
};
static void cloud_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_object *state_object)
{
ARG_UNUSED(state_object);
int err;
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN];
enum priv_cloud_msg msg = CLOUD_CONNECTION_FAILED;
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) {
LOG_INF("nRF Cloud CoAP connection successful");
msg = CLOUD_CONNECTION_SUCCESS;
} else if (err == -EACCES || err == -ENOEXEC || err == -ECONNREFUSED) {
LOG_WRN("nrf_cloud_coap_connect, error: %d", err);
LOG_WRN("nRF Cloud CoAP connection failed, unauthorized or invalid credentials");
msg = CLOUD_NOT_AUTHENTICATED;
} else {
LOG_WRN("nRF Cloud CoAP connection refused");
msg = CLOUD_CONNECTION_FAILED;
}
err = zbus_chan_pub(&PRIV_CLOUD_CHAN, &msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
}
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();
}
}
static void send_request_failed(void)
{
int err;
enum priv_cloud_msg cloud_msg = CLOUD_SEND_REQUEST_FAILED;
err = zbus_chan_pub(&PRIV_CLOUD_CHAN, &cloud_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
}
}
static void handle_network_data_message(const struct network_msg *msg)
{
int err;
bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
if (msg->type != NETWORK_QUALITY_SAMPLE_RESPONSE) {
return;
}
err = nrf_cloud_coap_sensor_send(CUSTOM_JSON_APPID_VAL_CONEVAL,
msg->conn_eval_params.energy_estimate,
NRF_CLOUD_NO_TIMESTAMP,
confirmable);
if (err) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
send_request_failed();
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) {
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
send_request_failed();
}
}
/* Storage handling functions */
static int send_storage_data_to_cloud(const struct storage_data_item *item)
{
int err;
int64_t timestamp_ms = NRF_CLOUD_NO_TIMESTAMP;
const bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
/* Get current timestamp */
err = date_time_now(×tamp_ms);
if (err) {
LOG_WRN("Failed to get current time, using no timestamp");
timestamp_ms = NRF_CLOUD_NO_TIMESTAMP;
}
#if defined(CONFIG_APP_POWER)
if (item->type == STORAGE_TYPE_BATTERY) {
double battery_percentage = item->data.BATTERY;
err = nrf_cloud_coap_sensor_send(CUSTOM_JSON_APPID_VAL_BATTERY,
battery_percentage,
timestamp_ms,
confirmable);
if (err) {
LOG_ERR("Failed to send battery data to cloud, error: %d", err);
return err;
}
LOG_DBG("Battery data sent to cloud: %.1f%%", battery_percentage);
/* Unused variable if no other sources compiled in */
(void)confirmable;
return 0;
}
#endif /* CONFIG_APP_POWER */
#if defined(CONFIG_APP_ENVIRONMENTAL)
if (item->type == STORAGE_TYPE_ENVIRONMENTAL) {
const struct environmental_msg *env = &item->data.ENVIRONMENTAL;
return cloud_environmental_send(env, timestamp_ms, confirmable);
}
#endif /* CONFIG_APP_ENVIRONMENTAL */
#if defined(CONFIG_APP_LOCATION)
if (item->type == STORAGE_TYPE_LOCATION) {
const struct location_msg *loc = &item->data.LOCATION;
cloud_location_handle_message(loc);
return 0;
}
#endif /* CONFIG_APP_LOCATION && CONFIG_LOCATION_METHOD_GNSS */
if (item->type == STORAGE_TYPE_NETWORK) {
const struct network_msg *net = &item->data.NETWORK;
handle_network_data_message(net);
return 0;
}
LOG_WRN("Unknown storage data type: %d", item->type);
/* Unused variable if no data sources are enabled */
(void)confirmable;
return -ENOTSUP;
}
static int request_storage_batch_data(uint32_t session_id)
{
int err;
struct storage_msg msg = {
.type = STORAGE_BATCH_REQUEST,
.session_id = session_id,
};
LOG_DBG("Requesting storage batch data, session_id: 0x%X", msg.session_id);
err = zbus_chan_pub(&STORAGE_CHAN, &msg, K_SECONDS(1));
if (err) {
LOG_ERR("Failed to request storage batch data, error: %d", err);
return err;
}
return 0;
}
static void handle_storage_batch_available(const struct storage_msg *msg)
{
int err;
struct storage_data_item item;
uint32_t items_processed = 0;
uint32_t items_available = msg->data_len;
uint32_t session_id = msg->session_id;
struct storage_msg close_msg = {
.type = STORAGE_BATCH_CLOSE,
.session_id = session_id,
};
bool session_error = false;
LOG_INF("Processing storage batch: %u items available", items_available);
/* Drain the batch buffer: read until timeout, abort on hard error */
while (!session_error) {
err = storage_batch_read(&item, K_MSEC(500));
if (err == -EAGAIN) {
LOG_DBG("No more data available in batch (timeout)");
break;
} else if (err) {
LOG_ERR("storage_batch_read failed, error: %d", err);
session_error = true;
continue;
}
/* Success: send the data item to cloud */
err = send_storage_data_to_cloud(&item);
if (err) {
LOG_ERR("Failed to send storage data to cloud, error: %d", err);
}
items_processed++;
}
LOG_DBG("Processed %u/%u storage items", items_processed, items_available);
if (!session_error && msg->more_data) {
LOG_DBG("More data available in batch, requesting next batch");
err = request_storage_batch_data(session_id);
if (err) {
LOG_ERR("Failed to request next storage batch data, error: %d", err);
}
return;
}
/* Close the batch session */
err = zbus_chan_pub(&STORAGE_CHAN, &close_msg, K_SECONDS(1));
if (err) {
LOG_ERR("Failed to close storage batch session, error: %d", err);
}
}
static void handle_storage_batch_empty(const struct storage_msg *msg)
{
int err;
struct storage_msg close_msg = {
.type = STORAGE_BATCH_CLOSE,
.session_id = msg->session_id,
};
LOG_DBG("Storage batch is empty, closing session");
err = zbus_chan_pub(&STORAGE_CHAN, &close_msg, K_SECONDS(1));
if (err) {
LOG_ERR("Failed to close empty storage batch session, error: %d", err);
}
}
static void handle_storage_batch_error(const struct storage_msg *msg)
{
int err;
struct storage_msg close_msg = {
.type = STORAGE_BATCH_CLOSE,
.session_id = msg->session_id,
};
LOG_ERR("Storage batch error occurred, closing session");
err = zbus_chan_pub(&STORAGE_CHAN, &close_msg, K_SECONDS(1));
if (err) {
LOG_ERR("Failed to close error storage batch session, error: %d", err);
}
}
static void handle_storage_batch_busy(const struct storage_msg *msg)
{
ARG_UNUSED(msg);
LOG_WRN("Storage batch is busy, will retry later");
/* Could implement retry logic here if needed */
}
static void handle_storage_data(const struct storage_msg *msg)
{
int err;
/* Handle real-time storage data (from flush or passthrough mode) */
struct storage_data_item item;
/* Extract data from the storage message buffer */
if (msg->data_len > sizeof(item.data)) {
LOG_ERR("Storage data too large: %d bytes", msg->data_len);
return;
}
item.type = msg->data_type;
memcpy(&item.data, msg->buffer, msg->data_len);
/* Send to cloud */
err = send_storage_data_to_cloud(&item);
if (err) {
LOG_ERR("Failed to send real-time storage data to cloud, error: %d", err);
}
}
static void shadow_poll(enum shadow_poll_type type)
{
int err;
struct cloud_msg msg = {
.type = (type == SHADOW_POLL_DELTA) ? CLOUD_SHADOW_RESPONSE_DELTA :
CLOUD_SHADOW_RESPONSE_DESIRED,
.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,
(type == SHADOW_POLL_DELTA) ? true : false,
COAP_CONTENT_FORMAT_APP_CBOR);
if (err) {
LOG_ERR("nrf_cloud_coap_shadow_get, error: %d", err);
send_request_failed();
return;
}
if (msg.response.buffer_data_len == 0) {
LOG_DBG("Shadow %s section not present",
(type == SHADOW_POLL_DELTA) ? "delta" : "desired");
return;
}
/* Workaround: 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 buffer is empty, ignore");
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("nrf_cloud_coap_patch, error: %d", err);
send_request_failed();
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;
}
}
static void handle_cloud_channel_message(struct cloud_state_object const *state_object)
{
int err;
const struct cloud_msg *msg = MSG_TO_CLOUD_MSG_PTR(state_object->msg_buf);
const bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
switch (msg->type) {
case CLOUD_PAYLOAD_JSON:
err = nrf_cloud_coap_json_message_send(msg->payload.buffer,
false, confirmable);
if (err) {
LOG_ERR("nrf_cloud_coap_json_message_send, error: %d", err);
send_request_failed();
}
break;
case CLOUD_POLL_SHADOW:
LOG_DBG("Poll shadow trigger received");
/* On shadow poll requests, we only poll for delta changes. This is because
* we have gotten our entire desired section when polling the shadow
* on an established connection.
*/
shadow_poll(SHADOW_POLL_DELTA);
break;
case CLOUD_PROVISIONING_REQUEST:
LOG_DBG("Provisioning request received");
smf_set_state(SMF_CTX(state_object), &states[STATE_PROVISIONING]);
break;
default:
break;
}
}
static void handle_priv_cloud_message(struct cloud_state_object const *state_object)
{
enum priv_cloud_msg msg = *(const enum priv_cloud_msg *)state_object->msg_buf;
if ((msg == CLOUD_SEND_REQUEST_FAILED) && (state_object->network_connected)) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTING]);
} else if ((msg == CLOUD_SEND_REQUEST_FAILED) && (!state_object->network_connected)) {
smf_set_state(SMF_CTX(state_object), &states[STATE_DISCONNECTED]);
}
}
static void handle_storage_channel_message(struct cloud_state_object const *state_object)
{
const struct storage_msg *msg = MSG_TO_STORAGE_MSG(state_object->msg_buf);
switch (msg->type) {
case STORAGE_BATCH_AVAILABLE:
LOG_DBG("Storage batch available, %d items, session_id: 0x%X",
msg->data_len, msg->session_id);
handle_storage_batch_available(msg);
break;
case STORAGE_BATCH_EMPTY:
LOG_DBG("Storage batch empty, session_id: 0x%X", msg->session_id);
handle_storage_batch_empty(msg);
break;
case STORAGE_BATCH_ERROR:
LOG_ERR("Storage batch error, session_id: 0x%X", msg->session_id);
handle_storage_batch_error(msg);
break;
case STORAGE_BATCH_BUSY:
LOG_WRN("Storage batch busy, session_id: 0x%X", msg->session_id);
handle_storage_batch_busy(msg);
break;
default:
break;
}
}
static void handle_storage_data_message(struct cloud_state_object const *state_object)
{
const struct storage_msg *msg = MSG_TO_STORAGE_MSG(state_object->msg_buf);
if (msg->type == STORAGE_DATA) {
LOG_DBG("Storage data received, type: %d, size: %d",
msg->data_type, msg->data_len);
handle_storage_data(msg);
}
}
static void network_connection_status_retain(struct cloud_state_object *state_object)
{
if (state_object->chan == &NETWORK_CHAN) {
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
if (msg.type == NETWORK_DISCONNECTED || msg.type == NETWORK_CONNECTED) {
/* Update network status to retain the last connection status */
state_object->network_connected =
(msg.type == NETWORK_CONNECTED) ? true : false;
}
}
}
/* State handlers */
static void state_running_entry(void *obj)
{
int err;
ARG_UNUSED(obj);
LOG_DBG("%s", __func__);
err = nrf_cloud_coap_init();
if (err) {
LOG_ERR("nrf_cloud_coap_init, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = cloud_provisioning_init();
if (err) {
LOG_ERR("nrf_provisioning_init, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static void state_disconnected_entry(void *obj)
{
int err;
struct cloud_msg cloud_msg = {
.type = CLOUD_DISCONNECTED,
};
ARG_UNUSED(obj);
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 enum smf_state_result state_disconnected_run(void *obj)
{
struct cloud_state_object const *state_object = obj;
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 SMF_EVENT_HANDLED;
}
return SMF_EVENT_PROPAGATE;
}
static void state_connecting_entry(void *obj)
{
/* Reset connection attempts counter */
struct cloud_state_object *state_object = obj;
LOG_DBG("%s", __func__);
state_object->connection_attempts = 0;
state_object->provisioning_ongoing = false;
}
static enum smf_state_result state_connecting_run(void *obj)
{
struct cloud_state_object *state_object = obj;
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 SMF_EVENT_HANDLED;
}
}
return SMF_EVENT_PROPAGATE;
}
static void state_connecting_attempt_entry(void *obj)
{
struct cloud_state_object *state_object = obj;
LOG_DBG("%s", __func__);
state_object->connection_attempts++;
}
static void state_connecting_provisioned_entry(void *obj)
{
struct cloud_state_object *state_object = obj;
LOG_DBG("%s", __func__);
state_object->provisioning_ongoing = false;
connect_to_cloud(state_object);
}
static enum smf_state_result state_connecting_provisioned_run(void *obj)
{
struct cloud_state_object *state_object = obj;
if (state_object->chan == &PRIV_CLOUD_CHAN) {
enum priv_cloud_msg msg = *(const enum priv_cloud_msg *)state_object->msg_buf;
if (msg == CLOUD_NOT_AUTHENTICATED) {
smf_set_state(SMF_CTX(state_object), &states[STATE_PROVISIONING]);
return SMF_EVENT_HANDLED;
} else if (msg == CLOUD_CONNECTION_SUCCESS) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTED]);
return SMF_EVENT_HANDLED;
} else if (msg == CLOUD_CONNECTION_FAILED) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTING_BACKOFF]);
return SMF_EVENT_HANDLED;
}
}
return SMF_EVENT_PROPAGATE;
}
static void state_connecting_provisioning_entry(void *obj)
{
int err;
struct cloud_state_object *state_object = obj;
struct location_msg location_msg = {
.type = LOCATION_SEARCH_CANCEL,
};
LOG_DBG("%s", __func__);
/* Cancel any ongoing location search during provisioning to allow writing credentials,
* which requires offline LTE functional mode.
*/
err = zbus_chan_pub(&LOCATION_CHAN, &location_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
state_object->provisioning_ongoing = true;
err = cloud_provisioning_trigger();
if (err) {
LOG_ERR("nrf_provisioning_trigger_manually, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static enum smf_state_result state_connecting_provisioning_run(void *obj)
{
struct cloud_state_object *state_object = obj;
if (state_object->chan == &PRIV_CLOUD_CHAN) {
enum priv_cloud_msg msg = *(const enum priv_cloud_msg *)state_object->msg_buf;
if (msg == CLOUD_PROVISIONING_FINISHED) {
smf_set_state(SMF_CTX(state_object), &states[STATE_PROVISIONED]);
return SMF_EVENT_HANDLED;
} else if (msg == CLOUD_PROVISIONING_FAILED && state_object->network_connected) {
smf_set_state(SMF_CTX(state_object), &states[STATE_CONNECTING_BACKOFF]);
return SMF_EVENT_HANDLED;
} else if (msg == CLOUD_PROVISIONING_FAILED && !state_object->network_connected) {
smf_set_state(SMF_CTX(state_object), &states[STATE_DISCONNECTED]);
return SMF_EVENT_HANDLED;
}
}
/* Its expected that the device goes online/offline a few times during provisioning.
* Therefore we handle network connected/disconnected events in this state preventing it
* from propagating up the state machine changing the cloud module's connectivity status.
*/
if (state_object->chan == &NETWORK_CHAN) {
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
if (msg.type == NETWORK_DISCONNECTED || msg.type == NETWORK_CONNECTED) {
return SMF_EVENT_HANDLED;
}
}
return SMF_EVENT_PROPAGATE;
}
static void state_connecting_backoff_entry(void *obj)
{
int err;
struct cloud_state_object *state_object = obj;
LOG_DBG("%s", __func__);
state_object->backoff_time = calculate_backoff_time(state_object->connection_attempts);
LOG_WRN("Connection attempt failed, backoff time: %u seconds",
state_object->backoff_time);
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 enum smf_state_result state_connecting_backoff_run(void *obj)
{
struct cloud_state_object const *state_object = obj;
if (state_object->chan == &PRIV_CLOUD_CHAN) {
const enum priv_cloud_msg msg = *(const enum priv_cloud_msg *)state_object->msg_buf;
/* If the backoff timer expired, we can either continue provisioning or
* connect to cloud if already provisioned. The provisioning ongoing flag helps us
* determine what substate of connecting attempt we are attempting to enter.
*/
if ((msg == CLOUD_BACKOFF_EXPIRED) && !state_object->provisioning_ongoing) {
smf_set_state(SMF_CTX(state_object), &states[STATE_PROVISIONED]);
return SMF_EVENT_HANDLED;
} else if ((msg == CLOUD_BACKOFF_EXPIRED) && state_object->provisioning_ongoing) {
smf_set_state(SMF_CTX(state_object), &states[STATE_PROVISIONING]);
return SMF_EVENT_HANDLED;
}
}
return SMF_EVENT_PROPAGATE;
}
static void state_connecting_backoff_exit(void *obj)
{
ARG_UNUSED(obj);
LOG_DBG("%s", __func__);
(void)k_work_cancel_delayable(&backoff_timer_work);
}
static void state_connected_entry(void *obj)
{
ARG_UNUSED(obj);