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_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 00032 #define MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 256 00033 #endif 00034 00035 namespace { 00036 char tx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; 00037 char rx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; 00038 const char ASCII_MAX = '~' - ' '; 00039 } 00040 00041 void prep_buffer(char *tx_buffer, size_t tx_size) { 00042 for (size_t i=0; i<tx_size; ++i) { 00043 tx_buffer[i] = (rand() % 10) + '0'; 00044 } 00045 } 00046 00047 void test_tcp_echo() { 00048 00049 NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION; 00050 int err = MBED_CONF_APP_CONNECT_STATEMENT; 00051 00052 if (err) { 00053 printf("MBED: failed to connect with an error of %d\r\n", err); 00054 TEST_ASSERT_EQUAL(0, err); 00055 } 00056 00057 printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address()); 00058 00059 bool result = false; 00060 00061 TCPSocket sock(net); 00062 00063 SocketAddress tcp_addr(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT); 00064 00065 if (sock.connect(tcp_addr) == 0) { 00066 printf("HTTP: Connected to %s:%d\r\n", MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT); 00067 printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer)); 00068 printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer)); 00069 00070 prep_buffer(tx_buffer, sizeof(tx_buffer)); 00071 sock.recv(rx_buffer, sizeof(MBED_CONF_APP_TCP_ECHO_PREFIX)); 00072 const int ret = sock.send(tx_buffer, sizeof(tx_buffer)); 00073 if (ret >= 0) { 00074 printf("sent %d bytes - %.*s \n", ret, ret, tx_buffer); 00075 } else { 00076 printf("Network error %d\n", ret); 00077 } 00078 00079 int n = sock.recv(rx_buffer, sizeof(rx_buffer)); 00080 if (n >= 0) { 00081 printf("recv %d bytes - %.*s \n", n, n, rx_buffer); 00082 } else { 00083 printf("Network error %d\n", n); 00084 } 00085 00086 result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer)); 00087 TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer)); 00088 TEST_ASSERT_EQUAL(true, result); 00089 } 00090 00091 sock.close(); 00092 net->disconnect(); 00093 TEST_ASSERT_EQUAL(true, result); 00094 } 00095 00096 00097 // Test setup 00098 utest::v1::status_t test_setup(const size_t number_of_cases) { 00099 char uuid[48] = {0}; 00100 GREENTEA_SETUP(240, "tcp_echo"); 00101 00102 // create mac address based on uuid 00103 uint64_t mac = 0; 00104 for (int i = 0; i < sizeof(uuid); i++) { 00105 mac += uuid[i]; 00106 } 00107 //mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1); 00108 00109 return verbose_test_setup_handler(number_of_cases); 00110 } 00111 00112 Case cases[] = { 00113 Case("TCP echo", test_tcp_echo), 00114 }; 00115 00116 Specification specification(test_setup, cases); 00117 00118 int main() { 00119 return !Harness::run(specification); 00120 }
Generated on Thu Jul 14 2022 14:36:19 by
