A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
Diff: cellular/Cellular.cpp
- Revision:
- 19:38794784e009
- Parent:
- 17:2d7c4ea7491b
- Child:
- 23:bc6f98a1eb22
--- a/cellular/Cellular.cpp Mon Dec 16 20:02:37 2013 +0000 +++ b/cellular/Cellular.cpp Mon Dec 16 23:02:22 2013 +0000 @@ -3,8 +3,27 @@ #include "Cellular.h" #include "MTSText.h" +#include "MTSSerial.h" -Cellular::Cellular(MTSBufferedIO& io) +Cellular* Cellular::instance = NULL; + +Cellular* Cellular::getInstance() { + if(instance == NULL) { + instance = new Cellular(NULL); + } + return instance; +} + + +Cellular* Cellular::getInstance(MTSBufferedIO* io) { + if(instance == NULL) { + instance = new Cellular(io); + } + instance->io = io; + return instance; +} + +Cellular::Cellular(MTSBufferedIO* io) : io(io) , echoMode(true) , pppConnected(false) @@ -206,6 +225,11 @@ } int Cellular::read(char* data, int max, int timeout) { + if(io == NULL) { + printf("[ERROR] MTSBufferedIO not set\r\n"); + return -1; + } + if(!socketOpened) { printf("[ERROR] Socket is not open\r\n"); return -1; @@ -216,8 +240,8 @@ Timer tmr; tmr.start(); while (tmr.read_ms() <= timeout && bytesRead < max) { - if (io.readable()) { - if(io.read(data[bytesRead]) == 1) { + if (io->readable()) { + if(io->read(data[bytesRead]) == 1) { bytesRead++; } } else { @@ -225,13 +249,18 @@ } } } else { - bytesRead = io.read(data, max); + bytesRead = io->read(data, max); } return bytesRead; } int Cellular::write(char* data, int length, int timeout) { + if(io == NULL) { + printf("[ERROR] MTSBufferedIO not set\r\n"); + return -1; + } + if(!socketOpened) { printf("[ERROR] Socket is not open\r\n"); return -1; @@ -243,8 +272,8 @@ Timer tmr; tmr.start(); while (tmr.read_ms() <= timeout && bytesWritten < length) { - if (io.writeable()) { - if(io.write(*data) == 1) { + if (io->writeable()) { + if(io->write(*data) == 1) { data++; bytesWritten++; } @@ -253,27 +282,35 @@ } } } else { - bytesWritten = io.write(data, length); + bytesWritten = io->write(data, length); } return bytesWritten; } unsigned int Cellular::readable() { + if(io == NULL) { + printf("[ERROR] MTSBufferedIO not set\r\n"); + return 0; + } if(!socketOpened) { printf("[ERROR] Socket is not open\r\n"); return 0; } - return io.readable(); + return io->readable(); } unsigned int Cellular::writeable() { + if(io == NULL) { + printf("[ERROR] MTSBufferedIO not set\r\n"); + return 0; + } if(!socketOpened) { printf("[ERROR] Socket is not open\r\n"); return 0; } - return io.writeable(); + return io->writeable(); } void Cellular::reset() { @@ -490,6 +527,10 @@ string Cellular::sendCommand(string command, int timeoutMillis, ESC_CHAR esc) { + if(io == NULL) { + printf("[ERROR] MTSBufferedIO not set\r\n"); + return ""; + } if(socketOpened) { printf("[ERROR] socket is open. Can not send AT commands\r\n"); return ""; @@ -506,11 +547,11 @@ cmd[size -1] = '\0'; } - io.rxClear(); - io.txClear(); + io->rxClear(); + io->txClear(); std::string result; - int status = io.write(cmd, size); - int available = io.rxAvailable(); + int status = io->write(cmd, size); + int available = io->rxAvailable(); int previous = -1; int timer = 0; char tmp[256]; @@ -521,9 +562,9 @@ wait(.1); timer = timer + 100; previous = available; - available = io.rxAvailable(); + available = io->rxAvailable(); - int size = io.read(tmp,255); //1 less than allocated + int size = io->read(tmp,255); //1 less than allocated if(size > 0) { result.append(tmp, size); }