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

Dependents:   mtsas mtsas mtsas mtsas

Cellular/Cellular.cpp

Committer:
Mike Fiore
Date:
2014-05-21
Revision:
10:c188cc05aed5
Parent:
9:1a03e3f3e7fe
Child:
11:4e428f689069

File content as of revision 10:c188cc05aed5:

#include "mbed.h"
#include "Cellular.h"
#include "MTSText.h"
#include "MTSLog.h"

using namespace mts;

bool Cellular::init(MTSBufferedIO* io)
{
    if (io == NULL) {
        return false;
    }
    this->io = io;

    return true;
}

bool Cellular::configureSignals(unsigned int DCD, unsigned int DTR, unsigned int RESET)
{
    //Set DCD - The radio will raise and lower this line
    if (DCD != NC) {
        dcd = new DigitalIn(PinName(DCD));
    }
    /* Set DTR - This line should be lowered when we want to talk to the radio and raised when we're done
    * for now we will lower it in the constructor and raise it in the destructor.
    */
    if (DTR != NC) {
        dtr = new DigitalOut(PinName(DTR));
        dtr->write(0);
    }
    //Set RESET - Set the hardware reset line to the radio
    if (RESET != NC) {
        resetLine = new DigitalOut(PinName(RESET));
    }
    return true;
}

std::string Cellular::getRegistrationNames(Registration registration)
{
    switch(registration) {
        case NOT_REGISTERED:
            return "NOT_REGISTERED";
        case REGISTERED:
            return "REGISTERED";
        case SEARCHING:
            return "SEARCHING";
        case DENIED:
            return "DENIED";
        case UNKNOWN:
            return "UNKNOWN";
        case ROAMING:
            return "ROAMING";
        default:
            return "UNKNOWN ENUM";
    }
}

Code Cellular::test()
{
    int i = 0;
    while (sendBasicCommand("AT", 1000) != SUCCESS) {
        i++;
        if (i >= 30) {
            logError("Could not talk to radio after 30 tries");
            i = 0;
        }
        wait(1);
    }
    return SUCCESS;
}

int Cellular::getSignalStrength()
{
    string response = sendCommand("AT+CSQ", 1000);
    if (response.find("OK") == string::npos) {
        return -1;
    }
    int start = response.find(':');
    int stop = response.find(',', start);
    string signal = response.substr(start + 2, stop - start - 2);
    int value;
    sscanf(signal.c_str(), "%d", &value);
    return value;
}

Cellular::Registration Cellular::getRegistration()
{
    string response = sendCommand("AT+CREG?", 5000);
    if (response.find("OK") == string::npos) {
        return UNKNOWN;
    }
    int start = response.find(',');
    int stop = response.find(' ', start);
    string regStat = response.substr(start + 1, stop - start - 1);
    int value;
    sscanf(regStat.c_str(), "%d", &value);
    switch (value) {
        case 0:
            return NOT_REGISTERED;
        case 1:
            return REGISTERED;
        case 2:
            return SEARCHING;
        case 3:
            return DENIED;
        case 4:
            return UNKNOWN;
        case 5:
            return ROAMING;
    }
    return UNKNOWN;
}

Code Cellular::setApn(const std::string& apn)
{
    Code code = sendBasicCommand("AT#APNSERV=\"" + apn + "\"", 1000);
    if (code != SUCCESS) {
        return code;
    }
    this->apn = apn;
    return code;
}


Code Cellular::setDns(const std::string& primary, const std::string& secondary)
{
    return sendBasicCommand("AT#DNS=1," + primary + "," + secondary, 1000);
}

Code Cellular::sendSMS(const Sms& sms)
{
    return sendSMS(sms.phoneNumber, sms.message);
}

Code Cellular::sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc)
{
    if(socketOpened) {
        logError("socket is open. Can not send AT commands");
        return ERROR;
    }

    string response = sendCommand(command, timeoutMillis, esc);
    if (response.size() == 0) {
        return NO_RESPONSE;
    } else if (response.find("OK") != string::npos) {
        return SUCCESS;
    } else if (response.find("ERROR") != string::npos) {
        return ERROR;
    } else {
        return FAILURE;
    }
}

string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, char esc)
{
    if(io == NULL) {
        logError("MTSBufferedIO not set");
        return "";
    }
    if(socketOpened) {
        logError("socket is open. Can not send AT commands");
        return "";
    }

    io->rxClear();
    io->txClear();
    std::string result;

    //Attempt to write command
    if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) {
        //Failed to write command
        if (command != "AT" && command != "at") {
            logError("failed to send command to radio within %d milliseconds", timeoutMillis);
        }
        return "";
    }

    //Send Escape Character
    if (esc != 0x00) {
        if(io->write(esc, timeoutMillis) != 1) {
            if (command != "AT" && command != "at") {
                logError("failed to send character '%c' (0x%02X) to radio within %d milliseconds", esc, esc, timeoutMillis);
            }
            return "";
        }
    }

    int timer = 0;
    size_t previous = 0;
    char tmp[256];
    tmp[255] = 0;
    bool started = !echoMode;
    bool done = false;
    do {
        wait(0.1);
        timer += 100;

        previous = result.size();
        //Make a non-blocking read call by passing timeout of zero
        int size = io->read(tmp,255,0);    //1 less than allocated (timeout is instant)
        if(size > 0) {
            result.append(tmp, size);
        }
        if(!started) {
            //In Echo Mode (Command will have echo'd + 2 characters for \r\n)
            if(result.size() > command.size() + 2) {
                started = true;
            }
        } else {
            done =  (result.size() == previous);
        }
        if(timer >= timeoutMillis) {
            if (command != "AT" && command != "at") {
                logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis);
            }
            done = true;
        }
    } while (!done);

    return result;
}

Code Cellular::sendSMS(const std::string& phoneNumber, const std::string& message)
{
    Code code = sendBasicCommand("AT+CMGF=1", 1000);
    if (code != SUCCESS) {
        return code;
    }
    string cmd = "AT+CMGS=\"+";
    cmd.append(phoneNumber);
    cmd.append("\"");
    string response1 = sendCommand(cmd, 1000);
    if (response1.find('>') == string::npos) {
        return NO_RESPONSE;
    }
    wait(.2);
    string  response2 = sendCommand(message, 4000, CTRL_Z);
    logInfo("SMS Response: %s", response2.c_str());
    if (response2.find("+CMGS:") == string::npos) {
        return FAILURE;
    }
    return SUCCESS;
}

std::vector<Cellular::Sms> Cellular::getReceivedSms()
{
    int smsNumber = 0;
    std::vector<Sms> vSms;
    std::string received = sendCommand("AT+CMGL=\"ALL\"", 4000);
    size_t pos = received.find("+CMGL: ");

    while (pos != std::string::npos) {
        Cellular::Sms sms;
        std::string line(Text::getLine(received, pos, pos));
        if(line.find("+CMGL: ") == std::string::npos) {
            continue;
        }

        //Start of SMS message
        std::vector<std::string> vSmsParts = Text::split(line, ',');
        if(vSmsParts.size() != 6) {
            logWarning("Expected 6 commas. SMS[%d] DATA[%s]. Continuing ...", smsNumber, line.c_str());
            continue;
        }

        sms.phoneNumber = vSmsParts[2];
        sms.timestamp = vSmsParts[4] + ", " + vSmsParts[5];

        if(pos == std::string::npos) {
            logWarning("Expected SMS body. SMS[%d]. Leaving ...", smsNumber);
            break;
        }
        //Check for the start of the next SMS message
        size_t bodyEnd = received.find("\r\n+CMGL: ", pos);
        if(bodyEnd == std::string::npos) {
            //This must be the last SMS message
            bodyEnd = received.find("\r\n\r\nOK", pos);
        }

        //Safety check that we found the boundary of this current SMS message
        if(bodyEnd != std::string::npos) {
            sms.message = received.substr(pos, bodyEnd - pos);
        } else {
            sms.message = received.substr(pos);
            logWarning("Expected to find end of SMS list. SMS[%d] DATA[%s].", smsNumber, sms.message.c_str());
        }
        vSms.push_back(sms);
        pos = bodyEnd;
        smsNumber++;
    }
    logInfo("Received %d SMS", smsNumber);
    return vSms;
}

Code Cellular::deleteOnlyReceivedReadSms()
{
    return sendBasicCommand("AT+CMGD=1,1", 1000);
}

Code Cellular::deleteAllReceivedSms()
{
    return sendBasicCommand("AT+CMGD=1,4", 1000);
}