Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@46:f6976fd64387, 2014-07-24 (annotated)
- Committer:
- Cumulocity
- Date:
- Thu Jul 24 16:02:47 2014 +0000
- Revision:
- 46:f6976fd64387
- Parent:
- 45:5d8feae26fe3
- Child:
- 47:89ae46d5c466
implement device credential reset; show username/password on display
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 | 41:804f6a0bda26 | 4 | #include "DeviceInfo.h" |
Cumulocity | 41:804f6a0bda26 | 5 | #include "MbedAgent.h" |
vwochnik | 23:0a48eebaaba8 | 6 | #include "apndb.h" |
vwochnik | 0:ed4d6fd405ea | 7 | |
vwochnik | 0:ed4d6fd405ea | 8 | int main() |
vwochnik | 0:ed4d6fd405ea | 9 | { |
Cumulocity | 41:804f6a0bda26 | 10 | MDMParser::DevStatus devStatus; |
Cumulocity | 42:104746744af8 | 11 | DeviceIO io; |
Cumulocity | 46:f6976fd64387 | 12 | int res; |
Cumulocity | 42:104746744af8 | 13 | |
Cumulocity | 42:104746744af8 | 14 | io.lcdPrint("DEVICE INIT"); |
vwochnik | 15:0ccf0f530a05 | 15 | |
vwochnik | 32:56804dd00193 | 16 | MDMSerial mdm; |
vwochnik | 34:8a0556f39161 | 17 | //mdm.setDebug(4); |
vwochnik | 27:bfd402593acc | 18 | |
vwochnik | 29:853741b9ea3b | 19 | if (!mdm.init(NULL, &devStatus)) { |
vwochnik | 28:2004400abeec | 20 | puts("Modem initialization failed. Check your PIN number."); |
Cumulocity | 42:104746744af8 | 21 | io.lcdPrint("MODEM INIT FAILURE"); |
vwochnik | 28:2004400abeec | 22 | return 1; |
vwochnik | 28:2004400abeec | 23 | } |
Cumulocity | 46:f6976fd64387 | 24 | |
Cumulocity | 46:f6976fd64387 | 25 | if (io.resetButtonPressed()) { |
Cumulocity | 46:f6976fd64387 | 26 | res = mdm.delFile("001_CREDENTIALS"); |
Cumulocity | 46:f6976fd64387 | 27 | if (res < 0) { |
Cumulocity | 46:f6976fd64387 | 28 | puts("Credential reset failed."); |
Cumulocity | 46:f6976fd64387 | 29 | io.lcdPrint("CREDENTIAL RESET", "FAILURE", "PLEASE RESTART DEVICE"); |
Cumulocity | 46:f6976fd64387 | 30 | } else { |
Cumulocity | 46:f6976fd64387 | 31 | puts("Credential reset successful."); |
Cumulocity | 46:f6976fd64387 | 32 | io.lcdPrint("CREDENTIAL RESET", "SUCCESS", "PLEASE RESTART DEVICE"); |
Cumulocity | 46:f6976fd64387 | 33 | } |
Cumulocity | 46:f6976fd64387 | 34 | return 0; |
Cumulocity | 46:f6976fd64387 | 35 | } |
vwochnik | 28:2004400abeec | 36 | |
Cumulocity | 42:104746744af8 | 37 | io.lcdPrint("IMEI", devStatus.imei); |
Cumulocity | 42:104746744af8 | 38 | |
Cumulocity | 42:104746744af8 | 39 | // print out basic device data |
Cumulocity | 42:104746744af8 | 40 | printf("IMEI: %s\n", devStatus.imei); |
Cumulocity | 42:104746744af8 | 41 | printf("IMSI: %s\n", devStatus.imsi); |
Cumulocity | 42:104746744af8 | 42 | |
vwochnik | 28:2004400abeec | 43 | puts("Searching for login..."); |
vwochnik | 28:2004400abeec | 44 | apndb_t *apn = apndb_get(devStatus.imsi); |
vwochnik | 28:2004400abeec | 45 | if (apn == NULL) { |
vwochnik | 28:2004400abeec | 46 | puts("No APN found. Stop."); |
Cumulocity | 46:f6976fd64387 | 47 | io.lcdPrint("NO APN FOUND", "IMEI:", devStatus.imsi); |
vwochnik | 28:2004400abeec | 48 | return 1; |
vwochnik | 4:363b4cc49445 | 49 | } |
vwochnik | 29:853741b9ea3b | 50 | |
Cumulocity | 41:804f6a0bda26 | 51 | if (!mdm.registerNet()) { |
vwochnik | 32:56804dd00193 | 52 | puts("Network registration failed."); |
Cumulocity | 42:104746744af8 | 53 | io.lcdPrint("NETWORK REG ERROR"); |
vwochnik | 28:2004400abeec | 54 | return 1; |
vwochnik | 28:2004400abeec | 55 | } |
vwochnik | 28:2004400abeec | 56 | |
vwochnik | 29:853741b9ea3b | 57 | printf("Carrier: %s\n", apn->carrier); |
vwochnik | 28:2004400abeec | 58 | puts("Joining Network."); |
Cumulocity | 42:104746744af8 | 59 | io.lcdPrint("JOINING CARRIER", apn->carrier); |
vwochnik | 29:853741b9ea3b | 60 | if (mdm.join(apn->apn) == NOIP) { |
Cumulocity | 42:104746744af8 | 61 | io.lcdPrint("NETWORK JOIN FAILURE"); |
vwochnik | 29:853741b9ea3b | 62 | puts("Could not join network. Make sure chosen carrier is correct and no credentials are required."); |
vwochnik | 28:2004400abeec | 63 | return 1; |
vwochnik | 28:2004400abeec | 64 | } |
vwochnik | 23:0a48eebaaba8 | 65 | |
Cumulocity | 41:804f6a0bda26 | 66 | DeviceInfo deviceInfo(mdm, devStatus); |
Cumulocity | 42:104746744af8 | 67 | MbedAgent agent(io, mdm, deviceInfo); |
vwochnik | 4:363b4cc49445 | 68 | |
Cumulocity | 41:804f6a0bda26 | 69 | puts("Starting agent ..."); |
Cumulocity | 41:804f6a0bda26 | 70 | if (!agent.init()) { |
Cumulocity | 42:104746744af8 | 71 | io.lcdPrint("AGENT INIT FAILURE"); |
Cumulocity | 41:804f6a0bda26 | 72 | puts("Initialization failure."); |
Cumulocity | 41:804f6a0bda26 | 73 | mdm.disconnect(); |
Cumulocity | 41:804f6a0bda26 | 74 | return 1; |
Cumulocity | 41:804f6a0bda26 | 75 | } |
vwochnik | 8:940c782eec5e | 76 | |
Cumulocity | 41:804f6a0bda26 | 77 | size_t tries = 3; |
Cumulocity | 41:804f6a0bda26 | 78 | |
vwochnik | 8:940c782eec5e | 79 | do { |
Cumulocity | 42:104746744af8 | 80 | io.lcdPrint("RUN AGENT"); |
Cumulocity | 41:804f6a0bda26 | 81 | puts("Running agent ..."); |
Cumulocity | 41:804f6a0bda26 | 82 | |
Cumulocity | 41:804f6a0bda26 | 83 | if (!agent.run()) { |
Cumulocity | 41:804f6a0bda26 | 84 | puts("Agent failure."); |
Cumulocity | 41:804f6a0bda26 | 85 | continue; |
Cumulocity | 41:804f6a0bda26 | 86 | } else { |
Cumulocity | 41:804f6a0bda26 | 87 | break; |
Cumulocity | 41:804f6a0bda26 | 88 | } |
Cumulocity | 41:804f6a0bda26 | 89 | } while (--tries > 0); |
vwochnik | 32:56804dd00193 | 90 | |
vwochnik | 30:daa499571db7 | 91 | mdm.disconnect(); |
vwochnik | 8:940c782eec5e | 92 | return 0; |
vwochnik | 4:363b4cc49445 | 93 | } |