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