Official reference client implementation for Cumulocity SmartREST on u-blox C027.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@67:c360a2b2c948, 2014-11-30 (annotated)
- Committer:
- vwochnik
- Date:
- Sun Nov 30 19:34:49 2014 +0000
- Revision:
- 67:c360a2b2c948
- Parent:
- 61:15719dbe8820
- Child:
- 68:0dc778a16d0d
refactor credentials persistence, add factory reset upon fire press button
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" |
vwochnik | 67:c360a2b2c948 | 6 | #include "DeviceMemory.h" |
Cumulocity | 41:804f6a0bda26 | 7 | #include "MbedAgent.h" |
Cumulocity | 47:89ae46d5c466 | 8 | #include "GPSTracker.h" |
vwochnik | 0:ed4d6fd405ea | 9 | |
vwochnik | 67:c360a2b2c948 | 10 | #include <stdio.h> |
vwochnik | 67:c360a2b2c948 | 11 | |
vwochnik | 52:8f1370084268 | 12 | /** |
vwochnik | 52:8f1370084268 | 13 | * SIM PIN. Null for no pin. |
vwochnik | 52:8f1370084268 | 14 | */ |
vwochnik | 52:8f1370084268 | 15 | #define SIM_PIN NULL |
vwochnik | 52:8f1370084268 | 16 | |
vwochnik | 52:8f1370084268 | 17 | /** |
vwochnik | 52:8f1370084268 | 18 | * SIM GPRS login data. Leave commented out for automatic setting. |
vwochnik | 52:8f1370084268 | 19 | */ |
vwochnik | 52:8f1370084268 | 20 | //#define SIM_APN "" |
vwochnik | 52:8f1370084268 | 21 | //#define SIM_USER "" |
vwochnik | 52:8f1370084268 | 22 | //#define SIM_PASS "" |
vwochnik | 52:8f1370084268 | 23 | |
vwochnik | 0:ed4d6fd405ea | 24 | int main() |
vwochnik | 0:ed4d6fd405ea | 25 | { |
Cumulocity | 41:804f6a0bda26 | 26 | MDMParser::DevStatus devStatus; |
Cumulocity | 46:f6976fd64387 | 27 | int res; |
vwochnik | 52:8f1370084268 | 28 | uint8_t status = 0; |
Cumulocity | 42:104746744af8 | 29 | |
vwochnik | 55:a0f7295ed6b6 | 30 | MDMRtos<MDMSerial> mdm; |
Cumulocity | 47:89ae46d5c466 | 31 | GPSI2C gps; |
vwochnik | 58:4cc0ae5a7058 | 32 | |
vwochnik | 67:c360a2b2c948 | 33 | mdm.setDebug(4); |
vwochnik | 27:bfd402593acc | 34 | |
vwochnik | 52:8f1370084268 | 35 | if (!mdm.init(SIM_PIN, &devStatus)) |
vwochnik | 52:8f1370084268 | 36 | status = 1; |
vwochnik | 52:8f1370084268 | 37 | else if (!gps.init()) |
vwochnik | 52:8f1370084268 | 38 | status = 2; |
Cumulocity | 47:89ae46d5c466 | 39 | |
Cumulocity | 47:89ae46d5c466 | 40 | DeviceIO io(gps); |
vwochnik | 52:8f1370084268 | 41 | |
vwochnik | 52:8f1370084268 | 42 | switch (status) { |
vwochnik | 52:8f1370084268 | 43 | case 1: |
vwochnik | 55:a0f7295ed6b6 | 44 | io.lcdPrint("MODEM INIT FAILURE", "CHECK SIM"); |
vwochnik | 52:8f1370084268 | 45 | break; |
vwochnik | 52:8f1370084268 | 46 | case 2: |
vwochnik | 52:8f1370084268 | 47 | io.lcdPrint("GPS INIT FAILURE"); |
vwochnik | 52:8f1370084268 | 48 | break; |
vwochnik | 52:8f1370084268 | 49 | } |
vwochnik | 52:8f1370084268 | 50 | |
vwochnik | 52:8f1370084268 | 51 | if (status != 0) |
vwochnik | 52:8f1370084268 | 52 | goto error; |
vwochnik | 52:8f1370084268 | 53 | |
Cumulocity | 47:89ae46d5c466 | 54 | io.lcdPrint("DEVICE INIT"); |
Cumulocity | 46:f6976fd64387 | 55 | |
vwochnik | 55:a0f7295ed6b6 | 56 | io.lcdPrint("REGISTER NETWORK", "IMEI", devStatus.imei); |
Cumulocity | 42:104746744af8 | 57 | |
Cumulocity | 41:804f6a0bda26 | 58 | if (!mdm.registerNet()) { |
Cumulocity | 42:104746744af8 | 59 | io.lcdPrint("NETWORK REG ERROR"); |
vwochnik | 52:8f1370084268 | 60 | goto error; |
vwochnik | 28:2004400abeec | 61 | } |
vwochnik | 28:2004400abeec | 62 | |
vwochnik | 55:a0f7295ed6b6 | 63 | io.lcdPrint("JOIN NETWORK"); |
vwochnik | 52:8f1370084268 | 64 | #ifdef SIM_APN |
vwochnik | 52:8f1370084268 | 65 | if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 66 | #else |
vwochnik | 55:a0f7295ed6b6 | 67 | if (mdm.join() == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 68 | #endif |
vwochnik | 52:8f1370084268 | 69 | io.lcdPrint("NETWORK JOIN FAILURE"); |
vwochnik | 52:8f1370084268 | 70 | goto error; |
vwochnik | 52:8f1370084268 | 71 | } |
vwochnik | 8:940c782eec5e | 72 | |
vwochnik | 52:8f1370084268 | 73 | { |
vwochnik | 52:8f1370084268 | 74 | uint8_t tries; |
vwochnik | 52:8f1370084268 | 75 | DeviceInfo deviceInfo(mdm, devStatus); |
vwochnik | 67:c360a2b2c948 | 76 | DeviceMemory deviceMemory(mdm); |
vwochnik | 67:c360a2b2c948 | 77 | |
vwochnik | 67:c360a2b2c948 | 78 | if (io.resetButtonPressed()) { |
vwochnik | 67:c360a2b2c948 | 79 | if (deviceMemory.resetPlatformCredentials()) |
vwochnik | 67:c360a2b2c948 | 80 | io.deviceFeedback().beepSuccess(); |
vwochnik | 67:c360a2b2c948 | 81 | else |
vwochnik | 67:c360a2b2c948 | 82 | io.deviceFeedback().beepFailure(); |
vwochnik | 67:c360a2b2c948 | 83 | Thread::wait(1000); |
vwochnik | 67:c360a2b2c948 | 84 | return; |
vwochnik | 67:c360a2b2c948 | 85 | } |
vwochnik | 67:c360a2b2c948 | 86 | |
vwochnik | 67:c360a2b2c948 | 87 | MbedAgent agent(io, mdm, deviceInfo, deviceMemory); |
vwochnik | 52:8f1370084268 | 88 | |
vwochnik | 52:8f1370084268 | 89 | io.lcdPrint("AGENT INIT"); |
vwochnik | 52:8f1370084268 | 90 | if (!agent.init()) { |
vwochnik | 52:8f1370084268 | 91 | io.lcdPrint("AGENT INIT FAILURE"); |
vwochnik | 52:8f1370084268 | 92 | goto error; |
vwochnik | 52:8f1370084268 | 93 | } |
vwochnik | 52:8f1370084268 | 94 | |
vwochnik | 52:8f1370084268 | 95 | tries = 3; |
vwochnik | 52:8f1370084268 | 96 | do { |
vwochnik | 52:8f1370084268 | 97 | io.lcdPrint("AGENT RUN"); |
vwochnik | 52:8f1370084268 | 98 | if (agent.run()) |
vwochnik | 52:8f1370084268 | 99 | break; |
vwochnik | 52:8f1370084268 | 100 | } while (--tries > 0); |
Cumulocity | 41:804f6a0bda26 | 101 | |
vwochnik | 52:8f1370084268 | 102 | if (tries == 0) { |
vwochnik | 52:8f1370084268 | 103 | io.lcdPrint("AGENT RUN FAILURE"); |
vwochnik | 52:8f1370084268 | 104 | goto error; |
Cumulocity | 41:804f6a0bda26 | 105 | } |
vwochnik | 52:8f1370084268 | 106 | } |
vwochnik | 32:56804dd00193 | 107 | |
vwochnik | 30:daa499571db7 | 108 | mdm.disconnect(); |
vwochnik | 8:940c782eec5e | 109 | return 0; |
vwochnik | 52:8f1370084268 | 110 | |
vwochnik | 52:8f1370084268 | 111 | error: |
vwochnik | 52:8f1370084268 | 112 | mdm.disconnect(); |
vwochnik | 52:8f1370084268 | 113 | return 1; |
vwochnik | 4:363b4cc49445 | 114 | } |