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