Webserver only w/o any other functions, single thread. Running on STM32F013+W5500
Dependencies: NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed
Fork of F103-Serial-to-Ethernet by
Revision 3:972ed747474c, committed 2014-09-21
- Comitter:
- olympux
- Date:
- Sun Sep 21 08:15:57 2014 +0000
- Parent:
- 2:18f10e7209f4
- Child:
- 4:568c97f2a407
- Commit message:
- Add TCP/UDP server/client. Tested TCP/UDP server separately.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Sep 20 21:33:04 2014 +0000 +++ b/main.cpp Sun Sep 21 08:15:57 2014 +0000 @@ -26,6 +26,7 @@ * Network configuration */ #define TCP_SERVER_PORT 10000 +#define UDP_LOCAL_PORT 11000 //#define USE_DHCP // DHCP or static #ifndef USE_DHCP @@ -35,6 +36,14 @@ const char * IP_Gateway = "192.168.0.1"; #endif +#define TCP_SERVER +//#define TCP_CLIENT +//#define UDP_SERVER +//#define UDP_CLIENT +//#define NTP + +char buffer[256]; // socket buffer + /* @@ -44,8 +53,9 @@ int len; char *msg; }; -Queue<message_t, 16> queue; +Queue<message_t, 16> uart_queue; +Mutex uart_mutex; /* @@ -55,11 +65,13 @@ message_t *p_message; while (true) { - osEvent evt = queue.get(); + osEvent evt = uart_queue.get(); if (evt.status == osEventMessage) { p_message = (message_t*)evt.value.p; - uart.printf("len=%d\n",p_message->len); - uart.printf("msg=%s\n",p_message->msg); + uart_mutex.lock(); + //uart.printf("len=%d\n", p_message->len); + uart.printf("%s\n", p_message->msg); + uart_mutex.unlock(); } } } @@ -67,17 +79,31 @@ int main() { - Thread t1(uart_thread); - //Thread t2(tcp_server_thread); + message_t message; - uint8_t mac[6]; + /* + * configure + */ + uart.baud(115200); + /* + * UI threads + */ + Thread t1(uart_thread); + + + + + /* + * Ethernet + */ + uint8_t mac[6]; + mbed_mac_address((char *)mac); // using the MAC address in LPC11U24 or LPC1178 mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; -// you can alo use WIZ550io's MAC address by enabling "USE_WIZ550IO_MAC" in wiznet.h - uart.printf("Start\n"); + printf("Start\n"); #ifdef USE_DHCP int ret = eth.init(mac); //Use DHCP #else @@ -93,41 +119,174 @@ ret = eth.connect(); if (!ret) { - uart.printf("IP: %s, MASK: %s, GW: %s\n", - eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); + uart.printf("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); } else { uart.printf("Error eth.connect() - ret = %d\n", ret); return -1; } + + +#ifdef TCP_SERVER + TCPSocketServer tcp_server; + TCPSocketConnection tcp_client; + + tcp_server.bind(TCP_SERVER_PORT); + tcp_server.listen(); +#endif + +#ifdef UDP_SERVER + UDPSocket udp_server; + Endpoint ep_udp_client; + + ret = udp_server.bind(UDP_LOCAL_PORT); + //printf("sock.bind = %d\n", ret); +#endif - TCPSocketServer server; - server.bind(TCP_SERVER_PORT); - server.listen(); +#ifdef TCP_CLIENT +TCPSocketConnection tcp_socket; +#endif +#ifdef UDP_CLIENT + UDPSocket udp_socket; + Endpoint ep_udp_server; +#endif + + +/* +* TCP server +*/ +#ifdef TCP_SERVER while (true) { - uart.printf("\nWait for new connection...\n"); - TCPSocketConnection client; - server.accept(client); - client.set_blocking(false, 10000); // Timeout after (10)s + //uart.printf("\nWait for new connection...\n"); + tcp_server.accept(tcp_client); + tcp_client.set_blocking(false, 10000); // Timeout after (10)s - uart.printf("Connection from: %s\n", client.get_address()); - char buffer[256]; - message_t message; + //uart.printf("Connection from: %s\n", client.get_address()); while (true) { - int n = client.receive(buffer, sizeof(buffer)); + int n = tcp_client.receive(buffer, sizeof(buffer)); if (n <= 0) break; // send to uart buffer[n] = '\0'; message.len = n; message.msg = buffer; - queue.put(&message); + uart_queue.put(&message); // echo to tcp client - client.send_all(buffer, n); + tcp_client.send_all(buffer, n); if (n <= 0) break; } - client.close(); + tcp_client.close(); + } +#endif + +/* +* UDP server +*/ +#ifdef UDP_SERVER + while (true) { + //printf("\nWait for packet...\n"); + int n = udp_server.receiveFrom(ep_udp_client, buffer, sizeof(buffer)); + if (n < 0) continue; + + // send to uart + buffer[n] = '\0'; + message.len = n; + message.msg = buffer; + uart_queue.put(&message); + + // echo + //printf("Received packet from: %s\n", client.get_address()); + udp_server.sendTo(ep_udp_client, buffer, n); + } +#endif + + +/* +* TCP client +*/ +#ifdef TCP_CLIENT + while (tcp_socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { + printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); + wait(1); } + + char hello[] = "Hello World\n"; + tcp_socket.send_all(hello, sizeof(hello) - 1); + + char buf[256]; + int n = tcp_socket.receive(buf, 256); + buf[n] = '\0'; + printf("%s", buf); + + tcp_socket.close(); + eth.disconnect(); + + while(true) {} +#endif + + +/* +* UDP client +*/ +#ifdef UDP_CLIENT + ret = udp_socket.init(); + udp_socket.bind(0); + printf("sock.bind = %d\n", ret); + if (ret == -1) printf("Socket creation Fail\n"); + + ep_udp_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); + + printf("\nSend UDP data\n"); + + char out_buffer[] = "Hello World\n"; + ret = udp_socket.sendTo(ep_udp_server, out_buffer, sizeof(out_buffer)); + if (ret < 0) printf("UDP Send Error\n"); + else printf("UDP Send: %d\n", ret); + + char in_buffer[256]; + int n = udp_socket.receiveFrom(ep_udp_server, in_buffer, sizeof(in_buffer)); + + in_buffer[n] = '\0'; + printf("%s\n", in_buffer); + + udp_socket.close(); + + eth.disconnect(); + while(1) {} +#endif + + +/* +* NTP +*/ +#ifdef NTP + time_t ctTime; + ctTime = time(NULL); + printf("1. Current Time is: %s\r\n", ctime(&ctTime)); + + printf("Trying to update time...\r\n"); + if (ntp.setTime("0.pool.ntp.org") == 0) { + ctTime = time(NULL); + printf("2. Current Time is: %s\r\n", ctime(&ctTime)); + + // resetting GMT+9 + set_time( time(NULL) + 32400 ); // 9x60x60 + // + printf("Set time successfully\r\n"); + //time_t ctTime; + ctTime = time(NULL); + printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); + } else { + printf("Error\r\n"); + } + + eth.disconnect(); + + while(1) { + } +#endif + + while(true) {} }