Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp
- Committer:
- xinlei
- Date:
- 2015-05-07
- Revision:
- 98:e369fc75c000
- Parent:
- 97:9f2de96941c4
- Child:
- 100:dbcd3bc51758
File content as of revision 98:e369fc75c000:
#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 "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); openRelay(); MDMRtos<MDMSerial> mdm; pMdm = &mdm; InterruptIn joystickUp(A2); InterruptIn joystickDown(A3); MDMParser::DevStatus devStatus; LCDDisplay::inst().setLines("Mbed Agent V2.1rc2", 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; } joystickUp.rise(&enableDebug); joystickDown.rise(&disableDebug); DigitalIn fireButton(D4); if (fireButton) { LCDDisplay::inst().setLines("Factory Reset"); if (resetPlatformCredentials()) { LCDDisplay::inst().setLines("Reset Success"); } else { LCDDisplay::inst().setLines("Reset Failure"); } mdm.disconnect(); mdm.powerOff(); Thread::wait(1000); 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; case -3: LCDDisplay::inst().setLines("Config sync failure"); break; default: agent.loop(); } mdm.disconnect(); mdm.powerOff(); return ret; } }