Cellular library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mtsas mtsas mtsas mtsas

Cellular/CellularFactory.cpp

Committer:
Vanger
Date:
2014-08-05
Revision:
51:ffc556ba33f7
Parent:
41:8b9b5098696f
Child:
52:2cb58398a4f9

File content as of revision 51:ffc556ba33f7:

#include "mbed.h"
#include "CellularFactory.h"
#include "MTSLog.h"
#include <string>

using namespace mts;

Cellular* CellularFactory::create(MTSBufferedIO* io) {
    bool uip;
    std::string model;
    std::string reply;
    Cellular::Radio type;
    Cellular* cell;
    
    /* wait for radio to get into a good state */
    while (true) {
        if (sendCommand(io, "AT", 1000).find("OK") != string::npos) {
            logTrace("radio replied");
            break;
        } else {
            logTrace("waiting on radio...");
        }
        wait(1);
    }
    
    /* "ATI4" gets us the model (HE910, DE910, etc) */
    for (int i = 0; i < 5; i++) {
        model = sendCommand(io, "ATI4", 3000);
        if (model.find("error") == string::npos && model.find("ERROR") == string::npos && !model.empty()) {
            /* didn't get an error - keep going */
            if(model.find("#STN") != string::npos) {
                //If response found is from unsolicited response #STN: from the radio,
                //then we got an unsolicited response, and to try again.
                continue;
            }
                break;
        }
        
        wait(1);
    }

    /* AT#VVERSION is a UIP specific AT command
     * if we get an error response, we're not using a UIP board */
    reply = sendCommand(io, "AT#VVERSION", 2000);
    if ((reply.find("ERROR") != string::npos) || (reply.find("error") != string::npos)) {
        uip = false;
    } else {
        uip = true;
    }
    

    if (uip && model.find("HE910") != string::npos) {
        type = Cellular::MTSMC_H5_IP;
        logDebug("radio model: HE910");
        cell = new UIP(type);
    } else if (uip && model.find("DE910") != string::npos) {
        type = Cellular::MTSMC_EV3_IP;
        logDebug("radio model: DE910");
        cell = new UIP(type);
    } else if (uip && model.find("CE910") != string::npos) {
        type = Cellular::MTSMC_C2_IP;
        logDebug("radio model: CE910");
        cell = new UIP(type);
    
    } else if (model.find("HE910") != string::npos) {
        type = Cellular::MTSMC_H5;
        logDebug("radio model: HE910");
        cell = new EasyIP(type);
    } else if (model.find("DE910") != string::npos) {
        type = Cellular::MTSMC_EV3;
        logDebug("radio model: DE910");
        cell = new EasyIP(type);
    } else if (model.find("GE910") != string::npos) {
        type = Cellular::MTSMC_G3;
        logDebug("radio model: GE910");
        cell = new EasyIP(type);
    } else if (model.find("CE910") != string::npos) {
        type = Cellular::MTSMC_C2;
        logDebug("radio model: CE910");
        cell = new EasyIP(type);
    
    } else {
        logError("cannot continue - could not determine radio type");
        return NULL;
    }

    if (! cell->init(io)) {
        logError("cellular initialization failed");
        return NULL;
    }

    return cell;
}