Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp
- Committer:
- xinlei
- Date:
- 2015-04-27
- Revision:
- 96:ea056f6be2e8
- Parent:
- 95:5dfdc8568e9f
- Child:
- 97:9f2de96941c4
File content as of revision 96:ea056f6be2e8:
#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; GPSI2C gps; LCDDisplay::inst().setLines("Mbed Agent V2.1rc2", getHost()); 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; } else if (!gps.init()) { LCDDisplay::inst().setLines("GPS Init Failure"); mdm.disconnect(); mdm.powerOff(); return 2; } joystickUp.rise(&enableDebug); joystickDown.rise(&disableDebug); DeviceMemory deviceMemory; DigitalIn fireButton(D4); if (fireButton) { LCDDisplay::inst().setLines("Factory Reset"); if (deviceMemory.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(gps, deviceInfo, deviceMemory); 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= getUsername(); 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; } }