Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers udpsocket_recv_timeout.cpp Source File

udpsocket_recv_timeout.cpp

00001 /*
00002  * Copyright (c) 2018, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "mbed.h"
00019 #include MBED_CONF_APP_HEADER_FILE
00020 #include "UDPSocket.h"
00021 #include "greentea-client/test_env.h"
00022 #include "unity/unity.h"
00023 #include "utest.h"
00024 #include "udp_tests.h"
00025 
00026 using namespace utest::v1;
00027 
00028 namespace
00029 {
00030     static const int SIGNAL_SIGIO = 0x1;
00031     static const int SIGIO_TIMEOUT = 5000; //[ms]
00032 }
00033 
00034 static void _sigio_handler(osThreadId id) {
00035     osSignalSet(id, SIGNAL_SIGIO);
00036 }
00037 
00038 void UDPSOCKET_RECV_TIMEOUT()
00039 {
00040     SocketAddress udp_addr;
00041     get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
00042     udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
00043 
00044     static const int DATA_LEN = 100;
00045     char buff[DATA_LEN] = {0};
00046 
00047     UDPSocket sock;
00048     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.open(get_interface()));
00049     sock.set_timeout(100);
00050     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00051 
00052     int recvd;
00053     Timer timer;
00054     SocketAddress temp_addr;
00055     int pkt_success = 0;
00056     for (int i = 0; i < 10; i++) {
00057         TEST_ASSERT_EQUAL(DATA_LEN, sock.sendto(udp_addr, buff, DATA_LEN));
00058         timer.reset();
00059         timer.start();
00060         recvd = sock.recvfrom(&temp_addr, buff, sizeof(buff));
00061         timer.stop();
00062 
00063         if (recvd == NSAPI_ERROR_WOULD_BLOCK ) {
00064             osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT);
00065             printf("MBED: recvfrom() took: %dms\n", timer.read_ms());
00066             TEST_ASSERT_INT_WITHIN(50, 150, timer.read_ms());
00067             continue;
00068         } else if (recvd < 0) {
00069             printf("[bt#%02d] network error %d\n", i, recvd);
00070             continue;
00071         } else if (temp_addr != udp_addr) {
00072             printf("[bt#%02d] packet from wrong address\n", i);
00073             continue;
00074         }
00075         TEST_ASSERT_EQUAL(DATA_LEN, recvd);
00076         pkt_success++;
00077     }
00078 
00079     TEST_ASSERT(pkt_success >= 5);
00080     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00081 }