Skip to content

Commit c38b709

Browse files
bridadan0xc0170
authored andcommitted
Cleaning up udp tests
1 parent ccad027 commit c38b709

File tree

2 files changed

+50
-55
lines changed
  • features/FEATURE_LWIP/TESTS/mbedmicro-net

2 files changed

+50
-55
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,31 @@ void test_udp_echo() {
8787
SocketAddress udp_addr(ipbuf, port);
8888

8989
int success = 0;
90-
for (int i = 0; success < ECHO_LOOPS; i++) {
90+
for (unsigned int i = 0; success < ECHO_LOOPS; i++) {
9191
prep_buffer(uuid, tx_buffer, sizeof(tx_buffer));
92-
const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
92+
int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
9393
if (ret >= 0) {
94-
printf("[%02d] sent %d bytes - %.*s \n", i, ret, ret, tx_buffer);
94+
printf("[%02u] sent %d bytes - %.*s \n", i, ret, ret, tx_buffer);
9595
} else {
96-
printf("[%02d] Network error %d\n", i, ret);
96+
printf("[%02u] Network error %d\n", i, ret);
9797
continue;
9898
}
9999

100100
SocketAddress temp_addr;
101-
const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
102-
if (n >= 0) {
103-
printf("[%02d] recv %d bytes - %.*s \n", i, n, n, tx_buffer);
101+
ret = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
102+
if (ret >= 0) {
103+
printf("[%02u] recv %d bytes - %.*s \n", i, ret, ret, tx_buffer);
104104
} else {
105-
printf("[%02d] Network error %d\n", i, n);
105+
printf("[%02u] Network error %d\n", i, ret);
106106
continue;
107107
}
108108

109109
if ((temp_addr == udp_addr &&
110-
n == sizeof(tx_buffer) &&
110+
ret == sizeof(tx_buffer) &&
111111
memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
112112
success += 1;
113113

114-
printf("[%02d] success #%d\n", i, success);
114+
printf("[%02u] success #%d\n", i, success);
115115
continue;
116116
}
117117

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

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ char uuid[GREENTEA_UUID_LENGTH] = {0};
4848
// would have a buffer filled with something like `2 33e5002c-9722-4685-817a-709cc69c4701 12594387`
4949
// where `2` is the thread id, `33e5002c-9722-4685-817a-709cc69c4701` is the UUID
5050
// and `12594387` is the random data
51-
void prep_buffer(int id, char *uuid, char *tx_buffer, size_t tx_size) {
51+
void prep_buffer(unsigned int id, char *uuid, char *tx_buffer, size_t tx_size) {
5252
size_t i = 0;
5353

5454
tx_buffer[i++] = '0' + id;
@@ -74,15 +74,15 @@ class Echo {
7474
UDPSocket sock;
7575
Thread thread;
7676
bool result;
77-
int id;
77+
unsigned int id;
7878
char *uuid;
7979

8080
public:
8181
// Limiting stack size to 1k
8282
Echo(): thread(osPriorityNormal, 1024), result(false) {
8383
}
8484

85-
void start(int id, char *uuid) {
85+
void start(unsigned int id, char *uuid) {
8686
this->id = id;
8787
this->uuid = uuid;
8888
osStatus status = thread.start(callback(this, &Echo::echo));
@@ -101,39 +101,39 @@ class Echo {
101101

102102
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
103103

104-
for (int i = 0; success < ECHO_LOOPS; i++) {
104+
for (unsigned int i = 0; success < ECHO_LOOPS; i++) {
105105
prep_buffer(id, uuid, tx_buffer, sizeof(tx_buffer));
106-
const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
106+
int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
107107
if (ret >= 0) {
108108
iomutex.lock();
109-
printf("[ID:%01d][%02d] sent %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
109+
printf("[ID:%01u][%02u] sent %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
110110
iomutex.unlock();
111111
} else {
112112
iomutex.lock();
113-
printf("[ID:%01d][%02d] Network error %d\n", id, i, ret);
113+
printf("[ID:%01u][%02u] Network error %d\n", id, i, ret);
114114
iomutex.unlock();
115115
continue;
116116
}
117117

118118
SocketAddress temp_addr;
119-
const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
120-
if (n >= 0) {
119+
ret = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
120+
if (ret >= 0) {
121121
iomutex.lock();
122-
printf("[ID:%01d][%02d] recv %d bytes - %.*s \n", id, i, n, n, tx_buffer);
122+
printf("[ID:%01u][%02u] recv %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
123123
iomutex.unlock();
124124
} else {
125125
iomutex.lock();
126-
printf("[ID:%01d][%02d] Network error %d\n", id, i, n);
126+
printf("[ID:%01u][%02u] Network error %d\n", id, i, ret);
127127
iomutex.unlock();
128128
continue;
129129
}
130130

131131
if ((temp_addr == udp_addr &&
132-
n == sizeof(tx_buffer) &&
132+
ret == sizeof(tx_buffer) &&
133133
memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
134134
success += 1;
135135
iomutex.lock();
136-
printf("[ID:%01d][%02d] success #%d\n", id, i, success);
136+
printf("[ID:%01u][%02u] success #%d\n", id, i, success);
137137
iomutex.unlock();
138138
continue;
139139
}
@@ -169,47 +169,42 @@ void test_udp_echo_parallel() {
169169
int err = net.connect();
170170
TEST_ASSERT_EQUAL(0, err);
171171

172-
if (err) {
173-
printf("MBED: failed to connect with an error of %d\r\n", err);
174-
GREENTEA_TESTSUITE_RESULT(false);
175-
} else {
176-
printf("UDP client IP Address is %s\n", net.get_ip_address());
177-
178-
greentea_send_kv("target_ip", net.get_ip_address());
172+
printf("UDP client IP Address is %s\n", net.get_ip_address());
179173

180-
char recv_key[] = "host_port";
181-
char ipbuf[60] = {0};
182-
char portbuf[16] = {0};
183-
unsigned int port = 0;
174+
greentea_send_kv("target_ip", net.get_ip_address());
184175

185-
greentea_send_kv("host_ip", " ");
186-
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
176+
char recv_key[] = "host_port";
177+
char ipbuf[60] = {0};
178+
char portbuf[16] = {0};
179+
unsigned int port = 0;
187180

188-
greentea_send_kv("host_port", " ");
189-
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
190-
sscanf(portbuf, "%u", &port);
181+
greentea_send_kv("host_ip", " ");
182+
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
191183

192-
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
193-
udp_addr.set_ip_address(ipbuf);
194-
udp_addr.set_port(port);
184+
greentea_send_kv("host_port", " ");
185+
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
186+
sscanf(portbuf, "%u", &port);
195187

196-
// Startup echo threads in parallel
197-
for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
198-
echoers[i] = new Echo;
199-
echoers[i]->start(i, uuid);
200-
}
188+
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
189+
udp_addr.set_ip_address(ipbuf);
190+
udp_addr.set_port(port);
201191

202-
bool result = true;
192+
// Startup echo threads in parallel
193+
for (unsigned int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
194+
echoers[i] = new Echo;
195+
echoers[i]->start(i, uuid);
196+
}
203197

204-
for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
205-
echoers[i]->join();
206-
result = result && echoers[i]->get_result();
207-
delete echoers[i];
208-
}
198+
bool result = true;
209199

210-
net.disconnect();
211-
TEST_ASSERT(result);
200+
for (unsigned int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
201+
echoers[i]->join();
202+
result = result && echoers[i]->get_result();
203+
delete echoers[i];
212204
}
205+
206+
net.disconnect();
207+
TEST_ASSERT(result);
213208
}
214209

215210

0 commit comments

Comments
 (0)