Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Files at this revision

API Documentation at this revision

Comitter:
xinlei
Date:
Fri May 15 14:07:51 2015 +0000
Parent:
112:8ab3899c4e3d
Child:
114:80610be056e2
Commit message:
data smoothing for pots readings

Changed in this revision

DeviceBootstrap.cpp Show annotated file Show diff for this revision Revisions of this file
DeviceBootstrap.h Show annotated file Show diff for this revision Revisions of this file
MbedAgent.cpp Show annotated file Show diff for this revision Revisions of this file
io/Storage.cpp Show annotated file Show diff for this revision Revisions of this file
io/Storage.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
measurement/Potentiometer.cpp Show annotated file Show diff for this revision Revisions of this file
operation/ControlParser.cpp Show annotated file Show diff for this revision Revisions of this file
util/SmartRestConf.cpp Show annotated file Show diff for this revision Revisions of this file
util/SmartRestConf.h Show annotated file Show diff for this revision Revisions of this file
--- a/DeviceBootstrap.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/DeviceBootstrap.cpp	Fri May 15 14:07:51 2015 +0000
@@ -1,8 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include "rtos.h"
 #include "DeviceBootstrap.h"
-#include "rtos.h"
 #include "Storage.h"
 #include "LCDDisplay.h"
 #include "ComposedRecord.h"
@@ -12,27 +12,30 @@
 #include "SmartRestConf.h"
 #include "logging.h"
 
-/** The username used for device bootstrapping. */
-#define BOOTSTRAP_USERNAME "management/devicebootstrap"
-
-/** The password used for device bootstrapping. */
+// Device bootstrap tenant, username and password
+#define BOOTSTRAP_TENANT "management"
+#define BOOTSTRAP_USERNAME "devicebootstrap"
 #define BOOTSTRAP_PASSWORD "Fhdt1bb1f"
 
 DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
-    DeviceInfo& deviceInfo) :
+    DeviceInfo& deviceInfo):
     _client(client),
     _deviceInfo(deviceInfo)
 {
-    *_username = *_password = '\0';
 }
 
-bool DeviceBootstrap::setUpCredentials()
+bool DeviceBootstrap::bootstrap()
 {
-    if (loadCredential(_username, _password, CREDENTIAL_LENGTH)) {
+    char tenant[CREDENTIAL_LENGTH];
+    char username[CREDENTIAL_LENGTH];
+    char password[CREDENTIAL_LENGTH];
+    if (loadCredential(tenant, username, password, CREDENTIAL_LENGTH)) {
+        setAuth(tenant, username, password);
+        aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr);
         return true;
     } else if (obtainFromPlatform()) {
-        if (!saveCredential(_username, _password, CREDENTIAL_LENGTH))
-            aError("Write credentials!\n");
+        if (!saveCredential(srTenant, srUsername, srPassword, CREDENTIAL_LENGTH))
+            aError("Save credentials!\n");
         return true;
     } else {
         return false;
@@ -49,13 +52,11 @@
     if (!record.add(msgId) || !record.add(identifier))
         return false;
 
-    // set authorization for bootstrap
-    setAuth(BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
-
     LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei());
 
-    uint8_t tries = 255;
-    do {
+    // set authorization for bootstrap
+    setAuth(BOOTSTRAP_TENANT, BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
+    for (uint8_t tries = 255; tries; --tries) {
         if (_client.send(record, "") != SMARTREST_SUCCESS) {
             _client.stop();
             Thread::wait(2000);
@@ -77,30 +78,14 @@
             (recvdRecord.values() != 6)) {
             return false;
         }
-        
-        setCredentials(recvdRecord.value(3).characterValue(),
-                       recvdRecord.value(4).characterValue(),
-                       recvdRecord.value(5).characterValue());        
-        LCDDisplay::inst().setLines("Bootstrap Success", _username, _password);
+
+        setAuth(recvdRecord.value(3).characterValue(),
+                recvdRecord.value(4).characterValue(),
+                recvdRecord.value(5).characterValue());
+        LCDDisplay::inst().setLines("Bootstrap Success", srTenant, srUsername);
+        aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr);
         return true;
-    } while (--tries > 0);
-
+    }
     LCDDisplay::inst().setLines("Bootstrap Failure");
     return false;
-}
-
-void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
-{
-    *_username = '\0';
-    if (tenant != NULL) {
-        strncpy(_username, tenant, CREDENTIAL_LENGTH);
-        _username[CREDENTIAL_LENGTH-1] = '\0';
-        if (strlen(_username)+1 < CREDENTIAL_LENGTH)
-            strcat(_username, "/");
-    }
-    strncat(_username, username, CREDENTIAL_LENGTH-strlen(_username));
-    _username[CREDENTIAL_LENGTH-1] = '\0';
-
-    strncpy(_password, password, CREDENTIAL_LENGTH);
-    _password[CREDENTIAL_LENGTH-1] = '\0';
-}
+}
\ No newline at end of file
--- a/DeviceBootstrap.h	Fri May 15 10:14:05 2015 +0000
+++ b/DeviceBootstrap.h	Fri May 15 14:07:51 2015 +0000
@@ -5,28 +5,19 @@
 #include "AbstractSmartRest.h"
 #include "DeviceInfo.h"
 
-#define CREDENTIAL_LENGTH 256
 
 class DeviceBootstrap
 {
 public:
     DeviceBootstrap(AbstractSmartRest&, DeviceInfo&);
-
-    bool setUpCredentials();
-    const char* username() const { return _username; }
-    const char* password() const { return _password; }
+    bool bootstrap();
 
 protected:
     bool obtainFromPlatform();
 
 private:
-    void setCredentials(const char *, const char*, const char*);
-
-private:
     AbstractSmartRest& _client;
     DeviceInfo& _deviceInfo;
-    char _username[CREDENTIAL_LENGTH];
-    char _password[CREDENTIAL_LENGTH];
 };
 
 #endif
\ No newline at end of file
--- a/MbedAgent.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/MbedAgent.cpp	Fri May 15 14:07:51 2015 +0000
@@ -74,13 +74,9 @@
 int MbedAgent::run()
 {
     // device bootstrapping process
-    if (!_bootstrap.setUpCredentials()) {
-        LCDDisplay::inst().setLines("Bootstrap error");
+    if (!_bootstrap.bootstrap()) {
         return -1;
     }
-    setAuth(_bootstrap.username(), _bootstrap.password());
-    aInfo("Set auth: %s:%s (%s)\n", srUsername, srPassword, srAuthStr);
-
     Thread::wait(2000);
 
     LCDDisplay::inst().setLines("Connect to Cloud", srHost);
@@ -106,7 +102,7 @@
     while (true) {
         int l = 0;
         for (size_t i = 0; i < N; ++i) {
-//            if (reporters[i] == NULL) {
+//            if (reporters[i] == &ConfigSync::inst()) {
             int l2 = reporters[i]->read(buf2+l, sizeof(buf2)-l, status, DISPLAY_LEN);
             if (l2) { // Refresh LCD display needed
                 LCDDisplay::inst().setThirdLine(status);
@@ -121,7 +117,8 @@
         }
         if (l) {
             l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
-            l = sock.sendOnly(buf, l);
+//            l = sock.sendOnly(buf, l);
+            l = sock.sendAndReceive(buf, l, sizeof(buf));
             if (l < 0)
                 aWarning("%s\n", status);
         }
--- a/io/Storage.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/io/Storage.cpp	Fri May 15 14:07:51 2015 +0000
@@ -6,16 +6,16 @@
 #define CONFIGURATION_FILE "002_CONFIGURATION"
 extern MDMSerial *pMdm;
 
-bool loadCredential(char *username, char *password, size_t len)
+bool loadCredential(char *tenant, char *username, char *password, size_t len)
 {
-    char buffer[len*2+3];
+    char buffer[len*3+3];
     int res = pMdm->readFile(CREDENTIALS_FILE, buffer, sizeof(buffer));
     if (res <= 0) {
         return false;
     } else {
         buffer[res] = 0;
-        int l = sscanf(buffer, "%s\n%s\n", username, password);
-        return l==2;
+        int l = sscanf(buffer, "%[^/]/%s\n%s\n", tenant, username, password);
+        return l==3;
     }
 }
 
@@ -24,10 +24,11 @@
     return pMdm->delFile(CREDENTIALS_FILE);
 }
 
-bool saveCredential(char *username, char *password, size_t len)
+bool saveCredential(const char *tenant, const char *username,
+                    const char *password, size_t len)
 {
-    char buffer[len*2+3];
-    int res = snprintf(buffer, sizeof(buffer), "%s\n%s\n", username, password);
+    char buffer[len*3+3];
+    int res = snprintf(buffer, sizeof(buffer), "%s/%s\n%s\n", tenant, username, password);
     pMdm->delFile(CREDENTIALS_FILE);
     return pMdm->writeFile(CREDENTIALS_FILE, buffer, res)==res;
 }
--- a/io/Storage.h	Fri May 15 10:14:05 2015 +0000
+++ b/io/Storage.h	Fri May 15 14:07:51 2015 +0000
@@ -4,13 +4,14 @@
 #include <stddef.h>
 
 /** loads credentials from persistent memory */
-bool loadCredential(char *username, char *password, size_t len);
+bool loadCredential(char *tenant, char *username, char *password, size_t len);
 
 /** saves credentials to persistent memory */
 bool resetCredential();
 
 /** removes credentials from persistent memory */
-bool saveCredential(char *username, char *password, size_t len);
+bool saveCredential(const char *tenant, const char *username,
+                    const char *password, size_t len);
 
 /** loads configuration from persistent memory */
 bool loadConfigFile(char *cfg, size_t len);
--- a/main.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/main.cpp	Fri May 15 14:07:51 2015 +0000
@@ -159,16 +159,12 @@
             ret = agent.run();
             if (ret == 0) {
                 char status[27];
-                const char* user= srUsername;
-                int len = strchr(user, '/')-user+sizeof("Tenant: ");
-                len = len <= 27 ? len : 27;
-                snprintf(status, len, "Tenant: %s", user);
+                snprintf(status, sizeof(status), "Tenant: %s", srTenant);
                 LCDDisplay::inst().setFirstLine(status);
+                agent.loop();
                 break;
             }
         }
-        if (ret == 0)
-            agent.loop();
         shutdown();
         return ret;
     }
--- a/measurement/Potentiometer.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/measurement/Potentiometer.cpp	Fri May 15 14:07:51 2015 +0000
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include "rtos.h"
 #include "Potentiometer.h"
 #include "SmartRestConf.h"
 #include "logging.h"
@@ -14,7 +15,24 @@
 size_t Potentiometer::read(char *buf, size_t maxLen, char *status, size_t num)
 {
         static const char *fmt = "107,%ld,%f,%f\r\n";
-        float data[2] = {(float)analog1*100, (float)analog2*100};
+        const unsigned short N = 10;
+        float data[2] = {0, 0};
+        float min[2] = {100, 100};
+        float max[2] = {0, 0};
+        // multiple sampling for data smoothing
+        for (unsigned short i = 0; i < N; ++i) {
+            float d0 = (float)analog1*100;
+            float d1 = (float)analog2*100;
+            data[0] += d0;
+            data[1] += d1;
+            min[0] = min[0] <= d0 ? min[0] : d0;
+            min[1] = min[1] <= d1 ? min[1] : d1;
+            max[0] = max[0] >= d0 ? max[0] : d0;
+            max[1] = max[1] >= d1 ? max[1] : d1;
+            Thread::wait(10);
+        }
+        data[0] = (data[0]-min[0]-max[0]) / (N-2);
+        data[1] = (data[1]-min[1]-max[1]) / (N-2);
 
         if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA &&
             abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) {
--- a/operation/ControlParser.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/operation/ControlParser.cpp	Fri May 15 14:07:51 2015 +0000
@@ -196,7 +196,7 @@
 
 void ControlParser::parseError(Token& tok)
 {
-        aError("CtrlParse: (%d) %.*s\n", tok.type, (int)tok.len, tok.p);
+        aError("CtrlParse: %.*s[%d]\n", (int)tok.len, tok.p, tok.type);
         parseRecover(tok);
 }
 
--- a/util/SmartRestConf.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/util/SmartRestConf.cpp	Fri May 15 14:07:51 2015 +0000
@@ -3,8 +3,9 @@
 #include "b64.h"
 #include "SmartRestConf.h"
 
-const char *srUsername = NULL;
-const char *srPassword = NULL;
+char srTenant[CREDENTIAL_LENGTH] = {0};
+char srUsername[CREDENTIAL_LENGTH] = {0};
+char srPassword[CREDENTIAL_LENGTH] = {0};
 char srAuthStr[100] = {0};
 const char *srX_ID = "com_cumulocity_MbedAgent_1.5.2";
 const char *srHost = "developer.cumulocity.com";
@@ -37,14 +38,19 @@
         }
 }
 
-static void setUsername(const char* username)
+static void setTenant(const char *tenant)
 {
-        srUsername = username;
+        snprintf(srTenant, sizeof(srTenant), "%s", tenant);
 }
 
-static void setPassword(const char* password)
+static void setUsername(const char *username)
 {
-        srPassword = password;
+        snprintf(srUsername, sizeof(srUsername), "%s", username);
+}
+
+static void setPassword(const char *password)
+{
+        snprintf(srPassword, sizeof(srPassword), "%s", password);
 }
 
 static void setSmartRestFmt()
@@ -66,14 +72,18 @@
         deviceID = id;
 }
 
-void setAuth(const char* username, const char* password)
+void setAuth(const char *tenant, const char *username, const char *password)
 {
+        if (tenant)
+            setTenant(tenant);
         if (username)
             setUsername(username);
         if (password)
             setPassword(password);
-        if (username || password) {
-            setAuthStr(username, password);
+        if (tenant || username || password) {
+            char s[CREDENTIAL_LENGTH*2];
+            snprintf(s, sizeof(s), "%s/%s", tenant, username);
+            setAuthStr(s, password);
             setSmartRestFmt();
         }
 }
\ No newline at end of file
--- a/util/SmartRestConf.h	Fri May 15 10:14:05 2015 +0000
+++ b/util/SmartRestConf.h	Fri May 15 14:07:51 2015 +0000
@@ -1,22 +1,26 @@
 #ifndef SMARTRESTCONF_H
 #define SMARTRESTCONF_H
+
 #define SMARTREST_SIZE 600
 #define SMARTREST_BODY_SIZE 350
 #define SMARTREST_SSL_SIZE 6000
 #define SMARTREST_SSL_BODY_SIZE 5000
 
-extern const char *srUsername;
-extern const char *srPassword;
+#define CREDENTIAL_LENGTH 128
+
+extern char srTenant[];
+extern char srUsername[];
+extern char srPassword[];
 extern char srAuthStr[];
+extern char fmtSmartRest[];
 extern const char *srX_ID;
 extern const char *srHost;
 extern long deviceID;
-extern char fmtSmartRest[];
 const int srPort = 80;
 const int srSSLPort = 443;
 
 void setDeviceID(long id);
-void setAuth(const char* username, const char* password);
-void setX_ID(const char* id);
+void setAuth(const char *tenant, const char *username, const char *password);
+void setX_ID(const char *id);
 
 #endif /* SMARTRESTCONF_H */