Nuvoton
/
NuMaker-mbed-wifi-tcp
NuMaker WiFi TCP Example
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include <algorithm> 00002 #include "mbed.h" 00003 #include "mbed_stats.h" 00004 #include "TCPSocket.h" 00005 00006 #define MBED_HEAP_STATS_ENABLED 1 00007 #define USE_HTTP_1_1 00008 //#define LOCAL_LAN 00009 00010 namespace { 00011 // Test connection information 00012 #ifndef LOCAL_LAN 00013 const char *HTTP_SERVER_NAME = "www.ifconfig.io"; 00014 #else 00015 const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com"; 00016 #endif 00017 00018 #ifndef LOCAL_LAN 00019 const char *HTTP_SERVER_FILE_PATH = "/method"; 00020 const int HTTP_SERVER_PORT = 80; 00021 #else 00022 const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/method.txt"; 00023 const int HTTP_SERVER_PORT = 8080; 00024 #endif 00025 00026 00027 const int RECV_BUFFER_SIZE = 512; 00028 00029 // Test related data 00030 const char *HTTP_OK_STR = "200 OK"; 00031 const char *HTTP_EXPECT_STR = "GET"; 00032 00033 // Test buffers 00034 char buffer[RECV_BUFFER_SIZE] = {0}; 00035 } 00036 00037 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { 00038 const char *f = std::search(first, last, s_first, s_last); 00039 return (f != last); 00040 } 00041 00042 int main() { 00043 #if MBED_HEAP_STATS_ENABLED 00044 mbed_stats_heap_t heap_stats; 00045 #endif 00046 00047 #ifdef MBED_MAJOR_VERSION 00048 printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); 00049 #endif 00050 00051 printf("Start WiFi test \r\n"); 00052 00053 bool result = true; 00054 int rc = 0; 00055 00056 printf("Start Connection ... \r\n"); 00057 00058 NetworkInterface *network_interface = NetworkInterface::get_default_instance(); 00059 if (NULL == network_interface) { 00060 printf("NULL network interface! Exiting application....\r\n"); 00061 return 0; 00062 } 00063 00064 printf("\n\rUsing WiFi \r\n"); 00065 printf("\n\rConnecting to WiFi..\r\n"); 00066 rc = network_interface->connect(); 00067 if(rc == 0) { 00068 printf("\n\rConnected to Network successfully\r\n"); 00069 } else { 00070 printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", rc); 00071 return 0; 00072 } 00073 00074 SocketAddress a; 00075 network_interface->get_ip_address(&a); 00076 printf("TCP client IP Address is %s\r\n", a.get_ip_address()); 00077 00078 //TCPSocket sock(network_interface); 00079 TCPSocket sock; 00080 sock.open(network_interface); 00081 00082 printf(" HTTP Connection ... \r\n"); 00083 network_interface->gethostbyname(HTTP_SERVER_NAME, &a); 00084 a.set_port(HTTP_SERVER_PORT); 00085 if (sock.connect(a) == 0) { 00086 printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); 00087 00088 // We are constructing GET command like this: 00089 #ifndef USE_HTTP_1_1 00090 // GET http://www.ifconfig.io/method HTTP/1.0\n\n 00091 strcpy(buffer, "GET http://"); 00092 strcat(buffer, HTTP_SERVER_NAME); 00093 strcat(buffer, HTTP_SERVER_FILE_PATH); 00094 strcat(buffer, " HTTP/1.0\n\n"); 00095 #else 00096 // GET /method HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n" 00097 strcpy(buffer, "GET "); 00098 strcat(buffer, HTTP_SERVER_FILE_PATH); 00099 strcat(buffer, " HTTP/1.1\r\nHost: "); 00100 strcat(buffer, HTTP_SERVER_NAME); 00101 strcat(buffer, "\r\nConnection: close\r\n\r\n"); 00102 #endif 00103 00104 // Send GET command 00105 sock.send(buffer, strlen(buffer)); 00106 00107 // Server will respond with HTTP GET's success code 00108 const int ret = sock.recv(buffer, sizeof(buffer) - 1); 00109 buffer[ret] = '\0'; 00110 00111 // Find 200 OK HTTP status in reply 00112 bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); 00113 // Find "deny" string in reply 00114 bool found_expect = find_substring(buffer, buffer + ret, HTTP_EXPECT_STR, HTTP_EXPECT_STR + strlen(HTTP_EXPECT_STR)); 00115 00116 if (!found_200_ok) result = false; 00117 if (!found_expect) result = false; 00118 00119 printf("HTTP: Received %d chars from server\r\n", ret); 00120 printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); 00121 printf("HTTP: Received '%s' status ... %s\r\n", HTTP_EXPECT_STR, found_expect ? "[OK]" : "[FAIL]"); 00122 printf("HTTP: Received massage:\r\n\r\n"); 00123 printf("%s", buffer); 00124 } 00125 00126 #if MBED_HEAP_STATS_ENABLED 00127 mbed_stats_heap_get(&heap_stats); 00128 printf("Current heap: %lu\r\n", heap_stats.current_size); 00129 printf("Max heap size: %lu\r\n", heap_stats.max_size); 00130 #endif 00131 00132 printf(" Close socket & disconnect ... \r\n"); 00133 sock.close(); 00134 00135 network_interface->disconnect(); 00136 printf(" End \r\n"); 00137 }
Generated on Wed Mar 8 2023 02:37:10 by 1.7.2