![](/media/cache/group/u-blox_logo_trans.png.50x50_q85.png)
portable version of the cumulocity demo
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Diff: main.cpp
- Revision:
- 41:804f6a0bda26
- Parent:
- 34:8a0556f39161
- Child:
- 42:104746744af8
diff -r 13c24225f189 -r 804f6a0bda26 main.cpp --- a/main.cpp Thu Jul 10 14:22:11 2014 +0000 +++ b/main.cpp Tue Jul 15 12:44:34 2014 +0000 @@ -1,26 +1,16 @@ -#include "common.h" +#include "mbed.h" +#include "rtos.h" +#include "MDM.h" +#include "DeviceInfo.h" +#include "MbedAgent.h" #include "apndb.h" -#include "io.h" - -C027 c027; -MDMSerial* pMdm; -sigq_t sigQ = {}; -char cCellId[8+1]; -MDMParser::DevStatus devStatus = {}; -MDMParser::NetStatus netStatus = {}; int main() { - int ret; size_t c; - - puts("Hello"); - - io_init(); - puts("IO initialized."); + MDMParser::DevStatus devStatus; MDMSerial mdm; //mdm.setDebug(4); - pMdm = &mdm; if (!mdm.init(NULL, &devStatus)) { puts("Modem initialization failed. Check your PIN number."); @@ -38,7 +28,7 @@ printf("IMEI: %s\n", devStatus.imei); printf("IMSI: %s\n", devStatus.imsi); - if (!mdm.registerNet(&netStatus)) { + if (!mdm.registerNet()) { puts("Network registration failed."); return 1; } @@ -50,116 +40,29 @@ return 1; } - // Shown on display - signalQuality(); + DeviceInfo deviceInfo(mdm, devStatus); + MbedAgent agent(mdm, deviceInfo); - puts("Starting program..."); + puts("Starting agent ..."); + if (!agent.init()) { + puts("Initialization failure."); + mdm.disconnect(); + return 1; + } - // restart program upon failure + size_t tries = 3; + do { - ret = program(); - if (ret) - puts("Restarting program..."); - } while (ret); + puts("Running agent ..."); + + if (!agent.run()) { + puts("Agent failure."); + continue; + } else { + break; + } + } while (--tries > 0); mdm.disconnect(); - c027.mdmPower(false); - return 0; } - -const char * imei() -{ - return devStatus.imei; -} - -const char * imsi() -{ - return devStatus.imsi; -} - -const char * cellId() -{ - if (!pMdm->checkNetStatus(&netStatus)) - return NULL; - - if (snprintf(cCellId, sizeof(cCellId), "%X", netStatus.ci) < 1) - return NULL; - - return cCellId; -} - -const char * iccid() -{ - return devStatus.ccid; -} - -sigq_t * signalQuality() -{ - sigQ.rssi = 0; sigQ.ber = 0; - if (!pMdm->checkNetStatus(&netStatus)) - return NULL; - - sigQ.rssi = netStatus.rssi; - sigQ.ber = netStatus.ber; - - lcd_signal(sigQ.rssi, sigQ.ber); - return &sigQ; -} - -void credentials_set(credentials_t *dst, const char *tenant, const char *username, const char *password) -{ - char *ptr = dst->username; - - strcpy(ptr, tenant); - ptr += strlen(tenant); - strcpy(ptr++, "/"); - strcpy(ptr, username); - strcpy(dst->password, password); -} - -bool credentials_read(credentials_t *dst) -{ - char buf[CREDENTIALS_BUFFER], *ptr; - - int res = pMdm->readFile(CREDENTIALS_FILE, buf, CREDENTIALS_BUFFER); - - if (res < 0) - return false; - - buf[res] = '\0'; - if ((ptr = strchr(buf, '\n')) == NULL) - return false; - *ptr = '\0'; - - ptr = buf; - strcpy(dst->username, ptr); - ptr += strlen(ptr)+1; - strcpy(dst->password, ptr); - return true; -} - -void credentials_write(credentials_t *src) -{ - char buf[CREDENTIALS_BUFFER], *ptr; - size_t len; - - ptr = buf; - len = strlen(src->username); - strcpy(ptr, src->username); - ptr += len; - - *ptr++ = '\n'; - len++; - - len += strlen(src->password); - strcpy(ptr, src->password); - - pMdm->delFile(CREDENTIALS_FILE); - pMdm->writeFile(CREDENTIALS_FILE, buf, len); -} - -void credentials_reset() -{ - pMdm->delFile(CREDENTIALS_FILE); -}