Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@55:a0f7295ed6b6, 2014-10-23 (annotated)
- Committer:
- vwochnik
- Date:
- Thu Oct 23 12:50:07 2014 +0000
- Revision:
- 55:a0f7295ed6b6
- Parent:
- 52:8f1370084268
- Child:
- 58:4cc0ae5a7058
thread safe smartrest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "mbed.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include "rtos.h" |
Cumulocity | 41:804f6a0bda26 | 3 | #include "MDM.h" |
Cumulocity | 47:89ae46d5c466 | 4 | #include "GPS.h" |
Cumulocity | 41:804f6a0bda26 | 5 | #include "DeviceInfo.h" |
Cumulocity | 41:804f6a0bda26 | 6 | #include "MbedAgent.h" |
Cumulocity | 47:89ae46d5c466 | 7 | #include "GPSTracker.h" |
vwochnik | 0:ed4d6fd405ea | 8 | |
vwochnik | 52:8f1370084268 | 9 | /** |
vwochnik | 52:8f1370084268 | 10 | * SIM PIN. Null for no pin. |
vwochnik | 52:8f1370084268 | 11 | */ |
vwochnik | 52:8f1370084268 | 12 | #define SIM_PIN NULL |
vwochnik | 52:8f1370084268 | 13 | |
vwochnik | 52:8f1370084268 | 14 | /** |
vwochnik | 52:8f1370084268 | 15 | * SIM GPRS login data. Leave commented out for automatic setting. |
vwochnik | 52:8f1370084268 | 16 | */ |
vwochnik | 52:8f1370084268 | 17 | //#define SIM_APN "" |
vwochnik | 52:8f1370084268 | 18 | //#define SIM_USER "" |
vwochnik | 52:8f1370084268 | 19 | //#define SIM_PASS "" |
vwochnik | 52:8f1370084268 | 20 | |
vwochnik | 0:ed4d6fd405ea | 21 | int main() |
vwochnik | 0:ed4d6fd405ea | 22 | { |
Cumulocity | 41:804f6a0bda26 | 23 | MDMParser::DevStatus devStatus; |
Cumulocity | 46:f6976fd64387 | 24 | int res; |
vwochnik | 52:8f1370084268 | 25 | uint8_t status = 0; |
Cumulocity | 42:104746744af8 | 26 | |
vwochnik | 55:a0f7295ed6b6 | 27 | MDMRtos<MDMSerial> mdm; |
Cumulocity | 47:89ae46d5c466 | 28 | GPSI2C gps; |
vwochnik | 27:bfd402593acc | 29 | |
vwochnik | 52:8f1370084268 | 30 | if (!mdm.init(SIM_PIN, &devStatus)) |
vwochnik | 52:8f1370084268 | 31 | status = 1; |
vwochnik | 52:8f1370084268 | 32 | else if (!gps.init()) |
vwochnik | 52:8f1370084268 | 33 | status = 2; |
Cumulocity | 47:89ae46d5c466 | 34 | |
Cumulocity | 47:89ae46d5c466 | 35 | DeviceIO io(gps); |
vwochnik | 52:8f1370084268 | 36 | |
vwochnik | 52:8f1370084268 | 37 | switch (status) { |
vwochnik | 52:8f1370084268 | 38 | case 1: |
vwochnik | 55:a0f7295ed6b6 | 39 | io.lcdPrint("MODEM INIT FAILURE", "CHECK SIM"); |
vwochnik | 52:8f1370084268 | 40 | break; |
vwochnik | 52:8f1370084268 | 41 | case 2: |
vwochnik | 52:8f1370084268 | 42 | io.lcdPrint("GPS INIT FAILURE"); |
vwochnik | 52:8f1370084268 | 43 | break; |
vwochnik | 52:8f1370084268 | 44 | } |
vwochnik | 52:8f1370084268 | 45 | |
vwochnik | 52:8f1370084268 | 46 | if (status != 0) |
vwochnik | 52:8f1370084268 | 47 | goto error; |
vwochnik | 52:8f1370084268 | 48 | |
Cumulocity | 47:89ae46d5c466 | 49 | io.lcdPrint("DEVICE INIT"); |
Cumulocity | 46:f6976fd64387 | 50 | |
Cumulocity | 48:d5e21301543e | 51 | /*if (io.resetButtonPressed()) { |
Cumulocity | 47:89ae46d5c466 | 52 | puts("Resetting program."); |
Cumulocity | 46:f6976fd64387 | 53 | res = mdm.delFile("001_CREDENTIALS"); |
Cumulocity | 46:f6976fd64387 | 54 | if (res < 0) { |
Cumulocity | 46:f6976fd64387 | 55 | puts("Credential reset failed."); |
Cumulocity | 46:f6976fd64387 | 56 | io.lcdPrint("CREDENTIAL RESET", "FAILURE", "PLEASE RESTART DEVICE"); |
Cumulocity | 46:f6976fd64387 | 57 | } else { |
Cumulocity | 46:f6976fd64387 | 58 | puts("Credential reset successful."); |
Cumulocity | 46:f6976fd64387 | 59 | io.lcdPrint("CREDENTIAL RESET", "SUCCESS", "PLEASE RESTART DEVICE"); |
Cumulocity | 46:f6976fd64387 | 60 | } |
Cumulocity | 46:f6976fd64387 | 61 | return 0; |
Cumulocity | 48:d5e21301543e | 62 | }*/ |
vwochnik | 28:2004400abeec | 63 | |
vwochnik | 55:a0f7295ed6b6 | 64 | io.lcdPrint("REGISTER NETWORK", "IMEI", devStatus.imei); |
Cumulocity | 42:104746744af8 | 65 | |
Cumulocity | 41:804f6a0bda26 | 66 | if (!mdm.registerNet()) { |
Cumulocity | 42:104746744af8 | 67 | io.lcdPrint("NETWORK REG ERROR"); |
vwochnik | 52:8f1370084268 | 68 | goto error; |
vwochnik | 28:2004400abeec | 69 | } |
vwochnik | 28:2004400abeec | 70 | |
vwochnik | 55:a0f7295ed6b6 | 71 | io.lcdPrint("JOIN NETWORK"); |
vwochnik | 52:8f1370084268 | 72 | #ifdef SIM_APN |
vwochnik | 52:8f1370084268 | 73 | if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 74 | #else |
vwochnik | 55:a0f7295ed6b6 | 75 | if (mdm.join() == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 76 | #endif |
vwochnik | 52:8f1370084268 | 77 | io.lcdPrint("NETWORK JOIN FAILURE"); |
vwochnik | 52:8f1370084268 | 78 | goto error; |
vwochnik | 52:8f1370084268 | 79 | } |
vwochnik | 8:940c782eec5e | 80 | |
vwochnik | 52:8f1370084268 | 81 | { |
vwochnik | 52:8f1370084268 | 82 | uint8_t tries; |
vwochnik | 52:8f1370084268 | 83 | DeviceInfo deviceInfo(mdm, devStatus); |
vwochnik | 52:8f1370084268 | 84 | MbedAgent agent(io, mdm, deviceInfo); |
vwochnik | 52:8f1370084268 | 85 | |
vwochnik | 52:8f1370084268 | 86 | io.lcdPrint("AGENT INIT"); |
vwochnik | 52:8f1370084268 | 87 | if (!agent.init()) { |
vwochnik | 52:8f1370084268 | 88 | io.lcdPrint("AGENT INIT FAILURE"); |
vwochnik | 52:8f1370084268 | 89 | goto error; |
vwochnik | 52:8f1370084268 | 90 | } |
vwochnik | 52:8f1370084268 | 91 | |
vwochnik | 52:8f1370084268 | 92 | tries = 3; |
vwochnik | 52:8f1370084268 | 93 | do { |
vwochnik | 52:8f1370084268 | 94 | io.lcdPrint("AGENT RUN"); |
vwochnik | 52:8f1370084268 | 95 | if (agent.run()) |
vwochnik | 52:8f1370084268 | 96 | break; |
vwochnik | 52:8f1370084268 | 97 | } while (--tries > 0); |
Cumulocity | 41:804f6a0bda26 | 98 | |
vwochnik | 52:8f1370084268 | 99 | if (tries == 0) { |
vwochnik | 52:8f1370084268 | 100 | io.lcdPrint("AGENT RUN FAILURE"); |
vwochnik | 52:8f1370084268 | 101 | goto error; |
Cumulocity | 41:804f6a0bda26 | 102 | } |
vwochnik | 52:8f1370084268 | 103 | } |
vwochnik | 32:56804dd00193 | 104 | |
vwochnik | 30:daa499571db7 | 105 | mdm.disconnect(); |
vwochnik | 8:940c782eec5e | 106 | return 0; |
vwochnik | 52:8f1370084268 | 107 | |
vwochnik | 52:8f1370084268 | 108 | error: |
vwochnik | 52:8f1370084268 | 109 | mdm.disconnect(); |
vwochnik | 52:8f1370084268 | 110 | return 1; |
vwochnik | 4:363b4cc49445 | 111 | } |