Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Revision:
32:56804dd00193
Parent:
30:daa499571db7
Child:
33:b7d7e6fc421e
diff -r 912c140ee050 -r 56804dd00193 main.cpp
--- 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);
+}