Skip to content

Commit 2761a8c

Browse files
committed
lwip: Added udp buffer flushing after dropped packets
Avoids getting stuck in loop where the device always picks up the previous test's packets
1 parent 9c98f3f commit 2761a8c

File tree

3 files changed

+83
-57
lines changed

3 files changed

+83
-57
lines changed

features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_dtls_handshake/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#define MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN 112, 384, 200, 219, 25
2424
#endif
2525

26+
#ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT
27+
#define MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT 1500
28+
#endif
29+
2630
uint8_t buffer[MBED_CFG_UDP_DTLS_HANDSHAKE_BUFFER_SIZE] = {0};
2731
int udp_dtls_handshake_pattern[] = {MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN};
2832
const int udp_dtls_handshake_count = sizeof(udp_dtls_handshake_pattern) / sizeof(int);
@@ -73,8 +77,7 @@ int main() {
7377

7478
UDPSocket sock;
7579
SocketAddress udp_addr(ipbuf, port);
76-
sock.set_blocking(true);
77-
sock.set_timeout(1500);
80+
sock.set_timeout(MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT);
7881

7982
for (int attempt = 0; attempt < MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES; attempt++) {
8083
err = sock.open(&eth);

features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo/main.cpp

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,67 +54,79 @@ int main() {
5454
if (err) {
5555
printf("MBED: failed to connect with an error of %d\r\n", err);
5656
GREENTEA_TESTSUITE_RESULT(false);
57-
} else {
58-
printf("UDP client IP Address is %s\n", eth.get_ip_address());
57+
return 0;
58+
}
5959

60-
greentea_send_kv("target_ip", eth.get_ip_address());
60+
printf("UDP client IP Address is %s\n", eth.get_ip_address());
6161

62-
char recv_key[] = "host_port";
63-
char ipbuf[60] = {0};
64-
char portbuf[16] = {0};
65-
unsigned int port = 0;
62+
greentea_send_kv("target_ip", eth.get_ip_address());
6663

67-
UDPSocket sock;
68-
sock.open(&eth);
69-
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
64+
char recv_key[] = "host_port";
65+
char ipbuf[60] = {0};
66+
char portbuf[16] = {0};
67+
unsigned int port = 0;
7068

71-
greentea_send_kv("host_ip", " ");
72-
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
73-
74-
greentea_send_kv("host_port", " ");
75-
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
76-
sscanf(portbuf, "%u", &port);
77-
78-
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
79-
SocketAddress udp_addr(ipbuf, port);
80-
81-
int success = 0;
82-
int i = 0;
83-
while (success < ECHO_LOOPS) {
84-
prep_buffer(uuid, uuid_len, tx_buffer, sizeof(tx_buffer));
85-
const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
86-
if (ret >= 0) {
87-
printf("[%02d] sent %d Bytes - %.*s \n", i, ret, ret, tx_buffer);
88-
} else {
89-
printf("[%02d] Network error %d\n", i, ret);
90-
i++;
91-
continue;
92-
}
69+
UDPSocket sock;
70+
sock.open(&eth);
71+
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
9372

94-
SocketAddress temp_addr;
95-
const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
96-
if (n >= 0) {
97-
printf("[%02d] receive %d Bytes - %.*s \n", i, n, n, tx_buffer);
98-
} else {
99-
printf("[%02d] Network error %d\n", i, n);
100-
i++;
101-
continue;
102-
}
73+
greentea_send_kv("host_ip", " ");
74+
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
10375

104-
if ((temp_addr == udp_addr &&
105-
n == sizeof(tx_buffer) &&
106-
memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
107-
success += 1;
76+
greentea_send_kv("host_port", " ");
77+
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
78+
sscanf(portbuf, "%u", &port);
10879

109-
printf("[%02d] success #%d\n", i, success);
110-
}
80+
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
81+
SocketAddress udp_addr(ipbuf, port);
82+
83+
int success = 0;
84+
int i = 0;
85+
while (success < ECHO_LOOPS) {
86+
prep_buffer(uuid, uuid_len, tx_buffer, sizeof(tx_buffer));
87+
const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
88+
if (ret >= 0) {
89+
printf("[%02d] sent %d bytes - %.*s \n", i, ret, ret, tx_buffer);
90+
} else {
91+
printf("[%02d] Network error %d\n", i, ret);
92+
i++;
93+
continue;
94+
}
95+
96+
SocketAddress temp_addr;
97+
const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
98+
if (n >= 0) {
99+
printf("[%02d] recv %d bytes - %.*s \n", i, n, n, tx_buffer);
100+
} else {
101+
printf("[%02d] Network error %d\n", i, n);
111102
i++;
103+
continue;
112104
}
113105

114-
bool result = success == ECHO_LOOPS;
106+
if ((temp_addr == udp_addr &&
107+
n == sizeof(tx_buffer) &&
108+
memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
109+
success += 1;
115110

116-
sock.close();
117-
eth.disconnect();
118-
GREENTEA_TESTSUITE_RESULT(result);
111+
printf("[%02d] success #%d\n", i, success);
112+
i++;
113+
continue;
114+
}
115+
116+
// failed, clean out any remaining bad packets
117+
sock.set_timeout(0);
118+
while (true) {
119+
err = sock.recvfrom(NULL, NULL, 0);
120+
if (err == NSAPI_ERROR_WOULD_BLOCK) {
121+
break;
122+
}
123+
}
124+
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
119125
}
126+
127+
bool result = success == ECHO_LOOPS;
128+
129+
sock.close();
130+
eth.disconnect();
131+
GREENTEA_TESTSUITE_RESULT(result);
120132
}

features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ class Echo {
8787
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
8888

8989
int i = 0;
90-
while(success < ECHO_LOOPS) {
90+
while (success < ECHO_LOOPS) {
9191
prep_buffer(id, uuid_buffer, uuid_len, tx_buffer, sizeof(tx_buffer));
9292
const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
9393
if (ret >= 0) {
9494
iomutex.lock();
95-
printf("[ID:%01d][%02d] sent %d Bytes - %.*s \n", id, i, ret, ret, tx_buffer);
95+
printf("[ID:%01d][%02d] sent %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
9696
iomutex.unlock();
9797
} else {
9898
iomutex.lock();
@@ -106,7 +106,7 @@ class Echo {
106106
const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
107107
if (n >= 0) {
108108
iomutex.lock();
109-
printf("[ID:%01d][%02d] receive %d Bytes - %.*s \n", id, i, n, n, tx_buffer);
109+
printf("[ID:%01d][%02d] recv %d bytes - %.*s \n", id, i, n, n, tx_buffer);
110110
iomutex.unlock();
111111
} else {
112112
iomutex.lock();
@@ -123,9 +123,20 @@ class Echo {
123123
iomutex.lock();
124124
printf("[ID:%01d][%02d] success #%d\n", id, i, success);
125125
iomutex.unlock();
126+
127+
i++;
128+
continue;
126129
}
127130

128-
i++;
131+
// failed, clean out any remaining bad packets
132+
sock.set_timeout(0);
133+
while (true) {
134+
err = sock.recvfrom(NULL, NULL, 0);
135+
if (err == NSAPI_ERROR_WOULD_BLOCK) {
136+
break;
137+
}
138+
}
139+
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
129140
}
130141

131142
result = success == ECHO_LOOPS;

0 commit comments

Comments
 (0)