portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "MDM.h"
00004 #include "GPS.h"
00005 #include "DeviceInfo.h"
00006 #include "MbedAgent.h"
00007 #include "apndb.h"
00008 #include "GPSTracker.h"
00009 
00010 /**
00011  * SIM PIN. Null for no pin.
00012  */
00013 #define SIM_PIN NULL
00014 
00015 /**
00016  * SIM GPRS login data. Leave commented out for automatic setting.
00017  */
00018 #define SIM_APN NULL
00019 #define SIM_USER NULL
00020 #define SIM_PASS NULL
00021 
00022 int main()
00023 {
00024     MDMParser::DevStatus devStatus;
00025     int res;
00026     uint8_t status = 0;
00027     apndb_t *apn;
00028 
00029     MDMSerial mdm;
00030     GPSI2C gps;
00031 
00032     if (!mdm.init(SIM_PIN, &devStatus))
00033         status = 1;
00034     else if (!gps.init())
00035         status = 2;
00036     
00037     DeviceIO io(gps);
00038 
00039     switch (status) {
00040     case 1:
00041         io.lcdPrint("MODEM INIT FAILURE", "REMOVE SIM PIN");
00042         break;
00043     case 2:
00044         io.lcdPrint("GPS INIT FAILURE");
00045         break;
00046     }
00047     
00048     if (status != 0)
00049         goto error;
00050     
00051     io.lcdPrint("DEVICE INIT");
00052     
00053     /*if (io.resetButtonPressed()) {
00054         puts("Resetting program.");
00055         res = mdm.delFile("001_CREDENTIALS");
00056         if (res < 0) {
00057             puts("Credential reset failed.");
00058             io.lcdPrint("CREDENTIAL RESET", "FAILURE", "PLEASE RESTART DEVICE");
00059         } else {
00060             puts("Credential reset successful.");
00061             io.lcdPrint("CREDENTIAL RESET", "SUCCESS", "PLEASE RESTART DEVICE");
00062         }
00063         return 0;
00064     }*/
00065 
00066     io.lcdPrint("IMEI", devStatus.imei);
00067 
00068 #ifndef SIM_APN
00069     apn = apndb_get(devStatus.imsi);
00070     if (apn == NULL) {
00071         io.lcdPrint("NO CARRIER FOUND", "CHECK IMSI", devStatus.imsi);
00072         goto error;
00073     }
00074 #endif
00075     
00076     if (!mdm.registerNet()) {
00077         io.lcdPrint("NETWORK REG ERROR");
00078         goto error;
00079     }
00080 
00081 #ifdef SIM_APN
00082     if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
00083         io.lcdPrint("NETWORK JOIN FAILURE");
00084         goto error;
00085     }
00086 #else
00087     io.lcdPrint("JOINING CARRIER", apn->carrier);
00088     if (mdm.join(apn->apn) == NOIP) {
00089         io.lcdPrint("NETWORK JOIN FAILURE");
00090         goto error;
00091     }
00092 #endif
00093     
00094     {
00095         uint8_t tries;
00096         DeviceInfo deviceInfo(mdm, devStatus);
00097         MbedAgent agent(io, mdm, deviceInfo);
00098     
00099         io.lcdPrint("AGENT INIT");
00100         if (!agent.init()) {
00101             io.lcdPrint("AGENT INIT FAILURE");
00102             goto error;
00103         }
00104         
00105         tries = 3;
00106         do {
00107             io.lcdPrint("AGENT RUN");
00108             if (agent.run())
00109                 break;
00110         } while (--tries > 0);
00111 
00112         if (tries == 0) {
00113             io.lcdPrint("AGENT RUN FAILURE");
00114             goto error;
00115         }
00116     }
00117     
00118     mdm.disconnect();
00119     return 0;
00120 
00121 error:
00122     io.lcdPrint("DISCONNECTING");
00123     mdm.disconnect();
00124     return 1;
00125 }