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_recv_100k.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 = 20000; //[ms] 00032 } 00033 00034 static void _tcpsocket_connect_to_chargen_srv(TCPSocket& sock) { 00035 SocketAddress tcp_addr; 00036 00037 get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr); 00038 tcp_addr.set_port(19); 00039 00040 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.open(get_interface())); 00041 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.connect(tcp_addr)); 00042 } 00043 00044 /** Generate RFC 864 example pattern. 00045 * 00046 * Pattern is 72 character lines of the ASCII printing characters ending with "\r\n". 00047 * There are 95 printing characters in the ASCII character set. 00048 * Example: `nc echo.mbedcloudtesting.com 19 | dd bs=1 count=222` 00049 * !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg 00050 * !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh 00051 * "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghi 00052 * 00053 * NOTE: Pattern starts with space, not ! 00054 * 00055 * \param offset Start pattern from offset 00056 * \param len Length of pattern to generate. 00057 */ 00058 static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len) 00059 { 00060 while (len--) { 00061 if (offset % 74 == 72) 00062 *buf++ = '\r'; 00063 else if (offset % 74 == 73) 00064 *buf++ = '\n'; 00065 else 00066 *buf++ = ' ' + (offset%74 + offset/74) % 95 ; 00067 offset++; 00068 } 00069 } 00070 00071 static void check_RFC_864_pattern(void *rx_buff, const size_t len, const size_t offset) 00072 { 00073 void *ref_buff = malloc(len); 00074 TEST_ASSERT_NOT_NULL(ref_buff); 00075 00076 generate_RFC_864_pattern(offset, (uint8_t*)ref_buff, len); 00077 bool match = memcmp(ref_buff, rx_buff, len) == 0; 00078 00079 free(ref_buff); 00080 TEST_ASSERT(match); 00081 } 00082 00083 void rcv_n_chk_against_rfc864_pattern(TCPSocket& sock) { 00084 static const size_t total_size = 1024 * 100; 00085 static const size_t buff_size = 1220; 00086 uint8_t buff[buff_size]; 00087 size_t recvd_size = 0; 00088 00089 // Verify received data 00090 while (recvd_size < total_size) { 00091 int rd = sock.recv(buff, buff_size); 00092 TEST_ASSERT(rd > 0); 00093 check_RFC_864_pattern(buff, rd, recvd_size); 00094 recvd_size += rd; 00095 } 00096 } 00097 00098 void TCPSOCKET_RECV_100K() 00099 { 00100 TCPSocket sock; 00101 _tcpsocket_connect_to_chargen_srv(sock); 00102 00103 Timer timer; 00104 timer.start(); 00105 rcv_n_chk_against_rfc864_pattern(sock); 00106 timer.stop(); 00107 00108 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close()); 00109 00110 printf("MBED: Time taken: %fs\n", timer.read()); 00111 } 00112 00113 void rcv_n_chk_against_rfc864_pattern_nonblock(TCPSocket& sock) { 00114 static const size_t total_size = 1024 * 100; 00115 static const size_t buff_size = 1220; 00116 uint8_t buff[buff_size]; 00117 size_t recvd_size = 0; 00118 00119 // Verify received data 00120 while (recvd_size < total_size) { 00121 int rd = sock.recv(buff, buff_size); 00122 TEST_ASSERT(rd > 0 || rd == NSAPI_ERROR_WOULD_BLOCK ); 00123 if (rd > 0) { 00124 check_RFC_864_pattern(buff, rd, recvd_size); 00125 recvd_size += rd; 00126 } else if (rd == NSAPI_ERROR_WOULD_BLOCK ) { 00127 TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status); 00128 } 00129 } 00130 } 00131 00132 static void _sigio_handler(osThreadId id) { 00133 osSignalSet(id, SIGNAL_SIGIO); 00134 } 00135 00136 void TCPSOCKET_RECV_100K_NONBLOCK() 00137 { 00138 TCPSocket sock; 00139 _tcpsocket_connect_to_chargen_srv(sock); 00140 sock.set_blocking(false); 00141 sock.sigio(callback(_sigio_handler, Thread::gettid())); 00142 00143 Timer timer; 00144 timer.start(); 00145 rcv_n_chk_against_rfc864_pattern_nonblock(sock); 00146 timer.stop(); 00147 00148 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close()); 00149 00150 printf("MBED: Time taken: %fs\n", timer.read()); 00151 }
Generated on Tue Jul 12 2022 12:45:50 by
