Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@104:fd3571349e5d, 2015-05-08 (annotated)
- Committer:
- xinlei
- Date:
- Fri May 08 14:50:43 2015 +0000
- Revision:
- 104:fd3571349e5d
- Parent:
- 101:ef2827b2d00a
- Child:
- 105:c61f0d62b625
quick fix for file renaming.
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" |
xinlei | 100:dbcd3bc51758 | 7 | |
Cumulocity | 41:804f6a0bda26 | 8 | #include "DeviceInfo.h" |
xinlei | 104:fd3571349e5d | 9 | #include "Storage.h" |
Cumulocity | 41:804f6a0bda26 | 10 | #include "MbedAgent.h" |
Cumulocity | 47:89ae46d5c466 | 11 | #include "GPSTracker.h" |
xinlei | 95:5dfdc8568e9f | 12 | #include "LCDDisplay.h" |
xinlei | 72:c5709ae7b193 | 13 | #include "logging.h" |
vwochnik | 67:c360a2b2c948 | 14 | |
vwochnik | 52:8f1370084268 | 15 | /** |
vwochnik | 52:8f1370084268 | 16 | * SIM PIN. Null for no pin. |
vwochnik | 52:8f1370084268 | 17 | */ |
vwochnik | 52:8f1370084268 | 18 | #define SIM_PIN NULL |
vwochnik | 52:8f1370084268 | 19 | |
vwochnik | 52:8f1370084268 | 20 | /** |
vwochnik | 52:8f1370084268 | 21 | * SIM GPRS login data. Leave commented out for automatic setting. |
vwochnik | 52:8f1370084268 | 22 | */ |
vwochnik | 52:8f1370084268 | 23 | //#define SIM_APN "" |
vwochnik | 52:8f1370084268 | 24 | //#define SIM_USER "" |
vwochnik | 52:8f1370084268 | 25 | //#define SIM_PASS "" |
xinlei | 96:ea056f6be2e8 | 26 | //MDMRtos<MDMSerial> *pMdm; |
xinlei | 96:ea056f6be2e8 | 27 | MDMSerial* pMdm; |
vwochnik | 52:8f1370084268 | 28 | |
xinlei | 92:0acd11870c6a | 29 | unsigned short getMNCLen(const char *imsi) |
xinlei | 92:0acd11870c6a | 30 | { |
xinlei | 80:757c3ff7b92b | 31 | if (strncmp(imsi, "310", 3) != 0) // Non American ISMI |
xinlei | 80:757c3ff7b92b | 32 | return 2; |
xinlei | 80:757c3ff7b92b | 33 | else |
xinlei | 80:757c3ff7b92b | 34 | return 3; |
xinlei | 80:757c3ff7b92b | 35 | } |
xinlei | 80:757c3ff7b92b | 36 | |
xinlei | 92:0acd11870c6a | 37 | void enableDebug() |
xinlei | 92:0acd11870c6a | 38 | { |
xinlei | 92:0acd11870c6a | 39 | setLevel(A_DEBUG); |
xinlei | 92:0acd11870c6a | 40 | if (pMdm) |
xinlei | 92:0acd11870c6a | 41 | pMdm->setDebug(3); |
xinlei | 92:0acd11870c6a | 42 | } |
xinlei | 92:0acd11870c6a | 43 | |
xinlei | 92:0acd11870c6a | 44 | void disableDebug() |
xinlei | 92:0acd11870c6a | 45 | { |
xinlei | 92:0acd11870c6a | 46 | setLevel(A_NONE); |
xinlei | 92:0acd11870c6a | 47 | if (pMdm) |
xinlei | 92:0acd11870c6a | 48 | pMdm->setDebug(-1); |
xinlei | 92:0acd11870c6a | 49 | } |
xinlei | 92:0acd11870c6a | 50 | |
vwochnik | 0:ed4d6fd405ea | 51 | int main() |
vwochnik | 0:ed4d6fd405ea | 52 | { |
xinlei | 93:61d44636f020 | 53 | set_time(1256729737); |
xinlei | 92:0acd11870c6a | 54 | MDMRtos<MDMSerial> mdm; |
xinlei | 92:0acd11870c6a | 55 | pMdm = &mdm; |
xinlei | 92:0acd11870c6a | 56 | InterruptIn joystickUp(A2); |
xinlei | 92:0acd11870c6a | 57 | InterruptIn joystickDown(A3); |
xinlei | 100:dbcd3bc51758 | 58 | joystickUp.rise(&enableDebug); |
xinlei | 100:dbcd3bc51758 | 59 | joystickDown.rise(&disableDebug); |
xinlei | 100:dbcd3bc51758 | 60 | |
Cumulocity | 41:804f6a0bda26 | 61 | MDMParser::DevStatus devStatus; |
xinlei | 101:ef2827b2d00a | 62 | LCDDisplay::inst().setLines("Mbed Agent V2.1rc3", srHost); |
xinlei | 76:b07effe83fb8 | 63 | if (!mdm.init(SIM_PIN, &devStatus)) { |
xinlei | 95:5dfdc8568e9f | 64 | LCDDisplay::inst().setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN"); |
xinlei | 87:b1de34154513 | 65 | mdm.disconnect(); |
xinlei | 87:b1de34154513 | 66 | mdm.powerOff(); |
xinlei | 87:b1de34154513 | 67 | return 1; |
xinlei | 87:b1de34154513 | 68 | } |
xinlei | 92:0acd11870c6a | 69 | |
xinlei | 92:0acd11870c6a | 70 | DigitalIn fireButton(D4); |
xinlei | 92:0acd11870c6a | 71 | if (fireButton) { |
xinlei | 100:dbcd3bc51758 | 72 | if (delCredential()) { |
xinlei | 95:5dfdc8568e9f | 73 | LCDDisplay::inst().setLines("Reset Success"); |
xinlei | 92:0acd11870c6a | 74 | } else { |
xinlei | 95:5dfdc8568e9f | 75 | LCDDisplay::inst().setLines("Reset Failure"); |
xinlei | 87:b1de34154513 | 76 | } |
xinlei | 100:dbcd3bc51758 | 77 | Thread::wait(2000); |
xinlei | 92:0acd11870c6a | 78 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 79 | mdm.powerOff(); |
xinlei | 89:0525121f307e | 80 | return 0; |
xinlei | 89:0525121f307e | 81 | } |
xinlei | 92:0acd11870c6a | 82 | aInfo("Main Thread: %p\r\n", Thread::gettid()); |
xinlei | 95:5dfdc8568e9f | 83 | LCDDisplay::inst().setLines("Register Network...", "IMEI", devStatus.imei); |
Cumulocity | 41:804f6a0bda26 | 84 | if (!mdm.registerNet()) { |
xinlei | 95:5dfdc8568e9f | 85 | LCDDisplay::inst().setLines("No Network Coverage"); |
xinlei | 92:0acd11870c6a | 86 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 87 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 88 | return 3; |
vwochnik | 28:2004400abeec | 89 | } |
vwochnik | 28:2004400abeec | 90 | |
xinlei | 95:5dfdc8568e9f | 91 | LCDDisplay::inst().setLines("Join Network"); |
vwochnik | 52:8f1370084268 | 92 | #ifdef SIM_APN |
vwochnik | 52:8f1370084268 | 93 | if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 94 | #else |
vwochnik | 55:a0f7295ed6b6 | 95 | if (mdm.join() == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 96 | #endif |
xinlei | 92:0acd11870c6a | 97 | char s[8] = "Unknown"; |
xinlei | 80:757c3ff7b92b | 98 | const char *p = devStatus.imsi; |
xinlei | 80:757c3ff7b92b | 99 | if (p) { |
xinlei | 80:757c3ff7b92b | 100 | snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3); |
xinlei | 80:757c3ff7b92b | 101 | } |
xinlei | 95:5dfdc8568e9f | 102 | LCDDisplay::inst().setLines("Wrong APN Settting", "MCC-MNC:", s); |
xinlei | 92:0acd11870c6a | 103 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 104 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 105 | return 4; |
vwochnik | 52:8f1370084268 | 106 | } |
xinlei | 92:0acd11870c6a | 107 | |
vwochnik | 52:8f1370084268 | 108 | { |
xinlei | 96:ea056f6be2e8 | 109 | DeviceInfo deviceInfo(devStatus); |
xinlei | 98:e369fc75c000 | 110 | MbedAgent agent(deviceInfo); |
xinlei | 92:0acd11870c6a | 111 | |
xinlei | 95:5dfdc8568e9f | 112 | LCDDisplay::inst().setLines("Agent Init"); |
vwochnik | 52:8f1370084268 | 113 | if (!agent.init()) { |
xinlei | 92:0acd11870c6a | 114 | mdm.disconnect(); |
xinlei | 92:0acd11870c6a | 115 | mdm.powerOff(); |
xinlei | 92:0acd11870c6a | 116 | return 5; |
vwochnik | 52:8f1370084268 | 117 | } |
xinlei | 95:5dfdc8568e9f | 118 | LCDDisplay::inst().setLines("Agent Run"); |
xinlei | 92:0acd11870c6a | 119 | int ret = 0; |
xinlei | 92:0acd11870c6a | 120 | for (uint8_t tries = 3; tries; --tries) { |
xinlei | 92:0acd11870c6a | 121 | ret = agent.run(); |
xinlei | 95:5dfdc8568e9f | 122 | if (ret == 0) { |
xinlei | 95:5dfdc8568e9f | 123 | char status[27]; |
xinlei | 98:e369fc75c000 | 124 | const char* user= srUsername; |
xinlei | 95:5dfdc8568e9f | 125 | int len = strchr(user, '/')-user+sizeof("Tenant: "); |
xinlei | 95:5dfdc8568e9f | 126 | len = len <= 27 ? len : 27; |
xinlei | 95:5dfdc8568e9f | 127 | snprintf(status, len, "Tenant: %s", user); |
xinlei | 95:5dfdc8568e9f | 128 | LCDDisplay::inst().setFirstLine(status); |
vwochnik | 52:8f1370084268 | 129 | break; |
xinlei | 95:5dfdc8568e9f | 130 | } |
xinlei | 92:0acd11870c6a | 131 | } |
Cumulocity | 41:804f6a0bda26 | 132 | |
xinlei | 92:0acd11870c6a | 133 | switch (ret) { |
xinlei | 95:5dfdc8568e9f | 134 | case -1: LCDDisplay::inst().setLines("Bootstrap error"); break; |
xinlei | 95:5dfdc8568e9f | 135 | case -2: LCDDisplay::inst().setLines("Integrate 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 | } |