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:
vwochnik
Date:
Wed May 28 17:51:12 2014 +0000
Parent:
31:912c140ee050
Child:
33:b7d7e6fc421e
Commit message:
fix

Changed in this revision

C027_Support.lib Show annotated file Show diff for this revision Revisions of this file
common.h Show annotated file Show diff for this revision Revisions of this file
io.cpp 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
program.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/C027_Support.lib	Mon May 26 08:26:01 2014 +0000
+++ b/C027_Support.lib	Wed May 28 17:51:12 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/ublox/code/C027_Support/#0a87d256cd24
+http://mbed.org/teams/ublox/code/C027_Support/#71e3b8bc9ab8
--- a/common.h	Mon May 26 08:26:01 2014 +0000
+++ b/common.h	Wed May 28 17:51:12 2014 +0000
@@ -8,6 +8,9 @@
 #include "MbedSmartRest.h"
 #include "rtos.h"
 
+#define CREDENTIALS_FILE "001_CREDENTIALS"
+#define CREDENTIALS_BUFFER 256
+
 #define A0  P0_23
 #define A1  P0_24
 #define A2  P0_25
@@ -42,6 +45,12 @@
     uint8_t ber; // BER in %
 } sigq_t;
 
+typedef struct
+{
+    char username[128];
+    char password[128];
+} credentials_t;
+
 extern C027 c027;
 //extern UbloxUSBGSMModem modem;
 extern MbedSmartRest client;
@@ -51,6 +60,10 @@
 const char * cellId();
 const char * iccid();
 sigq_t * signalQuality();
+void credentials_set(credentials_t *dst, const char *tenant, const char *username, const char *password);
+bool credentials_read(credentials_t *dst);
+void credentials_write(credentials_t *src);
+void credentials_reset();
 int program(void);
 
 #endif
\ No newline at end of file
--- a/io.cpp	Mon May 26 08:26:01 2014 +0000
+++ b/io.cpp	Wed May 28 17:51:12 2014 +0000
@@ -1,5 +1,6 @@
 #include "io.h"
 #include "rtos.h"
+#include "mbed.h"
 
 void timer_callback(void const*);
 void lcd_update(void);
--- a/main.cpp	Mon May 26 08:26:01 2014 +0000
+++ b/main.cpp	Wed May 28 17:51:12 2014 +0000
@@ -3,7 +3,7 @@
 #include "io.h"
 
 C027 c027;
-MDMSerial mdm;
+MDMSerial* pMdm;
 sigq_t sigQ = {};
 char cCellId[8+1];
 MDMParser::DevStatus devStatus = {};
@@ -13,16 +13,14 @@
 {
     int ret; size_t c;
     
-    puts("Started...");
+    puts("Hello");
+
     io_init();
     puts("IO initialized.");
 
-    c027.mdmPower(true);
-    c027.mdmWakeup();
-    c027.mdmReset();
-    Thread::wait(2000);
+    MDMSerial mdm;
+    pMdm = &mdm;
 
-    puts("Initializing modem.");
     if (!mdm.init(NULL, &devStatus)) {
         puts("Modem initialization failed. Check your PIN number.");
         return 1;
@@ -39,16 +37,8 @@
     printf("IMEI: %s\n", devStatus.imei);
     printf("IMSI: %s\n", devStatus.imsi);
 
-    // wait until we are connected
-    int i = 60;
-    while (!mdm.checkNetStatus(&netStatus)) {
-        if ((netStatus.reg == MDMParser::REG_DENIED) || (i-- == 0))
-            break;
-        Thread::wait(2000);
-    }
-
-    if ((netStatus.reg == MDMParser::REG_DENIED) || (i == 0)) {
-        puts("Network connection failed.");
+    if (!mdm.registerNet(&netStatus)) {
+        puts("Network registration failed.");
         return 1;
     }
 
@@ -58,12 +48,6 @@
         puts("Could not join network. Make sure chosen carrier is correct and no credentials are required.");
         return 1;
     }
-
-    // get before starting program as they're permanent
-    if ((!strlen(imei())) || (!strlen(iccid()))) {
-        puts("Couldn't read IMEI or ICCID.");
-        return 2;
-    }
     
     // Shown on display
     signalQuality();
@@ -76,7 +60,7 @@
         if (ret)
             puts("Restarting program...");
     } while (ret);
-
+    
     mdm.disconnect();
     c027.mdmPower(false);
     
@@ -95,7 +79,7 @@
 
 const char * cellId()
 {
-    if (!mdm.checkNetStatus(&netStatus))
+    if (!pMdm->checkNetStatus(&netStatus))
         return NULL;
 
     if (snprintf(cCellId, sizeof(cCellId), "%X", netStatus.ci) < 1)
@@ -112,7 +96,7 @@
 sigq_t * signalQuality()
 {
     sigQ.rssi = 0; sigQ.ber = 0;
-    if (!mdm.checkNetStatus(&netStatus))
+    if (!pMdm->checkNetStatus(&netStatus))
         return NULL;
 
     sigQ.rssi = netStatus.rssi;
@@ -121,3 +105,59 @@
     lcd_signal(sigQ.rssi, sigQ.ber);
     return &sigQ;
 }
+
+void credentials_set(credentials_t *dst, const char *tenant, const char *username, const char *password)
+{
+    char *ptr = dst->username;
+    
+    strcpy(ptr, tenant);
+    ptr += strlen(tenant);
+    strcpy(ptr++, "/");
+    strcpy(ptr, username);
+    strcpy(dst->password, password);
+}
+
+bool credentials_read(credentials_t *dst)
+{
+    char buf[CREDENTIALS_BUFFER], *ptr;
+
+    int res =  pMdm->readFile(CREDENTIALS_FILE, buf, CREDENTIALS_BUFFER);
+
+    if (res < 0)
+        return false;
+
+    if ((ptr = strchr(buf, '\n')) == NULL)
+        return false;
+    *ptr = '\0';
+    
+    ptr = buf;
+    strcpy(dst->username, ptr);
+    ptr += strlen(ptr)+1;
+    strcpy(dst->password, ptr);
+    return true;
+}
+
+void credentials_write(credentials_t *src)
+{
+    char buf[CREDENTIALS_BUFFER], *ptr;
+    size_t len;
+
+    ptr = buf;
+    len = strlen(src->username);
+    strcpy(ptr, src->username);
+    ptr += len;
+    
+    *ptr++ = '\n';
+    len++;
+    
+    len += strlen(src->password);
+    strcpy(ptr, src->password);
+    
+    pMdm->delFile(CREDENTIALS_FILE);
+    pMdm->writeFile(CREDENTIALS_FILE, buf, len);
+}
+
+void credentials_reset()
+{
+    pMdm->delFile(CREDENTIALS_FILE);
+}
--- a/mbed-rtos.lib	Mon May 26 08:26:01 2014 +0000
+++ b/mbed-rtos.lib	Wed May 28 17:51:12 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#5dfe422a963d
+http://mbed.org/users/mbed_official/code/mbed-rtos/#3761f69dbbb2
--- a/mbed.bld	Mon May 26 08:26:01 2014 +0000
+++ b/mbed.bld	Wed May 28 17:51:12 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877
\ No newline at end of file
--- a/program.cpp	Mon May 26 08:26:01 2014 +0000
+++ b/program.cpp	Wed May 28 17:51:12 2014 +0000
@@ -17,8 +17,7 @@
 void analogMeasurement(long deviceId, Aggregator& aggr);
 void motionMeasurement(long deviceId, Aggregator& aggr);
 
-char cUsername[48];
-char cPassword[48];
+credentials_t credentials = {};
 char cDeviceIdentifier[48];
 
 const char *srtplIdentifier = "com_u-blox_C027_REV-A_0.10_Test1233123";
@@ -57,7 +56,7 @@
 
 float interval = 120.0; // send measurements every two minutes
 
-MbedSmartRest client("developer.cumulocity.com", 80, cUsername, cPassword, cDeviceIdentifier);
+MbedSmartRest client("developer.cumulocity.com", 80, credentials.username, credentials.password, cDeviceIdentifier);
 
 int program(void)
 {
@@ -65,11 +64,14 @@
 
     // copy credentials and identifier into fields
     //TODO: implement bootstrap process
-    strcpy(cUsername, "vaillant/admin");
-    strcpy(cPassword, "klanpi");
+    if (!credentials_read(&credentials)) {
+        puts("Could not read credentials. Stop.");
+        return 1;
+    }
+
     strcpy(cDeviceIdentifier, srtplIdentifier);
 
-    lcd_tenant(cUsername);
+    lcd_tenant(credentials.username);
     puts("Hello!");
     
     puts("Bootstrapping");