takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpsocket_echotest_burst.cpp Source File

tcpsocket_echotest_burst.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 BURST_CNT = 100;
00032 static const int BURST_SIZE = 1220;
00033 }
00034 
00035 static void _sigio_handler(osThreadId id)
00036 {
00037     osSignalSet(id, SIGNAL_SIGIO);
00038 }
00039 
00040 void TCPSOCKET_ECHOTEST_BURST()
00041 {
00042     TCPSocket sock;
00043     tcpsocket_connect_to_echo_srv(sock);
00044     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00045 
00046     // TX buffer to be preserved for comparison
00047     fill_tx_buffer_ascii(tcp_global::tx_buffer, BURST_SIZE);
00048 
00049     int recvd;
00050     int bt_left;
00051     int sent;
00052     for (int i = 0; i < BURST_CNT; i++) {
00053         bt_left = BURST_SIZE;
00054         while (bt_left > 0) {
00055             sent = sock.send(&(tcp_global::tx_buffer[BURST_SIZE - bt_left]), bt_left);
00056             if (sent == NSAPI_ERROR_WOULD_BLOCK ) {
00057                 if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
00058                     TEST_FAIL();
00059                     goto END;
00060                 }
00061                 continue;
00062             } else if (sent < 0) {
00063                 printf("[%02d] network error %d\n", i, sent);
00064                 TEST_FAIL();
00065                 goto END;
00066             }
00067             bt_left -= sent;
00068         }
00069 
00070         bt_left = BURST_SIZE;
00071         while (bt_left > 0) {
00072             recvd = sock.recv(&(tcp_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE);
00073             if (recvd < 0) {
00074                 printf("[%02d] network error %d\n", i, recvd);
00075                 break;
00076             }
00077             bt_left -= recvd;
00078         }
00079 
00080         if (bt_left != 0) {
00081             drop_bad_packets(sock, 0);
00082             TEST_FAIL();
00083             goto END;
00084         }
00085 
00086         TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
00087     }
00088 END:
00089     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00090 }
00091 
00092 void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
00093 {
00094     TCPSocket sock;
00095     tcpsocket_connect_to_echo_srv(sock);
00096     sock.set_blocking(false);
00097     sock.sigio(callback(_sigio_handler, Thread::gettid()));
00098 
00099     // TX buffer to be preserved for comparison
00100     fill_tx_buffer_ascii(tcp_global::tx_buffer, BURST_SIZE);
00101 
00102     int sent;
00103     int recvd;
00104     int bt_left = 0;
00105     for (int i = 0; i < BURST_CNT; i++) {
00106         bt_left = BURST_SIZE;
00107         while (bt_left > 0) {
00108             sent = sock.send(&(tcp_global::tx_buffer[BURST_SIZE - bt_left]), bt_left);
00109             if (sent == NSAPI_ERROR_WOULD_BLOCK ) {
00110                 if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
00111                     TEST_FAIL();
00112                     goto END;
00113                 }
00114                 continue;
00115             } else if (sent < 0) {
00116                 printf("[%02d] network error %d\n", i, sent);
00117                 TEST_FAIL();
00118                 goto END;
00119             }
00120             bt_left -= sent;
00121         }
00122         if (bt_left != 0) {
00123             TEST_FAIL();
00124             goto END;
00125         }
00126 
00127         bt_left = BURST_SIZE;
00128         while (bt_left > 0) {
00129             recvd = sock.recv(&(tcp_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE);
00130             if (recvd == NSAPI_ERROR_WOULD_BLOCK ) {
00131                 if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
00132                     printf("[bt#%02d] packet timeout...", i);
00133                     break;
00134                 }
00135                 continue;
00136             } else if (recvd < 0) {
00137                 printf("[%02d] network error %d\n", i, recvd);
00138                 break;
00139             }
00140             bt_left -= recvd;
00141         }
00142 
00143         if (bt_left != 0) {
00144             printf("network error %d, missing %d bytes from a burst\n", recvd, bt_left);
00145             drop_bad_packets(sock, -1);
00146             TEST_FAIL();
00147             goto END;
00148         }
00149 
00150         TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
00151     }
00152 END:
00153     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , sock.close());
00154 }