Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

main.cpp

Committer:
xinlei
Date:
2015-02-25
Revision:
76:b07effe83fb8
Parent:
73:313975bfec96
Child:
77:f6717e4eccc4

File content as of revision 76:b07effe83fb8:

#include <stdio.h>

#include "mbed.h"
#include "rtos.h"
#include "MDM.h"
#include "GPS.h"
#include "DeviceInfo.h"
#include "DeviceMemory.h"
#include "MbedAgent.h"
#include "GPSTracker.h"
#include "DeviceConfiguration.h"
#include "logging.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()
{
    setLevel(A_DEBUG);
    MDMParser::DevStatus devStatus;
    int res;
    uint8_t status = 0;

    MDMRtos<MDMSerial> mdm;
    GPSI2C gps;
    DeviceIO io(gps);
    
    mdm.setDebug(4);
    io.lcdPrint("Device Init");
    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)
        goto error;
    
    io.lcdPrint("Register Network", "IMEI", devStatus.imei);
    if (!mdm.registerNet()) {
        io.lcdPrint("Network Reg Error");
        goto error;
    }

    io.lcdPrint("Join Network");
#ifdef SIM_APN
    if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
#else
    if (mdm.join() == NOIP) {
#endif
        io.lcdPrint("Network join failure", "Check your APN settting,", "username or password");
        goto error;
    }
    
    {
        DeviceInfo deviceInfo(mdm, devStatus);
        DeviceMemory deviceMemory(mdm);
        
        if (io.resetButtonPressed()) {
            io.lcdPrint("Factory Reset");
            if (deviceMemory.resetPlatformCredentials()) {
//                io.deviceFeedback().beepSuccess();
                io.lcdPrint("Reset Success");
            }
            else {
//                io.deviceFeedback().beepFailure();
                io.lcdPrint("Reset Failure");
            }
            Thread::wait(1000);
            return 0;
        }

        MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
    
        io.lcdPrint("Agent Init");
        if (!agent.init()) {
            io.lcdPrint("Agent Init Failure");
            goto error;
        }
        
        uint8_t tries = 3;
        do {
            io.lcdPrint("Agent Run");
            if (agent.run())
                break;
        } while (--tries > 0);

        if (tries == 0) {
            io.lcdPrint("Agent Run Failure");
            goto error;
        }
    }
    
    mdm.disconnect();
    mdm.powerOff();
    return 0;

error:
    mdm.disconnect();
    mdm.powerOff();
    return 1;
}