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-05-08
Revision:
104:fd3571349e5d
Parent:
101:ef2827b2d00a
Child:
105:c61f0d62b625

File content as of revision 104:fd3571349e5d:

#include <stdio.h>

#include "mbed.h"
#include "rtos.h"
#include "MDM.h"
#include "GPS.h"

#include "DeviceInfo.h"
#include "Storage.h"
#include "MbedAgent.h"
#include "GPSTracker.h"
#include "LCDDisplay.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 ""
//MDMRtos<MDMSerial> *pMdm;
MDMSerial* pMdm;

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()
{
    set_time(1256729737);
    MDMRtos<MDMSerial> mdm;
    pMdm = &mdm;
    InterruptIn joystickUp(A2);
    InterruptIn joystickDown(A3);
    joystickUp.rise(&enableDebug);
    joystickDown.rise(&disableDebug);

    MDMParser::DevStatus devStatus;
    LCDDisplay::inst().setLines("Mbed Agent V2.1rc3", srHost);
    if (!mdm.init(SIM_PIN, &devStatus)) {
        LCDDisplay::inst().setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN");
        mdm.disconnect();
        mdm.powerOff();
        return 1;
    }

    DigitalIn fireButton(D4);
    if (fireButton) {
        if (delCredential()) {
            LCDDisplay::inst().setLines("Reset Success");
        } else {
            LCDDisplay::inst().setLines("Reset Failure");
        }
        Thread::wait(2000);
        mdm.disconnect();
        mdm.powerOff();
        return 0;
    }
    aInfo("Main Thread: %p\r\n", Thread::gettid());    
    LCDDisplay::inst().setLines("Register Network...", "IMEI", devStatus.imei);
    if (!mdm.registerNet()) {
        LCDDisplay::inst().setLines("No Network Coverage");
        mdm.disconnect();
        mdm.powerOff();
        return 3;
    }

    LCDDisplay::inst().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";
        const char *p = devStatus.imsi;
        if (p) {
            snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3);
        }
        LCDDisplay::inst().setLines("Wrong APN Settting", "MCC-MNC:", s);
        mdm.disconnect();
        mdm.powerOff();
        return 4;
    }

    {
        DeviceInfo deviceInfo(devStatus);
        MbedAgent agent(deviceInfo);

        LCDDisplay::inst().setLines("Agent Init");
        if (!agent.init()) {
            mdm.disconnect();
            mdm.powerOff();
            return 5;
        }
        LCDDisplay::inst().setLines("Agent Run");
        int ret = 0;
        for (uint8_t tries = 3; tries; --tries) {
            ret = agent.run();
            if (ret == 0) {
                char status[27];
                const char* user= srUsername;
                int len = strchr(user, '/')-user+sizeof("Tenant: ");
                len = len <= 27 ? len : 27;
                snprintf(status, len, "Tenant: %s", user);
                LCDDisplay::inst().setFirstLine(status);
                break;
            }
        }

        switch (ret) {
            case -1: LCDDisplay::inst().setLines("Bootstrap error"); break;
            case -2: LCDDisplay::inst().setLines("Integrate failure"); break;
            default: agent.loop();
        }
        mdm.disconnect();
        mdm.powerOff();
        return ret;
    }
}