Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 * Copyright (c) 2013-2017, 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 "TCPSocket.h" 00025 #include "greentea-client/test_env.h" 00026 #include "unity/unity.h" 00027 #include "utest.h" 00028 00029 using namespace utest::v1; 00030 00031 #ifndef MBED_CONF_APP_TCP_CLIENT_ECHO_BUFFER_SIZE 00032 #define MBED_CONF_APP_TCP_CLIENT_ECHO_BUFFER_SIZE 256 00033 #endif 00034 00035 namespace 00036 { 00037 char tx_buffer[MBED_CONF_APP_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; 00038 char rx_buffer[MBED_CONF_APP_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; 00039 } 00040 00041 void prep_buffer(char *tx_buffer, size_t tx_size) 00042 { 00043 for (size_t i = 0; i < tx_size; ++i) { 00044 tx_buffer[i] = (rand() % 10) + '0'; 00045 } 00046 } 00047 00048 void test_tcp_echo() 00049 { 00050 int n = 0; 00051 NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION; 00052 int err = MBED_CONF_APP_CONNECT_STATEMENT; 00053 00054 if (err) { 00055 printf("MBED: failed to connect with an error of %d\r\n", err); 00056 TEST_ASSERT_EQUAL(0, err); 00057 } 00058 00059 printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address()); 00060 00061 bool result = false; 00062 00063 TCPSocket sock(net); 00064 00065 #if defined(MBED_CONF_APP_ECHO_SERVER_ADDR) && defined(MBED_CONF_APP_ECHO_SERVER_PORT) 00066 printf("TCP: Connect to %s:%d\r\n", MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT); 00067 SocketAddress tcp_addr(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT); 00068 #else /* MBED_CONF_APP_ECHO_SERVER_ADDR && MBED_CONF_APP_ECHO_SERVER_PORT */ 00069 char recv_key[] = "host_port"; 00070 char ipbuf[60] = {0}; 00071 char portbuf[16] = {0}; 00072 unsigned int port = 0; 00073 00074 greentea_send_kv("target_ip", net->get_ip_address()); 00075 greentea_send_kv("host_ip", " "); 00076 greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); 00077 00078 greentea_send_kv("host_port", " "); 00079 greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); 00080 sscanf(portbuf, "%u", &port); 00081 00082 printf("TCP: Connect to %s:%d\r\n", ipbuf, port); 00083 SocketAddress tcp_addr(ipbuf, port); 00084 #endif /* MBED_CONF_APP_ECHO_SERVER_ADDR && MBED_CONF_APP_ECHO_SERVER_PORT */ 00085 00086 if (sock.connect(tcp_addr) == 0) { 00087 printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer)); 00088 printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer)); 00089 00090 prep_buffer(tx_buffer, sizeof(tx_buffer)); 00091 #if defined(MBED_CONF_APP_TCP_ECHO_PREFIX) 00092 n = sock.recv(rx_buffer, sizeof(MBED_CONF_APP_TCP_ECHO_PREFIX)); 00093 if (n >= 0) { 00094 printf("recv-ed prefix: %d bytes - %.*s \n", n, n, rx_buffer); 00095 } else { 00096 printf("Network error in receiving prefix: %d\n", n); 00097 } 00098 #endif /* MBED_CONF_APP_TCP_ECHO_PREFIX */ 00099 const int ret = sock.send(tx_buffer, sizeof(tx_buffer)); 00100 if (ret >= 0) { 00101 printf("sent %d bytes - %.*s \n", ret, ret, tx_buffer); 00102 } else { 00103 printf("Network error %d\n", ret); 00104 } 00105 00106 n = sock.recv(rx_buffer, sizeof(rx_buffer)); 00107 if (n >= 0) { 00108 printf("recv %d bytes - %.*s \n", n, n, rx_buffer); 00109 } else { 00110 printf("Network error %d\n", n); 00111 } 00112 00113 result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer)); 00114 TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer)); 00115 TEST_ASSERT_EQUAL(true, result); 00116 } 00117 00118 sock.close(); 00119 net->disconnect(); 00120 TEST_ASSERT_EQUAL(true, result); 00121 } 00122 00123 00124 // Test setup 00125 utest::v1::status_t test_setup(const size_t number_of_cases) 00126 { 00127 GREENTEA_SETUP(240, "tcp_echo"); 00128 return verbose_test_setup_handler(number_of_cases); 00129 } 00130 00131 Case cases[] = { 00132 Case("TCP echo", test_tcp_echo), 00133 }; 00134 00135 Specification specification(test_setup, cases); 00136 00137 int main() 00138 { 00139 return !Harness::run(specification); 00140 }
Generated on Tue Jul 12 2022 12:22:09 by
1.7.2