@@ -20,17 +20,27 @@ LOG_MODULE_REGISTER(wifi_test, LOG_LEVEL_INF);
2020#include <net_private.h>
2121
2222K_SEM_DEFINE (wifi_event , 0 , 1 );
23+ K_SEM_DEFINE (icmp_event_sem , 0 , 1 );
2324
2425#define WIFI_MGMT_EVENTS \
2526 (NET_EVENT_WIFI_SCAN_DONE | NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_CONNECT_RESULT | \
2627 NET_EVENT_WIFI_DISCONNECT_RESULT)
2728
2829#define TEST_DATA "ICMP dummy data"
2930
31+ #define WIFI_PING_ERR_LEN 64
32+ #define SET_ERR (fmt , ...) \
33+ do { \
34+ if (err) { \
35+ snprintk(err, WIFI_PING_ERR_LEN, fmt, ##__VA_ARGS__); \
36+ } \
37+ } while (0)
38+
3039static struct wifi_context {
3140 struct net_if * iface ;
3241 uint32_t scan_result ;
3342 bool connecting ;
43+ bool connected ;
3444 int result ;
3545 struct net_mgmt_event_callback wifi_mgmt_cb ;
3646} wifi_ctx ;
@@ -71,6 +81,7 @@ static void wifi_connect_result(struct net_mgmt_event_callback *cb)
7181 if (wifi_ctx .result ) {
7282 LOG_INF ("Connection request failed (%d)" , wifi_ctx .result );
7383 } else {
84+ wifi_ctx .connected = true;
7485 LOG_INF ("Connected" );
7586 }
7687}
@@ -90,6 +101,7 @@ static void wifi_disconnect_result(struct net_mgmt_event_callback *cb)
90101 if (wifi_ctx .result ) {
91102 LOG_INF ("Disconnect failed (%d)" , wifi_ctx .result );
92103 } else {
104+ wifi_ctx .connected = false;
93105 LOG_INF ("Disconnected" );
94106 }
95107 } else {
@@ -151,7 +163,7 @@ static enum net_verdict icmp_event(struct net_icmp_ctx *ctx, struct net_pkt *pkt
151163 wifi_ctx .result = strcmp (buf , TEST_DATA );
152164
153165sem_give :
154- k_sem_give (& wifi_event );
166+ k_sem_give (& icmp_event_sem );
155167
156168 return wifi_ctx .result == 0 ? NET_OK : NET_DROP ;
157169}
@@ -165,7 +177,7 @@ static int wifi_scan(void)
165177 return ret ;
166178 }
167179
168- LOG_INF ("Wifi scan requested..." );
180+ LOG_INF ("Wi-Fi scan requested..." );
169181
170182 return 0 ;
171183}
@@ -232,6 +244,92 @@ static int wifi_state(void)
232244 return status .state ;
233245}
234246
247+ static int wifi_ping_gw (char * err )
248+ {
249+ struct net_icmp_ping_params params ;
250+ struct net_icmp_ctx icmp_ctx ;
251+ struct net_in_addr gw_addr_4 ;
252+ struct net_sockaddr_in dst4 = {0 };
253+ int retry = CONFIG_WIFI_PING_ATTEMPTS ;
254+ int ret ;
255+ int status = 0 ;
256+
257+ gw_addr_4 = net_if_ipv4_get_gw (wifi_ctx .iface );
258+
259+ if (gw_addr_4 .s_addr == 0 ) {
260+ SET_ERR ("Gateway address is not set" );
261+ status = - ENOENT ;
262+ goto exit ;
263+ }
264+
265+ ret = net_icmp_init_ctx (& icmp_ctx , NET_AF_INET , NET_ICMPV4_ECHO_REPLY , 0 , icmp_event );
266+
267+ if (ret ) {
268+ SET_ERR ("Cannot init ICMP (%d)" , ret );
269+ status = ret ;
270+ goto exit ;
271+ }
272+
273+ dst4 .sin_family = NET_AF_INET ;
274+ memcpy (& dst4 .sin_addr , & gw_addr_4 , sizeof (gw_addr_4 ));
275+
276+ params .identifier = 1234 ;
277+ params .sequence = 5678 ;
278+ params .tc_tos = 1 ;
279+ params .priority = 2 ;
280+ params .data = TEST_DATA ;
281+ params .data_size = sizeof (TEST_DATA );
282+
283+ k_sem_reset (& icmp_event_sem );
284+ wifi_ctx .result = - ETIMEDOUT ;
285+
286+ LOG_INF ("Pinging the gateway..." );
287+
288+ do {
289+ if (!wifi_ctx .connected ) {
290+ status = - EIO ;
291+ SET_ERR ("Wi-Fi not connected or dropped" );
292+ goto cleanup ;
293+ }
294+
295+ ret = net_icmp_send_echo_request (& icmp_ctx , wifi_ctx .iface ,
296+ (struct net_sockaddr * )& dst4 , & params , NULL );
297+
298+ if (ret ) {
299+ SET_ERR ("Cannot send ICMP echo request (%d)" , ret );
300+ status = ret ;
301+ goto cleanup ;
302+ }
303+
304+ if (k_sem_take (& icmp_event_sem , K_SECONDS (CONFIG_WIFI_PING_TIMEOUT )) == 0 ) {
305+ break ;
306+ }
307+
308+ retry -- ;
309+
310+ if (wifi_ctx .connected ) {
311+ LOG_INF ("No reply, retry %d" , CONFIG_WIFI_PING_ATTEMPTS - retry );
312+ }
313+
314+ } while (retry );
315+
316+ if (retry <= 0 ) {
317+ SET_ERR ("Gateway ping (ICMP) timed out" );
318+ status = - ETIMEDOUT ;
319+ goto cleanup ;
320+ }
321+
322+ if (wifi_ctx .result != 0 ) {
323+ SET_ERR ("ICMP data error (%d)" , wifi_ctx .result );
324+ status = wifi_ctx .result ;
325+ }
326+
327+ cleanup :
328+ net_icmp_cleanup_ctx (& icmp_ctx );
329+ exit :
330+ return status ;
331+ }
332+
235333ZTEST (wifi , test_0_scan )
236334{
237335 int ret ;
@@ -240,7 +338,7 @@ ZTEST(wifi, test_0_scan)
240338 zassert_equal (ret , 0 , "Scan request failed" );
241339
242340 zassert_equal (k_sem_take (& wifi_event , K_SECONDS (CONFIG_WIFI_SCAN_TIMEOUT )), 0 ,
243- "Wifi scan failed or timed out" );
341+ "Wi-Fi scan failed or timed out" );
244342
245343 LOG_INF ("Scan done" );
246344}
@@ -258,7 +356,7 @@ ZTEST(wifi, test_1_connect)
258356 zassert_equal (ret , 0 , "Connect request failed" );
259357
260358 zassert_equal (k_sem_take (& wifi_event , K_SECONDS (CONFIG_WIFI_CONNECT_TIMEOUT )), 0 ,
261- "Wifi connect timed out" );
359+ "Wi-Fi connect timed out" );
262360
263361 if (wifi_ctx .result ) {
264362 zassert (-- retry , "Connect failed" );
@@ -281,61 +379,44 @@ ZTEST(wifi, test_1_connect)
281379
282380ZTEST (wifi , test_2_icmp )
283381{
284- struct net_icmp_ping_params params ;
285- struct net_icmp_ctx icmp_ctx ;
286- struct net_in_addr gw_addr_4 ;
287- struct net_sockaddr_in dst4 = {0 };
288- int retry = CONFIG_WIFI_PING_ATTEMPTS ;
382+ char err [WIFI_PING_ERR_LEN ] = "" ;
289383 int ret ;
290384
291- gw_addr_4 = net_if_ipv4_get_gw (wifi_ctx .iface );
292- zassert_not_equal (gw_addr_4 .s_addr , 0 , "Gateway address is not set" );
293-
294- ret = net_icmp_init_ctx (& icmp_ctx , NET_AF_INET , NET_ICMPV4_ECHO_REPLY , 0 , icmp_event );
295- zassert_equal (ret , 0 , "Cannot init ICMP (%d)" , ret );
385+ ret = wifi_ping_gw (err );
296386
297- dst4 .sin_family = NET_AF_INET ;
298- memcpy (& dst4 .sin_addr , & gw_addr_4 , sizeof (gw_addr_4 ));
299-
300- params .identifier = 1234 ;
301- params .sequence = 5678 ;
302- params .tc_tos = 1 ;
303- params .priority = 2 ;
304- params .data = TEST_DATA ;
305- params .data_size = sizeof (TEST_DATA );
306-
307- LOG_INF ("Pinging the gateway..." );
308-
309- do {
310- ret = net_icmp_send_echo_request (& icmp_ctx , wifi_ctx .iface ,
311- (struct net_sockaddr * )& dst4 , & params , NULL );
312- zassert_equal (ret , 0 , "Cannot send ICMP echo request (%d)" , ret );
387+ zassert_equal (ret , 0 , "%s" , err );
388+ }
313389
314- int timeout = k_sem_take (& wifi_event , K_SECONDS (CONFIG_WIFI_PING_TIMEOUT ));
390+ #if defined(CONFIG_PM )
391+ ZTEST (wifi , test_3_icmp_pm )
392+ {
393+ char err [WIFI_PING_ERR_LEN ] = "" ;
394+ int ret ;
315395
316- if (timeout ) {
317- zassert (-- retry , "Gateway ping (ICMP) timed out on all attempts" );
318- LOG_INF ("No reply, retry %d" , CONFIG_WIFI_PING_ATTEMPTS - retry );
319- } else {
320- break ;
321- }
322- } while (retry );
396+ LOG_INF ("Enter sleep then ping gateway again..." );
397+ k_sleep (K_SECONDS (2 ));
323398
324- /* check result */
325- zassert_equal (wifi_ctx .result , 0 , "ICMP data error" );
399+ ret = wifi_ping_gw (err );
326400
327- net_icmp_cleanup_ctx ( & icmp_ctx );
401+ zassert_equal ( ret , 0 , "%s" , err );
328402}
403+ #endif
329404
405+ #if defined(CONFIG_PM )
406+ ZTEST (wifi , test_4_disconnect )
407+ #else
330408ZTEST (wifi , test_3_disconnect )
409+ #endif
331410{
332411 int ret ;
333412
413+ zassert (wifi_ctx .connected , "Wi-Fi already disconnected" );
414+
334415 ret = wifi_disconnect ();
335416 zassert_equal (ret , 0 , "Disconnect request failed" );
336417
337418 zassert_equal (k_sem_take (& wifi_event , K_SECONDS (CONFIG_WIFI_DISCONNECT_TIMEOUT )), 0 ,
338- "Wifi disconnect timed out" );
419+ "Wi-Fi disconnect timed out" );
339420
340421 zassert_equal (wifi_ctx .result , 0 , "Disconnect failed" );
341422}
0 commit comments