takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/netsocket/tcp/main.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2018, ARM Limited, All Rights Reserved |
kadonotakashi | 0:8fdf9a60065b | 3 | * SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kadonotakashi | 0:8fdf9a60065b | 6 | * not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | * |
kadonotakashi | 0:8fdf9a60065b | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kadonotakashi | 0:8fdf9a60065b | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | */ |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | #define WIFI 2 |
kadonotakashi | 0:8fdf9a60065b | 19 | #if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \ |
kadonotakashi | 0:8fdf9a60065b | 20 | (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID)) |
kadonotakashi | 0:8fdf9a60065b | 21 | #error [NOT_SUPPORTED] No network configuration found for this target. |
kadonotakashi | 0:8fdf9a60065b | 22 | #endif |
kadonotakashi | 0:8fdf9a60065b | 23 | #ifndef MBED_CONF_APP_ECHO_SERVER_ADDR |
kadonotakashi | 0:8fdf9a60065b | 24 | #error [NOT_SUPPORTED] Requires parameters from mbed_app.json |
kadonotakashi | 0:8fdf9a60065b | 25 | #endif |
kadonotakashi | 0:8fdf9a60065b | 26 | |
kadonotakashi | 0:8fdf9a60065b | 27 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 28 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 29 | #include "unity/unity.h" |
kadonotakashi | 0:8fdf9a60065b | 30 | #include "utest.h" |
kadonotakashi | 0:8fdf9a60065b | 31 | #include "utest/utest_stack_trace.h" |
kadonotakashi | 0:8fdf9a60065b | 32 | #include "tcp_tests.h" |
kadonotakashi | 0:8fdf9a60065b | 33 | |
kadonotakashi | 0:8fdf9a60065b | 34 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 35 | |
kadonotakashi | 0:8fdf9a60065b | 36 | namespace { |
kadonotakashi | 0:8fdf9a60065b | 37 | NetworkInterface *net; |
kadonotakashi | 0:8fdf9a60065b | 38 | Timer tc_bucket; // Timer to limit a test cases run time |
kadonotakashi | 0:8fdf9a60065b | 39 | } |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | char tcp_global::rx_buffer[RX_BUFF_SIZE]; |
kadonotakashi | 0:8fdf9a60065b | 42 | char tcp_global::tx_buffer[TX_BUFF_SIZE]; |
kadonotakashi | 0:8fdf9a60065b | 43 | |
kadonotakashi | 0:8fdf9a60065b | 44 | NetworkInterface *get_interface() |
kadonotakashi | 0:8fdf9a60065b | 45 | { |
kadonotakashi | 0:8fdf9a60065b | 46 | return net; |
kadonotakashi | 0:8fdf9a60065b | 47 | } |
kadonotakashi | 0:8fdf9a60065b | 48 | |
kadonotakashi | 0:8fdf9a60065b | 49 | void drop_bad_packets(TCPSocket &sock, int orig_timeout) |
kadonotakashi | 0:8fdf9a60065b | 50 | { |
kadonotakashi | 0:8fdf9a60065b | 51 | nsapi_error_t err; |
kadonotakashi | 0:8fdf9a60065b | 52 | sock.set_timeout(0); |
kadonotakashi | 0:8fdf9a60065b | 53 | while (true) { |
kadonotakashi | 0:8fdf9a60065b | 54 | err = sock.recv(NULL, 0); |
kadonotakashi | 0:8fdf9a60065b | 55 | if (err == NSAPI_ERROR_WOULD_BLOCK) { |
kadonotakashi | 0:8fdf9a60065b | 56 | break; |
kadonotakashi | 0:8fdf9a60065b | 57 | } |
kadonotakashi | 0:8fdf9a60065b | 58 | } |
kadonotakashi | 0:8fdf9a60065b | 59 | sock.set_timeout(orig_timeout); |
kadonotakashi | 0:8fdf9a60065b | 60 | } |
kadonotakashi | 0:8fdf9a60065b | 61 | |
kadonotakashi | 0:8fdf9a60065b | 62 | static void _ifup() |
kadonotakashi | 0:8fdf9a60065b | 63 | { |
kadonotakashi | 0:8fdf9a60065b | 64 | net = NetworkInterface::get_default_instance(); |
kadonotakashi | 0:8fdf9a60065b | 65 | nsapi_error_t err = net->connect(); |
kadonotakashi | 0:8fdf9a60065b | 66 | TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); |
kadonotakashi | 0:8fdf9a60065b | 67 | printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address()); |
kadonotakashi | 0:8fdf9a60065b | 68 | } |
kadonotakashi | 0:8fdf9a60065b | 69 | |
kadonotakashi | 0:8fdf9a60065b | 70 | static void _ifdown() |
kadonotakashi | 0:8fdf9a60065b | 71 | { |
kadonotakashi | 0:8fdf9a60065b | 72 | net->disconnect(); |
kadonotakashi | 0:8fdf9a60065b | 73 | printf("MBED: ifdown\n"); |
kadonotakashi | 0:8fdf9a60065b | 74 | } |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket &sock) |
kadonotakashi | 0:8fdf9a60065b | 77 | { |
kadonotakashi | 0:8fdf9a60065b | 78 | SocketAddress tcp_addr; |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr); |
kadonotakashi | 0:8fdf9a60065b | 81 | tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT); |
kadonotakashi | 0:8fdf9a60065b | 82 | |
kadonotakashi | 0:8fdf9a60065b | 83 | nsapi_error_t err = sock.open(get_interface()); |
kadonotakashi | 0:8fdf9a60065b | 84 | if (err != NSAPI_ERROR_OK) { |
kadonotakashi | 0:8fdf9a60065b | 85 | return err; |
kadonotakashi | 0:8fdf9a60065b | 86 | } |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | return sock.connect(tcp_addr); |
kadonotakashi | 0:8fdf9a60065b | 89 | } |
kadonotakashi | 0:8fdf9a60065b | 90 | |
kadonotakashi | 0:8fdf9a60065b | 91 | nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket &sock) |
kadonotakashi | 0:8fdf9a60065b | 92 | { |
kadonotakashi | 0:8fdf9a60065b | 93 | SocketAddress tcp_addr; |
kadonotakashi | 0:8fdf9a60065b | 94 | |
kadonotakashi | 0:8fdf9a60065b | 95 | get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr); |
kadonotakashi | 0:8fdf9a60065b | 96 | tcp_addr.set_port(9); |
kadonotakashi | 0:8fdf9a60065b | 97 | |
kadonotakashi | 0:8fdf9a60065b | 98 | nsapi_error_t err = sock.open(get_interface()); |
kadonotakashi | 0:8fdf9a60065b | 99 | if (err != NSAPI_ERROR_OK) { |
kadonotakashi | 0:8fdf9a60065b | 100 | return err; |
kadonotakashi | 0:8fdf9a60065b | 101 | } |
kadonotakashi | 0:8fdf9a60065b | 102 | |
kadonotakashi | 0:8fdf9a60065b | 103 | return sock.connect(tcp_addr); |
kadonotakashi | 0:8fdf9a60065b | 104 | } |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | void fill_tx_buffer_ascii(char *buff, size_t len) |
kadonotakashi | 0:8fdf9a60065b | 107 | { |
kadonotakashi | 0:8fdf9a60065b | 108 | for (size_t i = 0; i < len; ++i) { |
kadonotakashi | 0:8fdf9a60065b | 109 | buff[i] = (rand() % 43) + '0'; |
kadonotakashi | 0:8fdf9a60065b | 110 | } |
kadonotakashi | 0:8fdf9a60065b | 111 | } |
kadonotakashi | 0:8fdf9a60065b | 112 | |
kadonotakashi | 0:8fdf9a60065b | 113 | int split2half_rmng_tcp_test_time() |
kadonotakashi | 0:8fdf9a60065b | 114 | { |
kadonotakashi | 0:8fdf9a60065b | 115 | return (tcp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2; |
kadonotakashi | 0:8fdf9a60065b | 116 | } |
kadonotakashi | 0:8fdf9a60065b | 117 | |
kadonotakashi | 0:8fdf9a60065b | 118 | // Test setup |
kadonotakashi | 0:8fdf9a60065b | 119 | utest::v1::status_t greentea_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 120 | { |
kadonotakashi | 0:8fdf9a60065b | 121 | GREENTEA_SETUP(tcp_global::TESTS_TIMEOUT, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 122 | _ifup(); |
kadonotakashi | 0:8fdf9a60065b | 123 | tc_bucket.start(); |
kadonotakashi | 0:8fdf9a60065b | 124 | return greentea_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 125 | } |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure) |
kadonotakashi | 0:8fdf9a60065b | 128 | { |
kadonotakashi | 0:8fdf9a60065b | 129 | tc_bucket.stop(); |
kadonotakashi | 0:8fdf9a60065b | 130 | _ifdown(); |
kadonotakashi | 0:8fdf9a60065b | 131 | return greentea_test_teardown_handler(passed, failed, failure); |
kadonotakashi | 0:8fdf9a60065b | 132 | } |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | |
kadonotakashi | 0:8fdf9a60065b | 135 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 136 | Case("TCPSOCKET_ECHOTEST", TCPSOCKET_ECHOTEST), |
kadonotakashi | 0:8fdf9a60065b | 137 | Case("TCPSOCKET_ECHOTEST_NONBLOCK", TCPSOCKET_ECHOTEST_NONBLOCK), |
kadonotakashi | 0:8fdf9a60065b | 138 | Case("TCPSOCKET_OPEN_CLOSE_REPEAT", TCPSOCKET_OPEN_CLOSE_REPEAT), |
kadonotakashi | 0:8fdf9a60065b | 139 | Case("TCPSOCKET_OPEN_LIMIT", TCPSOCKET_OPEN_LIMIT), |
kadonotakashi | 0:8fdf9a60065b | 140 | Case("TCPSOCKET_THREAD_PER_SOCKET_SAFETY", TCPSOCKET_THREAD_PER_SOCKET_SAFETY), |
kadonotakashi | 0:8fdf9a60065b | 141 | #ifdef MBED_EXTENDED_TESTS |
kadonotakashi | 0:8fdf9a60065b | 142 | Case("TCPSOCKET_CONNECT_INVALID", TCPSOCKET_CONNECT_INVALID), |
kadonotakashi | 0:8fdf9a60065b | 143 | Case("TCPSOCKET_ECHOTEST_BURST", TCPSOCKET_ECHOTEST_BURST), |
kadonotakashi | 0:8fdf9a60065b | 144 | Case("TCPSOCKET_ECHOTEST_BURST_NONBLOCK", TCPSOCKET_ECHOTEST_BURST_NONBLOCK), |
kadonotakashi | 0:8fdf9a60065b | 145 | Case("TCPSOCKET_RECV_100K", TCPSOCKET_RECV_100K), |
kadonotakashi | 0:8fdf9a60065b | 146 | Case("TCPSOCKET_RECV_100K_NONBLOCK", TCPSOCKET_RECV_100K_NONBLOCK), |
kadonotakashi | 0:8fdf9a60065b | 147 | Case("TCPSOCKET_RECV_TIMEOUT", TCPSOCKET_RECV_TIMEOUT), |
kadonotakashi | 0:8fdf9a60065b | 148 | Case("TCPSOCKET_SEND_REPEAT", TCPSOCKET_SEND_REPEAT), |
kadonotakashi | 0:8fdf9a60065b | 149 | Case("TCPSOCKET_SEND_TIMEOUT", TCPSOCKET_SEND_TIMEOUT), |
kadonotakashi | 0:8fdf9a60065b | 150 | Case("TCPSOCKET_ENDPOINT_CLOSE", TCPSOCKET_ENDPOINT_CLOSE), |
kadonotakashi | 0:8fdf9a60065b | 151 | #endif |
kadonotakashi | 0:8fdf9a60065b | 152 | }; |
kadonotakashi | 0:8fdf9a60065b | 153 | |
kadonotakashi | 0:8fdf9a60065b | 154 | Specification specification(greentea_setup, cases, greentea_teardown); |
kadonotakashi | 0:8fdf9a60065b | 155 | |
kadonotakashi | 0:8fdf9a60065b | 156 | int main() |
kadonotakashi | 0:8fdf9a60065b | 157 | { |
kadonotakashi | 0:8fdf9a60065b | 158 | return !Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 159 | } |