takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/netsocket/tcp/tcpsocket_echotest.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2018, ARM Limited, All Rights Reserved |
kadonotakashi | 0:8fdf9a60065b | 3 | * SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kadonotakashi | 0:8fdf9a60065b | 6 | * not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | * |
kadonotakashi | 0:8fdf9a60065b | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kadonotakashi | 0:8fdf9a60065b | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | */ |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "TCPSocket.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "unity/unity.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "utest.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "tcp_tests.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | |
kadonotakashi | 0:8fdf9a60065b | 25 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 26 | |
kadonotakashi | 0:8fdf9a60065b | 27 | namespace { |
kadonotakashi | 0:8fdf9a60065b | 28 | static const int SIGNAL_SIGIO = 0x1; |
kadonotakashi | 0:8fdf9a60065b | 29 | static const int SIGIO_TIMEOUT = 5000; //[ms] |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | static const int BUFF_SIZE = 1200; |
kadonotakashi | 0:8fdf9a60065b | 32 | static const int PKTS = 22; |
kadonotakashi | 0:8fdf9a60065b | 33 | static const int pkt_sizes[PKTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \ |
kadonotakashi | 0:8fdf9a60065b | 34 | 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, \ |
kadonotakashi | 0:8fdf9a60065b | 35 | 1100, 1200 |
kadonotakashi | 0:8fdf9a60065b | 36 | }; |
kadonotakashi | 0:8fdf9a60065b | 37 | TCPSocket sock; |
kadonotakashi | 0:8fdf9a60065b | 38 | Semaphore tx_sem(0, 1); |
kadonotakashi | 0:8fdf9a60065b | 39 | |
kadonotakashi | 0:8fdf9a60065b | 40 | Timer tc_exec_time; |
kadonotakashi | 0:8fdf9a60065b | 41 | int time_allotted; |
kadonotakashi | 0:8fdf9a60065b | 42 | } |
kadonotakashi | 0:8fdf9a60065b | 43 | |
kadonotakashi | 0:8fdf9a60065b | 44 | static void _sigio_handler(osThreadId id) |
kadonotakashi | 0:8fdf9a60065b | 45 | { |
kadonotakashi | 0:8fdf9a60065b | 46 | osSignalSet(id, SIGNAL_SIGIO); |
kadonotakashi | 0:8fdf9a60065b | 47 | } |
kadonotakashi | 0:8fdf9a60065b | 48 | |
kadonotakashi | 0:8fdf9a60065b | 49 | void TCPSOCKET_ECHOTEST() |
kadonotakashi | 0:8fdf9a60065b | 50 | { |
kadonotakashi | 0:8fdf9a60065b | 51 | if (tcpsocket_connect_to_echo_srv(sock) != NSAPI_ERROR_OK) { |
kadonotakashi | 0:8fdf9a60065b | 52 | TEST_FAIL(); |
kadonotakashi | 0:8fdf9a60065b | 53 | return; |
kadonotakashi | 0:8fdf9a60065b | 54 | } |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | int recvd; |
kadonotakashi | 0:8fdf9a60065b | 57 | int sent; |
kadonotakashi | 0:8fdf9a60065b | 58 | int x = 0; |
kadonotakashi | 0:8fdf9a60065b | 59 | for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) { |
kadonotakashi | 0:8fdf9a60065b | 60 | fill_tx_buffer_ascii(tcp_global::tx_buffer, BUFF_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 61 | |
kadonotakashi | 0:8fdf9a60065b | 62 | sent = sock.send(tcp_global::tx_buffer, pkt_s); |
kadonotakashi | 0:8fdf9a60065b | 63 | if (sent < 0) { |
kadonotakashi | 0:8fdf9a60065b | 64 | printf("[Round#%02d] network error %d\n", x, sent); |
kadonotakashi | 0:8fdf9a60065b | 65 | TEST_FAIL(); |
kadonotakashi | 0:8fdf9a60065b | 66 | TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); |
kadonotakashi | 0:8fdf9a60065b | 67 | return; |
kadonotakashi | 0:8fdf9a60065b | 68 | } |
kadonotakashi | 0:8fdf9a60065b | 69 | |
kadonotakashi | 0:8fdf9a60065b | 70 | int bytes2recv = sent; |
kadonotakashi | 0:8fdf9a60065b | 71 | while (bytes2recv) { |
kadonotakashi | 0:8fdf9a60065b | 72 | recvd = sock.recv(&(tcp_global::rx_buffer[sent - bytes2recv]), bytes2recv); |
kadonotakashi | 0:8fdf9a60065b | 73 | if (recvd < 0) { |
kadonotakashi | 0:8fdf9a60065b | 74 | printf("[Round#%02d] network error %d\n", x, recvd); |
kadonotakashi | 0:8fdf9a60065b | 75 | TEST_FAIL(); |
kadonotakashi | 0:8fdf9a60065b | 76 | TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); |
kadonotakashi | 0:8fdf9a60065b | 77 | return; |
kadonotakashi | 0:8fdf9a60065b | 78 | } |
kadonotakashi | 0:8fdf9a60065b | 79 | bytes2recv -= recvd; |
kadonotakashi | 0:8fdf9a60065b | 80 | } |
kadonotakashi | 0:8fdf9a60065b | 81 | TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, sent)); |
kadonotakashi | 0:8fdf9a60065b | 82 | } |
kadonotakashi | 0:8fdf9a60065b | 83 | TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); |
kadonotakashi | 0:8fdf9a60065b | 84 | } |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | void tcpsocket_echotest_nonblock_receiver(void *receive_bytes) |
kadonotakashi | 0:8fdf9a60065b | 87 | { |
kadonotakashi | 0:8fdf9a60065b | 88 | int bytes2recv = *(int *)receive_bytes; |
kadonotakashi | 0:8fdf9a60065b | 89 | int recvd; |
kadonotakashi | 0:8fdf9a60065b | 90 | while (bytes2recv) { |
kadonotakashi | 0:8fdf9a60065b | 91 | recvd = sock.recv(&(tcp_global::rx_buffer[*(int *)receive_bytes - bytes2recv]), bytes2recv); |
kadonotakashi | 0:8fdf9a60065b | 92 | if (recvd == NSAPI_ERROR_WOULD_BLOCK) { |
kadonotakashi | 0:8fdf9a60065b | 93 | if (tc_exec_time.read() >= time_allotted) { |
kadonotakashi | 0:8fdf9a60065b | 94 | TEST_FAIL(); |
kadonotakashi | 0:8fdf9a60065b | 95 | break; |
kadonotakashi | 0:8fdf9a60065b | 96 | } |
kadonotakashi | 0:8fdf9a60065b | 97 | wait(1); |
kadonotakashi | 0:8fdf9a60065b | 98 | continue; |
kadonotakashi | 0:8fdf9a60065b | 99 | } else if (recvd < 0) { |
kadonotakashi | 0:8fdf9a60065b | 100 | TEST_FAIL(); |
kadonotakashi | 0:8fdf9a60065b | 101 | break; |
kadonotakashi | 0:8fdf9a60065b | 102 | } |
kadonotakashi | 0:8fdf9a60065b | 103 | bytes2recv -= recvd; |
kadonotakashi | 0:8fdf9a60065b | 104 | } |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, *(int *)receive_bytes)); |
kadonotakashi | 0:8fdf9a60065b | 107 | |
kadonotakashi | 0:8fdf9a60065b | 108 | static int round = 0; |
kadonotakashi | 0:8fdf9a60065b | 109 | printf("[Recevr#%02d] bytes received: %d\n", round++, *(int *)receive_bytes); |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | tx_sem.release(); |
kadonotakashi | 0:8fdf9a60065b | 112 | |
kadonotakashi | 0:8fdf9a60065b | 113 | } |
kadonotakashi | 0:8fdf9a60065b | 114 | |
kadonotakashi | 0:8fdf9a60065b | 115 | void TCPSOCKET_ECHOTEST_NONBLOCK() |
kadonotakashi | 0:8fdf9a60065b | 116 | { |
kadonotakashi | 0:8fdf9a60065b | 117 | tc_exec_time.start(); |
kadonotakashi | 0:8fdf9a60065b | 118 | time_allotted = split2half_rmng_tcp_test_time(); // [s] |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | tcpsocket_connect_to_echo_srv(sock); |
kadonotakashi | 0:8fdf9a60065b | 121 | sock.set_blocking(false); |
kadonotakashi | 0:8fdf9a60065b | 122 | sock.sigio(callback(_sigio_handler, Thread::gettid())); |
kadonotakashi | 0:8fdf9a60065b | 123 | |
kadonotakashi | 0:8fdf9a60065b | 124 | int bytes2send; |
kadonotakashi | 0:8fdf9a60065b | 125 | int sent; |
kadonotakashi | 0:8fdf9a60065b | 126 | int s_idx = 0; |
kadonotakashi | 0:8fdf9a60065b | 127 | Thread *thread; |
kadonotakashi | 0:8fdf9a60065b | 128 | unsigned char *stack_mem = (unsigned char *)malloc(tcp_global::TCP_OS_STACK_SIZE); |
kadonotakashi | 0:8fdf9a60065b | 129 | TEST_ASSERT_NOT_NULL(stack_mem); |
kadonotakashi | 0:8fdf9a60065b | 130 | |
kadonotakashi | 0:8fdf9a60065b | 131 | for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) { |
kadonotakashi | 0:8fdf9a60065b | 132 | pkt_s = pkt_sizes[s_idx]; |
kadonotakashi | 0:8fdf9a60065b | 133 | thread = new Thread(osPriorityNormal, |
kadonotakashi | 0:8fdf9a60065b | 134 | tcp_global::TCP_OS_STACK_SIZE, |
kadonotakashi | 0:8fdf9a60065b | 135 | stack_mem, |
kadonotakashi | 0:8fdf9a60065b | 136 | "receiver"); |
kadonotakashi | 0:8fdf9a60065b | 137 | TEST_ASSERT_EQUAL(osOK, thread->start(callback(tcpsocket_echotest_nonblock_receiver, &pkt_s))); |
kadonotakashi | 0:8fdf9a60065b | 138 | |
kadonotakashi | 0:8fdf9a60065b | 139 | fill_tx_buffer_ascii(tcp_global::tx_buffer, pkt_s); |
kadonotakashi | 0:8fdf9a60065b | 140 | |
kadonotakashi | 0:8fdf9a60065b | 141 | bytes2send = pkt_s; |
kadonotakashi | 0:8fdf9a60065b | 142 | while (bytes2send > 0) { |
kadonotakashi | 0:8fdf9a60065b | 143 | sent = sock.send(&(tcp_global::tx_buffer[pkt_s - bytes2send]), bytes2send); |
kadonotakashi | 0:8fdf9a60065b | 144 | if (sent == NSAPI_ERROR_WOULD_BLOCK) { |
kadonotakashi | 0:8fdf9a60065b | 145 | if (tc_exec_time.read() >= time_allotted || |
kadonotakashi | 0:8fdf9a60065b | 146 | osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { |
kadonotakashi | 0:8fdf9a60065b | 147 | thread->terminate(); |
kadonotakashi | 0:8fdf9a60065b | 148 | delete thread; |
kadonotakashi | 0:8fdf9a60065b | 149 | TEST_FAIL(); |
kadonotakashi | 0:8fdf9a60065b | 150 | goto END; |
kadonotakashi | 0:8fdf9a60065b | 151 | } |
kadonotakashi | 0:8fdf9a60065b | 152 | continue; |
kadonotakashi | 0:8fdf9a60065b | 153 | } else if (sent <= 0) { |
kadonotakashi | 0:8fdf9a60065b | 154 | printf("[Sender#%02d] network error %d\n", s_idx, sent); |
kadonotakashi | 0:8fdf9a60065b | 155 | thread->terminate(); |
kadonotakashi | 0:8fdf9a60065b | 156 | delete thread; |
kadonotakashi | 0:8fdf9a60065b | 157 | TEST_FAIL(); |
kadonotakashi | 0:8fdf9a60065b | 158 | goto END; |
kadonotakashi | 0:8fdf9a60065b | 159 | } |
kadonotakashi | 0:8fdf9a60065b | 160 | bytes2send -= sent; |
kadonotakashi | 0:8fdf9a60065b | 161 | } |
kadonotakashi | 0:8fdf9a60065b | 162 | printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s); |
kadonotakashi | 0:8fdf9a60065b | 163 | tx_sem.wait(split2half_rmng_tcp_test_time()); |
kadonotakashi | 0:8fdf9a60065b | 164 | thread->join(); |
kadonotakashi | 0:8fdf9a60065b | 165 | delete thread; |
kadonotakashi | 0:8fdf9a60065b | 166 | } |
kadonotakashi | 0:8fdf9a60065b | 167 | END: |
kadonotakashi | 0:8fdf9a60065b | 168 | tc_exec_time.stop(); |
kadonotakashi | 0:8fdf9a60065b | 169 | free(stack_mem); |
kadonotakashi | 0:8fdf9a60065b | 170 | TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); |
kadonotakashi | 0:8fdf9a60065b | 171 | } |