Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@93:61d44636f020, 2015-04-20 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 20 15:04:23 2015 +0000
- Revision:
- 93:61d44636f020
- Parent:
- 92:0acd11870c6a
- Child:
- 95:5dfdc8568e9f
Integration of new device push code base.
Who changed what in which revision?
User | Revision | Line number | New 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 | 93:61d44636f020 | 51 | set_time(1256729737); |
xinlei | 93:61d44636f020 | 52 | openRelay(); |
xinlei | 92:0acd11870c6a | 53 | MDMRtos<MDMSerial> mdm; |
xinlei | 92:0acd11870c6a | 54 | pMdm = &mdm; |
xinlei | 92:0acd11870c6a | 55 | InterruptIn joystickUp(A2); |
xinlei | 92:0acd11870c6a | 56 | InterruptIn joystickDown(A3); |
xinlei | 92:0acd11870c6a | 57 | LCDDisplay lcdDisplay; |
Cumulocity | 41:804f6a0bda26 | 58 | MDMParser::DevStatus devStatus; |
Cumulocity | 47:89ae46d5c466 | 59 | GPSI2C gps; |
xinlei | 93:61d44636f020 | 60 | lcdDisplay.setLines("Mbed Agent V2.1rc2", getHost()); |
xinlei | 76:b07effe83fb8 | 61 | if (!mdm.init(SIM_PIN, &devStatus)) { |
xinlei | 92:0acd11870c6a | 62 | lcdDisplay.setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN"); |
xinlei | 87:b1de34154513 | 63 | mdm.disconnect(); |
xinlei | 87:b1de34154513 | 64 | mdm.powerOff(); |
xinlei | 87:b1de34154513 | 65 | return 1; |
xinlei | 92:0acd11870c6a | 66 | } else if (!gps.init()) { |
xinlei | 92:0acd11870c6a | 67 | lcdDisplay.setLines("GPS Init Failure"); |
xinlei | 92:0acd11870c6a | 68 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 69 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 70 | return 2; |
xinlei | 87:b1de34154513 | 71 | } |
xinlei | 92:0acd11870c6a | 72 | joystickUp.rise(&enableDebug); |
xinlei | 92:0acd11870c6a | 73 | joystickDown.rise(&disableDebug); |
xinlei | 92:0acd11870c6a | 74 | |
xinlei | 89:0525121f307e | 75 | DeviceMemory deviceMemory(mdm); |
xinlei | 92:0acd11870c6a | 76 | DigitalIn fireButton(D4); |
xinlei | 89:0525121f307e | 77 | |
xinlei | 92:0acd11870c6a | 78 | if (fireButton) { |
xinlei | 92:0acd11870c6a | 79 | lcdDisplay.setLines("Factory Reset"); |
xinlei | 89:0525121f307e | 80 | if (deviceMemory.resetPlatformCredentials()) { |
xinlei | 92:0acd11870c6a | 81 | lcdDisplay.setLines("Reset Success"); |
xinlei | 92:0acd11870c6a | 82 | } else { |
xinlei | 92:0acd11870c6a | 83 | lcdDisplay.setLines("Reset Failure"); |
xinlei | 87:b1de34154513 | 84 | } |
xinlei | 92:0acd11870c6a | 85 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 86 | mdm.powerOff(); |
xinlei | 89:0525121f307e | 87 | Thread::wait(1000); |
xinlei | 89:0525121f307e | 88 | return 0; |
xinlei | 89:0525121f307e | 89 | } |
xinlei | 92:0acd11870c6a | 90 | aInfo("Main Thread: %p\r\n", Thread::gettid()); |
xinlei | 92:0acd11870c6a | 91 | lcdDisplay.setLines("Register Network...", "IMEI", devStatus.imei); |
Cumulocity | 41:804f6a0bda26 | 92 | if (!mdm.registerNet()) { |
xinlei | 92:0acd11870c6a | 93 | lcdDisplay.setLines("No Network Coverage"); |
xinlei | 92:0acd11870c6a | 94 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 95 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 96 | return 3; |
vwochnik | 28:2004400abeec | 97 | } |
vwochnik | 28:2004400abeec | 98 | |
xinlei | 92:0acd11870c6a | 99 | lcdDisplay.setLines("Join Network"); |
vwochnik | 52:8f1370084268 | 100 | #ifdef SIM_APN |
vwochnik | 52:8f1370084268 | 101 | if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 102 | #else |
vwochnik | 55:a0f7295ed6b6 | 103 | if (mdm.join() == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 104 | #endif |
xinlei | 92:0acd11870c6a | 105 | char s[8] = "Unknown"; |
xinlei | 80:757c3ff7b92b | 106 | const char *p = devStatus.imsi; |
xinlei | 80:757c3ff7b92b | 107 | if (p) { |
xinlei | 80:757c3ff7b92b | 108 | snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3); |
xinlei | 80:757c3ff7b92b | 109 | } |
xinlei | 92:0acd11870c6a | 110 | lcdDisplay.setLines("Wrong APN Settting", "MCC-MNC:", s); |
xinlei | 92:0acd11870c6a | 111 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 112 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 113 | return 4; |
vwochnik | 52:8f1370084268 | 114 | } |
xinlei | 92:0acd11870c6a | 115 | |
vwochnik | 52:8f1370084268 | 116 | { |
xinlei | 92:0acd11870c6a | 117 | DeviceInfo deviceInfo(mdm, devStatus); |
xinlei | 92:0acd11870c6a | 118 | MbedAgent agent(gps, mdm, lcdDisplay, deviceInfo, deviceMemory); |
xinlei | 92:0acd11870c6a | 119 | |
xinlei | 92:0acd11870c6a | 120 | lcdDisplay.setLines("Agent Init"); |
vwochnik | 52:8f1370084268 | 121 | if (!agent.init()) { |
xinlei | 92:0acd11870c6a | 122 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 123 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 124 | return 5; |
vwochnik | 52:8f1370084268 | 125 | } |
xinlei | 92:0acd11870c6a | 126 | lcdDisplay.setLines("Agent Run"); |
xinlei | 92:0acd11870c6a | 127 | int ret = 0; |
xinlei | 92:0acd11870c6a | 128 | for (uint8_t tries = 3; tries; --tries) { |
xinlei | 92:0acd11870c6a | 129 | ret = agent.run(); |
xinlei | 92:0acd11870c6a | 130 | if (ret == 0) |
vwochnik | 52:8f1370084268 | 131 | break; |
xinlei | 92:0acd11870c6a | 132 | } |
Cumulocity | 41:804f6a0bda26 | 133 | |
xinlei | 92:0acd11870c6a | 134 | switch (ret) { |
xinlei | 92:0acd11870c6a | 135 | case -1: lcdDisplay.setLines("Bootstrap error"); break; |
xinlei | 92:0acd11870c6a | 136 | case -2: lcdDisplay.setLines("Integrate failure"); break; |
xinlei | 92:0acd11870c6a | 137 | case -3: lcdDisplay.setLines("Config sync failure"); break; |
xinlei | 92:0acd11870c6a | 138 | default: agent.loop(); |
Cumulocity | 41:804f6a0bda26 | 139 | } |
xinlei | 92:0acd11870c6a | 140 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 141 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 142 | return ret; |
vwochnik | 52:8f1370084268 | 143 | } |
vwochnik | 4:363b4cc49445 | 144 | } |