NuMaker WiFi TCP Example

Committer:
ccli8
Date:
Tue Oct 08 15:48:42 2019 +0800
Revision:
24:38e186a10abe
Parent:
23:dc536ce49b37
Child:
26:f5163fee2653
Refactor network code

1. Remove network code irrelevant to WiFi
2. Move all WiFi configurations to mbed_app.json

Who changed what in which revision?

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