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

Dependents:   HelloMQTT

Committer:
group-ublox
Date:
Thu Aug 10 14:33:05 2017 +0000
Revision:
0:19aa55d66228
Initial commit

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 "greentea-client/test_env.h"
group-ublox 0:19aa55d66228 3 #include "unity.h"
group-ublox 0:19aa55d66228 4 #include "utest.h"
group-ublox 0:19aa55d66228 5 #include "ESP8266Interface.h"
group-ublox 0:19aa55d66228 6
group-ublox 0:19aa55d66228 7 using namespace utest::v1;
group-ublox 0:19aa55d66228 8
group-ublox 0:19aa55d66228 9 // Hostname for testing against
group-ublox 0:19aa55d66228 10 // Must have A and AAAA records
group-ublox 0:19aa55d66228 11 #ifndef MBED_DNS_TEST_HOST
group-ublox 0:19aa55d66228 12 #define MBED_DNS_TEST_HOST "connector.mbed.com"
group-ublox 0:19aa55d66228 13 #endif
group-ublox 0:19aa55d66228 14
group-ublox 0:19aa55d66228 15 #ifndef MBED_CFG_ESP8266_TX
group-ublox 0:19aa55d66228 16 #define MBED_CFG_ESP8266_TX D1
group-ublox 0:19aa55d66228 17 #endif
group-ublox 0:19aa55d66228 18
group-ublox 0:19aa55d66228 19 #ifndef MBED_CFG_ESP8266_RX
group-ublox 0:19aa55d66228 20 #define MBED_CFG_ESP8266_RX D0
group-ublox 0:19aa55d66228 21 #endif
group-ublox 0:19aa55d66228 22
group-ublox 0:19aa55d66228 23 #ifndef MBED_CFG_ESP8266_DEBUG
group-ublox 0:19aa55d66228 24 #define MBED_CFG_ESP8266_DEBUG false
group-ublox 0:19aa55d66228 25 #endif
group-ublox 0:19aa55d66228 26
group-ublox 0:19aa55d66228 27 #define STRINGIZE(x) STRINGIZE2(x)
group-ublox 0:19aa55d66228 28 #define STRINGIZE2(x) #x
group-ublox 0:19aa55d66228 29
group-ublox 0:19aa55d66228 30 // Address info from stack
group-ublox 0:19aa55d66228 31 const char *ip_literal;
group-ublox 0:19aa55d66228 32 nsapi_version_t ip_pref;
group-ublox 0:19aa55d66228 33 const char *ip_pref_repr;
group-ublox 0:19aa55d66228 34
group-ublox 0:19aa55d66228 35 // Network setup
group-ublox 0:19aa55d66228 36 ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
group-ublox 0:19aa55d66228 37 void net_bringup() {
group-ublox 0:19aa55d66228 38 int err = net.connect(STRINGIZE(MBED_CFG_ESP8266_SSID), STRINGIZE(MBED_CFG_ESP8266_PASS));
group-ublox 0:19aa55d66228 39 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 40 printf("MBED: Connected to network\n");
group-ublox 0:19aa55d66228 41 printf("MBED: IP Address: %s\n", net.get_ip_address());
group-ublox 0:19aa55d66228 42
group-ublox 0:19aa55d66228 43 ip_literal = net.get_ip_address();
group-ublox 0:19aa55d66228 44 ip_pref = SocketAddress(ip_literal).get_ip_version();
group-ublox 0:19aa55d66228 45 ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" :
group-ublox 0:19aa55d66228 46 (ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec";
group-ublox 0:19aa55d66228 47 }
group-ublox 0:19aa55d66228 48
group-ublox 0:19aa55d66228 49
group-ublox 0:19aa55d66228 50 // DNS tests
group-ublox 0:19aa55d66228 51 void test_dns_query() {
group-ublox 0:19aa55d66228 52 SocketAddress addr;
group-ublox 0:19aa55d66228 53 int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr);
group-ublox 0:19aa55d66228 54 printf("DNS: query \"%s\" => \"%s\"\n",
group-ublox 0:19aa55d66228 55 MBED_DNS_TEST_HOST, addr.get_ip_address());
group-ublox 0:19aa55d66228 56
group-ublox 0:19aa55d66228 57 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 58 TEST_ASSERT((bool)addr);
group-ublox 0:19aa55d66228 59 TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
group-ublox 0:19aa55d66228 60 }
group-ublox 0:19aa55d66228 61
group-ublox 0:19aa55d66228 62 void test_dns_query_pref() {
group-ublox 0:19aa55d66228 63 SocketAddress addr;
group-ublox 0:19aa55d66228 64 int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr, ip_pref);
group-ublox 0:19aa55d66228 65 printf("DNS: query %s \"%s\" => \"%s\"\n",
group-ublox 0:19aa55d66228 66 ip_pref_repr, MBED_DNS_TEST_HOST, addr.get_ip_address());
group-ublox 0:19aa55d66228 67
group-ublox 0:19aa55d66228 68 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 69 TEST_ASSERT((bool)addr);
group-ublox 0:19aa55d66228 70 TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
group-ublox 0:19aa55d66228 71 TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
group-ublox 0:19aa55d66228 72 }
group-ublox 0:19aa55d66228 73
group-ublox 0:19aa55d66228 74 void test_dns_literal() {
group-ublox 0:19aa55d66228 75 SocketAddress addr;
group-ublox 0:19aa55d66228 76 int err = net.gethostbyname(ip_literal, &addr);
group-ublox 0:19aa55d66228 77 printf("DNS: literal \"%s\" => \"%s\"\n",
group-ublox 0:19aa55d66228 78 ip_literal, addr.get_ip_address());
group-ublox 0:19aa55d66228 79
group-ublox 0:19aa55d66228 80 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 81 TEST_ASSERT((bool)addr);
group-ublox 0:19aa55d66228 82 TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
group-ublox 0:19aa55d66228 83 TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
group-ublox 0:19aa55d66228 84 }
group-ublox 0:19aa55d66228 85
group-ublox 0:19aa55d66228 86 void test_dns_literal_pref() {
group-ublox 0:19aa55d66228 87 SocketAddress addr;
group-ublox 0:19aa55d66228 88 int err = net.gethostbyname(ip_literal, &addr, ip_pref);
group-ublox 0:19aa55d66228 89 printf("DNS: literal %s \"%s\" => \"%s\"\n",
group-ublox 0:19aa55d66228 90 ip_pref_repr, ip_literal, addr.get_ip_address());
group-ublox 0:19aa55d66228 91
group-ublox 0:19aa55d66228 92 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 93 TEST_ASSERT((bool)addr);
group-ublox 0:19aa55d66228 94 TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
group-ublox 0:19aa55d66228 95 TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
group-ublox 0:19aa55d66228 96 TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
group-ublox 0:19aa55d66228 97 }
group-ublox 0:19aa55d66228 98
group-ublox 0:19aa55d66228 99
group-ublox 0:19aa55d66228 100 // Test setup
group-ublox 0:19aa55d66228 101 utest::v1::status_t test_setup(const size_t number_of_cases) {
group-ublox 0:19aa55d66228 102 GREENTEA_SETUP(120, "default_auto");
group-ublox 0:19aa55d66228 103 net_bringup();
group-ublox 0:19aa55d66228 104 return verbose_test_setup_handler(number_of_cases);
group-ublox 0:19aa55d66228 105 }
group-ublox 0:19aa55d66228 106
group-ublox 0:19aa55d66228 107 Case cases[] = {
group-ublox 0:19aa55d66228 108 Case("DNS query", test_dns_query),
group-ublox 0:19aa55d66228 109 Case("DNS preference query", test_dns_query_pref),
group-ublox 0:19aa55d66228 110 Case("DNS literal", test_dns_literal),
group-ublox 0:19aa55d66228 111 Case("DNS preference literal", test_dns_literal_pref),
group-ublox 0:19aa55d66228 112 };
group-ublox 0:19aa55d66228 113
group-ublox 0:19aa55d66228 114 Specification specification(test_setup, cases);
group-ublox 0:19aa55d66228 115
group-ublox 0:19aa55d66228 116 int main() {
group-ublox 0:19aa55d66228 117 return !Harness::run(specification);
group-ublox 0:19aa55d66228 118 }