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