Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Revision:
52:8f1370084268
Parent:
49:ac0ba9d54ebc
Child:
55:a0f7295ed6b6
--- a/main.cpp	Tue Jul 29 17:53:43 2014 +0000
+++ b/main.cpp	Wed Aug 13 10:55:11 2014 +0000
@@ -7,28 +7,47 @@
 #include "apndb.h"
 #include "GPSTracker.h"
 
+/**
+ * SIM PIN. Null for no pin.
+ */
+#define SIM_PIN NULL
+
+/**
+ * SIM GPRS login data. Leave commented out for automatic setting.
+ */
+//#define SIM_APN ""
+//#define SIM_USER ""
+//#define SIM_PASS ""
+
 int main()
 {
     MDMParser::DevStatus devStatus;
     int res;
+    uint8_t status = 0;
+    apndb_t *apn;
 
     MDMSerial mdm;
     GPSI2C gps;
-    //mdm.setDebug(4);
 
-    if (!mdm.init(NULL, &devStatus)) {
-        puts("Modem initialization failed. Check your PIN number.");
-        return 1;
-    }
-    puts("Modem initialized");
-
-    if (!gps.init()) {
-        puts("GPS initialization failed.");
-        return 1;
-    }
-    puts("Gps initialized.");
+    if (!mdm.init(SIM_PIN, &devStatus))
+        status = 1;
+    else if (!gps.init())
+        status = 2;
     
     DeviceIO io(gps);
+
+    switch (status) {
+    case 1:
+        io.lcdPrint("MODEM INIT FAILURE", "REMOVE SIM PIN");
+        break;
+    case 2:
+        io.lcdPrint("GPS INIT FAILURE");
+        break;
+    }
+    
+    if (status != 0)
+        goto error;
+    
     io.lcdPrint("DEVICE INIT");
     
     /*if (io.resetButtonPressed()) {
@@ -46,59 +65,61 @@
 
     io.lcdPrint("IMEI", devStatus.imei);
 
-    // print out basic device data
-    printf("IMEI: %s\n", devStatus.imei);
-    printf("IMSI: %s\n", devStatus.imsi);
-
-    puts("Searching for login...");
-    apndb_t *apn = apndb_get(devStatus.imsi);
+#ifndef SIM_APN
+    apn = apndb_get(devStatus.imsi);
     if (apn == NULL) {
-        puts("No APN found. Stop.");
-        io.lcdPrint("NO APN FOUND", "IMEI:", devStatus.imsi);
-        return 1;
+        io.lcdPrint("NO CARRIER FOUND", "CHECK IMSI", devStatus.imsi);
+        goto error;
     }
-    puts("Connected.");
+#endif
     
     if (!mdm.registerNet()) {
-        puts("Network registration failed.");
         io.lcdPrint("NETWORK REG ERROR");
-        return 1;
+        goto error;
     }
 
-    printf("Carrier: %s\n", apn->carrier);
-    puts("Joining Network.");
+#ifdef SIM_APN
+    if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
+        io.lcdPrint("NETWORK JOIN FAILURE");
+        goto error;
+    }
+#else
     io.lcdPrint("JOINING CARRIER", apn->carrier);
     if (mdm.join(apn->apn) == NOIP) {
         io.lcdPrint("NETWORK JOIN FAILURE");
-        puts("Could not join network. Make sure chosen carrier is correct and no credentials are required.");
-        return 1;
-    }
-    
-    DeviceInfo deviceInfo(mdm, devStatus);
-    MbedAgent agent(io, mdm, deviceInfo);
-
-    puts("Starting agent ...");
-    if (!agent.init()) {
-        io.lcdPrint("AGENT INIT FAILURE");
-        puts("Initialization failure.");
-        mdm.disconnect();
-        return 1;
+        goto error;
     }
+#endif
     
-    size_t tries = 3;
-
-    do {
-        io.lcdPrint("RUN AGENT");
-        puts("Running agent ...");
+    {
+        uint8_t tries;
+        DeviceInfo deviceInfo(mdm, devStatus);
+        MbedAgent agent(io, mdm, deviceInfo);
+    
+        io.lcdPrint("AGENT INIT");
+        if (!agent.init()) {
+            io.lcdPrint("AGENT INIT FAILURE");
+            goto error;
+        }
+        
+        tries = 3;
+        do {
+            io.lcdPrint("AGENT RUN");
+            if (agent.run())
+                break;
+        } while (--tries > 0);
 
-        if (!agent.run()) {
-            puts("Agent failure.");
-            continue;
-        } else {
-            break;
+        if (tries == 0) {
+            io.lcdPrint("AGENT RUN FAILURE");
+            goto error;
         }
-    } while (--tries > 0);
+    }
     
     mdm.disconnect();
     return 0;
+
+error:
+    io.lcdPrint("DISCONNECTING");
+    mdm.disconnect();
+    return 1;
 }