Version of easy-connect with the u-blox cellular platforms C027 and C030 added.
esp8266-driver/TESTS/net/udp_echo_parallel/main.cpp@6:304d3ba87a01, 2017-11-03 (annotated)
- Committer:
- RobMeades
- Date:
- Fri Nov 03 13:01:23 2017 +0000
- Revision:
- 6:304d3ba87a01
- Parent:
- 0:19aa55d66228
Add comment concerning N2XX baud rate.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-ublox | 0:19aa55d66228 | 1 | #ifndef MBED_EXTENDED_TESTS |
group-ublox | 0:19aa55d66228 | 2 | #error [NOT_SUPPORTED] Parallel tests are not supported by default |
group-ublox | 0:19aa55d66228 | 3 | #endif |
group-ublox | 0:19aa55d66228 | 4 | |
group-ublox | 0:19aa55d66228 | 5 | #include "mbed.h" |
group-ublox | 0:19aa55d66228 | 6 | #include "ESP8266Interface.h" |
group-ublox | 0:19aa55d66228 | 7 | #include "UDPSocket.h" |
group-ublox | 0:19aa55d66228 | 8 | #include "greentea-client/test_env.h" |
group-ublox | 0:19aa55d66228 | 9 | #include "unity/unity.h" |
group-ublox | 0:19aa55d66228 | 10 | #include "utest.h" |
group-ublox | 0:19aa55d66228 | 11 | |
group-ublox | 0:19aa55d66228 | 12 | using namespace utest::v1; |
group-ublox | 0:19aa55d66228 | 13 | |
group-ublox | 0:19aa55d66228 | 14 | |
group-ublox | 0:19aa55d66228 | 15 | #ifndef MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE |
group-ublox | 0:19aa55d66228 | 16 | #define MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE 64 |
group-ublox | 0:19aa55d66228 | 17 | #endif |
group-ublox | 0:19aa55d66228 | 18 | |
group-ublox | 0:19aa55d66228 | 19 | #ifndef MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT |
group-ublox | 0:19aa55d66228 | 20 | #define MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT 500 |
group-ublox | 0:19aa55d66228 | 21 | #endif |
group-ublox | 0:19aa55d66228 | 22 | |
group-ublox | 0:19aa55d66228 | 23 | #ifndef MBED_CFG_UDP_CLIENT_ECHO_THREADS |
group-ublox | 0:19aa55d66228 | 24 | #define MBED_CFG_UDP_CLIENT_ECHO_THREADS 3 |
group-ublox | 0:19aa55d66228 | 25 | #endif |
group-ublox | 0:19aa55d66228 | 26 | |
group-ublox | 0:19aa55d66228 | 27 | #ifndef MBED_CFG_ESP8266_TX |
group-ublox | 0:19aa55d66228 | 28 | #define MBED_CFG_ESP8266_TX D1 |
group-ublox | 0:19aa55d66228 | 29 | #endif |
group-ublox | 0:19aa55d66228 | 30 | |
group-ublox | 0:19aa55d66228 | 31 | #ifndef MBED_CFG_ESP8266_RX |
group-ublox | 0:19aa55d66228 | 32 | #define MBED_CFG_ESP8266_RX D0 |
group-ublox | 0:19aa55d66228 | 33 | #endif |
group-ublox | 0:19aa55d66228 | 34 | |
group-ublox | 0:19aa55d66228 | 35 | #ifndef MBED_CFG_ESP8266_DEBUG |
group-ublox | 0:19aa55d66228 | 36 | #define MBED_CFG_ESP8266_DEBUG false |
group-ublox | 0:19aa55d66228 | 37 | #endif |
group-ublox | 0:19aa55d66228 | 38 | |
group-ublox | 0:19aa55d66228 | 39 | #define STRINGIZE(x) STRINGIZE2(x) |
group-ublox | 0:19aa55d66228 | 40 | #define STRINGIZE2(x) #x |
group-ublox | 0:19aa55d66228 | 41 | |
group-ublox | 0:19aa55d66228 | 42 | |
group-ublox | 0:19aa55d66228 | 43 | const int ECHO_LOOPS = 16; |
group-ublox | 0:19aa55d66228 | 44 | ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG); |
group-ublox | 0:19aa55d66228 | 45 | SocketAddress udp_addr; |
group-ublox | 0:19aa55d66228 | 46 | Mutex iomutex; |
group-ublox | 0:19aa55d66228 | 47 | char uuid[48] = {0}; |
group-ublox | 0:19aa55d66228 | 48 | |
group-ublox | 0:19aa55d66228 | 49 | // NOTE: assuming that "id" stays in the single digits |
group-ublox | 0:19aa55d66228 | 50 | void prep_buffer(int id, char *uuid, char *tx_buffer, size_t tx_size) { |
group-ublox | 0:19aa55d66228 | 51 | size_t i = 0; |
group-ublox | 0:19aa55d66228 | 52 | |
group-ublox | 0:19aa55d66228 | 53 | tx_buffer[i++] = '0' + id; |
group-ublox | 0:19aa55d66228 | 54 | tx_buffer[i++] = ' '; |
group-ublox | 0:19aa55d66228 | 55 | |
group-ublox | 0:19aa55d66228 | 56 | memcpy(tx_buffer+i, uuid, strlen(uuid)); |
group-ublox | 0:19aa55d66228 | 57 | i += strlen(uuid); |
group-ublox | 0:19aa55d66228 | 58 | |
group-ublox | 0:19aa55d66228 | 59 | tx_buffer[i++] = ' '; |
group-ublox | 0:19aa55d66228 | 60 | |
group-ublox | 0:19aa55d66228 | 61 | for (; i<tx_size; ++i) { |
group-ublox | 0:19aa55d66228 | 62 | tx_buffer[i] = (rand() % 10) + '0'; |
group-ublox | 0:19aa55d66228 | 63 | } |
group-ublox | 0:19aa55d66228 | 64 | } |
group-ublox | 0:19aa55d66228 | 65 | |
group-ublox | 0:19aa55d66228 | 66 | |
group-ublox | 0:19aa55d66228 | 67 | // Each echo class is in charge of one parallel transaction |
group-ublox | 0:19aa55d66228 | 68 | class Echo { |
group-ublox | 0:19aa55d66228 | 69 | private: |
group-ublox | 0:19aa55d66228 | 70 | char tx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE]; |
group-ublox | 0:19aa55d66228 | 71 | char rx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE]; |
group-ublox | 0:19aa55d66228 | 72 | |
group-ublox | 0:19aa55d66228 | 73 | UDPSocket sock; |
group-ublox | 0:19aa55d66228 | 74 | Thread thread; |
group-ublox | 0:19aa55d66228 | 75 | bool result; |
group-ublox | 0:19aa55d66228 | 76 | int id; |
group-ublox | 0:19aa55d66228 | 77 | char *uuid; |
group-ublox | 0:19aa55d66228 | 78 | |
group-ublox | 0:19aa55d66228 | 79 | public: |
group-ublox | 0:19aa55d66228 | 80 | // Limiting stack size to 1k |
group-ublox | 0:19aa55d66228 | 81 | Echo(): thread(osPriorityNormal, 1024), result(false) { |
group-ublox | 0:19aa55d66228 | 82 | } |
group-ublox | 0:19aa55d66228 | 83 | |
group-ublox | 0:19aa55d66228 | 84 | void start(int id, char *uuid) { |
group-ublox | 0:19aa55d66228 | 85 | this->id = id; |
group-ublox | 0:19aa55d66228 | 86 | this->uuid = uuid; |
group-ublox | 0:19aa55d66228 | 87 | osStatus status = thread.start(callback(this, &Echo::echo)); |
group-ublox | 0:19aa55d66228 | 88 | } |
group-ublox | 0:19aa55d66228 | 89 | |
group-ublox | 0:19aa55d66228 | 90 | void join() { |
group-ublox | 0:19aa55d66228 | 91 | osStatus status = thread.join(); |
group-ublox | 0:19aa55d66228 | 92 | TEST_ASSERT_EQUAL(osOK, status); |
group-ublox | 0:19aa55d66228 | 93 | } |
group-ublox | 0:19aa55d66228 | 94 | |
group-ublox | 0:19aa55d66228 | 95 | void echo() { |
group-ublox | 0:19aa55d66228 | 96 | int success = 0; |
group-ublox | 0:19aa55d66228 | 97 | |
group-ublox | 0:19aa55d66228 | 98 | int err = sock.open(&net); |
group-ublox | 0:19aa55d66228 | 99 | TEST_ASSERT_EQUAL(0, err); |
group-ublox | 0:19aa55d66228 | 100 | |
group-ublox | 0:19aa55d66228 | 101 | sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT); |
group-ublox | 0:19aa55d66228 | 102 | |
group-ublox | 0:19aa55d66228 | 103 | for (int i = 0; success < ECHO_LOOPS; i++) { |
group-ublox | 0:19aa55d66228 | 104 | prep_buffer(id, uuid, tx_buffer, sizeof(tx_buffer)); |
group-ublox | 0:19aa55d66228 | 105 | const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer)); |
group-ublox | 0:19aa55d66228 | 106 | if (ret >= 0) { |
group-ublox | 0:19aa55d66228 | 107 | iomutex.lock(); |
group-ublox | 0:19aa55d66228 | 108 | printf("[ID:%01d][%02d] sent %d bytes - %.*s \n", id, i, ret, ret, tx_buffer); |
group-ublox | 0:19aa55d66228 | 109 | iomutex.unlock(); |
group-ublox | 0:19aa55d66228 | 110 | } else { |
group-ublox | 0:19aa55d66228 | 111 | iomutex.lock(); |
group-ublox | 0:19aa55d66228 | 112 | printf("[ID:%01d][%02d] Network error %d\n", id, i, ret); |
group-ublox | 0:19aa55d66228 | 113 | iomutex.unlock(); |
group-ublox | 0:19aa55d66228 | 114 | continue; |
group-ublox | 0:19aa55d66228 | 115 | } |
group-ublox | 0:19aa55d66228 | 116 | |
group-ublox | 0:19aa55d66228 | 117 | SocketAddress temp_addr; |
group-ublox | 0:19aa55d66228 | 118 | const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer)); |
group-ublox | 0:19aa55d66228 | 119 | if (n >= 0) { |
group-ublox | 0:19aa55d66228 | 120 | iomutex.lock(); |
group-ublox | 0:19aa55d66228 | 121 | printf("[ID:%01d][%02d] recv %d bytes - %.*s \n", id, i, n, n, tx_buffer); |
group-ublox | 0:19aa55d66228 | 122 | iomutex.unlock(); |
group-ublox | 0:19aa55d66228 | 123 | } else { |
group-ublox | 0:19aa55d66228 | 124 | iomutex.lock(); |
group-ublox | 0:19aa55d66228 | 125 | printf("[ID:%01d][%02d] Network error %d\n", id, i, n); |
group-ublox | 0:19aa55d66228 | 126 | iomutex.unlock(); |
group-ublox | 0:19aa55d66228 | 127 | continue; |
group-ublox | 0:19aa55d66228 | 128 | } |
group-ublox | 0:19aa55d66228 | 129 | |
group-ublox | 0:19aa55d66228 | 130 | if ((temp_addr == udp_addr && |
group-ublox | 0:19aa55d66228 | 131 | n == sizeof(tx_buffer) && |
group-ublox | 0:19aa55d66228 | 132 | memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) { |
group-ublox | 0:19aa55d66228 | 133 | success += 1; |
group-ublox | 0:19aa55d66228 | 134 | iomutex.lock(); |
group-ublox | 0:19aa55d66228 | 135 | printf("[ID:%01d][%02d] success #%d\n", id, i, success); |
group-ublox | 0:19aa55d66228 | 136 | iomutex.unlock(); |
group-ublox | 0:19aa55d66228 | 137 | continue; |
group-ublox | 0:19aa55d66228 | 138 | } |
group-ublox | 0:19aa55d66228 | 139 | |
group-ublox | 0:19aa55d66228 | 140 | // failed, clean out any remaining bad packets |
group-ublox | 0:19aa55d66228 | 141 | sock.set_timeout(0); |
group-ublox | 0:19aa55d66228 | 142 | while (true) { |
group-ublox | 0:19aa55d66228 | 143 | err = sock.recvfrom(NULL, NULL, 0); |
group-ublox | 0:19aa55d66228 | 144 | if (err == NSAPI_ERROR_WOULD_BLOCK) { |
group-ublox | 0:19aa55d66228 | 145 | break; |
group-ublox | 0:19aa55d66228 | 146 | } |
group-ublox | 0:19aa55d66228 | 147 | } |
group-ublox | 0:19aa55d66228 | 148 | sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT); |
group-ublox | 0:19aa55d66228 | 149 | } |
group-ublox | 0:19aa55d66228 | 150 | |
group-ublox | 0:19aa55d66228 | 151 | result = success == ECHO_LOOPS; |
group-ublox | 0:19aa55d66228 | 152 | |
group-ublox | 0:19aa55d66228 | 153 | err = sock.close(); |
group-ublox | 0:19aa55d66228 | 154 | TEST_ASSERT_EQUAL(0, err); |
group-ublox | 0:19aa55d66228 | 155 | if (err) { |
group-ublox | 0:19aa55d66228 | 156 | result = false; |
group-ublox | 0:19aa55d66228 | 157 | } |
group-ublox | 0:19aa55d66228 | 158 | } |
group-ublox | 0:19aa55d66228 | 159 | |
group-ublox | 0:19aa55d66228 | 160 | bool get_result() { |
group-ublox | 0:19aa55d66228 | 161 | return result; |
group-ublox | 0:19aa55d66228 | 162 | } |
group-ublox | 0:19aa55d66228 | 163 | }; |
group-ublox | 0:19aa55d66228 | 164 | |
group-ublox | 0:19aa55d66228 | 165 | Echo *echoers[MBED_CFG_UDP_CLIENT_ECHO_THREADS]; |
group-ublox | 0:19aa55d66228 | 166 | |
group-ublox | 0:19aa55d66228 | 167 | |
group-ublox | 0:19aa55d66228 | 168 | void test_udp_echo_parallel() { |
group-ublox | 0:19aa55d66228 | 169 | int err = net.connect(STRINGIZE(MBED_CFG_ESP8266_SSID), STRINGIZE(MBED_CFG_ESP8266_PASS)); |
group-ublox | 0:19aa55d66228 | 170 | TEST_ASSERT_EQUAL(0, err); |
group-ublox | 0:19aa55d66228 | 171 | |
group-ublox | 0:19aa55d66228 | 172 | if (err) { |
group-ublox | 0:19aa55d66228 | 173 | printf("MBED: failed to connect with an error of %d\r\n", err); |
group-ublox | 0:19aa55d66228 | 174 | GREENTEA_TESTSUITE_RESULT(false); |
group-ublox | 0:19aa55d66228 | 175 | } else { |
group-ublox | 0:19aa55d66228 | 176 | printf("UDP client IP Address is %s\n", net.get_ip_address()); |
group-ublox | 0:19aa55d66228 | 177 | |
group-ublox | 0:19aa55d66228 | 178 | greentea_send_kv("target_ip", net.get_ip_address()); |
group-ublox | 0:19aa55d66228 | 179 | |
group-ublox | 0:19aa55d66228 | 180 | char recv_key[] = "host_port"; |
group-ublox | 0:19aa55d66228 | 181 | char ipbuf[60] = {0}; |
group-ublox | 0:19aa55d66228 | 182 | char portbuf[16] = {0}; |
group-ublox | 0:19aa55d66228 | 183 | unsigned int port = 0; |
group-ublox | 0:19aa55d66228 | 184 | |
group-ublox | 0:19aa55d66228 | 185 | greentea_send_kv("host_ip", " "); |
group-ublox | 0:19aa55d66228 | 186 | greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); |
group-ublox | 0:19aa55d66228 | 187 | |
group-ublox | 0:19aa55d66228 | 188 | greentea_send_kv("host_port", " "); |
group-ublox | 0:19aa55d66228 | 189 | greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); |
group-ublox | 0:19aa55d66228 | 190 | sscanf(portbuf, "%u", &port); |
group-ublox | 0:19aa55d66228 | 191 | |
group-ublox | 0:19aa55d66228 | 192 | printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port); |
group-ublox | 0:19aa55d66228 | 193 | udp_addr.set_ip_address(ipbuf); |
group-ublox | 0:19aa55d66228 | 194 | udp_addr.set_port(port); |
group-ublox | 0:19aa55d66228 | 195 | |
group-ublox | 0:19aa55d66228 | 196 | // Startup echo threads in parallel |
group-ublox | 0:19aa55d66228 | 197 | for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) { |
group-ublox | 0:19aa55d66228 | 198 | echoers[i] = new Echo; |
group-ublox | 0:19aa55d66228 | 199 | echoers[i]->start(i, uuid); |
group-ublox | 0:19aa55d66228 | 200 | } |
group-ublox | 0:19aa55d66228 | 201 | |
group-ublox | 0:19aa55d66228 | 202 | bool result = true; |
group-ublox | 0:19aa55d66228 | 203 | |
group-ublox | 0:19aa55d66228 | 204 | for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) { |
group-ublox | 0:19aa55d66228 | 205 | echoers[i]->join(); |
group-ublox | 0:19aa55d66228 | 206 | result = result && echoers[i]->get_result(); |
group-ublox | 0:19aa55d66228 | 207 | delete echoers[i]; |
group-ublox | 0:19aa55d66228 | 208 | } |
group-ublox | 0:19aa55d66228 | 209 | |
group-ublox | 0:19aa55d66228 | 210 | net.disconnect(); |
group-ublox | 0:19aa55d66228 | 211 | TEST_ASSERT_EQUAL(true, result); |
group-ublox | 0:19aa55d66228 | 212 | } |
group-ublox | 0:19aa55d66228 | 213 | } |
group-ublox | 0:19aa55d66228 | 214 | |
group-ublox | 0:19aa55d66228 | 215 | |
group-ublox | 0:19aa55d66228 | 216 | // Test setup |
group-ublox | 0:19aa55d66228 | 217 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
group-ublox | 0:19aa55d66228 | 218 | GREENTEA_SETUP(120, "udp_echo"); |
group-ublox | 0:19aa55d66228 | 219 | return verbose_test_setup_handler(number_of_cases); |
group-ublox | 0:19aa55d66228 | 220 | } |
group-ublox | 0:19aa55d66228 | 221 | |
group-ublox | 0:19aa55d66228 | 222 | Case cases[] = { |
group-ublox | 0:19aa55d66228 | 223 | Case("UDP echo parallel", test_udp_echo_parallel), |
group-ublox | 0:19aa55d66228 | 224 | }; |
group-ublox | 0:19aa55d66228 | 225 | |
group-ublox | 0:19aa55d66228 | 226 | Specification specification(test_setup, cases); |
group-ublox | 0:19aa55d66228 | 227 | |
group-ublox | 0:19aa55d66228 | 228 | int main() { |
group-ublox | 0:19aa55d66228 | 229 | return !Harness::run(specification); |
group-ublox | 0:19aa55d66228 | 230 | } |
group-ublox | 0:19aa55d66228 | 231 |