Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@130:ca312ec4bd0f, 2015-08-07 (annotated)
- Committer:
- xinlei
- Date:
- Fri Aug 07 12:38:28 2015 +0000
- Revision:
- 130:ca312ec4bd0f
- Parent:
- 128:b81a6ed6addc
- Child:
- 133:fb53a2e461b2
GPSTracker: fix for new data format
Who changed what in which revision?
User | Revision | Line number | New 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 | 120:d4d44550e087 | 47 | setLevel(A_WARNING); |
xinlei | 92:0acd11870c6a | 48 | if (pMdm) |
xinlei | 120:d4d44550e087 | 49 | pMdm->setDebug(0); |
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 | 130:ca312ec4bd0f | 84 | LCDDisplay::inst().setLines("Mbed Agent V2.1", srHost); |
xinlei | 128:b81a6ed6addc | 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 | |
xinlei | 105:c61f0d62b625 | 91 | // CyaSSL_Init(); |
xinlei | 105:c61f0d62b625 | 92 | // pCtx = CyaSSL_CTX_new(CyaTLSv1_2_client_method()); |
xinlei | 105:c61f0d62b625 | 93 | // CyaSSL_Debugging_ON(); |
xinlei | 105:c61f0d62b625 | 94 | // if (pCtx == NULL) { |
xinlei | 105:c61f0d62b625 | 95 | // shutdown(); |
xinlei | 105:c61f0d62b625 | 96 | // return 1; |
xinlei | 105:c61f0d62b625 | 97 | // } |
xinlei | 105:c61f0d62b625 | 98 | // CyaSSL_CTX_set_verify(pCtx, SSL_VERIFY_NONE, 0); |
xinlei | 105:c61f0d62b625 | 99 | // 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 | 100 | // CyaSSL_SetIORecv(pCtx, recv); |
xinlei | 105:c61f0d62b625 | 101 | // CyaSSL_SetIOSend(pCtx, send); |
xinlei | 105:c61f0d62b625 | 102 | |
Cumulocity | 41:804f6a0bda26 | 103 | MDMParser::DevStatus devStatus; |
xinlei | 76:b07effe83fb8 | 104 | if (!mdm.init(SIM_PIN, &devStatus)) { |
xinlei | 95:5dfdc8568e9f | 105 | LCDDisplay::inst().setLines("Modem Init Failure", "No SIM card found", "Or SIM has PIN"); |
xinlei | 105:c61f0d62b625 | 106 | shutdown(); |
xinlei | 105:c61f0d62b625 | 107 | return 2; |
xinlei | 87:b1de34154513 | 108 | } |
xinlei | 110:ee9ae4a5e55c | 109 | joystickUp.rise(&enableDebug); |
xinlei | 110:ee9ae4a5e55c | 110 | joystickDown.rise(&disableDebug); |
xinlei | 110:ee9ae4a5e55c | 111 | |
xinlei | 92:0acd11870c6a | 112 | DigitalIn fireButton(D4); |
xinlei | 92:0acd11870c6a | 113 | if (fireButton) { |
xinlei | 110:ee9ae4a5e55c | 114 | LCDDisplay::inst().setLines("Factory resetting..."); |
xinlei | 110:ee9ae4a5e55c | 115 | Thread::wait(2000); // wait for Disk I/O to be ready |
xinlei | 110:ee9ae4a5e55c | 116 | if (resetCredential()) { |
xinlei | 95:5dfdc8568e9f | 117 | LCDDisplay::inst().setLines("Reset Success"); |
xinlei | 92:0acd11870c6a | 118 | } else { |
xinlei | 95:5dfdc8568e9f | 119 | LCDDisplay::inst().setLines("Reset Failure"); |
xinlei | 87:b1de34154513 | 120 | } |
xinlei | 105:c61f0d62b625 | 121 | shutdown(); |
xinlei | 89:0525121f307e | 122 | return 0; |
xinlei | 89:0525121f307e | 123 | } |
xinlei | 95:5dfdc8568e9f | 124 | LCDDisplay::inst().setLines("Register Network...", "IMEI", devStatus.imei); |
Cumulocity | 41:804f6a0bda26 | 125 | if (!mdm.registerNet()) { |
xinlei | 95:5dfdc8568e9f | 126 | LCDDisplay::inst().setLines("No Network Coverage"); |
xinlei | 105:c61f0d62b625 | 127 | shutdown(); |
xinlei | 92:0acd11870c6a | 128 | return 3; |
vwochnik | 28:2004400abeec | 129 | } |
vwochnik | 28:2004400abeec | 130 | |
xinlei | 95:5dfdc8568e9f | 131 | LCDDisplay::inst().setLines("Join Network"); |
vwochnik | 52:8f1370084268 | 132 | #ifdef SIM_APN |
vwochnik | 52:8f1370084268 | 133 | if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 134 | #else |
vwochnik | 55:a0f7295ed6b6 | 135 | if (mdm.join() == NOIP) { |
vwochnik | 55:a0f7295ed6b6 | 136 | #endif |
xinlei | 92:0acd11870c6a | 137 | char s[8] = "Unknown"; |
xinlei | 80:757c3ff7b92b | 138 | const char *p = devStatus.imsi; |
xinlei | 80:757c3ff7b92b | 139 | if (p) { |
xinlei | 80:757c3ff7b92b | 140 | snprintf(s, sizeof(s), "%.*s-%.*s", 3, p, getMNCLen(p), p+3); |
xinlei | 80:757c3ff7b92b | 141 | } |
xinlei | 116:5de54f09f754 | 142 | LCDDisplay::inst().setLines("Unknown APN Settting", "MCC-MNC:", s); |
xinlei | 105:c61f0d62b625 | 143 | shutdown(); |
xinlei | 92:0acd11870c6a | 144 | return 4; |
vwochnik | 52:8f1370084268 | 145 | } |
xinlei | 92:0acd11870c6a | 146 | |
vwochnik | 52:8f1370084268 | 147 | { |
xinlei | 96:ea056f6be2e8 | 148 | DeviceInfo deviceInfo(devStatus); |
xinlei | 98:e369fc75c000 | 149 | MbedAgent agent(deviceInfo); |
xinlei | 92:0acd11870c6a | 150 | |
xinlei | 95:5dfdc8568e9f | 151 | LCDDisplay::inst().setLines("Agent Init"); |
vwochnik | 52:8f1370084268 | 152 | if (!agent.init()) { |
xinlei | 105:c61f0d62b625 | 153 | shutdown(); |
xinlei | 92:0acd11870c6a | 154 | return 5; |
vwochnik | 52:8f1370084268 | 155 | } |
xinlei | 95:5dfdc8568e9f | 156 | LCDDisplay::inst().setLines("Agent Run"); |
xinlei | 92:0acd11870c6a | 157 | int ret = 0; |
xinlei | 92:0acd11870c6a | 158 | for (uint8_t tries = 3; tries; --tries) { |
xinlei | 92:0acd11870c6a | 159 | ret = agent.run(); |
xinlei | 95:5dfdc8568e9f | 160 | if (ret == 0) { |
xinlei | 95:5dfdc8568e9f | 161 | char status[27]; |
xinlei | 112:3872569be2af | 162 | snprintf(status, sizeof(status), "Tenant: %s", srTenant); |
xinlei | 95:5dfdc8568e9f | 163 | LCDDisplay::inst().setFirstLine(status); |
xinlei | 112:3872569be2af | 164 | agent.loop(); |
vwochnik | 52:8f1370084268 | 165 | break; |
xinlei | 95:5dfdc8568e9f | 166 | } |
xinlei | 92:0acd11870c6a | 167 | } |
xinlei | 105:c61f0d62b625 | 168 | shutdown(); |
xinlei | 92:0acd11870c6a | 169 | return ret; |
vwochnik | 52:8f1370084268 | 170 | } |
vwochnik | 4:363b4cc49445 | 171 | } |