@@ -48,7 +48,7 @@ char uuid[GREENTEA_UUID_LENGTH] = {0};
48
48
// would have a buffer filled with something like `2 33e5002c-9722-4685-817a-709cc69c4701 12594387`
49
49
// where `2` is the thread id, `33e5002c-9722-4685-817a-709cc69c4701` is the UUID
50
50
// 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) {
52
52
size_t i = 0 ;
53
53
54
54
tx_buffer[i++] = ' 0' + id;
@@ -74,15 +74,15 @@ class Echo {
74
74
UDPSocket sock;
75
75
Thread thread;
76
76
bool result;
77
- int id;
77
+ unsigned int id;
78
78
char *uuid;
79
79
80
80
public:
81
81
// Limiting stack size to 1k
82
82
Echo (): thread(osPriorityNormal, 1024 ), result(false ) {
83
83
}
84
84
85
- void start (int id, char *uuid) {
85
+ void start (unsigned int id, char *uuid) {
86
86
this ->id = id;
87
87
this ->uuid = uuid;
88
88
osStatus status = thread.start (callback (this , &Echo::echo));
@@ -101,39 +101,39 @@ class Echo {
101
101
102
102
sock.set_timeout (MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
103
103
104
- for (int i = 0 ; success < ECHO_LOOPS; i++) {
104
+ for (unsigned int i = 0 ; success < ECHO_LOOPS; i++) {
105
105
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));
107
107
if (ret >= 0 ) {
108
108
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);
110
110
iomutex.unlock ();
111
111
} else {
112
112
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);
114
114
iomutex.unlock ();
115
115
continue ;
116
116
}
117
117
118
118
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 ) {
121
121
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);
123
123
iomutex.unlock ();
124
124
} else {
125
125
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 );
127
127
iomutex.unlock ();
128
128
continue ;
129
129
}
130
130
131
131
if ((temp_addr == udp_addr &&
132
- n == sizeof (tx_buffer) &&
132
+ ret == sizeof (tx_buffer) &&
133
133
memcmp (rx_buffer, tx_buffer, sizeof (rx_buffer)) == 0 )) {
134
134
success += 1 ;
135
135
iomutex.lock ();
136
- printf (" [ID:%01d ][%02d ] success #%d\n " , id, i, success);
136
+ printf (" [ID:%01u ][%02u ] success #%d\n " , id, i, success);
137
137
iomutex.unlock ();
138
138
continue ;
139
139
}
@@ -169,47 +169,42 @@ void test_udp_echo_parallel() {
169
169
int err = net.connect ();
170
170
TEST_ASSERT_EQUAL (0 , err);
171
171
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 ());
179
173
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 ());
184
175
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 ;
187
180
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));
191
183
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);
195
187
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);
201
191
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
+ }
203
197
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 ;
209
199
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];
212
204
}
205
+
206
+ net.disconnect ();
207
+ TEST_ASSERT (result);
213
208
}
214
209
215
210
0 commit comments