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:
Thu Nov 27 18:01:15 2014 +0000
Parent:
22:e5f0aa929c44
Child:
24:ca0199b8a3aa
Commit message:
Update checking a package received through UDP is a configuration command or data

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
protocol.txt Show annotated file Show diff for this revision Revisions of this file
readme.txt Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Nov 16 17:39:16 2014 +0000
+++ b/main.cpp	Thu Nov 27 18:01:15 2014 +0000
@@ -1,7 +1,6 @@
 /*
 *
 *  Alarm and Monitoring application
-
 */
 #include "mbed.h"
 #include "eeprom.h"
@@ -218,8 +217,6 @@
 Queue<message_t, 16> uart_queue;
 Queue<bool, 1> auto_update_queue;
 
-Mutex uart_mutex;
-
 
 /*
 * Threads
@@ -372,7 +369,7 @@
         while (true) {};
     }
     
-    Thread::wait(2000); // turn on delay
+    Thread::wait(2000); // TCP/UDP stack delay
 
 /*
 * UDP server
@@ -473,101 +470,108 @@
             while (n > 0) {
                 // got some data, test it
                 DBG("UDP received (%s) from (%s) and port (%d)", udp_receiving_buffer, ep_udp_client.get_address(), ep_udp_client.get_port());
+                
                 // process received data
-                switch (n) {
-                    // length = 6, a QUERY command (discovery command, TCP port, or UDP port)
-                    // Format: NNIODS, NNIOTP, NNIOUP, NNIOTM
-                    case QUERY_CMD_LENGTH:
-                        // discovery command
-                        if (strstr(udp_receiving_buffer, QUERY_DISCOVERY_CMD) != NULL) {
-                            char str[30];
-                            sprintf(str, "%s%s", DEVICE_ID, eth.getIPAddress());
-                            udp_server.sendTo(ep_udp_client, str, strlen(str));
-                        } // NNIODS
-                        else if (strstr(udp_receiving_buffer, QUERY_IP_CMD) != NULL) {
-                            udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress()));
-                        } // NNIOIP
-                        else if (strstr(udp_receiving_buffer, QUERY_SUBNET_CMD) != NULL) {
-                            udp_server.sendTo(ep_udp_client, eth.getNetworkMask(), strlen(eth.getNetworkMask()));
-                        } // NNIOSN
-                        else if (strstr(udp_receiving_buffer, QUERY_GATEWAY_CMD) != NULL) {
-                            udp_server.sendTo(ep_udp_client, eth.getGateway(), strlen(eth.getGateway()));
-                        } // NNIOGW
-                        else if (strstr(udp_receiving_buffer, QUERY_MAC_CMD) != NULL) {
-                            udp_server.sendTo(ep_udp_client, eth.getMACAddress(), strlen(eth.getMACAddress()));
-                        } // NNIOMC
-                        // ask for TCP server port
-                        else if (strstr(udp_receiving_buffer, QUERY_TCP_PORT_CMD) != NULL) {
-                            char port[5];
-                            sprintf(port, "%5d", tcp_server_local_port);
-                            udp_server.sendTo(ep_udp_client, port, strlen(port));
-                        } // NNIOTP
-                        // ask for UDP server port
-                        else if (strstr(udp_receiving_buffer, QUERY_UDP_PORT_CMD) != NULL) {
-                            char port[5];
-                            sprintf(port, "%5d", udp_server_local_port);
-                            udp_server.sendTo(ep_udp_client, port, strlen(port));
-                        } // NNIOUP
-                        else if (strstr(udp_receiving_buffer, QUERY_UPDATE_TIME_CMD) != NULL) {
-#ifdef NTP
-                            char str_time[50];
-                            
-                            DBG("Trying to update time...");
-                            if (ntp.setTime("0.pool.ntp.org") == 0) {
-                                DBG("Set time successfully");
-                                time_t ctTime;
-                                ctTime = time(NULL);
+                // a configuration command always starts with NN
+                if ((udp_receiving_buffer[0] == 'N') && (udp_receiving_buffer[1] == 'N')) {
+                    switch (n) {
+                        // length = 6, a QUERY command (discovery command, TCP port, or UDP port)
+                        // Format: NNIODS, NNIOTP, NNIOUP, NNIOTM
+                        case QUERY_CMD_LENGTH:
+                            // discovery command
+                            if (strstr(udp_receiving_buffer, QUERY_DISCOVERY_CMD) != NULL) {
+                                char str[30];
+                                sprintf(str, "%s%s", DEVICE_ID, eth.getIPAddress());
+                                udp_server.sendTo(ep_udp_client, str, strlen(str));
+                            } // NNIODS
+                            else if (strstr(udp_receiving_buffer, QUERY_IP_CMD) != NULL) {
+                                udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress()));
+                            } // NNIOIP
+                            else if (strstr(udp_receiving_buffer, QUERY_SUBNET_CMD) != NULL) {
+                                udp_server.sendTo(ep_udp_client, eth.getNetworkMask(), strlen(eth.getNetworkMask()));
+                            } // NNIOSN
+                            else if (strstr(udp_receiving_buffer, QUERY_GATEWAY_CMD) != NULL) {
+                                udp_server.sendTo(ep_udp_client, eth.getGateway(), strlen(eth.getGateway()));
+                            } // NNIOGW
+                            else if (strstr(udp_receiving_buffer, QUERY_MAC_CMD) != NULL) {
+                                udp_server.sendTo(ep_udp_client, eth.getMACAddress(), strlen(eth.getMACAddress()));
+                            } // NNIOMC
+                            // ask for TCP server port
+                            else if (strstr(udp_receiving_buffer, QUERY_TCP_PORT_CMD) != NULL) {
+                                char port[5];
+                                sprintf(port, "%5d", tcp_server_local_port);
+                                udp_server.sendTo(ep_udp_client, port, strlen(port));
+                            } // NNIOTP
+                            // ask for UDP server port
+                            else if (strstr(udp_receiving_buffer, QUERY_UDP_PORT_CMD) != NULL) {
+                                char port[5];
+                                sprintf(port, "%5d", udp_server_local_port);
+                                udp_server.sendTo(ep_udp_client, port, strlen(port));
+                            } // NNIOUP
+                            else if (strstr(udp_receiving_buffer, QUERY_UPDATE_TIME_CMD) != NULL) {
+    #ifdef NTP
+                                char str_time[50];
                                 
-                                DBG("Time is set to (UTC): %s", ctime(&ctTime));
-                                sprintf(str_time, "%s", ctime(&ctTime));
+                                DBG("Trying to update time...");
+                                if (ntp.setTime("0.pool.ntp.org") == 0) {
+                                    DBG("Set time successfully");
+                                    time_t ctTime;
+                                    ctTime = time(NULL);
+                                    
+                                    DBG("Time is set to (UTC): %s", ctime(&ctTime));
+                                    sprintf(str_time, "%s", ctime(&ctTime));
+                                    udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
+                                }
+                                else {
+                                    WARN("Error");
+                                    sprintf(str_time, "ERR");
+                                    udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
+                                }
+    #elif
+                                WARN("NTP disabled");
+                                sprintf(str_time, "DIS");
                                 udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
-                            }
-                            else {
-                                WARN("Error");
-                                sprintf(str_time, "ERR");
-                                udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
-                            }
-#elif
-                            WARN("NTP disabled");
-                            sprintf(str_time, "DIS");
-                            udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
-#endif
-                        } // NNIOTM
-        
-                        break;
-                    // length = 19, SET NETWORK CONFIGURATION
-                    // Format: 4E 4E 49 4F      C0 A8 00 78        FF FF FF 00            C0 A8 00 01      00 00 01
-                    //        (NNIO;            IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1)
-                    case SET_NETWORK_CONFIG_CMD_LENGTH: {
-                        // check device id
-                        char* id = strstr(udp_receiving_buffer, DEVICE_ID);
-                        if (id == NULL)
+    #endif
+                            } // NNIOTM
+            
                             break;
-                        else if ((id - udp_receiving_buffer) > 0)
+                        // length = 19, SET NETWORK CONFIGURATION
+                        // Format: 4E 4E 49 4F      C0 A8 00 78        FF FF FF 00            C0 A8 00 01      00 00 01
+                        //        (NNIO;            IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1)
+                        case SET_NETWORK_CONFIG_CMD_LENGTH: {
+                            // check device id
+                            char* id = strstr(udp_receiving_buffer, DEVICE_ID);
+                            if (id == NULL)
+                                break;
+                            else if ((id - udp_receiving_buffer) > 0)
+                                break;
+            
+                            DBG("Received user configuration");
+                            write_eeprom_network(&udp_receiving_buffer[strlen(DEVICE_ID)]); // parameters from 5th char, 15-bytes
                             break;
-        
-                        DBG("Received user configuration");
-                        write_eeprom_network(&udp_receiving_buffer[strlen(DEVICE_ID)]); // parameters from 5th char, 15-bytes
-                        break;
-                    }
-                    // length = 12, SET TCP SERVER CONFIGURATION
-                    // auto update & its time period, TCP server configuration (IP & port)
-                    // Format: 4E 4E 49 4F   'Y'     01   C0 A8 00 09   E0 2E (LSB MSB)
-                    //         NNIO          Auto   1s   192.168.0.9   12000
-                    case UPDATE_TCP_SERVER_INFO_CMD_LENGTH: {
-                        char* id = strstr(udp_receiving_buffer, DEVICE_ID);
-                        if (id == NULL)
+                        }
+                        // length = 12, SET TCP SERVER CONFIGURATION
+                        // auto update & its time period, TCP server configuration (IP & port)
+                        // Format: 4E 4E 49 4F   'Y'     01   C0 A8 00 09   E0 2E (LSB MSB)
+                        //         NNIO          Auto   1s   192.168.0.9   12000
+                        case UPDATE_TCP_SERVER_INFO_CMD_LENGTH: {
+                            char* id = strstr(udp_receiving_buffer, DEVICE_ID);
+                            if (id == NULL)
+                                break;
+                            else if ((id - udp_receiving_buffer) > 0)
+                                break;
+                            
+                            DBG("Received TCP server configuration");
+                            write_eeprom_tcpserver(&udp_receiving_buffer[strlen(DEVICE_ID)]); // parameters from 5th char
                             break;
-                        else if ((id - udp_receiving_buffer) > 0)
+                        }
+                        default:
                             break;
-                        
-                        DBG("Received TCP server configuration");
-                        write_eeprom_tcpserver(&udp_receiving_buffer[strlen(DEVICE_ID)]); // parameters from 5th char
-                        break;
-                    }
-                    default:
-                        break;
-                } // switch (n)
+                    } // switch (n), check configuration command length
+                } // if starts with NN, a config command
+                else { // if not a command, check to see if it is a data package
+                    // process data package
+                }
                 
                 // wait to receive new config command
                 udp_server.set_blocking(true);
@@ -621,9 +625,9 @@
     memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); // LSB MSB
     val = ain1.read_u16(); // 16-bits normalised
     memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); // LSB MSB
-    val = 0x0180;
+    val = 0x1234;
     memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); // LSB MSB
-    val = 0x0180;
+    val = 0x5678;
     memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); // LSB MSB
     buf[SENDING_PROTOCOL_CR_POS] = 0x0D;
     buf[SENDING_PROTOCOL_CR_POS+1] = '\0';
--- a/protocol.txt	Sun Nov 16 17:39:16 2014 +0000
+++ b/protocol.txt	Thu Nov 27 18:01:15 2014 +0000
@@ -1,4 +1,5 @@
 CONFIGURATION SECTION (UDP)
+A configuration command always starts with "NN"
 1. DISCOVERY Command
     + UDP broadcast: 192.168.0.255 to port 11000
     + Send: NNIODS
--- a/readme.txt	Sun Nov 16 17:39:16 2014 +0000
+++ b/readme.txt	Thu Nov 27 18:01:15 2014 +0000
@@ -13,3 +13,6 @@
 v1.1 15/11/2014
     + Added: watchdog timer
     + Modified: enter config mode forever when received discovery command
+
+v1.11 27/11/2014
+    + Added: checking a configuration command to see if it starts with "NN". Otherwise, it could be data was sent using UDP connection.
\ No newline at end of file