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