7
7
#include <zephyr/kernel.h>
8
8
#include <zephyr/logging/log.h>
9
9
#include <zephyr/zbus/zbus.h>
10
- #include <zephyr/task_wdt/task_wdt.h>
11
10
#include <date_time.h>
12
- #include <net/nrf_cloud_coap.h>
13
- #include <nrf_cloud_coap_transport.h>
14
-
15
- #if defined(CONFIG_MEMFAULT )
16
- #include <memfault/core/trace_event.h>
17
- #endif /* CONFIG_MEMFAULT */
18
11
19
12
#include "message_channel.h"
20
13
21
14
/* Register log module */
22
15
LOG_MODULE_REGISTER (app , CONFIG_APP_LOG_LEVEL );
23
16
24
- /* Register subscriber */
25
- ZBUS_MSG_SUBSCRIBER_DEFINE (app );
26
-
27
- /* Observe channels */
28
- ZBUS_CHAN_ADD_OBS (TRIGGER_CHAN , app , 0 );
29
- ZBUS_CHAN_ADD_OBS (CLOUD_CHAN , app , 0 );
30
-
31
- #define MAX_MSG_SIZE (MAX(sizeof(enum trigger_type), sizeof(enum cloud_msg_type)))
32
-
33
- BUILD_ASSERT (CONFIG_APP_MODULE_WATCHDOG_TIMEOUT_SECONDS > CONFIG_APP_MODULE_EXEC_TIME_SECONDS_MAX ,
34
- "Watchdog timeout must be greater than maximum execution time" );
35
-
36
- static void shadow_get (bool delta_only )
37
- {
38
- int err ;
39
- uint8_t recv_buf [CONFIG_APP_MODULE_RECV_BUFFER_SIZE ] = { 0 };
40
- size_t recv_buf_len = sizeof (recv_buf );
41
-
42
- LOG_DBG ("Requesting device shadow from the device" );
43
-
44
- err = nrf_cloud_coap_shadow_get (recv_buf , & recv_buf_len , delta_only ,
45
- COAP_CONTENT_FORMAT_APP_JSON );
46
- if (err == - EACCES ) {
47
- LOG_WRN ("Not connected, error: %d" , err );
48
- return ;
49
- } else if (err == - ETIMEDOUT ) {
50
- LOG_WRN ("Request timed out, error: %d" , err );
51
- return ;
52
- } else if (err > 0 ) {
53
- LOG_WRN ("Cloud error: %d" , err );
54
-
55
- IF_ENABLED (CONFIG_MEMFAULT ,
56
- (MEMFAULT_TRACE_EVENT_WITH_STATUS (nrf_cloud_coap_shadow_get , err )));
57
-
58
- return ;
59
- } else if (err ) {
60
- LOG_ERR ("Failed to request shadow delta: %d" , err );
61
- return ;
62
- }
63
-
64
- /* No further processing of shadow is implemented */
65
- }
66
-
67
- static void task_wdt_callback (int channel_id , void * user_data )
68
- {
69
- LOG_ERR ("Watchdog expired, Channel: %d, Thread: %s" ,
70
- channel_id , k_thread_name_get ((k_tid_t )user_data ));
71
-
72
- SEND_FATAL_ERROR_WATCHDOG_TIMEOUT ();
73
- }
74
-
75
17
static void date_time_handler (const struct date_time_evt * evt ) {
76
18
if (evt -> type != DATE_TIME_NOT_OBTAINED ) {
77
19
int err ;
@@ -85,75 +27,12 @@ static void date_time_handler(const struct date_time_evt *evt) {
85
27
}
86
28
}
87
29
88
- static void app_task (void )
30
+ static int app_init (void )
89
31
{
90
- int err ;
91
- const struct zbus_channel * chan = NULL ;
92
- int task_wdt_id ;
93
- const uint32_t wdt_timeout_ms = (CONFIG_APP_MODULE_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC );
94
- const uint32_t execution_time_ms = (CONFIG_APP_MODULE_EXEC_TIME_SECONDS_MAX * MSEC_PER_SEC );
95
- const k_timeout_t zbus_wait_ms = K_MSEC (wdt_timeout_ms - execution_time_ms );
96
- uint8_t msg_buf [MAX_MSG_SIZE ];
97
-
98
- LOG_DBG ("Application module task started" );
99
-
100
- task_wdt_id = task_wdt_add (wdt_timeout_ms , task_wdt_callback , (void * )k_current_get ());
101
-
102
32
/* Setup handler for date_time library */
103
33
date_time_register_handler (date_time_handler );
104
34
105
- while (true) {
106
- err = task_wdt_feed (task_wdt_id );
107
- if (err ) {
108
- LOG_ERR ("task_wdt_feed, error: %d" , err );
109
- SEND_FATAL_ERROR ();
110
- return ;
111
- }
112
-
113
- err = zbus_sub_wait_msg (& app , & chan , & msg_buf , zbus_wait_ms );
114
- if (err == - ENOMSG ) {
115
- continue ;
116
- } else if (err ) {
117
- LOG_ERR ("zbus_sub_wait, error: %d" , err );
118
- SEND_FATAL_ERROR ();
119
- return ;
120
- }
121
-
122
- if (& CLOUD_CHAN == chan ) {
123
- LOG_DBG ("Cloud connection status received" );
124
-
125
- const enum cloud_msg_type * status = (const enum cloud_msg_type * )msg_buf ;
126
-
127
- if (* status == CLOUD_CONNECTED_READY_TO_SEND ) {
128
- LOG_DBG ("Cloud ready to send" );
129
-
130
- shadow_get (false);
131
- }
132
- }
133
-
134
- if (& TRIGGER_CHAN == chan ) {
135
- LOG_DBG ("Trigger received" );
136
-
137
- const enum trigger_type * type = (const enum trigger_type * )msg_buf ;
138
-
139
- if (* type == TRIGGER_POLL ) {
140
- LOG_DBG ("Poll trigger received" );
141
-
142
- shadow_get (true);
143
- }
144
- }
145
- }
146
- }
147
-
148
- K_THREAD_DEFINE (app_task_id ,
149
- CONFIG_APP_MODULE_THREAD_STACK_SIZE ,
150
- app_task , NULL , NULL , NULL , K_LOWEST_APPLICATION_THREAD_PRIO , 0 , 0 );
151
-
152
- static int watchdog_init (void )
153
- {
154
- __ASSERT ((task_wdt_init (NULL ) == 0 ), "Task watchdog init failure" );
155
-
156
35
return 0 ;
157
36
}
158
37
159
- SYS_INIT (watchdog_init , POST_KERNEL , CONFIG_APPLICATION_INIT_PRIORITY );
38
+ SYS_INIT (app_init , POST_KERNEL , CONFIG_APPLICATION_INIT_PRIORITY );
0 commit comments