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.
tcpsocket_echotest.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 #include "mbed.h" 00019 #include "TCPSocket.h" 00020 #include "greentea-client/test_env.h" 00021 #include "unity/unity.h" 00022 #include "utest.h" 00023 #include "tcp_tests.h" 00024 00025 using namespace utest::v1; 00026 00027 namespace { 00028 static const int SIGNAL_SIGIO = 0x1; 00029 static const int SIGIO_TIMEOUT = 5000; //[ms] 00030 00031 static const int BUFF_SIZE = 1200; 00032 static const int PKTS = 22; 00033 static const int pkt_sizes[PKTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \ 00034 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, \ 00035 1100, 1200 00036 }; 00037 TCPSocket sock; 00038 Semaphore tx_sem(0, 1); 00039 00040 Timer tc_exec_time; 00041 int time_allotted; 00042 } 00043 00044 static void _sigio_handler(osThreadId id) 00045 { 00046 osSignalSet(id, SIGNAL_SIGIO); 00047 } 00048 00049 void TCPSOCKET_ECHOTEST() 00050 { 00051 if (tcpsocket_connect_to_echo_srv(sock) != NSAPI_ERROR_OK ) { 00052 TEST_FAIL(); 00053 return; 00054 } 00055 00056 int recvd; 00057 int sent; 00058 int x = 0; 00059 for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) { 00060 fill_tx_buffer_ascii(tcp_global::tx_buffer, BUFF_SIZE); 00061 00062 sent = sock.send(tcp_global::tx_buffer, pkt_s); 00063 if (sent < 0) { 00064 printf("[Round#%02d] network error %d\n", x, sent); 00065 TEST_FAIL(); 00066 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close()); 00067 return; 00068 } 00069 00070 int bytes2recv = sent; 00071 while (bytes2recv) { 00072 recvd = sock.recv(&(tcp_global::rx_buffer[sent - bytes2recv]), bytes2recv); 00073 if (recvd < 0) { 00074 printf("[Round#%02d] network error %d\n", x, recvd); 00075 TEST_FAIL(); 00076 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close()); 00077 return; 00078 } 00079 bytes2recv -= recvd; 00080 } 00081 TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, sent)); 00082 } 00083 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close()); 00084 } 00085 00086 void tcpsocket_echotest_nonblock_receiver(void *receive_bytes) 00087 { 00088 int bytes2recv = *(int *)receive_bytes; 00089 int recvd; 00090 while (bytes2recv) { 00091 recvd = sock.recv(&(tcp_global::rx_buffer[*(int *)receive_bytes - bytes2recv]), bytes2recv); 00092 if (recvd == NSAPI_ERROR_WOULD_BLOCK ) { 00093 if (tc_exec_time.read() >= time_allotted) { 00094 TEST_FAIL(); 00095 break; 00096 } 00097 wait(1); 00098 continue; 00099 } else if (recvd < 0) { 00100 TEST_FAIL(); 00101 break; 00102 } 00103 bytes2recv -= recvd; 00104 } 00105 00106 TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, *(int *)receive_bytes)); 00107 00108 static int round = 0; 00109 printf("[Recevr#%02d] bytes received: %d\n", round++, *(int *)receive_bytes); 00110 00111 tx_sem.release(); 00112 00113 } 00114 00115 void TCPSOCKET_ECHOTEST_NONBLOCK() 00116 { 00117 tc_exec_time.start(); 00118 time_allotted = split2half_rmng_tcp_test_time(); // [s] 00119 00120 tcpsocket_connect_to_echo_srv(sock); 00121 sock.set_blocking(false); 00122 sock.sigio(callback(_sigio_handler, Thread::gettid())); 00123 00124 int bytes2send; 00125 int sent; 00126 int s_idx = 0; 00127 Thread *thread; 00128 unsigned char *stack_mem = (unsigned char *)malloc(tcp_global::TCP_OS_STACK_SIZE); 00129 TEST_ASSERT_NOT_NULL(stack_mem); 00130 00131 for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) { 00132 pkt_s = pkt_sizes[s_idx]; 00133 thread = new Thread(osPriorityNormal, 00134 tcp_global::TCP_OS_STACK_SIZE, 00135 stack_mem, 00136 "receiver"); 00137 TEST_ASSERT_EQUAL(osOK, thread->start(callback(tcpsocket_echotest_nonblock_receiver, &pkt_s))); 00138 00139 fill_tx_buffer_ascii(tcp_global::tx_buffer, pkt_s); 00140 00141 bytes2send = pkt_s; 00142 while (bytes2send > 0) { 00143 sent = sock.send(&(tcp_global::tx_buffer[pkt_s - bytes2send]), bytes2send); 00144 if (sent == NSAPI_ERROR_WOULD_BLOCK ) { 00145 if (tc_exec_time.read() >= time_allotted || 00146 osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { 00147 thread->terminate(); 00148 delete thread; 00149 TEST_FAIL(); 00150 goto END; 00151 } 00152 continue; 00153 } else if (sent <= 0) { 00154 printf("[Sender#%02d] network error %d\n", s_idx, sent); 00155 thread->terminate(); 00156 delete thread; 00157 TEST_FAIL(); 00158 goto END; 00159 } 00160 bytes2send -= sent; 00161 } 00162 printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s); 00163 tx_sem.wait(split2half_rmng_tcp_test_time()); 00164 thread->join(); 00165 delete thread; 00166 } 00167 END: 00168 tc_exec_time.stop(); 00169 free(stack_mem); 00170 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close()); 00171 }
Generated on Tue Aug 9 2022 00:37:21 by
1.7.2