1111#include <zephyr/task_wdt/task_wdt.h>
1212#include <net/nrf_cloud.h>
1313#include <net/nrf_cloud_coap.h>
14+ #include <nrf_cloud_coap_transport.h>
15+ #include <zephyr/net/coap.h>
1416#include <app_version.h>
1517
16- #if defined(CONFIG_MEMFAULT )
17- #include <memfault/core/trace_event.h>
18- #endif /* CONFIG_MEMFAULT */
19-
2018#include "cloud_module.h"
2119#include "app_common.h"
2220#include "network.h"
@@ -47,7 +45,7 @@ LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
4745#define ENV_MSG_SIZE 0
4846#endif /* CONFIG_APP_ENVIRONMENTAL) */
4947
50- #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_payload), \
48+ #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_msg), \
5149 MAX(sizeof(struct network_msg), \
5250 MAX(BAT_MSG_SIZE, ENV_MSG_SIZE))))
5351
@@ -59,7 +57,6 @@ BUILD_ASSERT(CONFIG_APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS >
5957ZBUS_MSG_SUBSCRIBER_DEFINE (cloud );
6058
6159/* Observe channels */
62- ZBUS_CHAN_ADD_OBS (PAYLOAD_CHAN , cloud , 0 );
6360ZBUS_CHAN_ADD_OBS (NETWORK_CHAN , cloud , 0 );
6461ZBUS_CHAN_ADD_OBS (CLOUD_CHAN , cloud , 0 );
6562
@@ -73,14 +70,6 @@ ZBUS_CHAN_ADD_OBS(POWER_CHAN, cloud, 0);
7370
7471/* Define channels provided by this module */
7572
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-
8473ZBUS_CHAN_DEFINE (CLOUD_CHAN ,
8574 struct cloud_msg ,
8675 NULL ,
@@ -467,13 +456,19 @@ static void state_connected_exit(void *o)
467456static void shadow_get (bool delta_only )
468457{
469458 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+ };
472465
473466 LOG_DBG ("Requesting device shadow from the device" );
474467
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 );
477472 if (err == - EACCES ) {
478473 LOG_WRN ("Not connected, error: %d" , err );
479474 return ;
@@ -486,12 +481,46 @@ static void shadow_get(bool delta_only)
486481 } else if (err > 0 ) {
487482 LOG_WRN ("Cloud error: %d" , err );
488483 return ;
484+ } else if (err == - E2BIG ) {
485+ LOG_WRN ("The provided buffer is not large enough, error: %d" , err );
486+ return ;
489487 } else if (err ) {
490488 LOG_ERR ("Failed to request shadow delta: %d" , err );
491489 return ;
492490 }
493491
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+ }
495524}
496525
497526static void state_connected_ready_entry (void * o )
@@ -640,17 +669,20 @@ static void state_connected_ready_run(void *o)
640669 }
641670#endif /* CONFIG_APP_ENVIRONMENTAL */
642671
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 );
645674
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+ }
654686 }
655687 }
656688
0 commit comments