takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 "UDPSocket.h"
00020 #include "greentea-client/test_env.h"
00021 #include "unity/unity.h"
00022 #include "utest.h"
00023 #include "udp_tests.h"
00024 
00025 using namespace utest::v1;
00026 
00027 namespace {
00028 static const int SIGNAL_SIGIO = 0x1;
00029 static const int SIGIO_TIMEOUT = 5000; //[ms]
00030 }
00031 
00032 static void _sigio_handler(osThreadId id)
00033 {
00034     osSignalSet(id, SIGNAL_SIGIO);
00035 }
00036 
00037 void UDPSOCKET_RECV_TIMEOUT()
00038 {
00039     SocketAddress udp_addr;
00040     get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
00041     udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
00042 
00043     static const int DATA_LEN = 100;
00044     char buff[DATA_LEN] = {0};
00045 
00046     UDPSocket sock;
00047     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.open(get_interface()));
00048     sock.set_timeout(100);
00049     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00050 
00051     int recvd;
00052     Timer timer;
00053     SocketAddress temp_addr;
00054     int pkt_success = 0;
00055     for (int i = 0; i < 10; i++) {
00056         TEST_ASSERT_EQUAL(DATA_LEN, sock.sendto(udp_addr, buff, DATA_LEN));
00057         timer.reset();
00058         timer.start();
00059         recvd = sock.recvfrom(&temp_addr, buff, sizeof(buff));
00060         timer.stop();
00061 
00062         if (recvd == NSAPI_ERROR_WOULD_BLOCK ) {
00063             osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT);
00064             printf("MBED: recvfrom() took: %dms\n", timer.read_ms());
00065             TEST_ASSERT_INT_WITHIN(50, 150, timer.read_ms());
00066             continue;
00067         } else if (recvd < 0) {
00068             printf("[bt#%02d] network error %d\n", i, recvd);
00069             continue;
00070         } else if (temp_addr != udp_addr) {
00071             printf("[bt#%02d] packet from wrong address\n", i);
00072             continue;
00073         }
00074         TEST_ASSERT_EQUAL(DATA_LEN, recvd);
00075         pkt_success++;
00076     }
00077 
00078     TEST_ASSERT(pkt_success >= 5);
00079     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00080 }