takashi kadono / Mbed OS Nucleo446_SSD1331

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpsocket_endpoint_close.cpp Source File

tcpsocket_endpoint_close.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 static nsapi_error_t _tcpsocket_connect_to_daytime_srv(TCPSocket &sock)
00038 {
00039     SocketAddress tcp_addr;
00040 
00041     get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
00042     tcp_addr.set_port(13);
00043 
00044     nsapi_error_t err = sock.open(get_interface());
00045     if (err != NSAPI_ERROR_OK ) {
00046         return err;
00047     }
00048 
00049     return sock.connect(tcp_addr);
00050 }
00051 
00052 
00053 void TCPSOCKET_ENDPOINT_CLOSE()
00054 {
00055     static const int MORE_THAN_AVAILABLE = 30;
00056     char buff[MORE_THAN_AVAILABLE];
00057     int time_allotted = split2half_rmng_tcp_test_time(); // [s]
00058     Timer tc_exec_time;
00059     tc_exec_time.start();
00060 
00061     TCPSocket sock;
00062     if (_tcpsocket_connect_to_daytime_srv(sock) != NSAPI_ERROR_OK ) {
00063         TEST_FAIL();
00064         return;
00065     }
00066     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00067 
00068     int recvd = 0;
00069     int recvd_total = 0;
00070     while (true) {
00071         recvd = sock.recv(&(buff[recvd_total]), MORE_THAN_AVAILABLE);
00072         if (recvd_total > 0 && recvd == 0) {
00073             break; // Endpoint closed socket, success
00074         } else if (recvd <= 0) {
00075             TEST_FAIL();
00076             break;
00077         } else if (recvd == NSAPI_ERROR_WOULD_BLOCK ) {
00078             if (tc_exec_time.read() >= time_allotted ||
00079                     osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
00080                 TEST_FAIL();
00081                 break;
00082             }
00083             continue;
00084         }
00085         recvd_total += recvd;
00086         TEST_ASSERT(recvd_total < MORE_THAN_AVAILABLE);
00087     }
00088     tc_exec_time.stop();
00089     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00090 }