u-blox / easy-connect

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 #ifndef MBED_EXTENDED_TESTS
group-ublox 0:19aa55d66228 2 #error [NOT_SUPPORTED] Parallel 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_ECHO_BUFFER_SIZE
group-ublox 0:19aa55d66228 16 #define MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 64
group-ublox 0:19aa55d66228 17 #endif
group-ublox 0:19aa55d66228 18
group-ublox 0:19aa55d66228 19 #ifndef MBED_CFG_TCP_CLIENT_ECHO_THREADS
group-ublox 0:19aa55d66228 20 #define MBED_CFG_TCP_CLIENT_ECHO_THREADS 3
group-ublox 0:19aa55d66228 21 #endif
group-ublox 0:19aa55d66228 22
group-ublox 0:19aa55d66228 23 #ifndef MBED_CFG_ESP8266_TX
group-ublox 0:19aa55d66228 24 #define MBED_CFG_ESP8266_TX D1
group-ublox 0:19aa55d66228 25 #endif
group-ublox 0:19aa55d66228 26
group-ublox 0:19aa55d66228 27 #ifndef MBED_CFG_ESP8266_RX
group-ublox 0:19aa55d66228 28 #define MBED_CFG_ESP8266_RX D0
group-ublox 0:19aa55d66228 29 #endif
group-ublox 0:19aa55d66228 30
group-ublox 0:19aa55d66228 31 #ifndef MBED_CFG_ESP8266_DEBUG
group-ublox 0:19aa55d66228 32 #define MBED_CFG_ESP8266_DEBUG false
group-ublox 0:19aa55d66228 33 #endif
group-ublox 0:19aa55d66228 34
group-ublox 0:19aa55d66228 35 #define STRINGIZE(x) STRINGIZE2(x)
group-ublox 0:19aa55d66228 36 #define STRINGIZE2(x) #x
group-ublox 0:19aa55d66228 37
group-ublox 0:19aa55d66228 38
group-ublox 0:19aa55d66228 39 ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
group-ublox 0:19aa55d66228 40 SocketAddress tcp_addr;
group-ublox 0:19aa55d66228 41 Mutex iomutex;
group-ublox 0:19aa55d66228 42
group-ublox 0:19aa55d66228 43 void prep_buffer(char *tx_buffer, size_t tx_size) {
group-ublox 0:19aa55d66228 44 for (size_t i=0; i<tx_size; ++i) {
group-ublox 0:19aa55d66228 45 tx_buffer[i] = (rand() % 10) + '0';
group-ublox 0:19aa55d66228 46 }
group-ublox 0:19aa55d66228 47 }
group-ublox 0:19aa55d66228 48
group-ublox 0:19aa55d66228 49
group-ublox 0:19aa55d66228 50 // Each echo class is in charge of one parallel transaction
group-ublox 0:19aa55d66228 51 class Echo {
group-ublox 0:19aa55d66228 52 private:
group-ublox 0:19aa55d66228 53 char tx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE];
group-ublox 0:19aa55d66228 54 char rx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE];
group-ublox 0:19aa55d66228 55
group-ublox 0:19aa55d66228 56 TCPSocket sock;
group-ublox 0:19aa55d66228 57 Thread thread;
group-ublox 0:19aa55d66228 58
group-ublox 0:19aa55d66228 59 public:
group-ublox 0:19aa55d66228 60 // Limiting stack size to 1k
group-ublox 0:19aa55d66228 61 Echo(): thread(osPriorityNormal, 1024) {
group-ublox 0:19aa55d66228 62 }
group-ublox 0:19aa55d66228 63
group-ublox 0:19aa55d66228 64 void start() {
group-ublox 0:19aa55d66228 65 osStatus status = thread.start(callback(this, &Echo::echo));
group-ublox 0:19aa55d66228 66 TEST_ASSERT_EQUAL(osOK, status);
group-ublox 0:19aa55d66228 67 }
group-ublox 0:19aa55d66228 68
group-ublox 0:19aa55d66228 69 void join() {
group-ublox 0:19aa55d66228 70 osStatus status = thread.join();
group-ublox 0:19aa55d66228 71 TEST_ASSERT_EQUAL(osOK, status);
group-ublox 0:19aa55d66228 72 }
group-ublox 0:19aa55d66228 73
group-ublox 0:19aa55d66228 74 void echo() {
group-ublox 0:19aa55d66228 75 int err = sock.open(&net);
group-ublox 0:19aa55d66228 76 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 77
group-ublox 0:19aa55d66228 78 err = sock.connect(tcp_addr);
group-ublox 0:19aa55d66228 79 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 80
group-ublox 0:19aa55d66228 81 iomutex.lock();
group-ublox 0:19aa55d66228 82 printf("HTTP: Connected to %s:%d\r\n",
group-ublox 0:19aa55d66228 83 tcp_addr.get_ip_address(), tcp_addr.get_port());
group-ublox 0:19aa55d66228 84 printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer));
group-ublox 0:19aa55d66228 85 printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer));
group-ublox 0:19aa55d66228 86 iomutex.unlock();
group-ublox 0:19aa55d66228 87
group-ublox 0:19aa55d66228 88 prep_buffer(tx_buffer, sizeof(tx_buffer));
group-ublox 0:19aa55d66228 89 sock.send(tx_buffer, sizeof(tx_buffer));
group-ublox 0:19aa55d66228 90
group-ublox 0:19aa55d66228 91 // Server will respond with HTTP GET's success code
group-ublox 0:19aa55d66228 92 const int ret = sock.recv(rx_buffer, sizeof(rx_buffer));
group-ublox 0:19aa55d66228 93 bool result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer));
group-ublox 0:19aa55d66228 94 TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer));
group-ublox 0:19aa55d66228 95 TEST_ASSERT_EQUAL(true, result);
group-ublox 0:19aa55d66228 96
group-ublox 0:19aa55d66228 97 err = sock.close();
group-ublox 0:19aa55d66228 98 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 99 }
group-ublox 0:19aa55d66228 100 };
group-ublox 0:19aa55d66228 101
group-ublox 0:19aa55d66228 102 Echo *echoers[MBED_CFG_TCP_CLIENT_ECHO_THREADS];
group-ublox 0:19aa55d66228 103
group-ublox 0:19aa55d66228 104
group-ublox 0:19aa55d66228 105 void test_tcp_echo_parallel() {
group-ublox 0:19aa55d66228 106 int err = net.connect(STRINGIZE(MBED_CFG_ESP8266_SSID), STRINGIZE(MBED_CFG_ESP8266_PASS));
group-ublox 0:19aa55d66228 107 TEST_ASSERT_EQUAL(0, err);
group-ublox 0:19aa55d66228 108
group-ublox 0:19aa55d66228 109 printf("MBED: TCPClient IP address is '%s'\n", net.get_ip_address());
group-ublox 0:19aa55d66228 110 printf("MBED: TCPClient waiting for server IP and port...\n");
group-ublox 0:19aa55d66228 111
group-ublox 0:19aa55d66228 112 greentea_send_kv("target_ip", net.get_ip_address());
group-ublox 0:19aa55d66228 113
group-ublox 0:19aa55d66228 114 char recv_key[] = "host_port";
group-ublox 0:19aa55d66228 115 char ipbuf[60] = {0};
group-ublox 0:19aa55d66228 116 char portbuf[16] = {0};
group-ublox 0:19aa55d66228 117 unsigned int port = 0;
group-ublox 0:19aa55d66228 118
group-ublox 0:19aa55d66228 119 greentea_send_kv("host_ip", " ");
group-ublox 0:19aa55d66228 120 greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 121
group-ublox 0:19aa55d66228 122 greentea_send_kv("host_port", " ");
group-ublox 0:19aa55d66228 123 greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
group-ublox 0:19aa55d66228 124 sscanf(portbuf, "%u", &port);
group-ublox 0:19aa55d66228 125
group-ublox 0:19aa55d66228 126 printf("MBED: Server IP address received: %s:%d \n", ipbuf, port);
group-ublox 0:19aa55d66228 127 tcp_addr.set_ip_address(ipbuf);
group-ublox 0:19aa55d66228 128 tcp_addr.set_port(port);
group-ublox 0:19aa55d66228 129
group-ublox 0:19aa55d66228 130 // Startup echo threads in parallel
group-ublox 0:19aa55d66228 131 for (int i = 0; i < MBED_CFG_TCP_CLIENT_ECHO_THREADS; i++) {
group-ublox 0:19aa55d66228 132 echoers[i] = new Echo;
group-ublox 0:19aa55d66228 133 echoers[i]->start();
group-ublox 0:19aa55d66228 134 }
group-ublox 0:19aa55d66228 135
group-ublox 0:19aa55d66228 136 for (int i = 0; i < MBED_CFG_TCP_CLIENT_ECHO_THREADS; i++) {
group-ublox 0:19aa55d66228 137 echoers[i]->join();
group-ublox 0:19aa55d66228 138 delete echoers[i];
group-ublox 0:19aa55d66228 139 }
group-ublox 0:19aa55d66228 140
group-ublox 0:19aa55d66228 141 net.disconnect();
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, "tcp_echo");
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("TCP echo parallel", test_tcp_echo_parallel),
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