@@ -29,14 +29,12 @@ namespace {
2929static const int SIGNAL_SIGIO_RX = 0x1 ;
3030static const int SIGNAL_SIGIO_TX = 0x2 ;
3131static const int SIGIO_TIMEOUT = 5000 ; // [ms]
32- static const int WAIT2RECV_TIMEOUT = 5000 ; // [ms]
3332static const int RETRIES = 2 ;
3433
3534static const double EXPECTED_LOSS_RATIO = 0.0 ;
3635static const double TOLERATED_LOSS_RATIO = 0.3 ;
3736
38- UDPSocket sock;
39- Semaphore tx_sem (0 , 1 );
37+ UDPSocket *sock;
4038EventFlags signals;
4139
4240static const int BUFF_SIZE = 1200 ;
@@ -110,33 +108,6 @@ void UDPSOCKET_ECHOTEST()
110108 TEST_ASSERT_EQUAL (NSAPI_ERROR_OK, sock.close ());
111109}
112110
113- void udpsocket_echotest_nonblock_receiver (void *receive_bytes)
114- {
115- int expt2recv = *(int *)receive_bytes;
116- int recvd;
117- for (int retry_cnt = 0 ; retry_cnt <= RETRIES; retry_cnt++) {
118- recvd = sock.recvfrom (NULL , rx_buffer, expt2recv);
119- if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
120- if (tc_exec_time.read () >= time_allotted) {
121- break ;
122- }
123- signals.wait_all (SIGNAL_SIGIO_RX, WAIT2RECV_TIMEOUT);
124- --retry_cnt;
125- continue ;
126- } else if (recvd < 0 ) {
127- printf (" sock.recvfrom returned %d\n " , recvd);
128- TEST_FAIL ();
129- break ;
130- } else if (recvd == expt2recv) {
131- break ;
132- }
133- }
134-
135- drop_bad_packets (sock, 0 ); // timeout equivalent to set_blocking(false)
136-
137- tx_sem.release ();
138- }
139-
140111void UDPSOCKET_ECHOTEST_NONBLOCK ()
141112{
142113 tc_exec_time.start ();
@@ -145,58 +116,66 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
145116 SocketAddress udp_addr;
146117 NetworkInterface::get_default_instance ()->gethostbyname (ECHO_SERVER_ADDR, &udp_addr);
147118 udp_addr.set_port (ECHO_SERVER_PORT);
148-
149- TEST_ASSERT_EQUAL (NSAPI_ERROR_OK, sock.open (NetworkInterface::get_default_instance ()));
150- sock.set_blocking (false );
151- sock.sigio (callback (_sigio_handler));
152-
119+ sock = new UDPSocket ();
120+ if (sock == NULL ) {
121+ TEST_FAIL_MESSAGE (" UDPSocket not created" );
122+ return ;
123+ }
124+ TEST_ASSERT_EQUAL (NSAPI_ERROR_OK, sock->open (NetworkInterface::get_default_instance ()));
125+ sock->set_blocking (false );
126+ sock->sigio (callback (_sigio_handler));
153127 int sent;
154128 int packets_sent = 0 ;
155129 int packets_recv = 0 ;
156- Thread *thread;
157- unsigned char *stack_mem = (unsigned char *)malloc (OS_STACK_SIZE);
158- TEST_ASSERT_NOT_NULL (stack_mem);
159-
160130 for (int s_idx = 0 ; s_idx < sizeof (pkt_sizes) / sizeof (*pkt_sizes); ++s_idx) {
161131 int pkt_s = pkt_sizes[s_idx];
162132 int packets_sent_prev = packets_sent;
163-
164- thread = new Thread (osPriorityNormal,
165- OS_STACK_SIZE,
166- stack_mem,
167- " receiver" );
168- TEST_ASSERT_EQUAL (osOK, thread->start (callback (udpsocket_echotest_nonblock_receiver, &pkt_s)));
169-
170133 for (int retry_cnt = 0 ; retry_cnt <= RETRIES; retry_cnt++) {
171134 fill_tx_buffer_ascii (tx_buffer, pkt_s);
172135
173- sent = sock. sendto (udp_addr, tx_buffer, pkt_s);
136+ sent = sock-> sendto (udp_addr, tx_buffer, pkt_s);
174137 if (sent == pkt_s) {
175138 packets_sent++;
176139 } else if (sent == NSAPI_ERROR_WOULD_BLOCK) {
177140 if (tc_exec_time.read () >= time_allotted ||
178- osSignalWait (SIGNAL_SIGIO_TX, SIGIO_TIMEOUT). status == osEventTimeout ) {
141+ signals. wait_all (SIGNAL_SIGIO_TX, SIGIO_TIMEOUT) == osFlagsErrorTimeout ) {
179142 continue ;
180143 }
181144 --retry_cnt;
182145 } else {
183146 printf (" [Round#%02d - Sender] error, returned %d\n " , s_idx, sent);
184147 continue ;
185148 }
186- if (!tx_sem.try_acquire_for (WAIT2RECV_TIMEOUT * 2 )) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom
187- continue ;
149+
150+ int recvd;
151+ for (int retry_recv = 0 ; retry_recv <= RETRIES; retry_recv++) {
152+ recvd = sock->recvfrom (NULL , rx_buffer, pkt_s);
153+ if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
154+ if (tc_exec_time.read () >= time_allotted) {
155+ break ;
156+ }
157+ signals.wait_all (SIGNAL_SIGIO_RX, SIGIO_TIMEOUT);
158+ --retry_recv;
159+ continue ;
160+ } else if (recvd < 0 ) {
161+ printf (" sock.recvfrom returned %d\n " , recvd);
162+ TEST_FAIL ();
163+ break ;
164+ } else if (recvd == pkt_s) {
165+ break ;
166+ }
167+ }
168+
169+ if (recvd == pkt_s) {
170+ break ;
188171 }
189- break ;
190172 }
191173 // Make sure that at least one packet of every size was sent.
192174 TEST_ASSERT_TRUE (packets_sent > packets_sent_prev);
193- thread->join ();
194- delete thread;
195175 if (memcmp (tx_buffer, rx_buffer, pkt_s) == 0 ) {
196176 packets_recv++;
197177 }
198178 }
199- free (stack_mem);
200179
201180 // Packet loss up to 30% tolerated
202181 if (packets_sent > 0 ) {
@@ -220,6 +199,7 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
220199
221200#endif
222201 }
223- TEST_ASSERT_EQUAL (NSAPI_ERROR_OK, sock.close ());
202+ TEST_ASSERT_EQUAL (NSAPI_ERROR_OK, sock->close ());
203+ delete sock;
224204 tc_exec_time.stop ();
225205}
0 commit comments