--- smsdaemon/GsmModem.cpp 2008/06/15 20:45:14 83 +++ smsdaemon/ModemTransceiver.cpp 2008/12/12 13:23:01 182 @@ -5,31 +5,33 @@ #include #include -#include #include -#include "SerialPort.h" +#include "serialport/SerialPort.h" -#include "GsmModem.h" +#include "ModemTransceiver.h" -#include "util.h" -#include "common.h" +#include "Util.h" +#include "Common.h" +#include "Logger.h" +#include "ConfigFile.h" #include "SmsPdu.h" +#include "Exceptions.h" using namespace std; -GsmModem::GsmModem(SerialPort& serialport) -: m_port(serialport) +ModemTransceiver::ModemTransceiver(SerialPort& serialport) + : m_port(serialport) { } -string GsmModem::GetResponse() +string ModemTransceiver::GetResponse() { SerialPort::DataBuffer buf; @@ -43,7 +45,7 @@ } -string GsmModem::Command(string command, string term) +string ModemTransceiver::Command(string command, string term) { time_t start,now; start = time(0); @@ -69,14 +71,14 @@ response += GetResponse(); Util::Sleep(1); - now = time(0); - if ( (now-start) > 10 ) - { - Common::instance()->logMessage( string("GsmModem::Command time out --") + command); - Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) ); + now = time(0); + if ( (now-start) > 10 ) + { + Logger::logMessage( string("ModemTransceiver::Command time out --") + command); + Logger::logMessage( string("Modem responded: ") + Util::str_trim(response) ); _timeout = true; - break; - } + break; + } } Util::Sleep(5); @@ -85,7 +87,7 @@ return response; } -vector GsmModem::ReadSms(bool readAll) +vector ModemTransceiver::ReadSms(bool readAll) { Command( "AT+CMGF=0" ); //Set SMS format to PDU @@ -96,7 +98,7 @@ cmd.append("=4"); string result = Command(cmd); - + vector retval; if (result.find(search) == string::npos) return retval; @@ -108,13 +110,22 @@ { unsigned int endpos = result.find(search,5); - + string sms_entry = result.substr(0,endpos); - retval.push_back( SMS::FromPduString(sms_entry) );; + + try + { + SMS sms = FromPduString(sms_entry); + + retval.push_back( sms ); + } + catch (smsnotfoundexception& e) //do nothing + { + } if (endpos == string::npos) break; - + result = result.substr(endpos, result.length() - endpos); } @@ -122,9 +133,9 @@ } -void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" +void ModemTransceiver::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" { - Common::instance()->logMessage( string("SMS pdu send") ); + Logger::logMessage( string("SMS pdu send") ); Command("AT+CMGF=0"); Util::Sleep(2); @@ -142,14 +153,14 @@ Common::instance()->smsCounter.outgoing++; } -void GsmModem::SendSms(string to, string message, bool allowMultipart) +void ModemTransceiver::SendSms(string to, string message, bool allowMultipart) { - Common::instance()->logMessage( string("SMS send to ") + to); + Logger::logMessage( string("SMS send to ") + to); if (to.at(0) == '+') to.erase(0,0); - vector pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart); + vector pdu_vec = SmsPdu::CreateSmsPdu(to, Util::str_latin2gsm(message), allowMultipart); for (unsigned i=0; i sms = ReadSms(true); for (unsigned int i= 0; i 10) + throw std::runtime_error("Sim card timed out:"); + Util::Sleep(100); + } + +} + +void ModemTransceiver::HandlePincode() +{ + string pin = Common::instance()->GetConfigfile()->GetValue("gsmmodem","pin"); + + string result = Command("AT+CPIN?"); + result = Util::str_trim(result); + result.erase(result.length() -2, 2); //remove trailing ok + result = Util::str_trim(result); + if (result != "+CPIN: READY") + { + if (result == "+CPIN: SIM PIN") + { + Command("AT^SSET=1"); + result = Command( string("AT+CPIN=")+pin ); + if ( result.substr(result.length()-4, 4) != "OK\r\n") + throw std::runtime_error(string("Illegal pincode: ") + result); + + WaitForSimcard(); + } + else + { + throw std::runtime_error(string("AT+CPIN? returned unhandled code: ") + result); + } + + } +} + +void ModemTransceiver::Init() { Command( "AT" ); if (_timeout) @@ -198,27 +255,70 @@ //Command("AT+CGSMS=2"); //SMS over GPRS preferred - //Set RealTimeClock ?? - - //Enter pin code ?? + HandlePincode(); } - - -void DebugGsmModem::SendSms(std::string to, std::string message, bool allowMultipart) +SMS ModemTransceiver::FromRawString(const std::string& input) { - _to=to; - _message = message; - _multipart = allowMultipart; + std::string smsline = input.substr(7, input.length() -7); //strip "+CMGL: " + + std::vector lines = Util::str_split(smsline, "\r\n"); + std::vector fields = Util::str_split(lines[0],","); - if (_print) + std::string body; + for (unsigned i=1; i 0) + body += "\r\n"; + body += body_line; + } } + + body = Util::str_trim(body); + + SMS sms; + + sms.SetIndex( fields[0] ); + + + std::string sender = fields[2]; + sender = Util::str_replace(sender, "\""); + sms.SetSender(sender); + + std::string timestamp = fields[4] + std::string(",") + fields[5]; + timestamp = Util::str_replace(timestamp, "\""); + sms.SetTimestamp(timestamp); + + sms.SetMessage( Util::str_gsm2latin(body) ); + + return sms; +} + +SMS ModemTransceiver::FromPduString(const std::string& input) +{ + std::string smsline = input.substr(7, input.length() -7); //strip "+CMGL: " + + std::vector lines = Util::str_split(smsline, "\r\n"); + std::vector fields = Util::str_split(lines[0],","); + + std::string index = fields[0]; + DeleteSms(index); + + SMS sms = SmsPdu::ParseSmsPdu(lines[1]); + + sms.SetIndex(index); + + sms.SetMessage( Util::str_gsm2latin(sms.GetMessage()) ) ; + + return sms; } + + +