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>
14
15
#include <app_version.h>
15
16
16
- #if defined(CONFIG_MEMFAULT )
17
- #include <memfault/core/trace_event.h>
18
- #endif /* CONFIG_MEMFAULT */
19
-
20
17
#include "cloud_module.h"
21
18
#include "app_common.h"
22
19
#include "network.h"
@@ -47,7 +44,7 @@ LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
47
44
#define ENV_MSG_SIZE 0
48
45
#endif /* CONFIG_APP_ENVIRONMENTAL) */
49
46
50
- #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_payload), \
47
+ #define MAX_MSG_SIZE (MAX(sizeof(struct cloud_msg), \
51
48
MAX(sizeof(struct network_msg), \
52
49
MAX(BAT_MSG_SIZE, ENV_MSG_SIZE))))
53
50
@@ -467,13 +464,19 @@ static void state_connected_exit(void *o)
467
464
static void shadow_get (bool delta_only )
468
465
{
469
466
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
+ };
472
473
473
474
LOG_DBG ("Requesting device shadow from the device" );
474
475
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 );
477
480
if (err == - EACCES ) {
478
481
LOG_WRN ("Not connected, error: %d" , err );
479
482
return ;
@@ -486,12 +489,46 @@ static void shadow_get(bool delta_only)
486
489
} else if (err > 0 ) {
487
490
LOG_WRN ("Cloud error: %d" , err );
488
491
return ;
492
+ } else if (err == - E2BIG ) {
493
+ LOG_WRN ("The provided buffer is not large enough, error: %d" , err );
494
+ return ;
489
495
} else if (err ) {
490
496
LOG_ERR ("Failed to request shadow delta: %d" , err );
491
497
return ;
492
498
}
493
499
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
+ }
495
532
}
496
533
497
534
static void state_connected_ready_entry (void * o )
@@ -641,9 +678,9 @@ static void state_connected_ready_run(void *o)
641
678
#endif /* CONFIG_APP_ENVIRONMENTAL */
642
679
643
680
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 );
645
682
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 );
647
684
if (err == - ENETUNREACH ) {
648
685
LOG_WRN ("Network is unreachable, error: %d" , err );
649
686
return ;
0 commit comments