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>
1415#include <app_version.h>
1516
16- #if defined(CONFIG_MEMFAULT )
17- #include <memfault/core/trace_event.h>
18- #endif /* CONFIG_MEMFAULT */
19-
2017#include "cloud_module.h"
2118#include "app_common.h"
2219#include "network.h"
@@ -47,7 +44,7 @@ LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
4744#define ENV_MSG_SIZE 0
4845#endif /* CONFIG_APP_ENVIRONMENTAL) */
4946
50- #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_payload), \
47+ #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_msg), \
5148 MAX(sizeof(struct network_msg), \
5249 MAX(BAT_MSG_SIZE, ENV_MSG_SIZE))))
5350
@@ -467,13 +464,19 @@ static void state_connected_exit(void *o)
467464static void shadow_get (bool delta_only )
468465{
469466 int err ;
470- uint8_t recv_buf [CONFIG_APP_MODULE_RECV_BUFFER_SIZE ] = { 0 };
471- size_t recv_buf_len = sizeof (recv_buf );
467+ struct cloud_msg msg = {
468+ .type = CLOUD_SHADOW_RESPONSE ,
469+ .response = {
470+ .buffer_data_len = sizeof (msg .response .buffer ),
471+ },
472+ };
472473
473474 LOG_DBG ("Requesting device shadow from the device" );
474475
475- err = nrf_cloud_coap_shadow_get (recv_buf , & recv_buf_len , delta_only ,
476- COAP_CONTENT_FORMAT_APP_JSON );
476+ err = nrf_cloud_coap_shadow_get (msg .response .buffer ,
477+ & msg .response .buffer_data_len ,
478+ delta_only ,
479+ COAP_CONTENT_FORMAT_APP_CBOR );
477480 if (err == - EACCES ) {
478481 LOG_WRN ("Not connected, error: %d" , err );
479482 return ;
@@ -486,12 +489,46 @@ static void shadow_get(bool delta_only)
486489 } else if (err > 0 ) {
487490 LOG_WRN ("Cloud error: %d" , err );
488491 return ;
492+ } else if (err == - E2BIG ) {
493+ LOG_WRN ("The provided buffer is not large enough, error: %d" , err );
494+ return ;
489495 } else if (err ) {
490496 LOG_ERR ("Failed to request shadow delta: %d" , err );
491497 return ;
492498 }
493499
494- /* No further processing of shadow is implemented */
500+ if (msg .response .buffer_data_len == 0 ) {
501+ LOG_DBG ("No shadow delta changes available" );
502+ return ;
503+ }
504+
505+ /* Workaroud: Sometimes nrf_cloud_coap_shadow_get() returns 0 even though obtaining
506+ * the shadow failed. Ignore the payload if the first 10 bytes are zero.
507+ */
508+ if (!memcmp (msg .response .buffer , "\0\0\0\0\0\0\0\0\0\0" , 10 )) {
509+ LOG_WRN ("Returned buffeør is empty, ignore" );
510+ return ;
511+ }
512+
513+ err = zbus_chan_pub (& CLOUD_CHAN , & msg , K_SECONDS (1 ));
514+ if (err ) {
515+ LOG_ERR ("zbus_chan_pub, error: %d" , err );
516+ SEND_FATAL_ERROR ();
517+ return ;
518+ }
519+
520+ /* Clear the shadow delta by reporting the same data back to the shadow reported state */
521+ err = nrf_cloud_coap_patch ("state/reported" , NULL ,
522+ msg .response .buffer ,
523+ msg .response .buffer_data_len ,
524+ COAP_CONTENT_FORMAT_APP_CBOR ,
525+ true,
526+ NULL ,
527+ NULL );
528+ if (err ) {
529+ LOG_ERR ("Failed to patch the device shadow, error: %d" , err );
530+ return ;
531+ }
495532}
496533
497534static void state_connected_ready_entry (void * o )
@@ -641,9 +678,9 @@ static void state_connected_ready_run(void *o)
641678#endif /* CONFIG_APP_ENVIRONMENTAL */
642679
643680 if (state_object -> chan == & PAYLOAD_CHAN ) {
644- const struct cloud_payload * payload = MSG_TO_PAYLOAD (state_object -> msg_buf );
681+ const struct cloud_msg msg = MSG_TO_CLOUD_MSG (state_object -> msg_buf );
645682
646- err = nrf_cloud_coap_json_message_send (payload -> buffer , false, confirmable );
683+ err = nrf_cloud_coap_json_message_send (msg . payload . buffer , false, confirmable );
647684 if (err == - ENETUNREACH ) {
648685 LOG_WRN ("Network is unreachable, error: %d" , err );
649686 return ;
0 commit comments