1111#include <zephyr/smf.h>
1212
1313#include "app_common.h"
14- #include "modules/modem_at/modem_at.h"
1514#include "modules/network/network.h"
16- #include "modules/cloud/cloud.h"
1715#if defined(CONFIG_APP_LOCATION )
1816#include "modules/location/location.h"
1917#endif
@@ -58,29 +56,57 @@ struct app_object {
5856 uint8_t msg_buf [MAX_MSG_SIZE ];
5957};
6058
61- static struct app_object app ;
62- static const struct smf_state states [];
59+ static void sync_timer_fn (struct k_work * work );
60+ static K_WORK_DELAYABLE_DEFINE (sync_timer , sync_timer_fn ) ;
61+
62+ static void request_module_updates (void );
63+ static enum smf_state_result disconnected_run (void * obj );
64+ static void connected_entry (void * obj );
65+ static enum smf_state_result connected_run (void * obj );
66+ static void connected_exit (void * obj );
67+ static void wdt_callback (int channel_id , void * user_data );
68+
69+ static const struct smf_state states [] = {
70+ [STATE_DISCONNECTED ] = SMF_CREATE_STATE (NULL , disconnected_run , NULL , NULL , NULL ),
71+ [STATE_CONNECTED ] = SMF_CREATE_STATE (connected_entry , connected_run , connected_exit ,
72+ NULL , NULL ),
73+ };
6374
64- /* Periodic sync trigger: publishes MAIN_SYNC; the state machine drives it. */
6575static void sync_timer_fn (struct k_work * work )
6676{
6777 ARG_UNUSED (work );
78+
6879 struct main_msg msg = { .type = MAIN_SYNC };
80+ int err ;
6981
70- (void )zbus_chan_pub (& main_chan , & msg , PUB_TIMEOUT );
82+ err = zbus_chan_pub (& main_chan , & msg , PUB_TIMEOUT );
83+ if (err ) {
84+ LOG_ERR ("zbus_chan_pub main_chan, error: %d" , err );
85+ }
7186}
7287
73- static K_WORK_DELAYABLE_DEFINE (sync_timer , sync_timer_fn ) ;
74-
75- static void run_sync (void )
88+ static void request_module_updates (void )
7689{
77- LOG_INF ("Sync" );
90+ int err ;
91+
92+ LOG_DBG ("Requesting module updates" );
7893#if defined(CONFIG_APP_LOCATION )
79- location_update ();
94+ struct location_msg loc_msg = { .type = LOCATION_FIX_REQUEST };
95+
96+ err = zbus_chan_pub (& location_chan , & loc_msg , PUB_TIMEOUT );
97+ if (err ) {
98+ LOG_ERR ("zbus_chan_pub location_chan, error: %d" , err );
99+ }
80100#endif
81101#if defined(CONFIG_APP_BATTERY )
82- (void )battery_report ();
102+ struct battery_msg bat_msg = { .type = BATTERY_SAMPLE };
103+
104+ err = zbus_chan_pub (& battery_chan , & bat_msg , PUB_TIMEOUT );
105+ if (err ) {
106+ LOG_ERR ("zbus_chan_pub battery_chan, error: %d" , err );
107+ }
83108#endif
109+ ARG_UNUSED (err );
84110}
85111
86112static enum smf_state_result disconnected_run (void * obj )
@@ -101,10 +127,14 @@ static enum smf_state_result disconnected_run(void *obj)
101127static void connected_entry (void * obj )
102128{
103129 ARG_UNUSED (obj );
130+ int err ;
131+
104132 LOG_INF ("Connected" );
105133
106- /* First sync shortly after connecting. */
107- (void )k_work_reschedule (& sync_timer , K_SECONDS (CONFIG_APP_SYNC_BOOT_DELAY_SECONDS ));
134+ err = k_work_reschedule (& sync_timer , K_SECONDS (CONFIG_APP_SYNC_BOOT_DELAY_SECONDS ));
135+ if (err < 0 ) {
136+ LOG_ERR ("k_work_reschedule sync_timer, error: %d" , err );
137+ }
108138}
109139
110140static enum smf_state_result connected_run (void * obj )
@@ -121,8 +151,13 @@ static enum smf_state_result connected_run(void *obj)
121151 const struct main_msg * msg = (const struct main_msg * )state -> msg_buf ;
122152
123153 if (msg -> type == MAIN_SYNC ) {
124- run_sync ();
125- (void )k_work_reschedule (& sync_timer , K_SECONDS (CONFIG_APP_SYNC_INTERVAL ));
154+ int err ;
155+
156+ request_module_updates ();
157+ err = k_work_reschedule (& sync_timer , K_SECONDS (CONFIG_APP_SYNC_INTERVAL ));
158+ if (err < 0 ) {
159+ LOG_ERR ("k_work_reschedule sync_timer, error: %d" , err );
160+ }
126161 }
127162 }
128163
@@ -135,12 +170,6 @@ static void connected_exit(void *obj)
135170 (void )k_work_cancel_delayable (& sync_timer );
136171}
137172
138- static const struct smf_state states [] = {
139- [STATE_DISCONNECTED ] = SMF_CREATE_STATE (NULL , disconnected_run , NULL , NULL , NULL ),
140- [STATE_CONNECTED ] = SMF_CREATE_STATE (connected_entry , connected_run , connected_exit ,
141- NULL , NULL ),
142- };
143-
144173static void wdt_callback (int channel_id , void * user_data )
145174{
146175 LOG_ERR ("Main watchdog expired, channel: %d, thread: %s" ,
@@ -158,6 +187,7 @@ int main(void)
158187 const uint32_t execution_time_ms =
159188 (CONFIG_APP_MAIN_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC );
160189 const k_timeout_t zbus_wait = K_MSEC (wdt_timeout_ms - execution_time_ms );
190+ static struct app_object app ;
161191
162192 task_wdt_id = task_wdt_add (wdt_timeout_ms , wdt_callback , (void * )k_current_get ());
163193 if (task_wdt_id < 0 ) {
@@ -166,12 +196,6 @@ int main(void)
166196 return - EFAULT ;
167197 }
168198
169- if (modem_at_setup () < 0 ) {
170- SEND_FATAL_ERROR ();
171- return - EFAULT ;
172- }
173- (void )cloud_provision ();
174-
175199 smf_set_initial (SMF_CTX (& app ), & states [STATE_DISCONNECTED ]);
176200
177201 while (true) {
0 commit comments