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

Revision:
18:ca499a2e7da6
Parent:
17:88ef7a078095
Child:
19:05934ee9ee67
--- a/main.cpp	Sat Oct 04 20:25:45 2014 +0000
+++ b/main.cpp	Tue Oct 21 20:39:15 2014 +0000
@@ -93,7 +93,7 @@
                                      // this section is for selecting protocol
                                      0xA212, // 0xA5A5 = enable TCP server
                                      0xA313, // 0xA5A5 = eanble TCP client
-                                     0xA414  // 0xA5A5 = enable UDP client
+                                     0xA414  // 0xA5A5 = enable UDP server
                                      };
 
 /*
@@ -141,6 +141,7 @@
 uint8_t u8server_ip_addr[4]; // server ip address in 8-bits
 char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr
 uint16_t u16tcp_server_port; // directly loaded from eeprom
+uint16_t u16enable_tcp_client, u16enable_tcp_server;// flags for enabling TCP client or TCP server
 
 char tcp_client_buffer[256]; // socket buffer
 char udp_server_buffer[256];
@@ -235,13 +236,12 @@
 
 // Timer thread for auto update
 void auto_update_timer_thread(void const* args) {
-    bool update_flag;
+    bool update_flag = true;
     
     Thread::wait(500);
     while(true) {
-        update_flag = true;
         auto_update_queue.put(&update_flag);
-        Thread::wait(1000*transmit_time_period);
+        Thread::wait(1000*transmit_time_period); // Thread::wait() in ms
     }
 }
 
@@ -250,21 +250,21 @@
 * Ethernet init
 */
 int ethernet_init(void) {
-    DBG("Start initialising ethernet\n");
+    DBG("Start initialising ethernet");
     int ret = eth.init(u8mac, str_ip_addr, str_ip_subnet, str_ip_gateway); // static
 
     if (!ret) {
-        DBG("Initialized, MAC: %s\n", eth.getMACAddress());
+        DBG("Initialized, MAC: %s", eth.getMACAddress());
     } else {
-        ERR("Error eth.init() - ret = %d\n", ret);
+        ERR("Error eth.init() - ret = %d", ret);
         return -1;
     }
 
     ret = eth.connect();
     if (!ret) {
-        DBG("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
+        DBG("IP: %s, MASK: %s, GW: %s", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
     } else {
-        ERR("Error eth.connect() - ret = %d\n", ret);
+        ERR("Error eth.connect() - ret = %d", ret);
         return -1;
     }
     
@@ -286,20 +286,13 @@
     * Configure
     */
     uart.baud(115200);
-    DBG("\r\nStarting...\r\n");
+    DBG("\r\nStarting...");
   
     /*
     * UI threads
     */
     Thread t1(uart_thread);
-    Thread t2(auto_update_timer_thread);
-    
-    //// send to uart
-    //buffer[n] = '\0';
-    //message.len = n;
-    //message.msg = buffer;
-    //uart_queue.put(&message);
-    
+    Thread t2(auto_update_timer_thread);    
     
     /*
     * FLASH
@@ -312,7 +305,7 @@
     */
     ret = ethernet_init();
     if (ret) {
-        ERR("Ethernet initialisation failed. App halted\r\n");
+        ERR("Ethernet initialisation failed. App halted.");
         while (true) {};
     }
     
@@ -323,17 +316,17 @@
 #ifdef TCP_SERVER
     tcp_server.bind(tcp_server_local_port);
     tcp_server.listen();
-    DBG("TCP server started...\r\n");
+    DBG("TCP server started...");
     tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
 #endif
 
 #ifdef TCP_CLIENT
-    //RtosTimer tcp_client_auto_update_timer(tcp_client_auto_update, osTimerPeriodic, NULL);
+
 #endif
     
 #ifdef UDP_SERVER
     ret = udp_server.bind(udp_server_local_port);
-    DBG("UDP started (sock.bind = %d)\r\n", ret);
+    DBG("UDP started (sock.bind = %d)", ret);
     udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
 #endif
 
@@ -343,43 +336,44 @@
     */
     while (true) {
 #ifdef TCP_CLIENT
-    // FOR AUTO TRANSMIT DEVICE STATUS
-    if (auto_transmit_flag == 0xA5A5) {
-        // connect to TCP server if required
-        if (!tcp_sock.is_connected()) {
-            ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port);
-            if (ret > -1) {
-                DBG("Successfully connected to %s on port %d\r\n", str_server_ip_addr, u16tcp_server_port);
-            }
-            else {
-                ERR("Unable to connect to %s on port %d\r\n", str_server_ip_addr, u16tcp_server_port);
+        // FOR AUTO TRANSMIT DEVICE STATUS
+        //if ((u16enable_tcp_client == 0xA5A5) && (auto_transmit_flag == 0xA5A5)) {
+        if (auto_transmit_flag == 0xA5A5) {
+            // connect to TCP server if required
+            if (!tcp_sock.is_connected()) {
+                ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port);
+                if (ret > -1) {
+                    DBG("Successfully connected to %s on port %d", str_server_ip_addr, u16tcp_server_port);
+                }
+                else {
+                    ERR("Unable to connect to %s on port %d", str_server_ip_addr, u16tcp_server_port);
+                }
             }
-        }
-        
-        // transmit data if connected
-        if (tcp_sock.is_connected()) {
-            osEvent evt = auto_update_queue.get(1); // timeout after 1ms
-            if (evt.status == osEventMessage) {
-                DBG("Updating...\r\n");
-                update_sending_frame(tcp_client_buffer);
-                tcp_sock.send_all(tcp_client_buffer, SENDING_PROTOCOL_LENGTH);
+                
+            // transmit data if connected
+            if (tcp_sock.is_connected()) {
+                osEvent evt = auto_update_queue.get(1); // timeout after 1ms
+                if (evt.status == osEventMessage) {
+                    DBG("Updating...");
+                    update_sending_frame(tcp_client_buffer);
+                    tcp_sock.send_all(tcp_client_buffer, SENDING_PROTOCOL_LENGTH);
+                }
             }
-        }
-    } // auto transmit
+        } // if tcp client enabled && auto transmit
 #endif
 
 
 // FOR INTERFACING
 #ifdef TCP_SERVER
         // no tcp client connected
-        if (!tcp_client.is_connected())
-        {
+        //if ((u16enable_tcp_server == 0xA5A5) && (!tcp_client.is_connected())) {
+        if (!tcp_client.is_connected()) {
             // wait for client within timeout
             ret = tcp_server.accept(tcp_client);
             
             // tcp client connected
             if (ret > -1) {
-                DBG("Connection from: %s\r\n", tcp_client.get_address());
+                DBG("Connection from: %s", tcp_client.get_address());
                 
                 // loop waiting and receiving data within timeout
                 tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
@@ -388,7 +382,7 @@
                     if (n <= 0) break;
                     
                     // got some data, test it
-                    DBG("TCP server received: %s\r\n", tcp_server_buffer);
+                    DBG("TCP server received: %s", tcp_server_buffer);
                     // process received data
                     switch (n) {
                         // length 58-bytes, Receiving protocol
@@ -400,12 +394,12 @@
                                 break;
                             else if ((id - tcp_server_buffer) > 0)
                                 break;
-                            DBG("Correct.\r\n");
+                            DBG("Correct.");
                             
                             // firstly, update outputs if required
                             // digital outputs
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                DBG("Update digital outputs\r\n");
+                                DBG("Update digital outputs");
                                 char str_dout[9];
                                 memcpy(str_dout, &tcp_server_buffer[RECEIVING_PROTOCOL_DO_POS], 8);
                                 str_dout[8] = '\0';
@@ -413,11 +407,11 @@
                             }
                             // analog output 0
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                DBG("Update analog output 0\r\n");
+                                DBG("Update analog output 0");
                             }
                             // analog output 1
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                DBG("Update analog output 1\r\n");
+                                DBG("Update analog output 1");
                             }
                             // UART
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
@@ -430,7 +424,7 @@
                             
                             // then, check query status command and sending protocol if required
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
-                                DBG("Sent device status through TCP\r\n");
+                                DBG("Sent device status through TCP");
                                 // sending protocol
                                 update_sending_frame(tcp_server_buffer);
                                 tcp_client.send_all(tcp_server_buffer, SENDING_PROTOCOL_LENGTH);
@@ -444,19 +438,19 @@
                 } // end loop if no data received within timeout
                 tcp_client.close();
             } // if client connected
-        } // if no client connected
+        } // if tcp server enabled && no client connected
 #endif
     
     
     
-// ONLY FOR CONFIGRATION
+// ALWAYS ENABLED, ONLY FOR CONFIGRATION
 #ifdef UDP_SERVER
         // wait for udp packet within timeout
         n = udp_server.receiveFrom(ep_udp_client, udp_server_buffer, sizeof(udp_server_buffer));
         if (n <= 0) continue;
 
         // got some data, test it
-        DBG("UDP received (%s) from (%s) and port (%d)\r\n", udp_server_buffer, ep_udp_client.get_address(), ep_udp_client.get_port());
+        DBG("UDP received (%s) from (%s) and port (%d)", udp_server_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)
@@ -496,23 +490,23 @@
 #ifdef NTP
                     char str_time[50];
                     
-                    DBG("Trying to update time...\r\n");
+                    DBG("Trying to update time...");
                     if (ntp.setTime("0.pool.ntp.org") == 0) {
-                        DBG("Set time successfully\r\n");
+                        DBG("Set time successfully");
                         time_t ctTime;
                         ctTime = time(NULL);
                         
-                        DBG("Time is set to (UTC): %s\r\n", ctime(&ctTime));
+                        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\r\n");
+                        WARN("Error");
                         sprintf(str_time, "ERR");
                         udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
                     }
 #elif
-                    WARN("NTP disabled\r\n");
+                    WARN("NTP disabled");
                     sprintf(str_time, "DIS");
                     udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
 #endif
@@ -530,7 +524,7 @@
                 else if ((id - udp_server_buffer) > 0)
                     break;
 
-                DBG("Received user configuration\r\n");
+                DBG("Received user configuration");
                 write_eeprom_network(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char, 15-bytes
                 break;
             }
@@ -545,7 +539,7 @@
                 else if ((id - udp_server_buffer) > 0)
                     break;
                 
-                DBG("Received TCP server configuration\r\n");
+                DBG("Received TCP server configuration");
                 write_eeprom_tcpserver(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char
                 break;
             }
@@ -560,7 +554,7 @@
 * Update digital outputs following receiving frame from TCP client
 */
 void update_digital_outputs(char* buf) {
-    DBG("Digital outputs: %s\n", buf);
+    DBG("Digital outputs: %s", buf);
     
     dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0;
     dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0;