1212#include "power.h"
1313#include "network.h"
1414#include "checks.h"
15+ #include "fota.h"
1516
17+ ZBUS_MSG_SUBSCRIBER_DEFINE (fota_subscriber );
1618ZBUS_MSG_SUBSCRIBER_DEFINE (location_subscriber );
1719ZBUS_MSG_SUBSCRIBER_DEFINE (network_subscriber );
1820ZBUS_MSG_SUBSCRIBER_DEFINE (power_subscriber );
21+ ZBUS_CHAN_ADD_OBS (FOTA_CHAN , fota_subscriber , 0 );
1922ZBUS_CHAN_ADD_OBS (LOCATION_CHAN , location_subscriber , 0 );
2023ZBUS_CHAN_ADD_OBS (NETWORK_CHAN , network_subscriber , 0 );
2124ZBUS_CHAN_ADD_OBS (POWER_CHAN , power_subscriber , 0 );
2225
2326LOG_MODULE_REGISTER (main_module_checks , 4 );
2427
25- void check_location_event (enum location_msg_type expected_location_type )
28+ void expect_location_event (enum location_msg_type expected_location_type )
2629{
2730 int err ;
2831 const struct zbus_channel * chan ;
@@ -51,7 +54,7 @@ void check_location_event(enum location_msg_type expected_location_type)
5154 TEST_ASSERT_EQUAL (expected_location_type , location_msg_type );
5255}
5356
54- void check_network_event (enum network_msg_type expected_network_type )
57+ void expect_network_event (enum network_msg_type expected_network_type )
5558{
5659 int err ;
5760 const struct zbus_channel * chan ;
@@ -80,7 +83,7 @@ void check_network_event(enum network_msg_type expected_network_type)
8083 TEST_ASSERT_EQUAL (expected_network_type , network_msg .type );
8184}
8285
83- void check_power_event (enum power_msg_type expected_power_type )
86+ void expect_power_event (enum power_msg_type expected_power_type )
8487{
8588 int err ;
8689 const struct zbus_channel * chan ;
@@ -109,8 +112,36 @@ void check_power_event(enum power_msg_type expected_power_type)
109112 TEST_ASSERT_EQUAL (expected_power_type , power_msg .type );
110113}
111114
115+ void expect_fota_event (enum fota_msg_type expected_fota_type )
116+ {
117+ int err ;
118+ const struct zbus_channel * chan ;
119+ enum fota_msg_type fota_msg_type ;
120+
121+ /* Allow the test thread to sleep so that the DUT's thread is allowed to run. */
122+ k_sleep (K_MSEC (100 ));
123+
124+ err = zbus_sub_wait_msg (& fota_subscriber , & chan , & fota_msg_type , K_MSEC (10000 ));
125+ if (err == - ENOMSG ) {
126+ LOG_ERR ("No FOTA event received" );
127+ TEST_FAIL ();
128+ } else if (err ) {
129+ LOG_ERR ("zbus_sub_wait, error: %d" , err );
130+ TEST_FAIL ();
112131
113- static void check_no_location_events (void )
132+ return ;
133+ }
134+
135+ if (chan != & FOTA_CHAN ) {
136+ LOG_ERR ("Received message from wrong channel, expected %s, got %s" ,
137+ zbus_chan_name (& FOTA_CHAN ), zbus_chan_name (chan ));
138+ TEST_FAIL ();
139+ }
140+
141+ TEST_ASSERT_EQUAL (expected_fota_type , fota_msg_type );
142+ }
143+
144+ static void expect_no_location_events (void )
114145{
115146 int err ;
116147 const struct zbus_channel * chan ;
@@ -130,7 +161,7 @@ static void check_no_location_events(void)
130161 TEST_FAIL ();
131162}
132163
133- static void check_no_network_events (void )
164+ static void expect_no_network_events (void )
134165{
135166 int err ;
136167 const struct zbus_channel * chan ;
@@ -150,7 +181,7 @@ static void check_no_network_events(void)
150181 TEST_FAIL ();
151182}
152183
153- static void check_no_power_events (void )
184+ static void expect_no_power_events (void )
154185{
155186 int err ;
156187 const struct zbus_channel * chan ;
@@ -170,13 +201,34 @@ static void check_no_power_events(void)
170201 TEST_FAIL ();
171202}
172203
173- void check_no_events (uint32_t timeout_sec )
204+ static void expect_no_fota_events (void )
205+ {
206+ int err ;
207+ const struct zbus_channel * chan ;
208+ enum fota_msg_type fota_msg_type ;
209+
210+ err = zbus_sub_wait_msg (& fota_subscriber , & chan , & fota_msg_type , K_MSEC (10000 ));
211+ if (err == - ENOMSG ) {
212+ return ;
213+ } else if (err ) {
214+ LOG_ERR ("zbus_sub_wait, error: %d" , err );
215+ TEST_FAIL ();
216+
217+ return ;
218+ }
219+
220+ LOG_ERR ("Received unexpected FOTA event: %d" , fota_msg_type );
221+ TEST_FAIL ();
222+ }
223+
224+ void expect_no_events (uint32_t timeout_sec )
174225{
175226 k_sleep (K_SECONDS (timeout_sec ));
176227
177- check_no_location_events ();
178- check_no_network_events ();
179- check_no_power_events ();
228+ expect_no_location_events ();
229+ expect_no_network_events ();
230+ expect_no_power_events ();
231+ expect_no_fota_events ();
180232}
181233
182234void purge_location_events (void )
@@ -237,11 +289,33 @@ void purge_power_events(void)
237289 }
238290}
239291
292+ void purge_fota_events (void )
293+ {
294+ while (true) {
295+ int err ;
296+ const struct zbus_channel * chan ;
297+ enum fota_msg_type fota_msg_type ;
298+
299+ err = zbus_sub_wait_msg (& fota_subscriber , & chan , & fota_msg_type , K_MSEC (100 ));
300+ if (err == - ENOMSG ) {
301+ break ;
302+ } else if (err ) {
303+ LOG_ERR ("zbus_sub_wait, error: %d" , err );
304+ TEST_FAIL ();
305+
306+ return ;
307+ }
308+ }
309+ }
310+
240311void purge_all_events (void )
241312{
313+ k_sleep (K_SECONDS (1 ));
314+
242315 purge_location_events ();
243316 purge_network_events ();
244317 purge_power_events ();
318+ purge_fota_events ();
245319}
246320
247321int wait_for_location_event (enum location_msg_type expected_type , uint32_t timeout_sec )
0 commit comments