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

Revision:
93:0acd11870c6a
Parent:
91:423177e8a401
Child:
94:61d44636f020
--- a/main.cpp	Fri Mar 20 14:27:10 2015 +0000
+++ b/main.cpp	Mon Apr 13 14:24:58 2015 +0000
@@ -22,113 +22,121 @@
 //#define SIM_APN ""
 //#define SIM_USER ""
 //#define SIM_PASS ""
+MDMRtos<MDMSerial> *pMdm = NULL;
 
-unsigned short getMNCLen(const char *imsi) {
+unsigned short getMNCLen(const char *imsi)
+{
     if (strncmp(imsi, "310", 3) != 0) // Non American ISMI
         return 2;
     else
         return 3;
 }
 
+void enableDebug()
+{
+        setLevel(A_DEBUG);
+        if (pMdm)
+            pMdm->setDebug(3);
+}
+
+void disableDebug()
+{
+       setLevel(A_NONE);
+       if (pMdm)
+           pMdm->setDebug(-1);
+}
+
 int main()
 {
+    MDMRtos<MDMSerial> mdm;
+    pMdm = &mdm;
+    InterruptIn joystickUp(A2);
+    InterruptIn joystickDown(A3);
+    LCDDisplay lcdDisplay;
     MDMParser::DevStatus devStatus;
-    uint8_t status = 0;
-
-    MDMRtos<MDMSerial> mdm;
     GPSI2C gps;
-    DeviceIO io(gps); 
-    
-    DigitalIn joystickUp(A2);
-    DigitalIn joystickDown(A3);
-    if (joystickUp) {
-        setLevel(A_DEBUG);
-        mdm.setDebug(3);
-        printf("Enable debug mode.\r\n");
-    } else {
-        setLevel(A_NONE);
-        mdm.setDebug(-1);
-    }        
-    
-    io.lcdPrint("Mbed Agent V2.1rc1", MBED_AGENT_HOST);
+    lcdDisplay.setLines("Mbed Agent V2.1rc1", getHost());
     if (!mdm.init(SIM_PIN, &devStatus)) {
-        status = 1;
-        io.lcdPrint("Modem Init Failure", "No SIM card found", "Or SIM has PIN code");
-    } else if (!gps.init()) {
-        status = 2;
-        io.lcdPrint("GPS Init Failure");
-    }
-    if (status != 0) {
+        lcdDisplay.setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN");
         mdm.disconnect();
         mdm.powerOff();
         return 1;
+    } else if (!gps.init()) {
+        lcdDisplay.setLines("GPS Init Failure");
+        mdm.disconnect();
+        mdm.powerOff();
+        return 2;
     }
-    
-    DeviceInfo deviceInfo(mdm, devStatus);
+    joystickUp.rise(&enableDebug);
+    joystickDown.rise(&disableDebug);
+
     DeviceMemory deviceMemory(mdm);
+    DigitalIn fireButton(D4);
 
-    if (io.resetButtonPressed()) {
-        io.lcdPrint("Factory Reset");
+    if (fireButton) {
+        lcdDisplay.setLines("Factory Reset");
         if (deviceMemory.resetPlatformCredentials()) {
-//            io.deviceFeedback().beepSuccess();
-            io.lcdPrint("Reset Success");
+            lcdDisplay.setLines("Reset Success");
+        } else {
+            lcdDisplay.setLines("Reset Failure");
         }
-        else {
-//            io.deviceFeedback().beepFailure();
-            io.lcdPrint("Reset Failure");
-        }
+        mdm.disconnect();
+        mdm.powerOff();
         Thread::wait(1000);
         return 0;
     }
-
-    io.lcdPrint("Register Network...", "IMEI", devStatus.imei);
+    aInfo("Main Thread: %p\r\n", Thread::gettid());    
+    lcdDisplay.setLines("Register Network...", "IMEI", devStatus.imei);
     if (!mdm.registerNet()) {
-        io.lcdPrint("No Network Coverage");
-        goto error;
+        lcdDisplay.setLines("No Network Coverage");
+        mdm.disconnect();
+        mdm.powerOff();
+        return 3;
     }
 
-    io.lcdPrint("Join Network");
+    lcdDisplay.setLines("Join Network");
 #ifdef SIM_APN
     if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
 #else
     if (mdm.join() == NOIP) {
 #endif
-        char s[8] = "unknown";
+        char s[8] = "Unknown";
         const char *p = devStatus.imsi;
         if (p) {
             snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3);
         }
-        io.lcdPrint("Wrong APN settting", "MCC-MNC:", s);
-        goto error;
+        lcdDisplay.setLines("Wrong APN Settting", "MCC-MNC:", s);
+        mdm.disconnect();
+        mdm.powerOff();
+        return 4;
     }
-    
+
     {
-        MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
-    
-        io.lcdPrint("Agent Init");
+        DeviceInfo deviceInfo(mdm, devStatus);
+        MbedAgent agent(gps, mdm, lcdDisplay, deviceInfo, deviceMemory);
+
+        lcdDisplay.setLines("Agent Init");
         if (!agent.init()) {
-            io.lcdPrint("Agent Init Failure", "Debug via serial port");
-            goto error;
+            mdm.disconnect();
+            mdm.powerOff();
+            return 5;
         }
-        uint8_t tries = 3;
-        do {
-            io.lcdPrint("Agent Run");
-            if (agent.run())
+        lcdDisplay.setLines("Agent Run");
+        int ret = 0;
+        for (uint8_t tries = 3; tries; --tries) {
+            ret = agent.run();
+            if (ret == 0)
                 break;
-        } while (--tries > 0);
+        }
 
-        if (tries == 0) {
-            io.lcdPrint("Integration/Config Failure");
-            goto error;
+        switch (ret) {
+            case -1: lcdDisplay.setLines("Bootstrap error"); break;
+            case -2: lcdDisplay.setLines("Integrate failure"); break;
+            case -3: lcdDisplay.setLines("Config sync failure"); break;
+            default: agent.loop();
         }
+        mdm.disconnect();
+        mdm.powerOff();
+        return ret;
     }
-    
-    mdm.disconnect();
-    mdm.powerOff();
-    return 0;
-
-error:
-    mdm.disconnect();
-    mdm.powerOff();
-    return 1;
 }