Local copy

Dependencies:   C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed

Fork of IBMIoTClientExampleForLPC1768 by Sam Danbury

Revision:
2:25ddff75a8c7
Parent:
0:6276e9f72327
Child:
3:ca5b84eb8f3b
--- a/src/QuickstartClient.cpp	Mon Jun 23 13:37:46 2014 +0000
+++ b/src/QuickstartClient.cpp	Mon Jun 23 20:12:18 2014 +0000
@@ -13,18 +13,63 @@
 #include "QuickstartClient.h"
 
 QuickstartClient::QuickstartClient(string mac) {
+    quickstartMode = true;
     connected = false;
     macAddress = mac;
     
-    //Generate topic string from mac address
-    memcpy(topic, "iot-1/d/", 8);
-    memcpy(topic + 8, macAddress.c_str(), macAddress.size());
-    memcpy(topic + 8 + macAddress.size(), "/evt/mbed-quickstart/json", 25);
-    topic[8 + macAddress.size() + 25] = '\0';
+    //Generate topic string
+    string str = "iot-2/evt/status/fmt/json";
+    strcpy(topic, str.c_str());
+    
+    loadConfig();
     
     tryMqttConnect();
 }
 
+void QuickstartClient::loadConfig() {
+    
+    ConfigFile cfg;
+    
+    char value[BUFSIZ];
+
+    if (cfg.read("/local/device.cfg")) {
+        quickstartMode = false;
+        
+        if (cfg.getValue("org", &value[0], sizeof(value))) {
+            stringstream ss(value);
+            ss >> org;
+        } else {
+            lcd.printf("No org defined in config\n");
+        }
+        
+        if (cfg.getValue("type", &value[0], sizeof(value))) {
+            stringstream ss(value);
+            ss >> type;
+        } else {
+            lcd.printf("No type defined in config\n");
+        }
+        
+        if (cfg.getValue("id", &value[0], sizeof(value))) {
+            stringstream ss(value);
+            ss >> id;
+        } else {
+            lcd.printf("No id defined in config\n");
+        }
+        
+        if (cfg.getValue("token", &value[0], sizeof(value))) {
+            stringstream ss(value);
+            ss >> token;
+        } else {
+            lcd.printf("No token defined in config\n");
+        }
+    } else {
+        org = "quickstart";
+        type = "iotsample-mbed-lpc1768";
+        id = macAddress;
+    }
+    
+}
+
 int QuickstartClient::reconnectDelay(int i) {
     if (i < 10) {
         return 3; //First 10 attempts try within 3 seconds
@@ -71,16 +116,20 @@
     //Connect to TCP socket
     mysock.connect(IBM_IOT_BROKER, IBM_IOT_PORT);
     
-    //Construct clientId from mac address
-    char clientId[23];
-    memcpy(clientId, "quickstart:", 11);
-    memcpy(clientId + 11, macAddress.c_str(), macAddress.size() + 1);
+    //Construct client ID
+    string str = string("d:") + org + ":" + type + ":" + id;
+    char clientId[ strlen(str.c_str()) + 1];
+    strcpy(clientId, str.c_str());
     
     //Set MQTT connect options
     data.clientID.cstring = clientId;
     data.keepAliveInterval = 20;
     data.cleansession = 1;
     data.MQTTVersion = 3;
+    if (!quickstartMode) {
+        data.password.cstring = token.c_str();
+          
+    }
     
     //Attempt MQTT connect
     len = MQTTSerialize_connect(buf, buflen, &data);