Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:615f90842ce8 1 #include <algorithm>
MACRUM 0:615f90842ce8 2 #include "mbed.h"
MACRUM 0:615f90842ce8 3 #include "ESP8266Interface.h"
MACRUM 0:615f90842ce8 4 #include "TCPSocket.h"
MACRUM 0:615f90842ce8 5 #include "greentea-client/test_env.h"
MACRUM 0:615f90842ce8 6 #include "unity/unity.h"
MACRUM 0:615f90842ce8 7 #include "utest.h"
MACRUM 0:615f90842ce8 8
MACRUM 0:615f90842ce8 9 using namespace utest::v1;
MACRUM 0:615f90842ce8 10
MACRUM 0:615f90842ce8 11 #ifndef MBED_CFG_ESP8266_TX
MACRUM 0:615f90842ce8 12 #define MBED_CFG_ESP8266_TX D1
MACRUM 0:615f90842ce8 13 #endif
MACRUM 0:615f90842ce8 14
MACRUM 0:615f90842ce8 15 #ifndef MBED_CFG_ESP8266_RX
MACRUM 0:615f90842ce8 16 #define MBED_CFG_ESP8266_RX D0
MACRUM 0:615f90842ce8 17 #endif
MACRUM 0:615f90842ce8 18
MACRUM 0:615f90842ce8 19 #ifndef MBED_CFG_ESP8266_DEBUG
MACRUM 0:615f90842ce8 20 #define MBED_CFG_ESP8266_DEBUG false
MACRUM 0:615f90842ce8 21 #endif
MACRUM 0:615f90842ce8 22
MACRUM 0:615f90842ce8 23
MACRUM 0:615f90842ce8 24 namespace {
MACRUM 0:615f90842ce8 25 // Test connection information
MACRUM 0:615f90842ce8 26 const char *HTTP_SERVER_NAME = "developer.mbed.org";
MACRUM 0:615f90842ce8 27 const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt";
MACRUM 0:615f90842ce8 28 const int HTTP_SERVER_PORT = 80;
MACRUM 0:615f90842ce8 29 #if defined(TARGET_VK_RZ_A1H)
MACRUM 0:615f90842ce8 30 const int RECV_BUFFER_SIZE = 300;
MACRUM 0:615f90842ce8 31 #else
MACRUM 0:615f90842ce8 32 const int RECV_BUFFER_SIZE = 512;
MACRUM 0:615f90842ce8 33 #endif
MACRUM 0:615f90842ce8 34 // Test related data
MACRUM 0:615f90842ce8 35 const char *HTTP_OK_STR = "200 OK";
MACRUM 0:615f90842ce8 36 const char *HTTP_HELLO_STR = "Hello world!";
MACRUM 0:615f90842ce8 37
MACRUM 0:615f90842ce8 38 // Test buffers
MACRUM 0:615f90842ce8 39 char buffer[RECV_BUFFER_SIZE] = {0};
MACRUM 0:615f90842ce8 40 }
MACRUM 0:615f90842ce8 41
MACRUM 0:615f90842ce8 42 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
MACRUM 0:615f90842ce8 43 const char *f = std::search(first, last, s_first, s_last);
MACRUM 0:615f90842ce8 44 return (f != last);
MACRUM 0:615f90842ce8 45 }
MACRUM 0:615f90842ce8 46
MACRUM 0:615f90842ce8 47 void test_tcp_hello_world() {
MACRUM 0:615f90842ce8 48 bool result = false;
MACRUM 0:615f90842ce8 49 ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
MACRUM 0:615f90842ce8 50 net.connect(MBED_CFG_ESP8266_SSID, MBED_CFG_ESP8266_PASS);
MACRUM 0:615f90842ce8 51 printf("TCP client IP Address is %s\r\n", net.get_ip_address());
MACRUM 0:615f90842ce8 52
MACRUM 0:615f90842ce8 53 TCPSocket sock(&net);
MACRUM 0:615f90842ce8 54 printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
MACRUM 0:615f90842ce8 55 if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
MACRUM 0:615f90842ce8 56 printf("HTTP: OK\r\n");
MACRUM 0:615f90842ce8 57
MACRUM 0:615f90842ce8 58 // We are constructing GET command like this:
MACRUM 0:615f90842ce8 59 // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
MACRUM 0:615f90842ce8 60 strcpy(buffer, "GET http://");
MACRUM 0:615f90842ce8 61 strcat(buffer, HTTP_SERVER_NAME);
MACRUM 0:615f90842ce8 62 strcat(buffer, HTTP_SERVER_FILE_PATH);
MACRUM 0:615f90842ce8 63 strcat(buffer, " HTTP/1.0\n\n");
MACRUM 0:615f90842ce8 64 // Send GET command
MACRUM 0:615f90842ce8 65 sock.send(buffer, strlen(buffer));
MACRUM 0:615f90842ce8 66
MACRUM 0:615f90842ce8 67 // Server will respond with HTTP GET's success code
MACRUM 0:615f90842ce8 68 const int ret = sock.recv(buffer, sizeof(buffer) - 1);
MACRUM 0:615f90842ce8 69 buffer[ret] = '\0';
MACRUM 0:615f90842ce8 70
MACRUM 0:615f90842ce8 71 // Find 200 OK HTTP status in reply
MACRUM 0:615f90842ce8 72 bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
MACRUM 0:615f90842ce8 73 // Find "Hello World!" string in reply
MACRUM 0:615f90842ce8 74 bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
MACRUM 0:615f90842ce8 75
MACRUM 0:615f90842ce8 76 TEST_ASSERT_TRUE(found_200_ok);
MACRUM 0:615f90842ce8 77 TEST_ASSERT_TRUE(found_hello);
MACRUM 0:615f90842ce8 78
MACRUM 0:615f90842ce8 79 if (found_200_ok && found_hello) result = true;
MACRUM 0:615f90842ce8 80
MACRUM 0:615f90842ce8 81 printf("HTTP: Received %d chars from server\r\n", ret);
MACRUM 0:615f90842ce8 82 printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
MACRUM 0:615f90842ce8 83 printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
MACRUM 0:615f90842ce8 84 printf("HTTP: Received message:\r\n");
MACRUM 0:615f90842ce8 85 printf("%s", buffer);
MACRUM 0:615f90842ce8 86 sock.close();
MACRUM 0:615f90842ce8 87 } else {
MACRUM 0:615f90842ce8 88 printf("HTTP: ERROR\r\n");
MACRUM 0:615f90842ce8 89 }
MACRUM 0:615f90842ce8 90
MACRUM 0:615f90842ce8 91 net.disconnect();
MACRUM 0:615f90842ce8 92 TEST_ASSERT_EQUAL(true, result);
MACRUM 0:615f90842ce8 93 }
MACRUM 0:615f90842ce8 94
MACRUM 0:615f90842ce8 95
MACRUM 0:615f90842ce8 96 // Test setup
MACRUM 0:615f90842ce8 97 utest::v1::status_t test_setup(const size_t number_of_cases) {
MACRUM 0:615f90842ce8 98 char uuid[48] = {0};
MACRUM 0:615f90842ce8 99 GREENTEA_SETUP_UUID(120, "default_auto", uuid, 48);
MACRUM 0:615f90842ce8 100
MACRUM 0:615f90842ce8 101 // create mac address based on uuid
MACRUM 0:615f90842ce8 102 uint64_t mac = 0;
MACRUM 0:615f90842ce8 103 for (int i = 0; i < sizeof(uuid); i++) {
MACRUM 0:615f90842ce8 104 mac += uuid[i];
MACRUM 0:615f90842ce8 105 }
MACRUM 0:615f90842ce8 106 mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1);
MACRUM 0:615f90842ce8 107
MACRUM 0:615f90842ce8 108 return verbose_test_setup_handler(number_of_cases);
MACRUM 0:615f90842ce8 109 }
MACRUM 0:615f90842ce8 110
MACRUM 0:615f90842ce8 111 Case cases[] = {
MACRUM 0:615f90842ce8 112 Case("TCP hello world", test_tcp_hello_world),
MACRUM 0:615f90842ce8 113 };
MACRUM 0:615f90842ce8 114
MACRUM 0:615f90842ce8 115 Specification specification(test_setup, cases);
MACRUM 0:615f90842ce8 116
MACRUM 0:615f90842ce8 117 int main() {
MACRUM 0:615f90842ce8 118 return !Harness::run(specification);
MACRUM 0:615f90842ce8 119 }
MACRUM 0:615f90842ce8 120