11
11
#include <zephyr/task_wdt/task_wdt.h>
12
12
#include <net/nrf_cloud.h>
13
13
#include <net/nrf_cloud_coap.h>
14
+ #include <nrf_cloud_coap_transport.h>
15
+ #include <zephyr/net/coap.h>
14
16
#include <app_version.h>
15
17
16
- #if defined(CONFIG_MEMFAULT )
17
- #include <memfault/core/trace_event.h>
18
- #endif /* CONFIG_MEMFAULT */
19
-
20
18
#include "cloud_module.h"
21
19
#include "app_common.h"
22
20
#include "network.h"
@@ -47,7 +45,7 @@ LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
47
45
#define ENV_MSG_SIZE 0
48
46
#endif /* CONFIG_APP_ENVIRONMENTAL) */
49
47
50
- #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_payload), \
48
+ #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_msg), \
51
49
MAX(sizeof(struct network_msg), \
52
50
MAX(BAT_MSG_SIZE, ENV_MSG_SIZE))))
53
51
@@ -59,7 +57,6 @@ BUILD_ASSERT(CONFIG_APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS >
59
57
ZBUS_MSG_SUBSCRIBER_DEFINE (cloud );
60
58
61
59
/* Observe channels */
62
- ZBUS_CHAN_ADD_OBS (PAYLOAD_CHAN , cloud , 0 );
63
60
ZBUS_CHAN_ADD_OBS (NETWORK_CHAN , cloud , 0 );
64
61
ZBUS_CHAN_ADD_OBS (CLOUD_CHAN , cloud , 0 );
65
62
@@ -73,14 +70,6 @@ ZBUS_CHAN_ADD_OBS(POWER_CHAN, cloud, 0);
73
70
74
71
/* Define channels provided by this module */
75
72
76
- ZBUS_CHAN_DEFINE (PAYLOAD_CHAN ,
77
- struct cloud_payload ,
78
- NULL ,
79
- NULL ,
80
- ZBUS_OBSERVERS_EMPTY ,
81
- ZBUS_MSG_INIT (0 )
82
- );
83
-
84
73
ZBUS_CHAN_DEFINE (CLOUD_CHAN ,
85
74
struct cloud_msg ,
86
75
NULL ,
@@ -467,13 +456,19 @@ static void state_connected_exit(void *o)
467
456
static void shadow_get (bool delta_only )
468
457
{
469
458
int err ;
470
- uint8_t recv_buf [CONFIG_APP_MODULE_RECV_BUFFER_SIZE ] = { 0 };
471
- size_t recv_buf_len = sizeof (recv_buf );
459
+ struct cloud_msg msg = {
460
+ .type = CLOUD_SHADOW_RESPONSE ,
461
+ .response = {
462
+ .buffer_data_len = sizeof (msg .response .buffer ),
463
+ },
464
+ };
472
465
473
466
LOG_DBG ("Requesting device shadow from the device" );
474
467
475
- err = nrf_cloud_coap_shadow_get (recv_buf , & recv_buf_len , delta_only ,
476
- COAP_CONTENT_FORMAT_APP_JSON );
468
+ err = nrf_cloud_coap_shadow_get (msg .response .buffer ,
469
+ & msg .response .buffer_data_len ,
470
+ delta_only ,
471
+ COAP_CONTENT_FORMAT_APP_CBOR );
477
472
if (err == - EACCES ) {
478
473
LOG_WRN ("Not connected, error: %d" , err );
479
474
return ;
@@ -486,12 +481,46 @@ static void shadow_get(bool delta_only)
486
481
} else if (err > 0 ) {
487
482
LOG_WRN ("Cloud error: %d" , err );
488
483
return ;
484
+ } else if (err == - E2BIG ) {
485
+ LOG_WRN ("The provided buffer is not large enough, error: %d" , err );
486
+ return ;
489
487
} else if (err ) {
490
488
LOG_ERR ("Failed to request shadow delta: %d" , err );
491
489
return ;
492
490
}
493
491
494
- /* No further processing of shadow is implemented */
492
+ if (msg .response .buffer_data_len == 0 ) {
493
+ LOG_DBG ("No shadow delta changes available" );
494
+ return ;
495
+ }
496
+
497
+ /* Workaroud: Sometimes nrf_cloud_coap_shadow_get() returns 0 even though obtaining
498
+ * the shadow failed. Ignore the payload if the first 10 bytes are zero.
499
+ */
500
+ if (!memcmp (msg .response .buffer , "\0\0\0\0\0\0\0\0\0\0" , 10 )) {
501
+ LOG_WRN ("Returned buffeør is empty, ignore" );
502
+ return ;
503
+ }
504
+
505
+ err = zbus_chan_pub (& CLOUD_CHAN , & msg , K_SECONDS (1 ));
506
+ if (err ) {
507
+ LOG_ERR ("zbus_chan_pub, error: %d" , err );
508
+ SEND_FATAL_ERROR ();
509
+ return ;
510
+ }
511
+
512
+ /* Clear the shadow delta by reporting the same data back to the shadow reported state */
513
+ err = nrf_cloud_coap_patch ("state/reported" , NULL ,
514
+ msg .response .buffer ,
515
+ msg .response .buffer_data_len ,
516
+ COAP_CONTENT_FORMAT_APP_CBOR ,
517
+ true,
518
+ NULL ,
519
+ NULL );
520
+ if (err ) {
521
+ LOG_ERR ("Failed to patch the device shadow, error: %d" , err );
522
+ return ;
523
+ }
495
524
}
496
525
497
526
static void state_connected_ready_entry (void * o )
@@ -640,17 +669,20 @@ static void state_connected_ready_run(void *o)
640
669
}
641
670
#endif /* CONFIG_APP_ENVIRONMENTAL */
642
671
643
- if (state_object -> chan == & PAYLOAD_CHAN ) {
644
- const struct cloud_payload * payload = MSG_TO_PAYLOAD (state_object -> msg_buf );
672
+ if (state_object -> chan == & CLOUD_CHAN ) {
673
+ const struct cloud_msg msg = MSG_TO_CLOUD_MSG (state_object -> msg_buf );
645
674
646
- err = nrf_cloud_coap_json_message_send (payload -> buffer , false, confirmable );
647
- if (err == - ENETUNREACH ) {
648
- LOG_WRN ("Network is unreachable, error: %d" , err );
649
- return ;
650
- } else if (err ) {
651
- LOG_ERR ("nrf_cloud_coap_sensor_send, error: %d" , err );
652
- SEND_FATAL_ERROR ();
653
- return ;
675
+ if (msg .type == CLOUD_PAYLOAD_JSON ) {
676
+ err = nrf_cloud_coap_json_message_send (msg .payload .buffer ,
677
+ false, confirmable );
678
+ if (err == - ENETUNREACH ) {
679
+ LOG_WRN ("Network is unreachable, error: %d" , err );
680
+ return ;
681
+ } else if (err ) {
682
+ LOG_ERR ("nrf_cloud_coap_sensor_send, error: %d" , err );
683
+ SEND_FATAL_ERROR ();
684
+ return ;
685
+ }
654
686
}
655
687
}
656
688
0 commit comments