Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed Mar 04 14:02:39 2015 +0000
Revision:
81:4a7761914901
Parent:
80:757c3ff7b92b
Child:
87:b1de34154513
Express mode: walk-around for stability issue.

Who changed what in which revision?

UserRevisionLine numberNew 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
xinlei 80:757c3ff7b92b 26 unsigned short getMNCLen(const char *imsi) {
xinlei 80:757c3ff7b92b 27 if (strncmp(imsi, "310", 3) != 0) // Non American ISMI
xinlei 80:757c3ff7b92b 28 return 2;
xinlei 80:757c3ff7b92b 29 else
xinlei 80:757c3ff7b92b 30 return 3;
xinlei 80:757c3ff7b92b 31 }
xinlei 80:757c3ff7b92b 32
vwochnik 0:ed4d6fd405ea 33 int main()
vwochnik 0:ed4d6fd405ea 34 {
Cumulocity 41:804f6a0bda26 35 MDMParser::DevStatus devStatus;
Cumulocity 46:f6976fd64387 36 int res;
vwochnik 52:8f1370084268 37 uint8_t status = 0;
Cumulocity 42:104746744af8 38
vwochnik 55:a0f7295ed6b6 39 MDMRtos<MDMSerial> mdm;
Cumulocity 47:89ae46d5c466 40 GPSI2C gps;
xinlei 77:f6717e4eccc4 41 DeviceIO io(gps);
vwochnik 58:4cc0ae5a7058 42
xinlei 77:f6717e4eccc4 43 DigitalIn joystickUp(A2);
xinlei 77:f6717e4eccc4 44 DigitalIn joystickDown(A3);
xinlei 77:f6717e4eccc4 45 if (joystickUp) {
xinlei 77:f6717e4eccc4 46 setLevel(A_DEBUG);
xinlei 77:f6717e4eccc4 47 mdm.setDebug(3);
xinlei 77:f6717e4eccc4 48 printf("Enable debug mode.\r\n");
xinlei 77:f6717e4eccc4 49 } else {
xinlei 81:4a7761914901 50 setLevel(A_NONE);
xinlei 81:4a7761914901 51 mdm.setDebug(-1);
xinlei 77:f6717e4eccc4 52 }
xinlei 77:f6717e4eccc4 53
xinlei 71:063c45e99578 54 io.lcdPrint("Device Init");
xinlei 76:b07effe83fb8 55 if (!mdm.init(SIM_PIN, &devStatus)) {
vwochnik 52:8f1370084268 56 status = 1;
xinlei 76:b07effe83fb8 57 io.lcdPrint("Modem Init Failure", "No SIM card found", "Or SIM has PIN code");
xinlei 76:b07effe83fb8 58 } else if (!gps.init()) {
vwochnik 52:8f1370084268 59 status = 2;
xinlei 71:063c45e99578 60 io.lcdPrint("GPS Init Failure");
xinlei 76:b07effe83fb8 61 }
vwochnik 52:8f1370084268 62 if (status != 0)
vwochnik 52:8f1370084268 63 goto error;
vwochnik 52:8f1370084268 64
xinlei 71:063c45e99578 65 io.lcdPrint("Register Network", "IMEI", devStatus.imei);
Cumulocity 41:804f6a0bda26 66 if (!mdm.registerNet()) {
xinlei 77:f6717e4eccc4 67 io.lcdPrint("No Network Coverage");
vwochnik 52:8f1370084268 68 goto error;
vwochnik 28:2004400abeec 69 }
vwochnik 28:2004400abeec 70
xinlei 71:063c45e99578 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
xinlei 80:757c3ff7b92b 77 char s[8] = "unknown";
xinlei 80:757c3ff7b92b 78 const char *p = devStatus.imsi;
xinlei 80:757c3ff7b92b 79 if (p) {
xinlei 80:757c3ff7b92b 80 snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3);
xinlei 80:757c3ff7b92b 81 }
xinlei 80:757c3ff7b92b 82 io.lcdPrint("Wrong APN settting", "MCC-MNC:", s);
vwochnik 52:8f1370084268 83 goto error;
vwochnik 52:8f1370084268 84 }
vwochnik 8:940c782eec5e 85
vwochnik 52:8f1370084268 86 {
vwochnik 52:8f1370084268 87 DeviceInfo deviceInfo(mdm, devStatus);
vwochnik 67:c360a2b2c948 88 DeviceMemory deviceMemory(mdm);
vwochnik 67:c360a2b2c948 89
vwochnik 67:c360a2b2c948 90 if (io.resetButtonPressed()) {
xinlei 71:063c45e99578 91 io.lcdPrint("Factory Reset");
xinlei 71:063c45e99578 92 if (deviceMemory.resetPlatformCredentials()) {
xinlei 71:063c45e99578 93 // io.deviceFeedback().beepSuccess();
xinlei 71:063c45e99578 94 io.lcdPrint("Reset Success");
xinlei 71:063c45e99578 95 }
xinlei 71:063c45e99578 96 else {
xinlei 71:063c45e99578 97 // io.deviceFeedback().beepFailure();
xinlei 71:063c45e99578 98 io.lcdPrint("Reset Failure");
xinlei 71:063c45e99578 99 }
xinlei 76:b07effe83fb8 100 Thread::wait(1000);
xinlei 76:b07effe83fb8 101 return 0;
vwochnik 67:c360a2b2c948 102 }
vwochnik 67:c360a2b2c948 103
vwochnik 67:c360a2b2c948 104 MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
vwochnik 52:8f1370084268 105
xinlei 71:063c45e99578 106 io.lcdPrint("Agent Init");
vwochnik 52:8f1370084268 107 if (!agent.init()) {
xinlei 77:f6717e4eccc4 108 io.lcdPrint("Agent Init Failure", "Debug via serial port");
vwochnik 52:8f1370084268 109 goto error;
vwochnik 52:8f1370084268 110 }
xinlei 76:b07effe83fb8 111 uint8_t tries = 3;
vwochnik 52:8f1370084268 112 do {
xinlei 73:313975bfec96 113 io.lcdPrint("Agent Run");
vwochnik 52:8f1370084268 114 if (agent.run())
vwochnik 52:8f1370084268 115 break;
vwochnik 52:8f1370084268 116 } while (--tries > 0);
Cumulocity 41:804f6a0bda26 117
vwochnik 52:8f1370084268 118 if (tries == 0) {
xinlei 77:f6717e4eccc4 119 io.lcdPrint("Integration/Config Failure");
vwochnik 52:8f1370084268 120 goto error;
Cumulocity 41:804f6a0bda26 121 }
vwochnik 52:8f1370084268 122 }
vwochnik 32:56804dd00193 123
vwochnik 30:daa499571db7 124 mdm.disconnect();
xinlei 76:b07effe83fb8 125 mdm.powerOff();
vwochnik 8:940c782eec5e 126 return 0;
vwochnik 52:8f1370084268 127
vwochnik 52:8f1370084268 128 error:
vwochnik 52:8f1370084268 129 mdm.disconnect();
xinlei 76:b07effe83fb8 130 mdm.powerOff();
vwochnik 52:8f1370084268 131 return 1;
vwochnik 4:363b4cc49445 132 }