Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@76:b07effe83fb8, 2015-02-25 (annotated)
- Committer:
- xinlei
- Date:
- Wed Feb 25 10:06:11 2015 +0000
- Revision:
- 76:b07effe83fb8
- Parent:
- 73:313975bfec96
- Child:
- 77:f6717e4eccc4
watchdog: added watchdog;
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 "" |
vwochnik | 52:8f1370084268 | 25 | |
vwochnik | 0:ed4d6fd405ea | 26 | int main() |
vwochnik | 0:ed4d6fd405ea | 27 | { |
xinlei | 72:c5709ae7b193 | 28 | setLevel(A_DEBUG); |
Cumulocity | 41:804f6a0bda26 | 29 | MDMParser::DevStatus devStatus; |
Cumulocity | 46:f6976fd64387 | 30 | int res; |
vwochnik | 52:8f1370084268 | 31 | uint8_t status = 0; |
Cumulocity | 42:104746744af8 | 32 | |
vwochnik | 55:a0f7295ed6b6 | 33 | MDMRtos<MDMSerial> mdm; |
Cumulocity | 47:89ae46d5c466 | 34 | GPSI2C gps; |
xinlei | 71:063c45e99578 | 35 | DeviceIO io(gps); |
vwochnik | 58:4cc0ae5a7058 | 36 | |
xinlei | 76:b07effe83fb8 | 37 | mdm.setDebug(4); |
xinlei | 71:063c45e99578 | 38 | io.lcdPrint("Device Init"); |
xinlei | 76:b07effe83fb8 | 39 | if (!mdm.init(SIM_PIN, &devStatus)) { |
vwochnik | 52:8f1370084268 | 40 | status = 1; |
xinlei | 76:b07effe83fb8 | 41 | io.lcdPrint("Modem Init Failure", "No SIM card found", "Or SIM has PIN code"); |
xinlei | 76:b07effe83fb8 | 42 | } else if (!gps.init()) { |
vwochnik | 52:8f1370084268 | 43 | status = 2; |
xinlei | 71:063c45e99578 | 44 | io.lcdPrint("GPS Init Failure"); |
xinlei | 76:b07effe83fb8 | 45 | } |
vwochnik | 52:8f1370084268 | 46 | if (status != 0) |
vwochnik | 52:8f1370084268 | 47 | goto error; |
vwochnik | 52:8f1370084268 | 48 | |
xinlei | 71:063c45e99578 | 49 | io.lcdPrint("Register Network", "IMEI", devStatus.imei); |
Cumulocity | 41:804f6a0bda26 | 50 | if (!mdm.registerNet()) { |
xinlei | 71:063c45e99578 | 51 | io.lcdPrint("Network Reg Error"); |
vwochnik | 52:8f1370084268 | 52 | goto error; |
vwochnik | 28:2004400abeec | 53 | } |
vwochnik | 28:2004400abeec | 54 | |
xinlei | 71:063c45e99578 | 55 | io.lcdPrint("Join Network"); |
vwochnik | 52:8f1370084268 | 56 | #ifdef SIM_APN |
vwochnik | 52:8f1370084268 | 57 | if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 58 | #else |
vwochnik | 55:a0f7295ed6b6 | 59 | if (mdm.join() == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 60 | #endif |
xinlei | 76:b07effe83fb8 | 61 | io.lcdPrint("Network join failure", "Check your APN settting,", "username or password"); |
vwochnik | 52:8f1370084268 | 62 | goto error; |
vwochnik | 52:8f1370084268 | 63 | } |
vwochnik | 8:940c782eec5e | 64 | |
vwochnik | 52:8f1370084268 | 65 | { |
vwochnik | 52:8f1370084268 | 66 | DeviceInfo deviceInfo(mdm, devStatus); |
vwochnik | 67:c360a2b2c948 | 67 | DeviceMemory deviceMemory(mdm); |
vwochnik | 67:c360a2b2c948 | 68 | |
vwochnik | 67:c360a2b2c948 | 69 | if (io.resetButtonPressed()) { |
xinlei | 71:063c45e99578 | 70 | io.lcdPrint("Factory Reset"); |
xinlei | 71:063c45e99578 | 71 | if (deviceMemory.resetPlatformCredentials()) { |
xinlei | 71:063c45e99578 | 72 | // io.deviceFeedback().beepSuccess(); |
xinlei | 71:063c45e99578 | 73 | io.lcdPrint("Reset Success"); |
xinlei | 71:063c45e99578 | 74 | } |
xinlei | 71:063c45e99578 | 75 | else { |
xinlei | 71:063c45e99578 | 76 | // io.deviceFeedback().beepFailure(); |
xinlei | 71:063c45e99578 | 77 | io.lcdPrint("Reset Failure"); |
xinlei | 71:063c45e99578 | 78 | } |
xinlei | 76:b07effe83fb8 | 79 | Thread::wait(1000); |
xinlei | 76:b07effe83fb8 | 80 | return 0; |
vwochnik | 67:c360a2b2c948 | 81 | } |
vwochnik | 67:c360a2b2c948 | 82 | |
vwochnik | 67:c360a2b2c948 | 83 | MbedAgent agent(io, mdm, deviceInfo, deviceMemory); |
vwochnik | 52:8f1370084268 | 84 | |
xinlei | 71:063c45e99578 | 85 | io.lcdPrint("Agent Init"); |
vwochnik | 52:8f1370084268 | 86 | if (!agent.init()) { |
xinlei | 71:063c45e99578 | 87 | io.lcdPrint("Agent Init Failure"); |
vwochnik | 52:8f1370084268 | 88 | goto error; |
vwochnik | 52:8f1370084268 | 89 | } |
vwochnik | 52:8f1370084268 | 90 | |
xinlei | 76:b07effe83fb8 | 91 | uint8_t tries = 3; |
vwochnik | 52:8f1370084268 | 92 | do { |
xinlei | 73:313975bfec96 | 93 | io.lcdPrint("Agent Run"); |
vwochnik | 52:8f1370084268 | 94 | if (agent.run()) |
vwochnik | 52:8f1370084268 | 95 | break; |
vwochnik | 52:8f1370084268 | 96 | } while (--tries > 0); |
Cumulocity | 41:804f6a0bda26 | 97 | |
vwochnik | 52:8f1370084268 | 98 | if (tries == 0) { |
xinlei | 71:063c45e99578 | 99 | io.lcdPrint("Agent Run Failure"); |
vwochnik | 52:8f1370084268 | 100 | goto error; |
Cumulocity | 41:804f6a0bda26 | 101 | } |
vwochnik | 52:8f1370084268 | 102 | } |
vwochnik | 32:56804dd00193 | 103 | |
vwochnik | 30:daa499571db7 | 104 | mdm.disconnect(); |
xinlei | 76:b07effe83fb8 | 105 | mdm.powerOff(); |
vwochnik | 8:940c782eec5e | 106 | return 0; |
vwochnik | 52:8f1370084268 | 107 | |
vwochnik | 52:8f1370084268 | 108 | error: |
vwochnik | 52:8f1370084268 | 109 | mdm.disconnect(); |
xinlei | 76:b07effe83fb8 | 110 | mdm.powerOff(); |
vwochnik | 52:8f1370084268 | 111 | return 1; |
vwochnik | 4:363b4cc49445 | 112 | } |