GigaDevice
/
GD32_example_ethernet_webserver
A simple example of ethernet webserver for mbed-os.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018 GigaDevice Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 */ 00005 00006 #include "mbed.h" 00007 #include "EthernetInterface.h" 00008 #include "TCPServer.h" 00009 #include "TCPSocket.h" 00010 00011 /* 1 = Use DHCP */ 00012 /* 0 = Use default IP */ 00013 #define DEMO_DHCP_ENABLE 0 00014 00015 #if (0 == DEMO_DHCP_ENABLE) 00016 #define DEVICE_IP "192.168.57.156" 00017 #define DEVICE_MASK "255.255.255.0" 00018 #define DEVICE_GATEWAY "192.168.57.1" 00019 #endif 00020 00021 const char html_contex[] = { 00022 /* HTTP header */ 00023 /* "HTTP/1.0 200 OK 00024 " (17 bytes) */ 00025 0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x30,0x20,0x32,0x30,0x30,0x20,0x4f,0x4b,0x0d, 00026 0x0a, 00027 /* "Content-type: text/html 00028 " (27 bytes) */ 00029 0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x74,0x79,0x70,0x65,0x3a,0x20,0x74,0x65, 00030 0x78,0x74,0x2f,0x68,0x74,0x6d,0x6c,0x0d,0x0a,0x0d,0x0a, 00031 /* raw file data (380 bytes) */ 00032 0x3c,0x21,0x44,0x4f,0x43,0x54,0x59,0x50,0x45,0x20,0x68,0x74,0x6d,0x6c,0x20,0x50, 00033 0x55,0x42,0x4c,0x49,0x43,0x20,0x22,0x2d,0x2f,0x2f,0x57,0x33,0x43,0x2f,0x2f,0x44, 00034 0x54,0x44,0x20,0x58,0x48,0x54,0x4d,0x4c,0x20,0x31,0x2e,0x30,0x20,0x54,0x72,0x61, 00035 0x6e,0x73,0x69,0x74,0x69,0x6f,0x6e,0x61,0x6c,0x2f,0x2f,0x45,0x4e,0x22,0x20,0x22, 00036 0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x77,0x77,0x77,0x2e,0x77,0x33,0x2e,0x6f,0x72, 00037 0x67,0x2f,0x54,0x52,0x2f,0x78,0x68,0x74,0x6d,0x6c,0x31,0x2f,0x44,0x54,0x44,0x2f, 00038 0x78,0x68,0x74,0x6d,0x6c,0x31,0x2d,0x74,0x72,0x61,0x6e,0x73,0x69,0x74,0x69,0x6f, 00039 0x6e,0x61,0x6c,0x2e,0x64,0x74,0x64,0x22,0x3e,0x0d,0x0a,0x3c,0x68,0x74,0x6d,0x6c, 00040 0x20,0x78,0x6d,0x6c,0x6e,0x73,0x3d,0x22,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x77, 00041 0x77,0x77,0x2e,0x77,0x33,0x2e,0x6f,0x72,0x67,0x2f,0x31,0x39,0x39,0x39,0x2f,0x78, 00042 0x68,0x74,0x6d,0x6c,0x22,0x3e,0x0d,0x0a,0x3c,0x68,0x65,0x61,0x64,0x3e,0x0d,0x0a, 00043 0x3c,0x6d,0x65,0x74,0x61,0x20,0x68,0x74,0x74,0x70,0x2d,0x65,0x71,0x75,0x69,0x76, 00044 0x3d,0x22,0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54,0x79,0x70,0x65,0x22,0x20, 00045 0x63,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x3d,0x22,0x74,0x65,0x78,0x74,0x2f,0x68,0x74, 00046 0x6d,0x6c,0x3b,0x20,0x63,0x68,0x61,0x72,0x73,0x65,0x74,0x3d,0x75,0x74,0x66,0x2d, 00047 0x38,0x22,0x20,0x2f,0x3e,0x0d,0x0a,0x3c,0x2f,0x68,0x65,0x61,0x64,0x3e,0x0d,0x0a, 00048 0x3c,0x62,0x6f,0x64,0x79,0x3e,0x0d,0x0a,0x3c,0x64,0x69,0x76,0x20,0x61,0x6c,0x69, 00049 0x67,0x6e,0x3d,0x22,0x63,0x65,0x6e,0x74,0x65,0x72,0x22,0x3e,0x0d,0x0a,0x20,0x20, 00050 0x3c,0x68,0x31,0x3e,0x3c,0x73,0x74,0x72,0x6f,0x6e,0x67,0x3e,0x48,0x65,0x6c,0x6c, 00051 0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x0d,0x0a,0x20,0x20,0x3c,0x2f,0x73,0x74,0x72, 00052 0x6f,0x6e,0x67,0x3e,0x3c,0x2f,0x68,0x31,0x3e,0x0d,0x0a,0x3c,0x2f,0x64,0x69,0x76, 00053 0x3e,0x0d,0x0a,0x3c,0x68,0x72,0x20,0x2f,0x3e,0x0d,0x0a,0x3c,0x70,0x3e,0x26,0x6e, 00054 0x62,0x73,0x70,0x3b,0x3c,0x2f,0x70,0x3e,0x0d,0x0a,0x3c,0x2f,0x62,0x6f,0x64,0x79, 00055 0x3e,0x0d,0x0a,0x3c,0x2f,0x68,0x74,0x6d,0x6c,0x3e,0x0d,0x0a,}; 00056 00057 EthernetInterface enet; 00058 00059 int main() 00060 { 00061 nsapi_size_or_error_t result; 00062 00063 printf("Mbed OS HTTP server example start \r\n"); 00064 00065 #if (0 == DEMO_DHCP_ENABLE) 00066 /* Disable DHCP */ 00067 enet.set_dhcp(0); 00068 enet.set_network(DEVICE_IP, DEVICE_MASK, DEVICE_GATEWAY); 00069 #else 00070 /* Enable DHCP */ 00071 enet.set_dhcp(1); 00072 #endif 00073 00074 if(enet.connect() != 0){ 00075 printf("Network initialization failed \r\n"); 00076 while(1); 00077 }else{ 00078 printf("The device IP address is '%s' \r\n", enet.get_ip_address()); 00079 printf("The device NetMask is '%s' \r\n", enet.get_netmask()); 00080 printf("The device GateWay is '%s' \r\n", enet.get_gateway()); 00081 } 00082 00083 TCPServer http_srv; 00084 TCPSocket cli_sock; 00085 SocketAddress cli_addr; 00086 00087 result = http_srv.open(&enet); 00088 if (result != 0) { 00089 printf("Error! http_srv.open() returned: %d\n", result); 00090 } 00091 00092 /* Bind socket to the HTTP port(80) */ 00093 result = http_srv.bind(enet.get_ip_address(), 80); 00094 if (result != 0) { 00095 printf("Error! http_srv.bind() returned: %d\n", result); 00096 } 00097 00098 http_srv.listen(5); 00099 00100 while (1) { 00101 http_srv.accept(&cli_sock, &cli_addr); 00102 printf("Server accept successful, remote is %s:%d\n", cli_addr.get_ip_address(), cli_addr.get_port()); 00103 cli_sock.send(html_contex, sizeof(html_contex)); 00104 } 00105 }
Generated on Tue Jul 12 2022 15:38:49 by 1.7.2