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_DTLS_HANDSHAKE_BUFFER_SIZE
group-ublox 0:19aa55d66228 12 #define MBED_CFG_UDP_DTLS_HANDSHAKE_BUFFER_SIZE 512
group-ublox 0:19aa55d66228 13 #endif
group-ublox 0:19aa55d66228 14
group-ublox 0:19aa55d66228 15 #ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES
group-ublox 0:19aa55d66228 16 #define MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES 16
group-ublox 0:19aa55d66228 17 #endif
group-ublox 0:19aa55d66228 18
group-ublox 0:19aa55d66228 19 #ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN
group-ublox 0:19aa55d66228 20 #define MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN 112, 384, 200, 219, 25
group-ublox 0:19aa55d66228 21 #endif
group-ublox 0:19aa55d66228 22
group-ublox 0:19aa55d66228 23 #ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT
group-ublox 0:19aa55d66228 24 #define MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT 1500
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 uint8_t buffer[MBED_CFG_UDP_DTLS_HANDSHAKE_BUFFER_SIZE] = {0};
group-ublox 0:19aa55d66228 43 int udp_dtls_handshake_pattern[] = {MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN};
group-ublox 0:19aa55d66228 44 const int udp_dtls_handshake_count = sizeof(udp_dtls_handshake_pattern) / sizeof(int);
group-ublox 0:19aa55d66228 45
group-ublox 0:19aa55d66228 46 void test_udp_dtls_handshake() {
group-ublox 0:19aa55d66228 47 ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
group-ublox 0:19aa55d66228 48 int err = net.connect(STRINGIZE(MBED_CFG_ESP8266_SSID), STRINGIZE(MBED_CFG_ESP8266_PASS));
group-ublox 0:19aa55d66228 49 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 50
group-ublox 0:19aa55d66228 51 printf("MBED: UDPClient IP address is '%s'\n", net.get_ip_address());
group-ublox 0:19aa55d66228 52 printf("MBED: UDPClient waiting for server IP and port...\n");
group-ublox 0:19aa55d66228 53
group-ublox 0:19aa55d66228 54 greentea_send_kv("target_ip", net.get_ip_address());
group-ublox 0:19aa55d66228 55
group-ublox 0:19aa55d66228 56 bool result = false;
group-ublox 0:19aa55d66228 57
group-ublox 0:19aa55d66228 58 char recv_key[] = "host_port";
group-ublox 0:19aa55d66228 59 char ipbuf[60] = {0};
group-ublox 0:19aa55d66228 60 char portbuf[16] = {0};
group-ublox 0:19aa55d66228 61 unsigned int port = 0;
group-ublox 0:19aa55d66228 62
group-ublox 0:19aa55d66228 63 greentea_send_kv("host_ip", " ");
group-ublox 0:19aa55d66228 64 greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 65
group-ublox 0:19aa55d66228 66 greentea_send_kv("host_port", " ");
group-ublox 0:19aa55d66228 67 greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 68 sscanf(portbuf, "%u", &port);
group-ublox 0:19aa55d66228 69
group-ublox 0:19aa55d66228 70 printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
group-ublox 0:19aa55d66228 71
group-ublox 0:19aa55d66228 72 // align each size to 4-bits
group-ublox 0:19aa55d66228 73 for (int i = 0; i < udp_dtls_handshake_count; i++) {
group-ublox 0:19aa55d66228 74 udp_dtls_handshake_pattern[i] = (~0xf & udp_dtls_handshake_pattern[i]) + 0x10;
group-ublox 0:19aa55d66228 75 }
group-ublox 0:19aa55d66228 76
group-ublox 0:19aa55d66228 77 printf("MBED: DTLS pattern [");
group-ublox 0:19aa55d66228 78 for (int i = 0; i < udp_dtls_handshake_count; i++) {
group-ublox 0:19aa55d66228 79 printf("%d", udp_dtls_handshake_pattern[i]);
group-ublox 0:19aa55d66228 80 if (i != udp_dtls_handshake_count-1) {
group-ublox 0:19aa55d66228 81 printf(", ");
group-ublox 0:19aa55d66228 82 }
group-ublox 0:19aa55d66228 83 }
group-ublox 0:19aa55d66228 84 printf("]\r\n");
group-ublox 0:19aa55d66228 85
group-ublox 0:19aa55d66228 86 UDPSocket sock;
group-ublox 0:19aa55d66228 87 SocketAddress udp_addr(ipbuf, port);
group-ublox 0:19aa55d66228 88 sock.set_timeout(MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT);
group-ublox 0:19aa55d66228 89
group-ublox 0:19aa55d66228 90 for (int attempt = 0; attempt < MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES; attempt++) {
group-ublox 0:19aa55d66228 91 err = sock.open(&net);
group-ublox 0:19aa55d66228 92 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 93
group-ublox 0:19aa55d66228 94 for (int i = 0; i < udp_dtls_handshake_count; i++) {
group-ublox 0:19aa55d66228 95 buffer[i] = udp_dtls_handshake_pattern[i] >> 4;
group-ublox 0:19aa55d66228 96 }
group-ublox 0:19aa55d66228 97
group-ublox 0:19aa55d66228 98 err = sock.sendto(udp_addr, buffer, udp_dtls_handshake_count);
group-ublox 0:19aa55d66228 99 printf("UDP: tx -> %d\r\n", err);
group-ublox 0:19aa55d66228 100 TEST_ASSERT_EQUAL(udp_dtls_handshake_count, err);
group-ublox 0:19aa55d66228 101
group-ublox 0:19aa55d66228 102 int step = 0;
group-ublox 0:19aa55d66228 103 while (step < udp_dtls_handshake_count) {
group-ublox 0:19aa55d66228 104 err = sock.recvfrom(NULL, buffer, sizeof(buffer));
group-ublox 0:19aa55d66228 105 printf("UDP: rx <- %d ", err);
group-ublox 0:19aa55d66228 106
group-ublox 0:19aa55d66228 107 // check length
group-ublox 0:19aa55d66228 108 if (err != udp_dtls_handshake_pattern[step]) {
group-ublox 0:19aa55d66228 109 printf("x (expected %d)\r\n", udp_dtls_handshake_pattern[step]);
group-ublox 0:19aa55d66228 110 break;
group-ublox 0:19aa55d66228 111 }
group-ublox 0:19aa55d66228 112
group-ublox 0:19aa55d66228 113 // check quick xor of packet
group-ublox 0:19aa55d66228 114 uint8_t check = 0;
group-ublox 0:19aa55d66228 115 for (int j = 0; j < udp_dtls_handshake_pattern[step]; j++) {
group-ublox 0:19aa55d66228 116 check ^= buffer[j];
group-ublox 0:19aa55d66228 117 }
group-ublox 0:19aa55d66228 118
group-ublox 0:19aa55d66228 119 if (check != 0) {
group-ublox 0:19aa55d66228 120 printf("x (checksum 0x%02x)\r\n", check);
group-ublox 0:19aa55d66228 121 break;
group-ublox 0:19aa55d66228 122 }
group-ublox 0:19aa55d66228 123
group-ublox 0:19aa55d66228 124 // successfully got a packet
group-ublox 0:19aa55d66228 125 printf("\r\n");
group-ublox 0:19aa55d66228 126 step += 1;
group-ublox 0:19aa55d66228 127 }
group-ublox 0:19aa55d66228 128
group-ublox 0:19aa55d66228 129 err = sock.close();
group-ublox 0:19aa55d66228 130 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 131
group-ublox 0:19aa55d66228 132 // got through all steps, test passed
group-ublox 0:19aa55d66228 133 if (step == udp_dtls_handshake_count) {
group-ublox 0:19aa55d66228 134 result = true;
group-ublox 0:19aa55d66228 135 break;
group-ublox 0:19aa55d66228 136 }
group-ublox 0:19aa55d66228 137 }
group-ublox 0:19aa55d66228 138
group-ublox 0:19aa55d66228 139 net.disconnect();
group-ublox 0:19aa55d66228 140 TEST_ASSERT_EQUAL(true, result);
group-ublox 0:19aa55d66228 141 }
group-ublox 0:19aa55d66228 142
group-ublox 0:19aa55d66228 143
group-ublox 0:19aa55d66228 144 // Test setup
group-ublox 0:19aa55d66228 145 utest::v1::status_t test_setup(const size_t number_of_cases) {
group-ublox 0:19aa55d66228 146 GREENTEA_SETUP(120, "udp_shotgun");
group-ublox 0:19aa55d66228 147 return verbose_test_setup_handler(number_of_cases);
group-ublox 0:19aa55d66228 148 }
group-ublox 0:19aa55d66228 149
group-ublox 0:19aa55d66228 150 Case cases[] = {
group-ublox 0:19aa55d66228 151 Case("UDP DTLS handshake", test_udp_dtls_handshake),
group-ublox 0:19aa55d66228 152 };
group-ublox 0:19aa55d66228 153
group-ublox 0:19aa55d66228 154 Specification specification(test_setup, cases);
group-ublox 0:19aa55d66228 155
group-ublox 0:19aa55d66228 156 int main() {
group-ublox 0:19aa55d66228 157 return !Harness::run(specification);
group-ublox 0:19aa55d66228 158 }
group-ublox 0:19aa55d66228 159