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 Vincent Wochnik

Committer:
xinlei
Date:
Wed Mar 11 10:02:35 2015 +0000
Revision:
88:b1de34154513
Parent:
81:4a7761914901
Child:
89:8ab476939897
speed up 'factory reset' by placing it before network register

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 }
xinlei 88:b1de34154513 62 if (status != 0) {
xinlei 88:b1de34154513 63 mdm.disconnect();
xinlei 88:b1de34154513 64 mdm.powerOff();
xinlei 88:b1de34154513 65 return 1;
xinlei 88:b1de34154513 66 }
vwochnik 52:8f1370084268 67
xinlei 88:b1de34154513 68 DeviceInfo deviceInfo(mdm, devStatus);
xinlei 88:b1de34154513 69 DeviceMemory deviceMemory(mdm);
xinlei 88:b1de34154513 70
xinlei 88:b1de34154513 71 if (io.resetButtonPressed()) {
xinlei 88:b1de34154513 72 io.lcdPrint("Factory Reset");
xinlei 88:b1de34154513 73 if (deviceMemory.resetPlatformCredentials()) {
xinlei 88:b1de34154513 74 // io.deviceFeedback().beepSuccess();
xinlei 88:b1de34154513 75 io.lcdPrint("Reset Success");
xinlei 88:b1de34154513 76 }
xinlei 88:b1de34154513 77 else {
xinlei 88:b1de34154513 78 // io.deviceFeedback().beepFailure();
xinlei 88:b1de34154513 79 io.lcdPrint("Reset Failure");
xinlei 88:b1de34154513 80 }
xinlei 88:b1de34154513 81 Thread::wait(1000);
xinlei 88:b1de34154513 82 return 0;
xinlei 88:b1de34154513 83 }
xinlei 88:b1de34154513 84
xinlei 71:063c45e99578 85 io.lcdPrint("Register Network", "IMEI", devStatus.imei);
Cumulocity 41:804f6a0bda26 86 if (!mdm.registerNet()) {
xinlei 77:f6717e4eccc4 87 io.lcdPrint("No Network Coverage");
vwochnik 52:8f1370084268 88 goto error;
vwochnik 28:2004400abeec 89 }
vwochnik 28:2004400abeec 90
xinlei 71:063c45e99578 91 io.lcdPrint("Join Network");
vwochnik 52:8f1370084268 92 #ifdef SIM_APN
vwochnik 52:8f1370084268 93 if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
vwochnik 55:a0f7295ed6b6 94 #else
vwochnik 55:a0f7295ed6b6 95 if (mdm.join() == NOIP) {
vwochnik 55:a0f7295ed6b6 96 #endif
xinlei 80:757c3ff7b92b 97 char s[8] = "unknown";
xinlei 80:757c3ff7b92b 98 const char *p = devStatus.imsi;
xinlei 80:757c3ff7b92b 99 if (p) {
xinlei 80:757c3ff7b92b 100 snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3);
xinlei 80:757c3ff7b92b 101 }
xinlei 80:757c3ff7b92b 102 io.lcdPrint("Wrong APN settting", "MCC-MNC:", s);
vwochnik 52:8f1370084268 103 goto error;
vwochnik 52:8f1370084268 104 }
vwochnik 8:940c782eec5e 105
vwochnik 52:8f1370084268 106 {
vwochnik 67:c360a2b2c948 107 MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
vwochnik 52:8f1370084268 108
xinlei 71:063c45e99578 109 io.lcdPrint("Agent Init");
vwochnik 52:8f1370084268 110 if (!agent.init()) {
xinlei 77:f6717e4eccc4 111 io.lcdPrint("Agent Init Failure", "Debug via serial port");
vwochnik 52:8f1370084268 112 goto error;
vwochnik 52:8f1370084268 113 }
xinlei 76:b07effe83fb8 114 uint8_t tries = 3;
vwochnik 52:8f1370084268 115 do {
xinlei 73:313975bfec96 116 io.lcdPrint("Agent Run");
vwochnik 52:8f1370084268 117 if (agent.run())
vwochnik 52:8f1370084268 118 break;
vwochnik 52:8f1370084268 119 } while (--tries > 0);
Cumulocity 41:804f6a0bda26 120
vwochnik 52:8f1370084268 121 if (tries == 0) {
xinlei 77:f6717e4eccc4 122 io.lcdPrint("Integration/Config Failure");
vwochnik 52:8f1370084268 123 goto error;
Cumulocity 41:804f6a0bda26 124 }
vwochnik 52:8f1370084268 125 }
vwochnik 32:56804dd00193 126
vwochnik 30:daa499571db7 127 mdm.disconnect();
xinlei 76:b07effe83fb8 128 mdm.powerOff();
vwochnik 8:940c782eec5e 129 return 0;
vwochnik 52:8f1370084268 130
vwochnik 52:8f1370084268 131 error:
vwochnik 52:8f1370084268 132 mdm.disconnect();
xinlei 76:b07effe83fb8 133 mdm.powerOff();
vwochnik 52:8f1370084268 134 return 1;
vwochnik 4:363b4cc49445 135 }