takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpsocket_recv_timeout.cpp Source File

tcpsocket_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 "TCPSocket.h"
00020 #include "greentea-client/test_env.h"
00021 #include "unity/unity.h"
00022 #include "utest.h"
00023 #include "tcp_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 TCPSOCKET_RECV_TIMEOUT()
00038 {
00039     static const int DATA_LEN = 100;
00040     char buff[DATA_LEN] = {0};
00041     int time_allotted = split2half_rmng_tcp_test_time(); // [s]
00042     Timer tc_exec_time;
00043     tc_exec_time.start();
00044 
00045     TCPSocket sock;
00046     tcpsocket_connect_to_echo_srv(sock);
00047     sock.set_timeout(100);
00048     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00049 
00050     int recvd = 0;
00051     int pkt_unrecvd;
00052     Timer timer;
00053     for (int i = 0; i < 5; i++) {
00054         pkt_unrecvd = DATA_LEN;
00055         TEST_ASSERT_EQUAL(DATA_LEN, sock.send(buff, DATA_LEN));
00056 
00057         while (pkt_unrecvd) {
00058             timer.reset();
00059             timer.start();
00060             recvd = sock.recv(&(buff[DATA_LEN - pkt_unrecvd]), pkt_unrecvd);
00061             timer.stop();
00062 
00063             if (recvd == NSAPI_ERROR_WOULD_BLOCK ) {
00064                 if (tc_exec_time.read() >= time_allotted ||
00065                         (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout)) {
00066                     TEST_FAIL();
00067                     goto CLEANUP;
00068                 }
00069                 printf("MBED: recv() took: %dms\n", timer.read_ms());
00070                 TEST_ASSERT_INT_WITHIN(50, 150, timer.read_ms());
00071                 continue;
00072             } else if (recvd < 0) {
00073                 printf("[pkt#%02d] network error %d\n", i, recvd);
00074                 TEST_FAIL();
00075                 goto CLEANUP;
00076             }
00077             pkt_unrecvd -= recvd;
00078         }
00079     }
00080 
00081 CLEANUP:
00082     tc_exec_time.stop();
00083     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00084 }