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-10
Revision:
6:642e7c233e83
Parent:
5:dee05a7c70f9
Child:
7:f77afd49c35d

File content as of revision 6:642e7c233e83:

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

C027 c027;
ATCommandsInterface *cmdIface = NULL;
char cIMEI[21] = "";
char cCellId[9] = "";
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;
    }
    
    printf("IMEI: %s\r\n", imei());
    printf("Cell-ID: %s\r\n", cellId());
    sigq_t *sq = signalQuality();
    printf("RSSI: %d dBm, BER: %d %%\r\n", sq->rssi, sq->ber);

    //puts("Starting program...");    
    //ret = program();

    modem.disconnect();
    c027.mdmPower(false);
    
    return ret;
}

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

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