takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpsocket_recv_100k.cpp Source File

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