Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Mar 16 10:16:48 2015 +0000
Revision:
88:8ab476939897
Parent:
87:b1de34154513
Child:
89:0525121f307e
Bumped version to v2.1rc1.

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