Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 13 13:03:20 2015 +0000
Revision:
107:f1ee3e1eb126
Parent:
106:fc5f25f0e0d5
Child:
110:ee9ae4a5e55c
ConfigSync is now singleton

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"
xinlei 100:dbcd3bc51758 7
Cumulocity 41:804f6a0bda26 8 #include "DeviceInfo.h"
xinlei 104:fd3571349e5d 9 #include "Storage.h"
Cumulocity 41:804f6a0bda26 10 #include "MbedAgent.h"
Cumulocity 47:89ae46d5c466 11 #include "GPSTracker.h"
xinlei 95:5dfdc8568e9f 12 #include "LCDDisplay.h"
xinlei 72:c5709ae7b193 13 #include "logging.h"
vwochnik 67:c360a2b2c948 14
vwochnik 52:8f1370084268 15 /**
vwochnik 52:8f1370084268 16 * SIM PIN. Null for no pin.
vwochnik 52:8f1370084268 17 */
vwochnik 52:8f1370084268 18 #define SIM_PIN NULL
vwochnik 52:8f1370084268 19
vwochnik 52:8f1370084268 20 /**
vwochnik 52:8f1370084268 21 * SIM GPRS login data. Leave commented out for automatic setting.
vwochnik 52:8f1370084268 22 */
vwochnik 52:8f1370084268 23 //#define SIM_APN ""
vwochnik 52:8f1370084268 24 //#define SIM_USER ""
vwochnik 52:8f1370084268 25 //#define SIM_PASS ""
vwochnik 52:8f1370084268 26
xinlei 105:c61f0d62b625 27 MDMSerial* pMdm = NULL;
xinlei 105:c61f0d62b625 28 //CYASSL_CTX *pCtx = NULL;
xinlei 105:c61f0d62b625 29
xinlei 105:c61f0d62b625 30 static unsigned short getMNCLen(const char *imsi)
xinlei 92:0acd11870c6a 31 {
xinlei 80:757c3ff7b92b 32 if (strncmp(imsi, "310", 3) != 0) // Non American ISMI
xinlei 80:757c3ff7b92b 33 return 2;
xinlei 80:757c3ff7b92b 34 else
xinlei 80:757c3ff7b92b 35 return 3;
xinlei 80:757c3ff7b92b 36 }
xinlei 80:757c3ff7b92b 37
xinlei 105:c61f0d62b625 38 static void enableDebug()
xinlei 92:0acd11870c6a 39 {
xinlei 92:0acd11870c6a 40 setLevel(A_DEBUG);
xinlei 92:0acd11870c6a 41 if (pMdm)
xinlei 92:0acd11870c6a 42 pMdm->setDebug(3);
xinlei 92:0acd11870c6a 43 }
xinlei 92:0acd11870c6a 44
xinlei 105:c61f0d62b625 45 static void disableDebug()
xinlei 92:0acd11870c6a 46 {
xinlei 92:0acd11870c6a 47 setLevel(A_NONE);
xinlei 92:0acd11870c6a 48 if (pMdm)
xinlei 92:0acd11870c6a 49 pMdm->setDebug(-1);
xinlei 92:0acd11870c6a 50 }
xinlei 92:0acd11870c6a 51
xinlei 105:c61f0d62b625 52 static void shutdown()
xinlei 105:c61f0d62b625 53 {
xinlei 105:c61f0d62b625 54 // CyaSSL_CTX_free(pCtx);
xinlei 105:c61f0d62b625 55 // CyaSSL_Cleanup();
xinlei 105:c61f0d62b625 56 pMdm->disconnect();
xinlei 105:c61f0d62b625 57 pMdm->powerOff();
xinlei 105:c61f0d62b625 58 }
xinlei 105:c61f0d62b625 59
xinlei 105:c61f0d62b625 60 //static int send(CYASSL *ssl, char *buf, int size, void* ctx)
xinlei 105:c61f0d62b625 61 //{
xinlei 105:c61f0d62b625 62 // int sockfd = *(int*)ctx;
xinlei 105:c61f0d62b625 63 // int ret = pMdm->socketSend(sockfd, buf, size);
xinlei 105:c61f0d62b625 64 // printf("[send](%d, %p): %d/%d\n", sockfd, ssl, ret, size);
xinlei 105:c61f0d62b625 65 // if (ret >= 0)
xinlei 105:c61f0d62b625 66 // return ret;
xinlei 105:c61f0d62b625 67 // else
xinlei 105:c61f0d62b625 68 // return CYASSL_CBIO_ERR_GENERAL;
xinlei 105:c61f0d62b625 69 //}
xinlei 105:c61f0d62b625 70 //
xinlei 105:c61f0d62b625 71 //static int recv(CYASSL *ssl, char *buf, int maxSize, void* ctx)
xinlei 105:c61f0d62b625 72 //{
xinlei 105:c61f0d62b625 73 // int sockfd = *(int*)ctx;
xinlei 105:c61f0d62b625 74 // int ret = pMdm->socketRecv(sockfd, buf, maxSize);
xinlei 105:c61f0d62b625 75 // printf("[recv](%d, %p): %d/%d\n", sockfd, ssl, ret, maxSize);
xinlei 105:c61f0d62b625 76 // if (ret >= 0)
xinlei 105:c61f0d62b625 77 // return ret;
xinlei 105:c61f0d62b625 78 // else
xinlei 105:c61f0d62b625 79 // return CYASSL_CBIO_ERR_GENERAL;
xinlei 105:c61f0d62b625 80 //}
xinlei 105:c61f0d62b625 81
vwochnik 0:ed4d6fd405ea 82 int main()
vwochnik 0:ed4d6fd405ea 83 {
xinlei 105:c61f0d62b625 84 LCDDisplay::inst().setLines("Mbed Agent V2.1rc3", srHost);
xinlei 93:61d44636f020 85 set_time(1256729737);
xinlei 92:0acd11870c6a 86 MDMRtos<MDMSerial> mdm;
xinlei 92:0acd11870c6a 87 pMdm = &mdm;
xinlei 92:0acd11870c6a 88 InterruptIn joystickUp(A2);
xinlei 92:0acd11870c6a 89 InterruptIn joystickDown(A3);
xinlei 100:dbcd3bc51758 90 joystickUp.rise(&enableDebug);
xinlei 100:dbcd3bc51758 91 joystickDown.rise(&disableDebug);
xinlei 100:dbcd3bc51758 92
xinlei 105:c61f0d62b625 93 // CyaSSL_Init();
xinlei 105:c61f0d62b625 94 // pCtx = CyaSSL_CTX_new(CyaTLSv1_2_client_method());
xinlei 105:c61f0d62b625 95 // CyaSSL_Debugging_ON();
xinlei 105:c61f0d62b625 96 // if (pCtx == NULL) {
xinlei 105:c61f0d62b625 97 // shutdown();
xinlei 105:c61f0d62b625 98 // return 1;
xinlei 105:c61f0d62b625 99 // }
xinlei 105:c61f0d62b625 100 // CyaSSL_CTX_set_verify(pCtx, SSL_VERIFY_NONE, 0);
xinlei 105:c61f0d62b625 101 // CyaSSL_CTX_set_cipher_list(pCtx, "DHE-RSA-AES128-GCM-SHA256:ECDH-RSA-AES128-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA");
xinlei 105:c61f0d62b625 102 // CyaSSL_SetIORecv(pCtx, recv);
xinlei 105:c61f0d62b625 103 // CyaSSL_SetIOSend(pCtx, send);
xinlei 105:c61f0d62b625 104
Cumulocity 41:804f6a0bda26 105 MDMParser::DevStatus devStatus;
xinlei 76:b07effe83fb8 106 if (!mdm.init(SIM_PIN, &devStatus)) {
xinlei 95:5dfdc8568e9f 107 LCDDisplay::inst().setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN");
xinlei 105:c61f0d62b625 108 shutdown();
xinlei 105:c61f0d62b625 109 return 2;
xinlei 87:b1de34154513 110 }
xinlei 92:0acd11870c6a 111
xinlei 92:0acd11870c6a 112 DigitalIn fireButton(D4);
xinlei 92:0acd11870c6a 113 if (fireButton) {
xinlei 100:dbcd3bc51758 114 if (delCredential()) {
xinlei 95:5dfdc8568e9f 115 LCDDisplay::inst().setLines("Reset Success");
xinlei 92:0acd11870c6a 116 } else {
xinlei 95:5dfdc8568e9f 117 LCDDisplay::inst().setLines("Reset Failure");
xinlei 87:b1de34154513 118 }
xinlei 100:dbcd3bc51758 119 Thread::wait(2000);
xinlei 105:c61f0d62b625 120 shutdown();
xinlei 89:0525121f307e 121 return 0;
xinlei 89:0525121f307e 122 }
xinlei 95:5dfdc8568e9f 123 LCDDisplay::inst().setLines("Register Network...", "IMEI", devStatus.imei);
Cumulocity 41:804f6a0bda26 124 if (!mdm.registerNet()) {
xinlei 95:5dfdc8568e9f 125 LCDDisplay::inst().setLines("No Network Coverage");
xinlei 105:c61f0d62b625 126 shutdown();
xinlei 92:0acd11870c6a 127 return 3;
vwochnik 28:2004400abeec 128 }
vwochnik 28:2004400abeec 129
xinlei 95:5dfdc8568e9f 130 LCDDisplay::inst().setLines("Join Network");
vwochnik 52:8f1370084268 131 #ifdef SIM_APN
vwochnik 52:8f1370084268 132 if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
vwochnik 55:a0f7295ed6b6 133 #else
vwochnik 55:a0f7295ed6b6 134 if (mdm.join() == NOIP) {
vwochnik 55:a0f7295ed6b6 135 #endif
xinlei 92:0acd11870c6a 136 char s[8] = "Unknown";
xinlei 80:757c3ff7b92b 137 const char *p = devStatus.imsi;
xinlei 80:757c3ff7b92b 138 if (p) {
xinlei 80:757c3ff7b92b 139 snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3);
xinlei 80:757c3ff7b92b 140 }
xinlei 95:5dfdc8568e9f 141 LCDDisplay::inst().setLines("Wrong APN Settting", "MCC-MNC:", s);
xinlei 105:c61f0d62b625 142 shutdown();
xinlei 92:0acd11870c6a 143 return 4;
vwochnik 52:8f1370084268 144 }
xinlei 92:0acd11870c6a 145
vwochnik 52:8f1370084268 146 {
xinlei 96:ea056f6be2e8 147 DeviceInfo deviceInfo(devStatus);
xinlei 98:e369fc75c000 148 MbedAgent agent(deviceInfo);
xinlei 92:0acd11870c6a 149
xinlei 95:5dfdc8568e9f 150 LCDDisplay::inst().setLines("Agent Init");
vwochnik 52:8f1370084268 151 if (!agent.init()) {
xinlei 105:c61f0d62b625 152 shutdown();
xinlei 92:0acd11870c6a 153 return 5;
vwochnik 52:8f1370084268 154 }
xinlei 95:5dfdc8568e9f 155 LCDDisplay::inst().setLines("Agent Run");
xinlei 92:0acd11870c6a 156 int ret = 0;
xinlei 92:0acd11870c6a 157 for (uint8_t tries = 3; tries; --tries) {
xinlei 92:0acd11870c6a 158 ret = agent.run();
xinlei 95:5dfdc8568e9f 159 if (ret == 0) {
xinlei 95:5dfdc8568e9f 160 char status[27];
xinlei 98:e369fc75c000 161 const char* user= srUsername;
xinlei 95:5dfdc8568e9f 162 int len = strchr(user, '/')-user+sizeof("Tenant: ");
xinlei 95:5dfdc8568e9f 163 len = len <= 27 ? len : 27;
xinlei 95:5dfdc8568e9f 164 snprintf(status, len, "Tenant: %s", user);
xinlei 95:5dfdc8568e9f 165 LCDDisplay::inst().setFirstLine(status);
vwochnik 52:8f1370084268 166 break;
xinlei 95:5dfdc8568e9f 167 }
xinlei 92:0acd11870c6a 168 }
xinlei 105:c61f0d62b625 169 if (ret == 0)
xinlei 105:c61f0d62b625 170 agent.loop();
xinlei 105:c61f0d62b625 171 shutdown();
xinlei 92:0acd11870c6a 172 return ret;
vwochnik 52:8f1370084268 173 }
vwochnik 4:363b4cc49445 174 }