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

main.cpp

Committer:
xinlei
Date:
2015-03-16
Revision:
89:8ab476939897
Parent:
88:b1de34154513
Child:
90:0525121f307e

File content as of revision 89:8ab476939897:

#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 ""

unsigned short getMNCLen(const char *imsi) {
    if (strncmp(imsi, "310", 3) != 0) // Non American ISMI
        return 2;
    else
        return 3;
}

int main()
{
    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("Device Init V2.1rc1");
    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) {
        mdm.disconnect();
        mdm.powerOff();
        return 1;
    }
    
        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;
        }

    io.lcdPrint("Register Network", "IMEI", devStatus.imei);
    if (!mdm.registerNet()) {
        io.lcdPrint("No Network Coverage");
        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
        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;
    }
    
    {
        MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
    
        io.lcdPrint("Agent Init");
        if (!agent.init()) {
            io.lcdPrint("Agent Init Failure", "Debug via serial port");
            goto error;
        }
        uint8_t tries = 3;
        do {
            io.lcdPrint("Agent Run");
            if (agent.run())
                break;
        } while (--tries > 0);

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

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