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 Chau Vo

Files at this revision

API Documentation at this revision

Comitter:
olympux
Date:
Sun Sep 21 20:00:21 2014 +0000
Parent:
6:d054e394fba3
Child:
8:64848959adb9
Commit message:
Added commands for discovery, query ports

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Sep 21 19:35:32 2014 +0000
+++ b/main.cpp	Sun Sep 21 20:00:21 2014 +0000
@@ -50,9 +50,9 @@
 #define UDP_SERVER_RECEIVE_TIMEOUT         200
 
 // for static IP setting
-char * IP_Addr    = "192.168.0.120";
-char * IP_Subnet  = "255.255.255.0";
-char * IP_Gateway = "192.168.0.1";
+char * IP_Addr;
+char * IP_Subnet;
+char * IP_Gateway;
 char   ip_addr[16], ip_subnet[16], ip_gateway[16]; // loaded from eeprom
 
 uint16_t tcp_server_port = 10000; // fixed
@@ -103,11 +103,11 @@
     mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; 
     
     
-    printf("Start\n");
+    //printf("Start\n");
     int ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static
 
     if (!ret) {
-        uart.printf("Initialized, MAC: %s\n", eth.getMACAddress());
+        //uart.printf("Initialized, MAC: %s\n", eth.getMACAddress());
     } else {
         uart.printf("Error eth.init() - ret = %d\n", ret);
         return -1;
@@ -115,7 +115,7 @@
 
     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;
@@ -184,11 +184,11 @@
     sprintf(ip_addr, "%d.%d.%d.%d", (uint8_t)u16ip_addr[0], (uint8_t)u16ip_addr[1], (uint8_t)u16ip_addr[2], (uint8_t)u16ip_addr[3]);
     sprintf(ip_subnet, "%d.%d.%d.%d", (uint8_t)u16ip_subnet[0], (uint8_t)u16ip_subnet[1], (uint8_t)u16ip_subnet[2], (uint8_t)u16ip_subnet[3]);
     sprintf(ip_gateway, "%d.%d.%d.%d", (uint8_t)u16ip_gateway[0], (uint8_t)u16ip_gateway[1], (uint8_t)u16ip_gateway[2], (uint8_t)u16ip_gateway[3]);
-    printf("IP: %s\n", ip_addr);
-    printf("MASK: %s\n", ip_subnet);
-    printf("GW: %s\n", ip_gateway);
-    printf("TCP: %d\n", tcp_server_port);
-    printf("UDP: %d\n", udp_server_port);
+    //printf("IP: %s\n", ip_addr);
+    //printf("MASK: %s\n", ip_subnet);
+    //printf("GW: %s\n", ip_gateway);
+    //printf("TCP: %d\n", tcp_server_port);
+    //printf("UDP: %d\n", udp_server_port);
 }
 
 
@@ -235,7 +235,7 @@
     
     tcp_server.bind(tcp_server_port);
     tcp_server.listen();
-    uart.printf("\nWait for new connection...\n");
+    //uart.printf("\nWait for new connection...\n");
     tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
 #endif
     
@@ -244,7 +244,7 @@
     Endpoint ep_udp_client;
     
     ret = udp_server.bind(udp_server_port);
-    printf("sock.bind = %d\n", ret);
+    //printf("sock.bind = %d\n", ret);
     udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
 #endif
 
@@ -260,7 +260,7 @@
             
             // tcp client connected
             if (ret > -1) {
-                uart.printf("Connection from: %s\n", tcp_client.get_address());
+                //uart.printf("Connection from: %s\n", tcp_client.get_address());
                 
                 // loop waiting and receiving data within timeout
                 tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
@@ -285,30 +285,50 @@
 #endif
     
 
-#ifdef UDP_SERVER
+#ifdef UDP_SERVER // used for setting configuration
         // wait for udp packet within timeout
         int n = udp_server.receiveFrom(ep_udp_client, buffer, sizeof(buffer));
         if (n < 0) continue;
 
         // got some data, process it
         // send to uart
-        buffer[n] = '\0';
-        message.len = n;
-        message.msg = buffer;
-        uart_queue.put(&message);
+        //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);
+        //udp_server.sendTo(ep_udp_client, buffer, n);
         
         // process received data
         switch (n) {
+            // length = 4, may be this is a discovery command, TCP port, or UDP port
+            // Format: NNDS, NNTP or NNUP
+            case 4:
+                // discovery command
+                if (strstr(buffer, "NNDS") != NULL) {
+                    udp_server.sendTo(ep_udp_client, ip_addr, strlen(ip_addr));
+                }
+                // ask for TCP server port
+                else if (strstr(buffer, "NNTP") != NULL) {
+                    char port[5];
+                    sprintf(port, "%5d", tcp_server_port);
+                    udp_server.sendTo(ep_udp_client, port, strlen(port));
+                }
+                // ask for UDP server port
+                else if (strstr(buffer, "NNUP") != NULL) {
+                    char port[5];
+                    sprintf(port, "%5d", udp_server_port);
+                    udp_server.sendTo(ep_udp_client, port, strlen(port));
+                }
+                break;
             // length = 14, maybe this is a command to set network configuration
             // Format: 4E 4E C0 A8 00 78 FF FF FF 00 C0 A8 0 1 (NN 192.168.0.120; 255.255.255.0; 192.168.0.1)
             case 14:
                 // check if two first chars = NN
                 if (strstr(buffer, "NN") != NULL) {
-                    printf("Received new network configuration\n");
+                    //printf("Received new network configuration\n");
                     write_eeprom(&buffer[2]); // parameters from 3rd char
                 }
                 break;