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 #include "mbed.h"
group-ublox 0:19aa55d66228 2 #include "ESP8266Interface.h"
group-ublox 0:19aa55d66228 3 #include "UDPSocket.h"
group-ublox 0:19aa55d66228 4 #include "greentea-client/test_env.h"
group-ublox 0:19aa55d66228 5 #include "unity/unity.h"
group-ublox 0:19aa55d66228 6 #include "utest.h"
group-ublox 0:19aa55d66228 7
group-ublox 0:19aa55d66228 8 using namespace utest::v1;
group-ublox 0:19aa55d66228 9
group-ublox 0:19aa55d66228 10
group-ublox 0:19aa55d66228 11 #ifndef MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE
group-ublox 0:19aa55d66228 12 #define MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE 64
group-ublox 0:19aa55d66228 13 #endif
group-ublox 0:19aa55d66228 14
group-ublox 0:19aa55d66228 15 #ifndef MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT
group-ublox 0:19aa55d66228 16 #define MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT 500
group-ublox 0:19aa55d66228 17 #endif
group-ublox 0:19aa55d66228 18
group-ublox 0:19aa55d66228 19 #ifndef MBED_CFG_ESP8266_TX
group-ublox 0:19aa55d66228 20 #define MBED_CFG_ESP8266_TX D1
group-ublox 0:19aa55d66228 21 #endif
group-ublox 0:19aa55d66228 22
group-ublox 0:19aa55d66228 23 #ifndef MBED_CFG_ESP8266_RX
group-ublox 0:19aa55d66228 24 #define MBED_CFG_ESP8266_RX D0
group-ublox 0:19aa55d66228 25 #endif
group-ublox 0:19aa55d66228 26
group-ublox 0:19aa55d66228 27 #ifndef MBED_CFG_ESP8266_DEBUG
group-ublox 0:19aa55d66228 28 #define MBED_CFG_ESP8266_DEBUG false
group-ublox 0:19aa55d66228 29 #endif
group-ublox 0:19aa55d66228 30
group-ublox 0:19aa55d66228 31 #define STRINGIZE(x) STRINGIZE2(x)
group-ublox 0:19aa55d66228 32 #define STRINGIZE2(x) #x
group-ublox 0:19aa55d66228 33
group-ublox 0:19aa55d66228 34
group-ublox 0:19aa55d66228 35 namespace {
group-ublox 0:19aa55d66228 36 char tx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
group-ublox 0:19aa55d66228 37 char rx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
group-ublox 0:19aa55d66228 38 const char ASCII_MAX = '~' - ' ';
group-ublox 0:19aa55d66228 39 const int ECHO_LOOPS = 16;
group-ublox 0:19aa55d66228 40 char uuid[48] = {0};
group-ublox 0:19aa55d66228 41 }
group-ublox 0:19aa55d66228 42
group-ublox 0:19aa55d66228 43 void prep_buffer(char *uuid, char *tx_buffer, size_t tx_size) {
group-ublox 0:19aa55d66228 44 size_t i = 0;
group-ublox 0:19aa55d66228 45
group-ublox 0:19aa55d66228 46 memcpy(tx_buffer, uuid, strlen(uuid));
group-ublox 0:19aa55d66228 47 i += strlen(uuid);
group-ublox 0:19aa55d66228 48
group-ublox 0:19aa55d66228 49 tx_buffer[i++] = ' ';
group-ublox 0:19aa55d66228 50
group-ublox 0:19aa55d66228 51 for (; i<tx_size; ++i) {
group-ublox 0:19aa55d66228 52 tx_buffer[i] = (rand() % 10) + '0';
group-ublox 0:19aa55d66228 53 }
group-ublox 0:19aa55d66228 54 }
group-ublox 0:19aa55d66228 55
group-ublox 0:19aa55d66228 56 void test_udp_echo() {
group-ublox 0:19aa55d66228 57 ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
group-ublox 0:19aa55d66228 58
group-ublox 0:19aa55d66228 59 int err = net.connect(STRINGIZE(MBED_CFG_ESP8266_SSID), STRINGIZE(MBED_CFG_ESP8266_PASS));
group-ublox 0:19aa55d66228 60 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 61
group-ublox 0:19aa55d66228 62 if (err) {
group-ublox 0:19aa55d66228 63 printf("MBED: failed to connect with an error of %d\r\n", err);
group-ublox 0:19aa55d66228 64 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 65 }
group-ublox 0:19aa55d66228 66
group-ublox 0:19aa55d66228 67 printf("UDP client IP Address is %s\n", net.get_ip_address());
group-ublox 0:19aa55d66228 68
group-ublox 0:19aa55d66228 69 greentea_send_kv("target_ip", net.get_ip_address());
group-ublox 0:19aa55d66228 70
group-ublox 0:19aa55d66228 71 char recv_key[] = "host_port";
group-ublox 0:19aa55d66228 72 char ipbuf[60] = {0};
group-ublox 0:19aa55d66228 73 char portbuf[16] = {0};
group-ublox 0:19aa55d66228 74 unsigned int port = 0;
group-ublox 0:19aa55d66228 75
group-ublox 0:19aa55d66228 76 UDPSocket sock;
group-ublox 0:19aa55d66228 77 sock.open(&net);
group-ublox 0:19aa55d66228 78 sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
group-ublox 0:19aa55d66228 79
group-ublox 0:19aa55d66228 80 greentea_send_kv("host_ip", " ");
group-ublox 0:19aa55d66228 81 greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 82
group-ublox 0:19aa55d66228 83 greentea_send_kv("host_port", " ");
group-ublox 0:19aa55d66228 84 greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 85 sscanf(portbuf, "%u", &port);
group-ublox 0:19aa55d66228 86
group-ublox 0:19aa55d66228 87 printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
group-ublox 0:19aa55d66228 88 SocketAddress udp_addr(ipbuf, port);
group-ublox 0:19aa55d66228 89
group-ublox 0:19aa55d66228 90 int success = 0;
group-ublox 0:19aa55d66228 91 for (int i = 0; success < ECHO_LOOPS; i++) {
group-ublox 0:19aa55d66228 92 prep_buffer(uuid, tx_buffer, sizeof(tx_buffer));
group-ublox 0:19aa55d66228 93 const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
group-ublox 0:19aa55d66228 94 if (ret >= 0) {
group-ublox 0:19aa55d66228 95 printf("[%02d] sent %d bytes - %.*s \n", i, ret, ret, tx_buffer);
group-ublox 0:19aa55d66228 96 } else {
group-ublox 0:19aa55d66228 97 printf("[%02d] Network error %d\n", i, ret);
group-ublox 0:19aa55d66228 98 continue;
group-ublox 0:19aa55d66228 99 }
group-ublox 0:19aa55d66228 100
group-ublox 0:19aa55d66228 101 SocketAddress temp_addr;
group-ublox 0:19aa55d66228 102 const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
group-ublox 0:19aa55d66228 103 if (n >= 0) {
group-ublox 0:19aa55d66228 104 printf("[%02d] recv %d bytes - %.*s \n", i, n, n, tx_buffer);
group-ublox 0:19aa55d66228 105 } else {
group-ublox 0:19aa55d66228 106 printf("[%02d] Network error %d\n", i, n);
group-ublox 0:19aa55d66228 107 continue;
group-ublox 0:19aa55d66228 108 }
group-ublox 0:19aa55d66228 109
group-ublox 0:19aa55d66228 110 if ((temp_addr == udp_addr &&
group-ublox 0:19aa55d66228 111 n == sizeof(tx_buffer) &&
group-ublox 0:19aa55d66228 112 memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
group-ublox 0:19aa55d66228 113 success += 1;
group-ublox 0:19aa55d66228 114
group-ublox 0:19aa55d66228 115 printf("[%02d] success #%d\n", i, success);
group-ublox 0:19aa55d66228 116 continue;
group-ublox 0:19aa55d66228 117 }
group-ublox 0:19aa55d66228 118
group-ublox 0:19aa55d66228 119 // failed, clean out any remaining bad packets
group-ublox 0:19aa55d66228 120 sock.set_timeout(0);
group-ublox 0:19aa55d66228 121 while (true) {
group-ublox 0:19aa55d66228 122 err = sock.recvfrom(NULL, NULL, 0);
group-ublox 0:19aa55d66228 123 if (err == NSAPI_ERROR_WOULD_BLOCK) {
group-ublox 0:19aa55d66228 124 break;
group-ublox 0:19aa55d66228 125 }
group-ublox 0:19aa55d66228 126 }
group-ublox 0:19aa55d66228 127 sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
group-ublox 0:19aa55d66228 128 }
group-ublox 0:19aa55d66228 129
group-ublox 0:19aa55d66228 130 sock.close();
group-ublox 0:19aa55d66228 131 net.disconnect();
group-ublox 0:19aa55d66228 132 TEST_ASSERT_EQUAL(ECHO_LOOPS, success);
group-ublox 0:19aa55d66228 133 }
group-ublox 0:19aa55d66228 134
group-ublox 0:19aa55d66228 135
group-ublox 0:19aa55d66228 136 // Test setup
group-ublox 0:19aa55d66228 137 utest::v1::status_t test_setup(const size_t number_of_cases) {
group-ublox 0:19aa55d66228 138 GREENTEA_SETUP(120, "udp_echo");
group-ublox 0:19aa55d66228 139 return verbose_test_setup_handler(number_of_cases);
group-ublox 0:19aa55d66228 140 }
group-ublox 0:19aa55d66228 141
group-ublox 0:19aa55d66228 142 Case cases[] = {
group-ublox 0:19aa55d66228 143 Case("UDP echo", test_udp_echo),
group-ublox 0:19aa55d66228 144 };
group-ublox 0:19aa55d66228 145
group-ublox 0:19aa55d66228 146 Specification specification(test_setup, cases);
group-ublox 0:19aa55d66228 147
group-ublox 0:19aa55d66228 148 int main() {
group-ublox 0:19aa55d66228 149 return !Harness::run(specification);
group-ublox 0:19aa55d66228 150 }
group-ublox 0:19aa55d66228 151