Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp
- Committer:
- vwochnik
- Date:
- 2014-03-24
- Revision:
- 23:0a48eebaaba8
- Parent:
- 20:ef9cc1b42e9d
- Child:
- 24:8f9b678a7932
File content as of revision 23:0a48eebaaba8:
#include "common.h" #include "apndb.h" #include "io.h" #include "ATResultBuffer.h" C027 c027; ATCommandsInterface *cmdIface = NULL; char cIMEI[21] = ""; char cIMSI[16] = ""; char cCellId[9] = ""; char cICCID[24] = ""; sigq_t sigQ = {0, 0}; int main() { int ret; puts("Started..."); io_init(); puts("IO initialized."); c027.mdmWakeup(); c027.mdmReset(); c027.mdmPower(true); UbloxUSBGSMModem modem; cmdIface = modem.getATCommandsInterface(); puts("Connecting..."); if (modem.connect("")) { puts("Searching for login..."); // get imsi number printf("IMSI: %s\n", imsi()); apndb_t *apn = apndb_get(imsi()); if (apn == NULL) puts("No APN found."); else printf("APN: %s\n", apn->apn); if (modem.connect("public4.m2minternet.com")) { puts("GPRS connection failure."); return 2; } printf("IMSI: %s\n", imsi()); apn = apndb_get(imsi()); if (apn == NULL) puts("No APN found."); else printf("APN: %s\n", apn->apn); } // get before starting program as they're permanent if ((!strlen(imei())) || (!strlen(iccid()))) { puts("Couldn't read IMEI or ICCID."); return 2; } // Shown on display signalQuality(); puts("Starting program..."); // restart program upon failure do { ret = program(); if (ret) puts("Restarting program..."); } while (ret); modem.disconnect(); c027.mdmPower(false); return 0; } const char * imei() { ATCommandsInterface::ATResult result; ATResultBuffer buffer; if ((cmdIface == NULL) || (strlen(cIMEI))) return cIMEI; cmdIface->execute("AT+CGSN", &buffer, &result); if (ATCommandsInterface::ATResult::AT_OK == result.result) strcpy(cIMEI, buffer.getLastLine()); else cIMEI[0] = '\0'; return cIMEI; } const char * imsi() { ATCommandsInterface::ATResult result; ATResultBuffer buffer; if ((cmdIface == NULL) || (strlen(cIMSI))) return cIMSI; cmdIface->execute("AT+CIMI", &buffer, &result); if (ATCommandsInterface::ATResult::AT_OK == result.result) strcpy(cIMSI, buffer.getLastLine()); else cIMSI[0] = '\0'; return cIMSI; } const char * cellId() { int len; uint32_t cellId; ATCommandsInterface::ATResult result; ATResultBuffer buffer; cCellId[0] = '\0'; if (cmdIface == NULL) return cCellId; cmdIface->executeSimple("AT+CREG=2", &result); if (ATCommandsInterface::ATResult::AT_OK != result.result) return cCellId; cmdIface->execute("AT+CREG?", &buffer, &result); if (buffer.getLastLine() == NULL) return cCellId; sscanf(buffer.getLastLine(), "+CREG: 2,%*d,\"%*lx\",\"%lx\",%*d%n", &cellId, &len); if (len != strlen(buffer.getLastLine())) return cCellId; snprintf(cCellId, sizeof(cCellId), "%lX", cellId); return cCellId; } const char * iccid() { int len; ATCommandsInterface::ATResult result; ATResultBuffer buffer; if ((cmdIface == NULL) || (strlen(cICCID))) return cICCID; cmdIface->execute("AT+CCID?", &buffer, &result); if (buffer.getLastLine() == NULL) return cCellId; sscanf(buffer.getLastLine(), "+CCID: %s%n", cICCID, &len); if (len != strlen(buffer.getLastLine())) cICCID[0] = '\0'; return cICCID; } sigq_t * signalQuality() { int rssi = 0, ber = 0, len = 0; ATCommandsInterface::ATResult result; ATResultBuffer buffer; sigQ.rssi = 0; sigQ.ber = 0; lcd_signal(0, 0); if (cmdIface == NULL) return &sigQ; cmdIface->execute("AT+CSQ", &buffer, &result); if (buffer.getLastLine() == NULL) return &sigQ; sscanf(buffer.getLastLine(), "+CSQ: %d,%d%n", &rssi, &ber, &len); if (len != strlen(buffer.getLastLine())) return &sigQ; if (rssi <= 31) sigQ.rssi = -113 + (2 * rssi); else sigQ.rssi = 0; switch (ber) { case 0: sigQ.ber = 49; break; case 1: sigQ.ber = 43; break; case 2: sigQ.ber = 37; break; case 3: sigQ.ber = 31; break; case 4: sigQ.ber = 25; break; case 5: sigQ.ber = 19; break; case 6: sigQ.ber = 13; break; case 7: sigQ.ber = 7; break; default: sigQ.ber = 0; } lcd_signal(sigQ.rssi, sigQ.ber); return &sigQ; }