66 * Change Logs:
77 * Date Author Notes
88 * 2019-03-18 ChenYong First version
9+ * 2025-01-04 Evlers add statistics and more inupt parameters to ping command
910 */
1011
1112#include <stdio.h>
@@ -1286,7 +1287,7 @@ int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_
12861287
12871288 struct netdev * netdev = RT_NULL ;
12881289 struct netdev_ping_resp ping_resp ;
1289- rt_uint32_t index ;
1290+ rt_uint32_t index , received , loss , max_time , min_time , avg_time ;
12901291 int ret = 0 ;
12911292 rt_bool_t isbind = RT_FALSE ;
12921293
@@ -1326,6 +1327,8 @@ int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_
13261327 }
13271328 }
13281329
1330+ max_time = avg_time = received = 0 ;
1331+ min_time = 0xFFFFFFFF ;
13291332 for (index = 0 ; index < times ; index ++ )
13301333 {
13311334 int delay_tick = 0 ;
@@ -1337,7 +1340,7 @@ int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_
13371340 if (ret == - RT_ETIMEOUT )
13381341 {
13391342 rt_kprintf ("ping: from %s icmp_seq=%d timeout\n" ,
1340- (ip_addr_isany (& (ping_resp .ip_addr ))) ? target_name : inet_ntoa (ping_resp .ip_addr ), index );
1343+ (ip_addr_isany (& (ping_resp .ip_addr ))) ? target_name : inet_ntoa (ping_resp .ip_addr ), index + 1 );
13411344 }
13421345 else if (ret == - RT_ERROR )
13431346 {
@@ -1350,28 +1353,61 @@ int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_
13501353 if (ping_resp .ttl == 0 )
13511354 {
13521355 rt_kprintf ("%d bytes from %s icmp_seq=%d time=%d ms\n" ,
1353- ping_resp .data_len , inet_ntoa (ping_resp .ip_addr ), index , ping_resp .ticks );
1356+ ping_resp .data_len , inet_ntoa (ping_resp .ip_addr ), index + 1 , ping_resp .ticks );
13541357 }
13551358 else
13561359 {
13571360 rt_kprintf ("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n" ,
1358- ping_resp .data_len , inet_ntoa (ping_resp .ip_addr ), index , ping_resp .ttl , ping_resp .ticks );
1361+ ping_resp .data_len , inet_ntoa (ping_resp .ip_addr ), index + 1 , ping_resp .ttl , ping_resp .ticks );
13591362 }
1363+ received += 1 ;
1364+ if (ping_resp .ticks > max_time )
1365+ {
1366+ max_time = ping_resp .ticks ;
1367+ }
1368+ else if (ping_resp .ticks < min_time )
1369+ {
1370+ min_time = ping_resp .ticks ;
1371+ }
1372+ avg_time += ping_resp .ticks ;
13601373 }
13611374
13621375 /* if the response time is more than NETDEV_PING_DELAY, no need to delay */
13631376 delay_tick = ((rt_tick_get () - start_tick ) > NETDEV_PING_DELAY ) || (index == times ) ? 0 : NETDEV_PING_DELAY ;
13641377 rt_thread_delay (delay_tick );
13651378 }
13661379
1380+ /* print ping statistics */
1381+ loss = (uint32_t )((1 - ((float )received ) / index ) * 100 );
1382+ avg_time = (uint32_t )(avg_time / received );
1383+ #if NETDEV_IPV4 && NETDEV_IPV6
1384+ if (IP_IS_V4_VAL (& ping_resp .ip_addr ))
1385+ {
1386+ rt_kprintf ("\n--- %s ping statistics ---\n" , inet_ntoa (* ip_2_ip4 (& ping_resp .ip_addr )));
1387+ }
1388+ else
1389+ {
1390+ rt_kprintf ("\n--- %s ping statistics ---\n" , inet6_ntoa (* ip_2_ip6 (& ping_resp .ip_addr )));
1391+ }
1392+ #elif NETDEV_IPV4
1393+ rt_kprintf ("\n--- %s ping statistics ---\n" , inet_ntoa (ping_resp .ip_addr ));
1394+ #elif NETDEV_IPV6
1395+ rt_kprintf ("\n--- %s ping statistics ---\n" , inet6_ntoa (ping_resp .ip_addr ));
1396+ #endif
1397+ rt_kprintf ("%d packets transmitted, %d received, %d%% packet loss\n" , index , received , loss );
1398+ if (received > 0 )
1399+ {
1400+ rt_kprintf ("minimum = %dms, maximum = %dms, average = %dms\n" , min_time , max_time , avg_time );
1401+ }
1402+
13671403 return RT_EOK ;
13681404}
13691405
13701406int netdev_ping (int argc , char * * argv )
13711407{
13721408 if (argc == 1 )
13731409 {
1374- rt_kprintf ("Please input: ping <host address> [netdev name]\n" );
1410+ rt_kprintf ("Please input: ping <host address> [netdev name] [times] [data size] \n" );
13751411 }
13761412 else if (argc == 2 )
13771413 {
@@ -1381,6 +1417,14 @@ int netdev_ping(int argc, char **argv)
13811417 {
13821418 netdev_cmd_ping (argv [1 ], argv [2 ], 4 , 0 );
13831419 }
1420+ else if (argc == 4 )
1421+ {
1422+ netdev_cmd_ping (argv [1 ], argv [2 ], atoi (argv [3 ]), 0 );
1423+ }
1424+ else if (argc == 5 )
1425+ {
1426+ netdev_cmd_ping (argv [1 ], argv [2 ], atoi (argv [3 ]), atoi (argv [4 ]));
1427+ }
13841428
13851429 return 0 ;
13861430}
0 commit comments