takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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