Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@52:8f1370084268, 2014-08-13 (annotated)
- Committer:
- vwochnik
- Date:
- Wed Aug 13 10:55:11 2014 +0000
- Revision:
- 52:8f1370084268
- Parent:
- 49:ac0ba9d54ebc
- Child:
- 55:a0f7295ed6b6
minor refactoring and improvement
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" |
vwochnik | 23:0a48eebaaba8 | 7 | #include "apndb.h" |
Cumulocity | 47:89ae46d5c466 | 8 | #include "GPSTracker.h" |
vwochnik | 0:ed4d6fd405ea | 9 | |
vwochnik | 52:8f1370084268 | 10 | /** |
vwochnik | 52:8f1370084268 | 11 | * SIM PIN. Null for no pin. |
vwochnik | 52:8f1370084268 | 12 | */ |
vwochnik | 52:8f1370084268 | 13 | #define SIM_PIN NULL |
vwochnik | 52:8f1370084268 | 14 | |
vwochnik | 52:8f1370084268 | 15 | /** |
vwochnik | 52:8f1370084268 | 16 | * SIM GPRS login data. Leave commented out for automatic setting. |
vwochnik | 52:8f1370084268 | 17 | */ |
vwochnik | 52:8f1370084268 | 18 | //#define SIM_APN "" |
vwochnik | 52:8f1370084268 | 19 | //#define SIM_USER "" |
vwochnik | 52:8f1370084268 | 20 | //#define SIM_PASS "" |
vwochnik | 52:8f1370084268 | 21 | |
vwochnik | 0:ed4d6fd405ea | 22 | int main() |
vwochnik | 0:ed4d6fd405ea | 23 | { |
Cumulocity | 41:804f6a0bda26 | 24 | MDMParser::DevStatus devStatus; |
Cumulocity | 46:f6976fd64387 | 25 | int res; |
vwochnik | 52:8f1370084268 | 26 | uint8_t status = 0; |
vwochnik | 52:8f1370084268 | 27 | apndb_t *apn; |
Cumulocity | 42:104746744af8 | 28 | |
vwochnik | 32:56804dd00193 | 29 | MDMSerial mdm; |
Cumulocity | 47:89ae46d5c466 | 30 | GPSI2C gps; |
vwochnik | 27:bfd402593acc | 31 | |
vwochnik | 52:8f1370084268 | 32 | if (!mdm.init(SIM_PIN, &devStatus)) |
vwochnik | 52:8f1370084268 | 33 | status = 1; |
vwochnik | 52:8f1370084268 | 34 | else if (!gps.init()) |
vwochnik | 52:8f1370084268 | 35 | status = 2; |
Cumulocity | 47:89ae46d5c466 | 36 | |
Cumulocity | 47:89ae46d5c466 | 37 | DeviceIO io(gps); |
vwochnik | 52:8f1370084268 | 38 | |
vwochnik | 52:8f1370084268 | 39 | switch (status) { |
vwochnik | 52:8f1370084268 | 40 | case 1: |
vwochnik | 52:8f1370084268 | 41 | io.lcdPrint("MODEM INIT FAILURE", "REMOVE SIM PIN"); |
vwochnik | 52:8f1370084268 | 42 | break; |
vwochnik | 52:8f1370084268 | 43 | case 2: |
vwochnik | 52:8f1370084268 | 44 | io.lcdPrint("GPS INIT FAILURE"); |
vwochnik | 52:8f1370084268 | 45 | break; |
vwochnik | 52:8f1370084268 | 46 | } |
vwochnik | 52:8f1370084268 | 47 | |
vwochnik | 52:8f1370084268 | 48 | if (status != 0) |
vwochnik | 52:8f1370084268 | 49 | goto error; |
vwochnik | 52:8f1370084268 | 50 | |
Cumulocity | 47:89ae46d5c466 | 51 | io.lcdPrint("DEVICE INIT"); |
Cumulocity | 46:f6976fd64387 | 52 | |
Cumulocity | 48:d5e21301543e | 53 | /*if (io.resetButtonPressed()) { |
Cumulocity | 47:89ae46d5c466 | 54 | puts("Resetting program."); |
Cumulocity | 46:f6976fd64387 | 55 | res = mdm.delFile("001_CREDENTIALS"); |
Cumulocity | 46:f6976fd64387 | 56 | if (res < 0) { |
Cumulocity | 46:f6976fd64387 | 57 | puts("Credential reset failed."); |
Cumulocity | 46:f6976fd64387 | 58 | io.lcdPrint("CREDENTIAL RESET", "FAILURE", "PLEASE RESTART DEVICE"); |
Cumulocity | 46:f6976fd64387 | 59 | } else { |
Cumulocity | 46:f6976fd64387 | 60 | puts("Credential reset successful."); |
Cumulocity | 46:f6976fd64387 | 61 | io.lcdPrint("CREDENTIAL RESET", "SUCCESS", "PLEASE RESTART DEVICE"); |
Cumulocity | 46:f6976fd64387 | 62 | } |
Cumulocity | 46:f6976fd64387 | 63 | return 0; |
Cumulocity | 48:d5e21301543e | 64 | }*/ |
vwochnik | 28:2004400abeec | 65 | |
Cumulocity | 42:104746744af8 | 66 | io.lcdPrint("IMEI", devStatus.imei); |
Cumulocity | 42:104746744af8 | 67 | |
vwochnik | 52:8f1370084268 | 68 | #ifndef SIM_APN |
vwochnik | 52:8f1370084268 | 69 | apn = apndb_get(devStatus.imsi); |
vwochnik | 28:2004400abeec | 70 | if (apn == NULL) { |
vwochnik | 52:8f1370084268 | 71 | io.lcdPrint("NO CARRIER FOUND", "CHECK IMSI", devStatus.imsi); |
vwochnik | 52:8f1370084268 | 72 | goto error; |
vwochnik | 4:363b4cc49445 | 73 | } |
vwochnik | 52:8f1370084268 | 74 | #endif |
vwochnik | 29:853741b9ea3b | 75 | |
Cumulocity | 41:804f6a0bda26 | 76 | if (!mdm.registerNet()) { |
Cumulocity | 42:104746744af8 | 77 | io.lcdPrint("NETWORK REG ERROR"); |
vwochnik | 52:8f1370084268 | 78 | goto error; |
vwochnik | 28:2004400abeec | 79 | } |
vwochnik | 28:2004400abeec | 80 | |
vwochnik | 52:8f1370084268 | 81 | #ifdef SIM_APN |
vwochnik | 52:8f1370084268 | 82 | if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { |
vwochnik | 52:8f1370084268 | 83 | io.lcdPrint("NETWORK JOIN FAILURE"); |
vwochnik | 52:8f1370084268 | 84 | goto error; |
vwochnik | 52:8f1370084268 | 85 | } |
vwochnik | 52:8f1370084268 | 86 | #else |
Cumulocity | 42:104746744af8 | 87 | io.lcdPrint("JOINING CARRIER", apn->carrier); |
vwochnik | 29:853741b9ea3b | 88 | if (mdm.join(apn->apn) == NOIP) { |
Cumulocity | 42:104746744af8 | 89 | io.lcdPrint("NETWORK JOIN FAILURE"); |
vwochnik | 52:8f1370084268 | 90 | goto error; |
Cumulocity | 41:804f6a0bda26 | 91 | } |
vwochnik | 52:8f1370084268 | 92 | #endif |
vwochnik | 8:940c782eec5e | 93 | |
vwochnik | 52:8f1370084268 | 94 | { |
vwochnik | 52:8f1370084268 | 95 | uint8_t tries; |
vwochnik | 52:8f1370084268 | 96 | DeviceInfo deviceInfo(mdm, devStatus); |
vwochnik | 52:8f1370084268 | 97 | MbedAgent agent(io, mdm, deviceInfo); |
vwochnik | 52:8f1370084268 | 98 | |
vwochnik | 52:8f1370084268 | 99 | io.lcdPrint("AGENT INIT"); |
vwochnik | 52:8f1370084268 | 100 | if (!agent.init()) { |
vwochnik | 52:8f1370084268 | 101 | io.lcdPrint("AGENT INIT FAILURE"); |
vwochnik | 52:8f1370084268 | 102 | goto error; |
vwochnik | 52:8f1370084268 | 103 | } |
vwochnik | 52:8f1370084268 | 104 | |
vwochnik | 52:8f1370084268 | 105 | tries = 3; |
vwochnik | 52:8f1370084268 | 106 | do { |
vwochnik | 52:8f1370084268 | 107 | io.lcdPrint("AGENT RUN"); |
vwochnik | 52:8f1370084268 | 108 | if (agent.run()) |
vwochnik | 52:8f1370084268 | 109 | break; |
vwochnik | 52:8f1370084268 | 110 | } while (--tries > 0); |
Cumulocity | 41:804f6a0bda26 | 111 | |
vwochnik | 52:8f1370084268 | 112 | if (tries == 0) { |
vwochnik | 52:8f1370084268 | 113 | io.lcdPrint("AGENT RUN FAILURE"); |
vwochnik | 52:8f1370084268 | 114 | goto error; |
Cumulocity | 41:804f6a0bda26 | 115 | } |
vwochnik | 52:8f1370084268 | 116 | } |
vwochnik | 32:56804dd00193 | 117 | |
vwochnik | 30:daa499571db7 | 118 | mdm.disconnect(); |
vwochnik | 8:940c782eec5e | 119 | return 0; |
vwochnik | 52:8f1370084268 | 120 | |
vwochnik | 52:8f1370084268 | 121 | error: |
vwochnik | 52:8f1370084268 | 122 | io.lcdPrint("DISCONNECTING"); |
vwochnik | 52:8f1370084268 | 123 | mdm.disconnect(); |
vwochnik | 52:8f1370084268 | 124 | return 1; |
vwochnik | 4:363b4cc49445 | 125 | } |