Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Apr 13 14:24:58 2015 +0000
Revision:
92:0acd11870c6a
Parent:
90:423177e8a401
Child:
93:61d44636f020
v2.1rc1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 72:c5709ae7b193 1 #include <stdio.h>
xinlei 72:c5709ae7b193 2
Cumulocity 41:804f6a0bda26 3 #include "mbed.h"
Cumulocity 41:804f6a0bda26 4 #include "rtos.h"
Cumulocity 41:804f6a0bda26 5 #include "MDM.h"
Cumulocity 47:89ae46d5c466 6 #include "GPS.h"
Cumulocity 41:804f6a0bda26 7 #include "DeviceInfo.h"
vwochnik 67:c360a2b2c948 8 #include "DeviceMemory.h"
Cumulocity 41:804f6a0bda26 9 #include "MbedAgent.h"
Cumulocity 47:89ae46d5c466 10 #include "GPSTracker.h"
vwochnik 68:0dc778a16d0d 11 #include "DeviceConfiguration.h"
xinlei 72:c5709ae7b193 12 #include "logging.h"
vwochnik 67:c360a2b2c948 13
vwochnik 52:8f1370084268 14 /**
vwochnik 52:8f1370084268 15 * SIM PIN. Null for no pin.
vwochnik 52:8f1370084268 16 */
vwochnik 52:8f1370084268 17 #define SIM_PIN NULL
vwochnik 52:8f1370084268 18
vwochnik 52:8f1370084268 19 /**
vwochnik 52:8f1370084268 20 * SIM GPRS login data. Leave commented out for automatic setting.
vwochnik 52:8f1370084268 21 */
vwochnik 52:8f1370084268 22 //#define SIM_APN ""
vwochnik 52:8f1370084268 23 //#define SIM_USER ""
vwochnik 52:8f1370084268 24 //#define SIM_PASS ""
xinlei 92:0acd11870c6a 25 MDMRtos<MDMSerial> *pMdm = NULL;
vwochnik 52:8f1370084268 26
xinlei 92:0acd11870c6a 27 unsigned short getMNCLen(const char *imsi)
xinlei 92:0acd11870c6a 28 {
xinlei 80:757c3ff7b92b 29 if (strncmp(imsi, "310", 3) != 0) // Non American ISMI
xinlei 80:757c3ff7b92b 30 return 2;
xinlei 80:757c3ff7b92b 31 else
xinlei 80:757c3ff7b92b 32 return 3;
xinlei 80:757c3ff7b92b 33 }
xinlei 80:757c3ff7b92b 34
xinlei 92:0acd11870c6a 35 void enableDebug()
xinlei 92:0acd11870c6a 36 {
xinlei 92:0acd11870c6a 37 setLevel(A_DEBUG);
xinlei 92:0acd11870c6a 38 if (pMdm)
xinlei 92:0acd11870c6a 39 pMdm->setDebug(3);
xinlei 92:0acd11870c6a 40 }
xinlei 92:0acd11870c6a 41
xinlei 92:0acd11870c6a 42 void disableDebug()
xinlei 92:0acd11870c6a 43 {
xinlei 92:0acd11870c6a 44 setLevel(A_NONE);
xinlei 92:0acd11870c6a 45 if (pMdm)
xinlei 92:0acd11870c6a 46 pMdm->setDebug(-1);
xinlei 92:0acd11870c6a 47 }
xinlei 92:0acd11870c6a 48
vwochnik 0:ed4d6fd405ea 49 int main()
vwochnik 0:ed4d6fd405ea 50 {
xinlei 92:0acd11870c6a 51 MDMRtos<MDMSerial> mdm;
xinlei 92:0acd11870c6a 52 pMdm = &mdm;
xinlei 92:0acd11870c6a 53 InterruptIn joystickUp(A2);
xinlei 92:0acd11870c6a 54 InterruptIn joystickDown(A3);
xinlei 92:0acd11870c6a 55 LCDDisplay lcdDisplay;
Cumulocity 41:804f6a0bda26 56 MDMParser::DevStatus devStatus;
Cumulocity 47:89ae46d5c466 57 GPSI2C gps;
xinlei 92:0acd11870c6a 58 lcdDisplay.setLines("Mbed Agent V2.1rc1", getHost());
xinlei 76:b07effe83fb8 59 if (!mdm.init(SIM_PIN, &devStatus)) {
xinlei 92:0acd11870c6a 60 lcdDisplay.setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN");
xinlei 87:b1de34154513 61 mdm.disconnect();
xinlei 87:b1de34154513 62 mdm.powerOff();
xinlei 87:b1de34154513 63 return 1;
xinlei 92:0acd11870c6a 64 } else if (!gps.init()) {
xinlei 92:0acd11870c6a 65 lcdDisplay.setLines("GPS Init Failure");
xinlei 92:0acd11870c6a 66 mdm.disconnect();
xinlei 92:0acd11870c6a 67 mdm.powerOff();
xinlei 92:0acd11870c6a 68 return 2;
xinlei 87:b1de34154513 69 }
xinlei 92:0acd11870c6a 70 joystickUp.rise(&enableDebug);
xinlei 92:0acd11870c6a 71 joystickDown.rise(&disableDebug);
xinlei 92:0acd11870c6a 72
xinlei 89:0525121f307e 73 DeviceMemory deviceMemory(mdm);
xinlei 92:0acd11870c6a 74 DigitalIn fireButton(D4);
xinlei 89:0525121f307e 75
xinlei 92:0acd11870c6a 76 if (fireButton) {
xinlei 92:0acd11870c6a 77 lcdDisplay.setLines("Factory Reset");
xinlei 89:0525121f307e 78 if (deviceMemory.resetPlatformCredentials()) {
xinlei 92:0acd11870c6a 79 lcdDisplay.setLines("Reset Success");
xinlei 92:0acd11870c6a 80 } else {
xinlei 92:0acd11870c6a 81 lcdDisplay.setLines("Reset Failure");
xinlei 87:b1de34154513 82 }
xinlei 92:0acd11870c6a 83 mdm.disconnect();
xinlei 92:0acd11870c6a 84 mdm.powerOff();
xinlei 89:0525121f307e 85 Thread::wait(1000);
xinlei 89:0525121f307e 86 return 0;
xinlei 89:0525121f307e 87 }
xinlei 92:0acd11870c6a 88 aInfo("Main Thread: %p\r\n", Thread::gettid());
xinlei 92:0acd11870c6a 89 lcdDisplay.setLines("Register Network...", "IMEI", devStatus.imei);
Cumulocity 41:804f6a0bda26 90 if (!mdm.registerNet()) {
xinlei 92:0acd11870c6a 91 lcdDisplay.setLines("No Network Coverage");
xinlei 92:0acd11870c6a 92 mdm.disconnect();
xinlei 92:0acd11870c6a 93 mdm.powerOff();
xinlei 92:0acd11870c6a 94 return 3;
vwochnik 28:2004400abeec 95 }
vwochnik 28:2004400abeec 96
xinlei 92:0acd11870c6a 97 lcdDisplay.setLines("Join Network");
vwochnik 52:8f1370084268 98 #ifdef SIM_APN
vwochnik 52:8f1370084268 99 if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
vwochnik 55:a0f7295ed6b6 100 #else
vwochnik 55:a0f7295ed6b6 101 if (mdm.join() == NOIP) {
vwochnik 55:a0f7295ed6b6 102 #endif
xinlei 92:0acd11870c6a 103 char s[8] = "Unknown";
xinlei 80:757c3ff7b92b 104 const char *p = devStatus.imsi;
xinlei 80:757c3ff7b92b 105 if (p) {
xinlei 80:757c3ff7b92b 106 snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3);
xinlei 80:757c3ff7b92b 107 }
xinlei 92:0acd11870c6a 108 lcdDisplay.setLines("Wrong APN Settting", "MCC-MNC:", s);
xinlei 92:0acd11870c6a 109 mdm.disconnect();
xinlei 92:0acd11870c6a 110 mdm.powerOff();
xinlei 92:0acd11870c6a 111 return 4;
vwochnik 52:8f1370084268 112 }
xinlei 92:0acd11870c6a 113
vwochnik 52:8f1370084268 114 {
xinlei 92:0acd11870c6a 115 DeviceInfo deviceInfo(mdm, devStatus);
xinlei 92:0acd11870c6a 116 MbedAgent agent(gps, mdm, lcdDisplay, deviceInfo, deviceMemory);
xinlei 92:0acd11870c6a 117
xinlei 92:0acd11870c6a 118 lcdDisplay.setLines("Agent Init");
vwochnik 52:8f1370084268 119 if (!agent.init()) {
xinlei 92:0acd11870c6a 120 mdm.disconnect();
xinlei 92:0acd11870c6a 121 mdm.powerOff();
xinlei 92:0acd11870c6a 122 return 5;
vwochnik 52:8f1370084268 123 }
xinlei 92:0acd11870c6a 124 lcdDisplay.setLines("Agent Run");
xinlei 92:0acd11870c6a 125 int ret = 0;
xinlei 92:0acd11870c6a 126 for (uint8_t tries = 3; tries; --tries) {
xinlei 92:0acd11870c6a 127 ret = agent.run();
xinlei 92:0acd11870c6a 128 if (ret == 0)
vwochnik 52:8f1370084268 129 break;
xinlei 92:0acd11870c6a 130 }
Cumulocity 41:804f6a0bda26 131
xinlei 92:0acd11870c6a 132 switch (ret) {
xinlei 92:0acd11870c6a 133 case -1: lcdDisplay.setLines("Bootstrap error"); break;
xinlei 92:0acd11870c6a 134 case -2: lcdDisplay.setLines("Integrate failure"); break;
xinlei 92:0acd11870c6a 135 case -3: lcdDisplay.setLines("Config sync failure"); break;
xinlei 92:0acd11870c6a 136 default: agent.loop();
Cumulocity 41:804f6a0bda26 137 }
xinlei 92:0acd11870c6a 138 mdm.disconnect();
xinlei 92:0acd11870c6a 139 mdm.powerOff();
xinlei 92:0acd11870c6a 140 return ret;
vwochnik 52:8f1370084268 141 }
vwochnik 4:363b4cc49445 142 }