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

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.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 #ifndef MBED_CONF_APP_CONNECT_STATEMENT
00019 #error [NOT_SUPPORTED] No network configuration found for this target.
00020 #endif
00021 
00022 #include "mbed.h"
00023 #include MBED_CONF_APP_HEADER_FILE
00024 #include "greentea-client/test_env.h"
00025 #include "unity/unity.h"
00026 #include "utest.h"
00027 #include "utest/utest_stack_trace.h"
00028 #include "tcp_tests.h"
00029 
00030 using namespace utest::v1;
00031 
00032 namespace
00033 {
00034     NetworkInterface* net;
00035 }
00036 
00037 char tcp_global::rx_buffer[RX_BUFF_SIZE];
00038 char tcp_global::tx_buffer[TX_BUFF_SIZE];
00039 
00040 NetworkInterface* get_interface()
00041 {
00042     return net;
00043 }
00044 
00045 void drop_bad_packets(TCPSocket& sock, int orig_timeout) {
00046     nsapi_error_t err;
00047     sock.set_timeout(0);
00048     while (true) {
00049         err = sock.recv(NULL, 0);
00050         if (err == NSAPI_ERROR_WOULD_BLOCK ) {
00051             break;
00052         }
00053     }
00054     sock.set_timeout(orig_timeout);
00055 }
00056 
00057 static void _ifup() {
00058     net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
00059     nsapi_error_t err = MBED_CONF_APP_CONNECT_STATEMENT;
00060     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , err);
00061     printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address());
00062 }
00063 
00064 static void _ifdown() {
00065     net->disconnect();
00066     printf("MBED: ifdown\n");
00067 }
00068 
00069 void tcpsocket_connect_to_echo_srv(TCPSocket& sock) {
00070     SocketAddress tcp_addr;
00071 
00072     get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
00073     tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
00074 
00075     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.open(get_interface()));
00076     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.connect(tcp_addr));
00077 }
00078 
00079 void tcpsocket_connect_to_discard_srv(TCPSocket& sock) {
00080     SocketAddress tcp_addr;
00081 
00082     get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
00083     tcp_addr.set_port(9);
00084 
00085     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.open(get_interface()));
00086     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.connect(tcp_addr));
00087 }
00088 
00089 void fill_tx_buffer_ascii(char *buff, size_t len)
00090 {
00091     for (size_t i = 0; i<len; ++i) {
00092         buff[i] = (rand() % 43) + '0';
00093     }
00094 }
00095 
00096 // Test setup
00097 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00098 {
00099     GREENTEA_SETUP(240, "default_auto");
00100     _ifup();
00101     return greentea_test_setup_handler(number_of_cases);
00102 }
00103 
00104 void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
00105 {
00106     _ifdown();
00107     return greentea_test_teardown_handler(passed, failed, failure);
00108 }
00109 
00110 
00111 Case cases[] = {
00112     Case("TCPSOCKET_ECHOTEST", TCPSOCKET_ECHOTEST),
00113     Case("TCPSOCKET_ECHOTEST_NONBLOCK", TCPSOCKET_ECHOTEST_NONBLOCK),
00114     Case("TCPSOCKET_OPEN_CLOSE_REPEAT", TCPSOCKET_OPEN_CLOSE_REPEAT),
00115     Case("TCPSOCKET_OPEN_LIMIT", TCPSOCKET_OPEN_LIMIT),
00116     Case("TCPSOCKET_THREAD_PER_SOCKET_SAFETY", TCPSOCKET_THREAD_PER_SOCKET_SAFETY),
00117 #ifdef MBED_EXTENDED_TESTS
00118     Case("TCPSOCKET_CONNECT_INVALID", TCPSOCKET_CONNECT_INVALID),
00119     Case("TCPSOCKET_ECHOTEST_BURST", TCPSOCKET_ECHOTEST_BURST),
00120     Case("TCPSOCKET_ECHOTEST_BURST_NONBLOCK", TCPSOCKET_ECHOTEST_BURST_NONBLOCK),
00121     Case("TCPSOCKET_RECV_100K", TCPSOCKET_RECV_100K),
00122     Case("TCPSOCKET_RECV_100K_NONBLOCK", TCPSOCKET_RECV_100K_NONBLOCK),
00123     Case("TCPSOCKET_RECV_TIMEOUT", TCPSOCKET_RECV_TIMEOUT),
00124     Case("TCPSOCKET_SEND_REPEAT", TCPSOCKET_SEND_REPEAT),
00125     Case("TCPSOCKET_SEND_TIMEOUT", TCPSOCKET_SEND_TIMEOUT),
00126     Case("TCPSOCKET_ENDPOINT_CLOSE", TCPSOCKET_ENDPOINT_CLOSE),
00127 #endif
00128 };
00129 
00130 Specification specification(greentea_setup, cases, greentea_teardown);
00131 
00132 int main()
00133 {
00134     return !Harness::run(specification);
00135 }