Nuvoton
/
NuMaker-mbed-wifi-tcp
NuMaker WiFi TCP Example
main.cpp@26:f5163fee2653, 2019-12-03 (annotated)
- Committer:
- cyliang
- Date:
- Tue Dec 03 10:54:15 2019 +0000
- Revision:
- 26:f5163fee2653
- Parent:
- 24:38e186a10abe
- Child:
- 27:9b7ba5c587da
Change the default protocol as HTTP1.1 instead of HTTP1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cyliang | 0:2198c8de64fe | 1 | #include <algorithm> |
cyliang | 0:2198c8de64fe | 2 | #include "mbed.h" |
ccli8 |
15:32a6a29ffcb3 | 3 | #include "mbed_stats.h" |
cyliang | 0:2198c8de64fe | 4 | #include "TCPSocket.h" |
cyliang | 0:2198c8de64fe | 5 | |
cyliang | 20:41c819860b4d | 6 | #define MBED_HEAP_STATS_ENABLED 1 |
cyliang | 26:f5163fee2653 | 7 | #define USE_HTTP_1_1 |
cyliang | 0:2198c8de64fe | 8 | //#define LOCAL_LAN |
cyliang | 0:2198c8de64fe | 9 | |
cyliang | 0:2198c8de64fe | 10 | namespace { |
cyliang | 0:2198c8de64fe | 11 | // Test connection information |
cyliang | 0:2198c8de64fe | 12 | #ifndef LOCAL_LAN |
cyliang | 20:41c819860b4d | 13 | const char *HTTP_SERVER_NAME = "www.ifconfig.io"; |
cyliang | 0:2198c8de64fe | 14 | #else |
cyliang | 0:2198c8de64fe | 15 | const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com"; |
cyliang | 0:2198c8de64fe | 16 | #endif |
cyliang | 0:2198c8de64fe | 17 | |
cyliang | 0:2198c8de64fe | 18 | #ifndef LOCAL_LAN |
cyliang | 20:41c819860b4d | 19 | const char *HTTP_SERVER_FILE_PATH = "/method"; |
cyliang | 0:2198c8de64fe | 20 | const int HTTP_SERVER_PORT = 80; |
cyliang | 0:2198c8de64fe | 21 | #else |
cyliang | 20:41c819860b4d | 22 | const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/method.txt"; |
cyliang | 0:2198c8de64fe | 23 | const int HTTP_SERVER_PORT = 8080; |
cyliang | 0:2198c8de64fe | 24 | #endif |
cyliang | 0:2198c8de64fe | 25 | |
cyliang | 0:2198c8de64fe | 26 | |
cyliang | 0:2198c8de64fe | 27 | const int RECV_BUFFER_SIZE = 512; |
cyliang | 0:2198c8de64fe | 28 | |
cyliang | 0:2198c8de64fe | 29 | // Test related data |
cyliang | 0:2198c8de64fe | 30 | const char *HTTP_OK_STR = "200 OK"; |
cyliang | 20:41c819860b4d | 31 | const char *HTTP_EXPECT_STR = "GET"; |
cyliang | 0:2198c8de64fe | 32 | |
cyliang | 0:2198c8de64fe | 33 | // Test buffers |
cyliang | 0:2198c8de64fe | 34 | char buffer[RECV_BUFFER_SIZE] = {0}; |
cyliang | 0:2198c8de64fe | 35 | } |
cyliang | 0:2198c8de64fe | 36 | |
cyliang | 0:2198c8de64fe | 37 | bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { |
cyliang | 0:2198c8de64fe | 38 | const char *f = std::search(first, last, s_first, s_last); |
cyliang | 0:2198c8de64fe | 39 | return (f != last); |
cyliang | 0:2198c8de64fe | 40 | } |
cyliang | 0:2198c8de64fe | 41 | |
cyliang | 0:2198c8de64fe | 42 | int main() { |
ccli8 |
15:32a6a29ffcb3 | 43 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
15:32a6a29ffcb3 | 44 | mbed_stats_heap_t heap_stats; |
ccli8 |
15:32a6a29ffcb3 | 45 | #endif |
ccli8 |
15:32a6a29ffcb3 | 46 | |
ccli8 |
19:79f168fa9a8a | 47 | printf("Start WiFi test \r\n"); |
cyliang | 0:2198c8de64fe | 48 | |
cyliang | 0:2198c8de64fe | 49 | bool result = true; |
ccli8 |
15:32a6a29ffcb3 | 50 | int rc = 0; |
cyliang | 0:2198c8de64fe | 51 | |
ccli8 |
19:79f168fa9a8a | 52 | printf("Start Connection ... \r\n"); |
cyliang | 0:2198c8de64fe | 53 | |
ccli8 |
24:38e186a10abe | 54 | NetworkInterface *network_interface = NetworkInterface::get_default_instance(); |
ccli8 |
24:38e186a10abe | 55 | if (NULL == network_interface) { |
ccli8 |
24:38e186a10abe | 56 | printf("NULL network interface! Exiting application....\r\n"); |
ccli8 |
24:38e186a10abe | 57 | return 0; |
ccli8 |
24:38e186a10abe | 58 | } |
cyliang | 0:2198c8de64fe | 59 | |
ccli8 |
19:79f168fa9a8a | 60 | printf("\n\rUsing WiFi \r\n"); |
ccli8 |
19:79f168fa9a8a | 61 | printf("\n\rConnecting to WiFi..\r\n"); |
ccli8 |
24:38e186a10abe | 62 | rc = network_interface->connect(); |
cyliang | 0:2198c8de64fe | 63 | if(rc == 0) { |
ccli8 |
19:79f168fa9a8a | 64 | printf("\n\rConnected to Network successfully\r\n"); |
cyliang | 0:2198c8de64fe | 65 | } else { |
ccli8 |
19:79f168fa9a8a | 66 | printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", rc); |
cyliang | 0:2198c8de64fe | 67 | return 0; |
cyliang | 0:2198c8de64fe | 68 | } |
cyliang | 0:2198c8de64fe | 69 | |
ccli8 |
19:79f168fa9a8a | 70 | printf("TCP client IP Address is %s\r\n", network_interface->get_ip_address()); |
cyliang | 0:2198c8de64fe | 71 | |
cyliang | 0:2198c8de64fe | 72 | TCPSocket sock(network_interface); |
ccli8 |
19:79f168fa9a8a | 73 | printf(" HTTP Connection ... \r\n"); |
cyliang | 0:2198c8de64fe | 74 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
ccli8 |
19:79f168fa9a8a | 75 | printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
cyliang | 0:2198c8de64fe | 76 | |
cyliang | 0:2198c8de64fe | 77 | // We are constructing GET command like this: |
cyliang | 21:662058dda3b1 | 78 | #ifndef USE_HTTP_1_1 |
cyliang | 20:41c819860b4d | 79 | // GET http://www.ifconfig.io/method HTTP/1.0\n\n |
cyliang | 0:2198c8de64fe | 80 | strcpy(buffer, "GET http://"); |
cyliang | 0:2198c8de64fe | 81 | strcat(buffer, HTTP_SERVER_NAME); |
cyliang | 0:2198c8de64fe | 82 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
cyliang | 21:662058dda3b1 | 83 | strcat(buffer, " HTTP/1.0\n\n"); |
cyliang | 21:662058dda3b1 | 84 | #else |
cyliang | 21:662058dda3b1 | 85 | // GET /method HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n" |
cyliang | 21:662058dda3b1 | 86 | strcpy(buffer, "GET "); |
cyliang | 21:662058dda3b1 | 87 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
cyliang | 21:662058dda3b1 | 88 | strcat(buffer, " HTTP/1.1\r\nHost: "); |
cyliang | 21:662058dda3b1 | 89 | strcat(buffer, HTTP_SERVER_NAME); |
cyliang | 21:662058dda3b1 | 90 | strcat(buffer, "\r\nConnection: close\r\n\r\n"); |
cyliang | 21:662058dda3b1 | 91 | #endif |
cyliang | 20:41c819860b4d | 92 | |
cyliang | 0:2198c8de64fe | 93 | // Send GET command |
cyliang | 0:2198c8de64fe | 94 | sock.send(buffer, strlen(buffer)); |
cyliang | 0:2198c8de64fe | 95 | |
cyliang | 0:2198c8de64fe | 96 | // Server will respond with HTTP GET's success code |
cyliang | 0:2198c8de64fe | 97 | const int ret = sock.recv(buffer, sizeof(buffer) - 1); |
cyliang | 0:2198c8de64fe | 98 | buffer[ret] = '\0'; |
cyliang | 20:41c819860b4d | 99 | |
cyliang | 0:2198c8de64fe | 100 | // Find 200 OK HTTP status in reply |
cyliang | 0:2198c8de64fe | 101 | bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); |
cyliang | 20:41c819860b4d | 102 | // Find "deny" string in reply |
cyliang | 20:41c819860b4d | 103 | bool found_expect = find_substring(buffer, buffer + ret, HTTP_EXPECT_STR, HTTP_EXPECT_STR + strlen(HTTP_EXPECT_STR)); |
cyliang | 0:2198c8de64fe | 104 | |
cyliang | 0:2198c8de64fe | 105 | if (!found_200_ok) result = false; |
cyliang | 20:41c819860b4d | 106 | if (!found_expect) result = false; |
cyliang | 0:2198c8de64fe | 107 | |
ccli8 |
19:79f168fa9a8a | 108 | printf("HTTP: Received %d chars from server\r\n", ret); |
ccli8 |
19:79f168fa9a8a | 109 | printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); |
cyliang | 20:41c819860b4d | 110 | printf("HTTP: Received '%s' status ... %s\r\n", HTTP_EXPECT_STR, found_expect ? "[OK]" : "[FAIL]"); |
ccli8 |
19:79f168fa9a8a | 111 | printf("HTTP: Received massage:\r\n\r\n"); |
ccli8 |
19:79f168fa9a8a | 112 | printf("%s", buffer); |
cyliang | 0:2198c8de64fe | 113 | } |
cyliang | 0:2198c8de64fe | 114 | |
ccli8 |
15:32a6a29ffcb3 | 115 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
15:32a6a29ffcb3 | 116 | mbed_stats_heap_get(&heap_stats); |
ccli8 |
15:32a6a29ffcb3 | 117 | printf("Current heap: %lu\r\n", heap_stats.current_size); |
ccli8 |
15:32a6a29ffcb3 | 118 | printf("Max heap size: %lu\r\n", heap_stats.max_size); |
cyliang | 0:2198c8de64fe | 119 | #endif |
ccli8 |
15:32a6a29ffcb3 | 120 | |
ccli8 |
19:79f168fa9a8a | 121 | printf(" Close socket & disconnect ... \r\n"); |
cyliang | 0:2198c8de64fe | 122 | sock.close(); |
ccli8 |
24:38e186a10abe | 123 | |
ccli8 |
24:38e186a10abe | 124 | network_interface->disconnect(); |
ccli8 |
19:79f168fa9a8a | 125 | printf(" End \r\n"); |
cyliang | 0:2198c8de64fe | 126 | } |