Knight KE / Mbed OS Game_Master
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 MBED_CONF_APP_HEADER_FILE
00020 #include "TCPSocket.h"
00021 #include "greentea-client/test_env.h"
00022 #include "unity/unity.h"
00023 #include "utest.h"
00024 #include "tcp_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 static void _tcpsocket_connect_to_daytime_srv(TCPSocket& sock) {
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     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.open(get_interface()));
00045     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.connect(tcp_addr));
00046 }
00047 
00048 
00049 void TCPSOCKET_ENDPOINT_CLOSE()
00050 {
00051     static const int MORE_THAN_AVAILABLE = 30;
00052     char buff[MORE_THAN_AVAILABLE];
00053 
00054     TCPSocket sock;
00055     _tcpsocket_connect_to_daytime_srv(sock);
00056     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00057 
00058     int recvd = 0;
00059     int recvd_total = 0;
00060     while (true) {
00061          recvd = sock.recv(&(buff[recvd_total]), MORE_THAN_AVAILABLE);
00062          if (recvd_total > 0 && recvd == 0) {
00063              break; // Endpoint closed socket, success
00064          } else if (recvd == 0) {
00065             TEST_FAIL();
00066          } else if (recvd == NSAPI_ERROR_WOULD_BLOCK ) {
00067              if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
00068                  TEST_FAIL();
00069              }
00070              continue;
00071          }
00072          recvd_total += recvd;
00073          TEST_ASSERT(recvd_total < MORE_THAN_AVAILABLE);
00074     }
00075     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00076 }