|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, ARM Limited, All Rights Reserved |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | + * not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "mbed.h" |
| 19 | +#include MBED_CONF_APP_HEADER_FILE |
| 20 | +#include "UDPSocket.h" |
| 21 | +#include "greentea-client/test_env.h" |
| 22 | +#include "unity/unity.h" |
| 23 | +#include "utest.h" |
| 24 | +#include "udp_tests.h" |
| 25 | + |
| 26 | +using namespace utest::v1; |
| 27 | + |
| 28 | +namespace |
| 29 | +{ |
| 30 | + static const int SIGNAL_SIGIO = 0x1; |
| 31 | + static const int SIGIO_TIMEOUT = 5000; //[ms] |
| 32 | + static const int WAIT2RECV_TIMEOUT = 1000; //[ms] |
| 33 | + static const int RETRIES = 2; |
| 34 | + |
| 35 | + UDPSocket sock; |
| 36 | + Semaphore tx_sem(0, 1); |
| 37 | + |
| 38 | + static const int BUFF_SIZE = 1200; |
| 39 | + char rx_buffer[BUFF_SIZE] = {0}; |
| 40 | + char tx_buffer[BUFF_SIZE] = {0}; |
| 41 | + |
| 42 | + static const int PKTS = 22; |
| 43 | + static const int pkt_sizes[PKTS] = {1,2,3,4,5,6,7,8,9,10, \ |
| 44 | + 100,200,300,400,500,600,700,800,900,1000,\ |
| 45 | + 1100,1200}; |
| 46 | +} |
| 47 | + |
| 48 | +static void _sigio_handler(osThreadId id) { |
| 49 | + osSignalSet(id, SIGNAL_SIGIO); |
| 50 | +} |
| 51 | + |
| 52 | +void test_udpsocket_echotest() |
| 53 | +{ |
| 54 | + SocketAddress udp_addr; |
| 55 | + get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr); |
| 56 | + udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT); |
| 57 | + |
| 58 | + UDPSocket sock; |
| 59 | + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface())); |
| 60 | + |
| 61 | + int recvd; |
| 62 | + int sent; |
| 63 | + int s_idx = 0; |
| 64 | + for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; pkt_s = ++s_idx) { |
| 65 | + pkt_s = pkt_sizes[s_idx]; |
| 66 | + |
| 67 | + fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE); |
| 68 | + |
| 69 | + for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) { |
| 70 | + sent = sock.sendto(udp_addr, tx_buffer, pkt_s); |
| 71 | + if (sent != pkt_s) { |
| 72 | + printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent); |
| 73 | + continue; |
| 74 | + } |
| 75 | + recvd = sock.recvfrom(NULL, rx_buffer, pkt_s); |
| 76 | + if (recvd == pkt_s) { |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, pkt_s)); |
| 81 | + } |
| 82 | + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); |
| 83 | +} |
| 84 | + |
| 85 | +void udpsocket_echotest_nonblock_receiver(void *receive_bytes) |
| 86 | +{ |
| 87 | + int expt2recv = *(int*)receive_bytes; |
| 88 | + int recvd; |
| 89 | + for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) { |
| 90 | + recvd = sock.recvfrom(NULL, rx_buffer, expt2recv); |
| 91 | + if (recvd == NSAPI_ERROR_WOULD_BLOCK) { |
| 92 | + wait_ms(WAIT2RECV_TIMEOUT); |
| 93 | + continue; |
| 94 | + } else if (recvd == expt2recv) { |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, expt2recv)); |
| 100 | + drop_bad_packets(sock); |
| 101 | + sock.set_blocking(false); |
| 102 | + |
| 103 | + tx_sem.release(); |
| 104 | +} |
| 105 | + |
| 106 | +void test_udpsocket_echotest_nonblock() |
| 107 | +{ |
| 108 | + SocketAddress udp_addr; |
| 109 | + get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr); |
| 110 | + udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT); |
| 111 | + |
| 112 | + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface())); |
| 113 | + sock.set_blocking(false); |
| 114 | + sock.sigio(callback(_sigio_handler, Thread::gettid())); |
| 115 | + |
| 116 | + int sent; |
| 117 | + int s_idx = 0; |
| 118 | + Thread *thread; |
| 119 | + unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE); |
| 120 | + TEST_ASSERT_NOT_NULL(stack_mem); |
| 121 | + |
| 122 | + for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) { |
| 123 | + pkt_s = pkt_sizes[s_idx]; |
| 124 | + |
| 125 | + thread = new Thread(osPriorityNormal, |
| 126 | + OS_STACK_SIZE, |
| 127 | + stack_mem, |
| 128 | + "receiver"); |
| 129 | + TEST_ASSERT_EQUAL(osOK, thread->start(callback(udpsocket_echotest_nonblock_receiver, &pkt_s))); |
| 130 | + |
| 131 | + for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) { |
| 132 | + fill_tx_buffer_ascii(tx_buffer, pkt_s); |
| 133 | + |
| 134 | + sent = sock.sendto(udp_addr, tx_buffer, pkt_s); |
| 135 | + if (sent == NSAPI_ERROR_WOULD_BLOCK) { |
| 136 | + if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + } else if (sent != pkt_s) { |
| 140 | + printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent); |
| 141 | + continue; |
| 142 | + } |
| 143 | + if (tx_sem.wait(WAIT2RECV_TIMEOUT*2) == 0) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom |
| 144 | + continue; |
| 145 | + } |
| 146 | + break; |
| 147 | + } |
| 148 | + thread->join(); |
| 149 | + delete thread; |
| 150 | + } |
| 151 | + free(stack_mem); |
| 152 | + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); |
| 153 | +} |
0 commit comments