Version of easy-connect with the u-blox cellular platforms C027 and C030 added.

Dependents:   HelloMQTT

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?

UserRevisionLine numberNew contents of line
group-ublox 0:19aa55d66228 1 #ifndef MBED_EXTENDED_TESTS
group-ublox 0:19aa55d66228 2 #error [NOT_SUPPORTED] Pressure 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_PACKET_PRESSURE_MIN
group-ublox 0:19aa55d66228 16 #define MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MIN 64
group-ublox 0:19aa55d66228 17 #endif
group-ublox 0:19aa55d66228 18
group-ublox 0:19aa55d66228 19 #ifndef MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MAX
group-ublox 0:19aa55d66228 20 #define MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MAX 0x80000
group-ublox 0:19aa55d66228 21 #endif
group-ublox 0:19aa55d66228 22
group-ublox 0:19aa55d66228 23 #ifndef MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_TIMEOUT
group-ublox 0:19aa55d66228 24 #define MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_TIMEOUT 100
group-ublox 0:19aa55d66228 25 #endif
group-ublox 0:19aa55d66228 26
group-ublox 0:19aa55d66228 27 #ifndef MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_SEED
group-ublox 0:19aa55d66228 28 #define MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_SEED 0x6d626564
group-ublox 0:19aa55d66228 29 #endif
group-ublox 0:19aa55d66228 30
group-ublox 0:19aa55d66228 31 #ifndef MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_DEBUG
group-ublox 0:19aa55d66228 32 #define MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_DEBUG false
group-ublox 0:19aa55d66228 33 #endif
group-ublox 0:19aa55d66228 34
group-ublox 0:19aa55d66228 35 #ifndef MBED_CFG_ESP8266_TX
group-ublox 0:19aa55d66228 36 #define MBED_CFG_ESP8266_TX D1
group-ublox 0:19aa55d66228 37 #endif
group-ublox 0:19aa55d66228 38
group-ublox 0:19aa55d66228 39 #ifndef MBED_CFG_ESP8266_RX
group-ublox 0:19aa55d66228 40 #define MBED_CFG_ESP8266_RX D0
group-ublox 0:19aa55d66228 41 #endif
group-ublox 0:19aa55d66228 42
group-ublox 0:19aa55d66228 43 #ifndef MBED_CFG_ESP8266_DEBUG
group-ublox 0:19aa55d66228 44 #define MBED_CFG_ESP8266_DEBUG false
group-ublox 0:19aa55d66228 45 #endif
group-ublox 0:19aa55d66228 46
group-ublox 0:19aa55d66228 47 #define STRINGIZE(x) STRINGIZE2(x)
group-ublox 0:19aa55d66228 48 #define STRINGIZE2(x) #x
group-ublox 0:19aa55d66228 49
group-ublox 0:19aa55d66228 50
group-ublox 0:19aa55d66228 51 // Simple xorshift pseudorandom number generator
group-ublox 0:19aa55d66228 52 class RandSeq {
group-ublox 0:19aa55d66228 53 private:
group-ublox 0:19aa55d66228 54 uint32_t x;
group-ublox 0:19aa55d66228 55 uint32_t y;
group-ublox 0:19aa55d66228 56 static const int A = 15;
group-ublox 0:19aa55d66228 57 static const int B = 18;
group-ublox 0:19aa55d66228 58 static const int C = 11;
group-ublox 0:19aa55d66228 59
group-ublox 0:19aa55d66228 60 public:
group-ublox 0:19aa55d66228 61 RandSeq(uint32_t seed=MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_SEED)
group-ublox 0:19aa55d66228 62 : x(seed), y(seed) {}
group-ublox 0:19aa55d66228 63
group-ublox 0:19aa55d66228 64 uint32_t next(void) {
group-ublox 0:19aa55d66228 65 x ^= x << A;
group-ublox 0:19aa55d66228 66 x ^= x >> B;
group-ublox 0:19aa55d66228 67 x ^= y ^ (y >> C);
group-ublox 0:19aa55d66228 68 return x + y;
group-ublox 0:19aa55d66228 69 }
group-ublox 0:19aa55d66228 70
group-ublox 0:19aa55d66228 71 void skip(size_t size) {
group-ublox 0:19aa55d66228 72 for (size_t i = 0; i < size; i++) {
group-ublox 0:19aa55d66228 73 next();
group-ublox 0:19aa55d66228 74 }
group-ublox 0:19aa55d66228 75 }
group-ublox 0:19aa55d66228 76
group-ublox 0:19aa55d66228 77 void buffer(uint8_t *buffer, size_t size) {
group-ublox 0:19aa55d66228 78 RandSeq lookahead = *this;
group-ublox 0:19aa55d66228 79
group-ublox 0:19aa55d66228 80 for (size_t i = 0; i < size; i++) {
group-ublox 0:19aa55d66228 81 buffer[i] = lookahead.next() & 0xff;
group-ublox 0:19aa55d66228 82 }
group-ublox 0:19aa55d66228 83 }
group-ublox 0:19aa55d66228 84
group-ublox 0:19aa55d66228 85 int cmp(uint8_t *buffer, size_t size) {
group-ublox 0:19aa55d66228 86 RandSeq lookahead = *this;
group-ublox 0:19aa55d66228 87
group-ublox 0:19aa55d66228 88 for (size_t i = 0; i < size; i++) {
group-ublox 0:19aa55d66228 89 int diff = buffer[i] - (lookahead.next() & 0xff);
group-ublox 0:19aa55d66228 90 if (diff != 0) {
group-ublox 0:19aa55d66228 91 return diff;
group-ublox 0:19aa55d66228 92 }
group-ublox 0:19aa55d66228 93 }
group-ublox 0:19aa55d66228 94 return 0;
group-ublox 0:19aa55d66228 95 }
group-ublox 0:19aa55d66228 96 };
group-ublox 0:19aa55d66228 97
group-ublox 0:19aa55d66228 98 // Shared buffer for network transactions
group-ublox 0:19aa55d66228 99 uint8_t *buffer;
group-ublox 0:19aa55d66228 100 size_t buffer_size;
group-ublox 0:19aa55d66228 101
group-ublox 0:19aa55d66228 102 // Tries to get the biggest buffer possible on the device. Exponentially
group-ublox 0:19aa55d66228 103 // grows a buffer until heap runs out of space, and uses half to leave
group-ublox 0:19aa55d66228 104 // space for the rest of the program
group-ublox 0:19aa55d66228 105 void generate_buffer(uint8_t **buffer, size_t *size, size_t min, size_t max) {
group-ublox 0:19aa55d66228 106 size_t i = min;
group-ublox 0:19aa55d66228 107 while (i < max) {
group-ublox 0:19aa55d66228 108 void *b = malloc(i);
group-ublox 0:19aa55d66228 109 if (!b) {
group-ublox 0:19aa55d66228 110 i /= 4;
group-ublox 0:19aa55d66228 111 if (i < min) {
group-ublox 0:19aa55d66228 112 i = min;
group-ublox 0:19aa55d66228 113 }
group-ublox 0:19aa55d66228 114 break;
group-ublox 0:19aa55d66228 115 }
group-ublox 0:19aa55d66228 116 free(b);
group-ublox 0:19aa55d66228 117 i *= 2;
group-ublox 0:19aa55d66228 118 }
group-ublox 0:19aa55d66228 119
group-ublox 0:19aa55d66228 120 *buffer = (uint8_t *)malloc(i);
group-ublox 0:19aa55d66228 121 *size = i;
group-ublox 0:19aa55d66228 122 TEST_ASSERT(buffer);
group-ublox 0:19aa55d66228 123 }
group-ublox 0:19aa55d66228 124
group-ublox 0:19aa55d66228 125 void test_udp_packet_pressure() {
group-ublox 0:19aa55d66228 126 generate_buffer(&buffer, &buffer_size,
group-ublox 0:19aa55d66228 127 MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MIN,
group-ublox 0:19aa55d66228 128 MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MAX);
group-ublox 0:19aa55d66228 129 printf("MBED: Generated buffer %d\r\n", buffer_size);
group-ublox 0:19aa55d66228 130
group-ublox 0:19aa55d66228 131 ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
group-ublox 0:19aa55d66228 132 int err = net.connect(STRINGIZE(MBED_CFG_ESP8266_SSID), STRINGIZE(MBED_CFG_ESP8266_PASS));
group-ublox 0:19aa55d66228 133 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 134
group-ublox 0:19aa55d66228 135 printf("MBED: UDPClient IP address is '%s'\n", net.get_ip_address());
group-ublox 0:19aa55d66228 136 printf("MBED: UDPClient waiting for server IP and port...\n");
group-ublox 0:19aa55d66228 137
group-ublox 0:19aa55d66228 138 greentea_send_kv("target_ip", net.get_ip_address());
group-ublox 0:19aa55d66228 139
group-ublox 0:19aa55d66228 140 char recv_key[] = "host_port";
group-ublox 0:19aa55d66228 141 char ipbuf[60] = {0};
group-ublox 0:19aa55d66228 142 char portbuf[16] = {0};
group-ublox 0:19aa55d66228 143 unsigned int port = 0;
group-ublox 0:19aa55d66228 144
group-ublox 0:19aa55d66228 145 greentea_send_kv("host_ip", " ");
group-ublox 0:19aa55d66228 146 greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 147
group-ublox 0:19aa55d66228 148 greentea_send_kv("host_port", " ");
group-ublox 0:19aa55d66228 149 greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 150 sscanf(portbuf, "%u", &port);
group-ublox 0:19aa55d66228 151
group-ublox 0:19aa55d66228 152 printf("MBED: Server IP address received: %s:%d \n", ipbuf, port);
group-ublox 0:19aa55d66228 153
group-ublox 0:19aa55d66228 154 UDPSocket sock;
group-ublox 0:19aa55d66228 155 SocketAddress udp_addr(ipbuf, port);
group-ublox 0:19aa55d66228 156
group-ublox 0:19aa55d66228 157 Timer timer;
group-ublox 0:19aa55d66228 158 timer.start();
group-ublox 0:19aa55d66228 159
group-ublox 0:19aa55d66228 160 // Tests exponentially growing sequences
group-ublox 0:19aa55d66228 161 for (size_t size = MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MIN;
group-ublox 0:19aa55d66228 162 size < MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MAX;
group-ublox 0:19aa55d66228 163 size *= 2) {
group-ublox 0:19aa55d66228 164 err = sock.open(&net);
group-ublox 0:19aa55d66228 165 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 166 printf("UDP: %s:%d streaming %d bytes\r\n", ipbuf, port, size);
group-ublox 0:19aa55d66228 167
group-ublox 0:19aa55d66228 168 sock.set_blocking(false);
group-ublox 0:19aa55d66228 169
group-ublox 0:19aa55d66228 170 // Loop to send/recv all data
group-ublox 0:19aa55d66228 171 RandSeq tx_seq;
group-ublox 0:19aa55d66228 172 RandSeq rx_seq;
group-ublox 0:19aa55d66228 173 size_t rx_count = 0;
group-ublox 0:19aa55d66228 174 size_t tx_count = 0;
group-ublox 0:19aa55d66228 175 int known_time = timer.read_ms();
group-ublox 0:19aa55d66228 176 size_t window = buffer_size;
group-ublox 0:19aa55d66228 177
group-ublox 0:19aa55d66228 178 while (tx_count < size || rx_count < size) {
group-ublox 0:19aa55d66228 179 // Send out packets
group-ublox 0:19aa55d66228 180 if (tx_count < size) {
group-ublox 0:19aa55d66228 181 size_t chunk_size = size - tx_count;
group-ublox 0:19aa55d66228 182 if (chunk_size > window) {
group-ublox 0:19aa55d66228 183 chunk_size = window;
group-ublox 0:19aa55d66228 184 }
group-ublox 0:19aa55d66228 185
group-ublox 0:19aa55d66228 186 tx_seq.buffer(buffer, chunk_size);
group-ublox 0:19aa55d66228 187 int td = sock.sendto(udp_addr, buffer, chunk_size);
group-ublox 0:19aa55d66228 188
group-ublox 0:19aa55d66228 189 if (td > 0) {
group-ublox 0:19aa55d66228 190 if (MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_DEBUG) {
group-ublox 0:19aa55d66228 191 printf("UDP: tx -> %d\r\n", td);
group-ublox 0:19aa55d66228 192 }
group-ublox 0:19aa55d66228 193 tx_seq.skip(td);
group-ublox 0:19aa55d66228 194 tx_count += td;
group-ublox 0:19aa55d66228 195 } else if (td != NSAPI_ERROR_WOULD_BLOCK) {
group-ublox 0:19aa55d66228 196 // We may fail to send because of buffering issues, revert to
group-ublox 0:19aa55d66228 197 // last good sequence and cut buffer in half
group-ublox 0:19aa55d66228 198 if (window > MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MIN) {
group-ublox 0:19aa55d66228 199 window /= 2;
group-ublox 0:19aa55d66228 200 }
group-ublox 0:19aa55d66228 201
group-ublox 0:19aa55d66228 202 if (MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_DEBUG) {
group-ublox 0:19aa55d66228 203 printf("UDP: Not sent (%d), window = %d\r\n", td, window);
group-ublox 0:19aa55d66228 204 }
group-ublox 0:19aa55d66228 205 }
group-ublox 0:19aa55d66228 206 }
group-ublox 0:19aa55d66228 207
group-ublox 0:19aa55d66228 208 // Prioritize recieving over sending packets to avoid flooding
group-ublox 0:19aa55d66228 209 // the network while handling erronous packets
group-ublox 0:19aa55d66228 210 while (rx_count < size) {
group-ublox 0:19aa55d66228 211 int rd = sock.recvfrom(NULL, buffer, buffer_size);
group-ublox 0:19aa55d66228 212 TEST_ASSERT(rd > 0 || rd == NSAPI_ERROR_WOULD_BLOCK);
group-ublox 0:19aa55d66228 213
group-ublox 0:19aa55d66228 214 if (rd > 0) {
group-ublox 0:19aa55d66228 215 if (MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_DEBUG) {
group-ublox 0:19aa55d66228 216 printf("UDP: rx <- %d\r\n", rd);
group-ublox 0:19aa55d66228 217 }
group-ublox 0:19aa55d66228 218
group-ublox 0:19aa55d66228 219 if (rx_seq.cmp(buffer, rd) == 0) {
group-ublox 0:19aa55d66228 220 rx_seq.skip(rd);
group-ublox 0:19aa55d66228 221 rx_count += rd;
group-ublox 0:19aa55d66228 222 known_time = timer.read_ms();
group-ublox 0:19aa55d66228 223 if (window < MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MAX) {
group-ublox 0:19aa55d66228 224 window += MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MIN;
group-ublox 0:19aa55d66228 225 }
group-ublox 0:19aa55d66228 226 }
group-ublox 0:19aa55d66228 227 } else if (timer.read_ms() - known_time >
group-ublox 0:19aa55d66228 228 MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_TIMEOUT) {
group-ublox 0:19aa55d66228 229 // Dropped packet or out of order, revert to last good sequence
group-ublox 0:19aa55d66228 230 // and cut buffer in half
group-ublox 0:19aa55d66228 231 tx_seq = rx_seq;
group-ublox 0:19aa55d66228 232 tx_count = rx_count;
group-ublox 0:19aa55d66228 233 known_time = timer.read_ms();
group-ublox 0:19aa55d66228 234 if (window > MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MIN) {
group-ublox 0:19aa55d66228 235 window /= 2;
group-ublox 0:19aa55d66228 236 }
group-ublox 0:19aa55d66228 237
group-ublox 0:19aa55d66228 238 if (MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_DEBUG) {
group-ublox 0:19aa55d66228 239 printf("UDP: Dropped, window = %d\r\n", window);
group-ublox 0:19aa55d66228 240 }
group-ublox 0:19aa55d66228 241 } else if (rd == NSAPI_ERROR_WOULD_BLOCK) {
group-ublox 0:19aa55d66228 242 break;
group-ublox 0:19aa55d66228 243 }
group-ublox 0:19aa55d66228 244 }
group-ublox 0:19aa55d66228 245 }
group-ublox 0:19aa55d66228 246
group-ublox 0:19aa55d66228 247 err = sock.close();
group-ublox 0:19aa55d66228 248 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 249 }
group-ublox 0:19aa55d66228 250
group-ublox 0:19aa55d66228 251 timer.stop();
group-ublox 0:19aa55d66228 252 printf("MBED: Time taken: %fs\r\n", timer.read());
group-ublox 0:19aa55d66228 253 printf("MBED: Speed: %.3fkb/s\r\n",
group-ublox 0:19aa55d66228 254 8*(2*MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MAX -
group-ublox 0:19aa55d66228 255 MBED_CFG_UDP_CLIENT_PACKET_PRESSURE_MIN) / (1000*timer.read()));
group-ublox 0:19aa55d66228 256
group-ublox 0:19aa55d66228 257 net.disconnect();
group-ublox 0:19aa55d66228 258 }
group-ublox 0:19aa55d66228 259
group-ublox 0:19aa55d66228 260
group-ublox 0:19aa55d66228 261 // Test setup
group-ublox 0:19aa55d66228 262 utest::v1::status_t test_setup(const size_t number_of_cases) {
group-ublox 0:19aa55d66228 263 GREENTEA_SETUP(120, "udp_echo");
group-ublox 0:19aa55d66228 264 return verbose_test_setup_handler(number_of_cases);
group-ublox 0:19aa55d66228 265 }
group-ublox 0:19aa55d66228 266
group-ublox 0:19aa55d66228 267 Case cases[] = {
group-ublox 0:19aa55d66228 268 Case("UDP packet pressure", test_udp_packet_pressure),
group-ublox 0:19aa55d66228 269 };
group-ublox 0:19aa55d66228 270
group-ublox 0:19aa55d66228 271 Specification specification(test_setup, cases);
group-ublox 0:19aa55d66228 272
group-ublox 0:19aa55d66228 273 int main() {
group-ublox 0:19aa55d66228 274 return !Harness::run(specification);
group-ublox 0:19aa55d66228 275 }
group-ublox 0:19aa55d66228 276