Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

main.cpp

Committer:
vwochnik
Date:
2014-02-14
Revision:
12:beb64aa0da86
Parent:
8:940c782eec5e
Child:
13:4045b6ac448d

File content as of revision 12:beb64aa0da86:

#include "common.h"
#include "ATResultBuffer.h"

C027 c027;
ATCommandsInterface *cmdIface = NULL;
char cIMEI[21] = "";
char cCellId[9] = "";
char cICCID[24] = "";
sigq_t sigQ = {0, 0};

int main()
{
    int ret;
    
    puts("Started...");
    c027.mdmWakeup();
    c027.mdmReset();
    c027.mdmPower(true);
    UbloxUSBGSMModem modem;
    cmdIface = modem.getATCommandsInterface();
    
    puts("Connecting...");
    if (modem.connect("public4.m2minternet.com")) {
        puts("GPRS connection failure.");
        return 2;
    }
    
    // get before starting program as they're permanent
    if (!strlen(imei())) || (!strlen(iccid()))) {
        puts("Couldn't read IMEI or ICCID.");
        return 2;
    }

    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 * 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;

    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;
    }

    return &sigQ;
}