Machine Vision Status TCP Server

Dependencies:   C12832 EthernetInterface mbed-rtos mbed ConfigFile

Files at this revision

API Documentation at this revision

Comitter:
dwini
Date:
Mon Jun 15 14:41:41 2015 +0000
Parent:
8:845dfadaa70d
Commit message:
Add config file

Changed in this revision

ConfigFile.lib Show annotated file Show diff for this revision Revisions of this file
Configuration.cpp Show annotated file Show diff for this revision Revisions of this file
Configuration.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile.lib	Mon Jun 15 14:41:41 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Configuration.cpp	Mon Jun 15 14:41:41 2015 +0000
@@ -0,0 +1,148 @@
+#include "Configuration.h"
+#include "Log.h"
+
+#define CFG_BUFF_SIZE   128
+
+namespace MachineVision{
+    
+    Configuration::Configuration(void) {
+        // Defaults
+        this->use_dhcp = true;
+        this->ip = "10.0.0.100";
+        this->netmask = "255.255.255.0";
+        this->gateway = "10.0.0.254";
+        this->tcp_port = 6666;
+    }
+    
+    bool Configuration::writeConfiguration(LocalFileSystem * fs, char * configfile) {
+        // Create config
+        ConfigFile cfg;
+        char buffer[CFG_BUFF_SIZE];
+        
+        sprintf(buffer, "%d", this->useDhcp());        
+        if (!cfg.setValue("DHCP", buffer)) {
+            Log::e("Failed to set value for DHCP.\n");
+            return false;
+        }    
+     
+        if (!cfg.setValue("IP_ADDRESS", (char *)this->getIpAddress().c_str())) {
+            Log::e("Failed to set value for IP_ADDRESS.\n");
+            return false;
+        }   
+     
+        if (!cfg.setValue("NETMASK", (char *)this->getNetmask().c_str())) {
+            Log::e("Failed to set value for NETMASK.\n");
+            return false;
+        }    
+     
+        if (!cfg.setValue("GATEWAY", (char *)this->getGateway().c_str())) {
+            Log::e("Failed to set value for GATEWAY.\n");
+            return false;
+        }    
+     
+        sprintf(buffer, "%d", this->getTcpPort());     
+        if (!cfg.setValue("TCP_PORT", buffer)) {
+            Log::e("Failed to set value for TCP_PORT.\n");
+            return false;
+        }    
+
+        // Write config to local filesystem
+        if (!cfg.write(configfile)) {
+            Log::e("Failed to write configuration file\r\n");
+            return false;
+        }
+        
+        return true;
+    }
+    
+    /**
+     * Read config from filesystem.
+     *
+     * @param configfile The path to the config file
+     */
+    bool Configuration::readFromFile(LocalFileSystem * fs, char * configfile) {       
+        // Read config from local filesystem
+        ConfigFile cfg;
+        if (!cfg.read(configfile)) {
+            Log::e("Failure to read configuration file\r\n");
+            return false;
+        }
+
+        // Parse config params
+        char buffer[CFG_BUFF_SIZE];
+        if (readConfigParameter(&cfg, "DHCP", buffer, CFG_BUFF_SIZE)){
+            if (buffer[0] == '0'){   // No DHCP
+                this->use_dhcp = false;
+            } else {
+                this->use_dhcp = true;
+            }
+        } else {
+            Log::e("DHCP config could not be read\r\n");
+            return false;
+        }
+
+        if (!this->use_dhcp) {      // Static IP
+            if (readConfigParameter(&cfg, "IP_ADDRESS", buffer, CFG_BUFF_SIZE)){
+                this->ip = std::string(buffer);
+            } else {
+                Log::e("IP_ADDRESS config could not be read\r\n");
+                return false;
+            }
+            
+            if (readConfigParameter(&cfg, "NETMASK", buffer, CFG_BUFF_SIZE)){
+                this->netmask = std::string(buffer);
+            } else {
+                Log::e("NETMASK config could not be read\r\n");
+                return false;
+            }
+            
+            if (readConfigParameter(&cfg, "GATEWAY", buffer, CFG_BUFF_SIZE)){
+                this->gateway = std::string(buffer);
+            } else {
+                Log::e("GATEWAY config could not be read\r\n");
+                return false;
+            }
+        }
+            
+        if (readConfigParameter(&cfg, "TCP_PORT", buffer, CFG_BUFF_SIZE)){
+            this->tcp_port = atoi(buffer);
+        } else {
+            Log::e("TCP_PORT config could not be read\r\n");
+            return false;
+        }
+        
+        return true;
+    }
+    
+    bool Configuration::readConfigParameter(ConfigFile * cfg, char * key, char * value, int buffer_size) {
+        if (cfg->getValue(key, &value[0], buffer_size)) {
+            Log::v("Read config: '%s'='%s'\r\n", key, value);
+            return true;
+        } else {
+            Log::e("Could not find config key: '%s'\r\n", key);
+            return false;
+        }
+    }
+    
+    bool Configuration::useDhcp(void) {
+        return this->use_dhcp;
+    }
+    
+    std::string Configuration::getIpAddress(void) {
+        return this->ip;
+    }
+    
+    std::string Configuration::getNetmask(void) {
+        return this->netmask;
+    }
+    
+    std::string Configuration::getGateway(void) {
+        return this->gateway;
+    }
+    
+    int Configuration::getTcpPort(void) {
+        return this->tcp_port;
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Configuration.h	Mon Jun 15 14:41:41 2015 +0000
@@ -0,0 +1,35 @@
+#ifndef CONFIGURATION_HEADER
+#define CONFIGURATION_HEADER
+
+#include <string>
+#include "ConfigFile.h"
+
+namespace MachineVision{
+    
+    class Configuration{
+        
+        public:
+            bool use_dhcp;
+            std::string ip;
+            std::string netmask;
+            std::string gateway;
+            int tcp_port;
+            
+        public:
+            bool useDhcp(void);
+            std::string getIpAddress(void);
+            std::string getNetmask(void);
+            std::string getGateway(void);
+            int getTcpPort(void);
+
+        public:
+            Configuration(void);
+            bool readFromFile(LocalFileSystem * fs, char * configfile);
+            bool writeConfiguration(LocalFileSystem * fs, char * configfile);
+            
+        private:
+            bool readConfigParameter(ConfigFile * cfg, char * key, char * value, int buffer_size);
+    };
+}
+
+#endif
--- a/main.cpp	Mon Jun 15 12:07:05 2015 +0000
+++ b/main.cpp	Mon Jun 15 14:41:41 2015 +0000
@@ -3,12 +3,10 @@
 #include "Log.h"
 #include "TcpDaemon.h"
 #include "StatusIndicator.h"
+#include "Configuration.h"
 
-#define TCP_SERVER_PORT 6666
 #define LCD_LINE_HEIGHT 12
-#define IP_ADDRESS "10.0.0.100"
-#define NETMASK "255.255.255.0"
-#define GATEWAY "10.0.0.254"
+#define CONFIG_FILE "/local/brails.cfg"
 
 Serial pc(USBTX,USBRX);
 DigitalOut error_led(LED1);
@@ -17,11 +15,11 @@
 using namespace MachineVision;
 
 void setLcdServerInfo(char * ip) {
-    lcd.cls();
-    lcd.locate(0,0);
-    lcd.printf("IP: %s", ip);
-    lcd.locate(0,14);
-    lcd.printf("Port: %d", TCP_SERVER_PORT);
+//    lcd.cls();
+//    lcd.locate(0,0);
+//    lcd.printf("IP: %s", ip);
+//    lcd.locate(0,14);
+//    lcd.printf("Port: %d", TCP_SERVER_PORT);
 }
 
 int main (void) {
@@ -30,15 +28,42 @@
     lcd.cls();
     lcd.locate(0,0);
     lcd.printf("Starting ...");
+    
+    // Read config from local filesystem
+    LocalFileSystem local("local");
+    Configuration config;
+
+    if (config.readFromFile(&local, CONFIG_FILE)) {
+        Log::v("Using config:\r\n\tDHCP: %d\r\n\tIP: %s\r\n\tNETMASK: %s\r\n\tGATEWAY: %s\r\n\tTCP_PORT: %d\r\n", config.useDhcp(), config.getIpAddress(), config.getNetmask(), config.getGateway(), config.getTcpPort());
+    } else {
+        // Create default config
+        Log::w("Creating default config file\r\n");
+        if (config.writeConfiguration(&local, CONFIG_FILE)) {
+            Log::w("Created default config file\r\n");
+            
+            // Try a read now
+            if (config.readFromFile(&local, CONFIG_FILE)) {
+                Log::v("Using config:\r\n\tDHCP: %d\r\n\tIP: %s\r\n\tNETMASK: %s\r\n\tGATEWAY: %s\r\n\tTCP_PORT: %d\r\n", config.useDhcp(), config.getIpAddress(), config.getNetmask(), config.getGateway(), config.getTcpPort());
+            } else {
+                Log::w("Still could not read config. Giving up. Check filesystem as it may be corrupt.\r\n");
+                error("Still could not read config. Giving up. Check filesystem as it may be corrupt.\r\n");
+            }
+        } else {
+            Log::w("Creating default config file failed. Check filesystem as it may be corrupt.\r\n");
+        }
+    }
 
     while (true) {
         // Setup ethernet interface    
         EthernetInterface eth;
         Log::v("Bringing ethernet interface online\r\n");
         
-        
-        //int success = eth.init();         //Use DHCP
-        int success = eth.init(IP_ADDRESS, NETMASK, GATEWAY);
+        int success = 0;
+        if (config.useDhcp()){
+            success = eth.init();
+        } else {
+            success = eth.init(config.getIpAddress().c_str(), config.getNetmask().c_str(), config.getGateway().c_str());
+        }
         
         if (success < 0 || eth.connect() < 0) {    // Default timeout of 15 seconds
             Log::w("Could not bring ethernet interface online\r\n");
@@ -51,12 +76,11 @@
         
             // Start the daemon
             PlcStatusIndicator status_indicator(p21);
-            //PlcStatusIndicator status_indicator(LED4);
-            TcpDaemon daemon(TCP_SERVER_PORT, LED2, LED3, &status_indicator);
-            Log::v("TCP daemon listening @ TCP_SERVER_PORT = %d\r\n", TCP_SERVER_PORT);
+            TcpDaemon daemon(config.getTcpPort(), LED2, LED3, &status_indicator);
+            Log::v("TCP daemon listening @ TCP_SERVER_PORT = %d\r\n", config.getTcpPort());
             daemon.startListening();
         }
         
-        pc.printf("Fail");
+        wait(5);
     }
 }