summaryrefslogtreecommitdiff
path: root/components/net/netdev/src/netdev.c
diff options
context:
space:
mode:
authorymj0440 <[email protected]>2026-10-12 20:46:51 +0800
committerRbb666 <[email protected]>2026-10-15 09:10:50 +0800
commit20881ec5710f0aa78cc6d483c5c6d0f09d6105b8 (patch)
tree0d2ae91e8c3900ac19ea574752098baf161478a3 /components/net/netdev/src/netdev.c
parent8acbb5df8ec50d9c76f7cb407cc7970e7eecdb28 (diff)
fix(netdev): correct ping delay logic for last packet and avoid rt_thread_delay(0)
The 'index == times' condition in the delay_tick calculation is never true, so the delay was still applied after the last ping, contrary to the comment. Skip the delay on the final ping by checking 'index != times - 1'. Also only call rt_thread_delay() when delay_tick > 0, since passing 0 returns -RT_EINVAL.
Diffstat (limited to 'components/net/netdev/src/netdev.c')
-rw-r--r--components/net/netdev/src/netdev.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c
index b57e844504..6b6429ed4b 100644
--- a/components/net/netdev/src/netdev.c
+++ b/components/net/netdev/src/netdev.c
@@ -1371,8 +1371,7 @@ int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_
min_time = 0xFFFFFFFF;
for (index = 0; index < times; index++)
{
- int delay_tick = 0;
- rt_tick_t start_tick = 0;
+ rt_tick_t start_tick = 0, cost_tick = 0, delay_tick = 0;
rt_memset(&ping_resp, 0x00, sizeof(struct netdev_ping_resp));
start_tick = rt_tick_get();
@@ -1413,8 +1412,19 @@ int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_
}
/* if the response time is more than NETDEV_PING_DELAY, no need to delay */
- delay_tick = ((rt_tick_get() - start_tick) > NETDEV_PING_DELAY) || (index == times) ? 0 : NETDEV_PING_DELAY;
- rt_thread_delay(delay_tick);
+ cost_tick = rt_tick_get() - start_tick;
+ if (index != times - 1)
+ {
+ if (cost_tick < NETDEV_PING_DELAY)
+ {
+ delay_tick = NETDEV_PING_DELAY - cost_tick;
+ }
+ }
+
+ if (delay_tick > 0)
+ {
+ rt_thread_delay(delay_tick);
+ }
}
if (times == 0)