/* using http://sourceforge.net/projects/libserial/ */ #include #include #include #include #include #include #include "SerialPort.h" #include "GsmModem.h" #include "util.h" #include "common.h" using namespace std; GsmModem::GsmModem(SerialPort& serialport) : m_port(serialport) { Init(); } string GsmModem::GetResponse() { SerialPort::DataBuffer buf; m_port.Read(buf); buf.push_back(0); std::string str((char*) &buf[0]); return str; } string GsmModem::Command(string command, string term) { time_t start,now; start = time(0); command.append("\r"); m_port.Write(command); Util::Sleep(25); string response = GetResponse(); unsigned int tlen = term.length(); while ( 1 ) { if (response.length() >= tlen) { if (response.substr(response.length()-tlen,tlen) == term) break; } response += GetResponse(); Util::Sleep(25); now = time(0); if ( (now-start) > 10 ) { Common::instance()->logMessage( string("GsmModem::Command time out --") + command); break; } } Util::Sleep(5); // cout << response.length() << ":" << response << endl; // DumpString(response); return response; } vector GsmModem::ReadSms(bool readAll) { const string search = "+CMGL: "; std::string cmd = "AT+CMGL"; if (readAll) cmd.append("=ALL"); string result = Command(cmd); vector retval; if (result.find(search) == string::npos) return retval; result = result.substr(2, result.length() - 8); //remove trailing "\r\nOK\r\n" and initial "\r\n" while ( 1 ) { unsigned int endpos = result.find(search,5); string sms_entry = result.substr(0,endpos); retval.push_back( SMS::FromRawString(sms_entry) );; if (endpos == string::npos) break; result = result.substr(endpos, result.length() - endpos); } return retval; } void GsmModem::SendSms(string to, string message) { Common::instance()->logMessage( string("SMS send to ") + to); Command("AT+CMGF=1");///Allways telling the format makes the application more stable Util::Sleep(2); string line1 = "AT+CMGS="; line1.append(to); line1.append("\r"); Command(line1,"> "); message.append("\032\r"); // \032 == Ctrl+Z Command(message); Util::Sleep(50); //Give the modem some time to send the sms and be ready again } void GsmModem::DeleteSms(std::string smsIndex) { string cmd = "AT+CMGD="; cmd.append(smsIndex); Command(cmd); } int GsmModem::DeleteAllSms() { vector sms = ReadSms(true); for (unsigned int i= 0; i