Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
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 #ifndef MBED_EXTENDED_TESTS 00023 #error [NOT_SUPPORTED] Parallel tests are not supported by default 00024 #endif 00025 00026 #include "mbed.h" 00027 #include MBED_CONF_APP_HEADER_FILE 00028 #include "TCPSocket.h" 00029 #include "greentea-client/test_env.h" 00030 #include "unity/unity.h" 00031 #include "utest.h" 00032 00033 using namespace utest::v1; 00034 00035 00036 #ifndef MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 00037 #define MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 64 00038 #endif 00039 00040 #ifndef MBED_CFG_TCP_CLIENT_ECHO_THREADS 00041 #define MBED_CFG_TCP_CLIENT_ECHO_THREADS 3 00042 #endif 00043 00044 #define STRINGIZE(x) STRINGIZE2(x) 00045 #define STRINGIZE2(x) #x 00046 00047 00048 NetworkInterface* net; 00049 SocketAddress tcp_addr; 00050 Mutex iomutex; 00051 00052 void prep_buffer(char *tx_buffer, size_t tx_size) { 00053 for (size_t i=0; i<tx_size; ++i) { 00054 tx_buffer[i] = (rand() % 10) + '0'; 00055 } 00056 } 00057 00058 00059 // Each echo class is in charge of one parallel transaction 00060 class Echo { 00061 private: 00062 char tx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE]; 00063 char rx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE]; 00064 00065 TCPSocket sock; 00066 Thread thread; 00067 00068 public: 00069 // Limiting stack size to 1k 00070 Echo(): thread(osPriorityNormal, 1024) { 00071 } 00072 00073 void start() { 00074 osStatus status = thread.start(callback(this, &Echo::echo)); 00075 TEST_ASSERT_EQUAL(osOK, status); 00076 } 00077 00078 void join() { 00079 osStatus status = thread.join(); 00080 TEST_ASSERT_EQUAL(osOK, status); 00081 } 00082 00083 void echo() { 00084 int err = sock.open(net); 00085 TEST_ASSERT_EQUAL(0, err); 00086 00087 err = sock.connect(tcp_addr); 00088 TEST_ASSERT_EQUAL(0, err); 00089 00090 //recv connection prefix message 00091 sock.recv(rx_buffer, sizeof(MBED_CONF_APP_TCP_ECHO_PREFIX)); 00092 memset(rx_buffer, 0, sizeof(rx_buffer)); 00093 00094 iomutex.lock(); 00095 printf("HTTP: Connected to %s:%d\r\n", 00096 tcp_addr.get_ip_address(), tcp_addr.get_port()); 00097 printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer)); 00098 printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer)); 00099 iomutex.unlock(); 00100 00101 prep_buffer(tx_buffer, sizeof(tx_buffer)); 00102 sock.send(tx_buffer, sizeof(tx_buffer)); 00103 00104 // Server will respond with HTTP GET's success code 00105 const int ret = sock.recv(rx_buffer, sizeof(rx_buffer)); 00106 bool result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer)); 00107 TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer)); 00108 TEST_ASSERT_EQUAL(true, result); 00109 00110 err = sock.close(); 00111 TEST_ASSERT_EQUAL(0, err); 00112 } 00113 }; 00114 00115 Echo *echoers[MBED_CFG_TCP_CLIENT_ECHO_THREADS]; 00116 00117 00118 void test_tcp_echo_parallel() { 00119 net = MBED_CONF_APP_OBJECT_CONSTRUCTION; 00120 int err = MBED_CONF_APP_CONNECT_STATEMENT; 00121 TEST_ASSERT_EQUAL(0, err); 00122 00123 printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address()); 00124 00125 tcp_addr.set_ip_address(MBED_CONF_APP_ECHO_SERVER_ADDR); 00126 tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT); 00127 00128 // Startup echo threads in parallel 00129 for (int i = 0; i < MBED_CFG_TCP_CLIENT_ECHO_THREADS; i++) { 00130 echoers[i] = new Echo; 00131 echoers[i]->start(); 00132 } 00133 00134 for (int i = 0; i < MBED_CFG_TCP_CLIENT_ECHO_THREADS; i++) { 00135 echoers[i]->join(); 00136 delete echoers[i]; 00137 } 00138 00139 net->disconnect(); 00140 } 00141 00142 // Test setup 00143 utest::v1::status_t test_setup(const size_t number_of_cases) { 00144 GREENTEA_SETUP(120, "tcp_echo"); 00145 return verbose_test_setup_handler(number_of_cases); 00146 } 00147 00148 Case cases[] = { 00149 Case("TCP echo parallel", test_tcp_echo_parallel), 00150 }; 00151 00152 Specification specification(test_setup, cases); 00153 00154 int main() { 00155 return !Harness::run(specification); 00156 }
Generated on Sun Jul 17 2022 08:25:27 by 1.7.2