--- smsdaemon/GsmModem.cpp 2008/06/11 16:14:28 58 +++ smsdaemon/ModemTransceiver.cpp 2008/12/08 21:49:49 158 @@ -2,35 +2,35 @@ */ #include - #include -#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 "SmsPdu.h" +#include "SmsHelper.h" using namespace std; -GsmModem::GsmModem(SerialPort& serialport) +ModemTransceiver::ModemTransceiver(SerialPort& serialport) : m_port(serialport) { } -string GsmModem::GetResponse() +string ModemTransceiver::GetResponse() { SerialPort::DataBuffer buf; @@ -44,7 +44,7 @@ } -string GsmModem::Command(string command, string term) +string ModemTransceiver::Command(string command, string term) { time_t start,now; start = time(0); @@ -73,7 +73,8 @@ now = time(0); if ( (now-start) > 10 ) { - Common::instance()->logMessage( string("GsmModem::Command time out --") + command); + Logger::logMessage( string("ModemTransceiver::Command time out --") + command); + Logger::logMessage( string("Modem responded: ") + Util::str_trim(response) ); _timeout = true; break; } @@ -81,18 +82,19 @@ Util::Sleep(5); -// cout << response.length() << ":" << response << endl; -// DumpString(response); return response; } -vector GsmModem::ReadSms(bool readAll) +vector ModemTransceiver::ReadSms(bool readAll) { + + Command( "AT+CMGF=0" ); //Set SMS format to PDU + const string search = "+CMGL: "; std::string cmd = "AT+CMGL"; if (readAll) - cmd.append("=ALL"); + cmd.append("=4"); string result = Command(cmd); @@ -109,7 +111,7 @@ string sms_entry = result.substr(0,endpos); - retval.push_back( SMS::FromRawString(sms_entry) );; + retval.push_back( SmsHelper::FromPduString(sms_entry) );; if (endpos == string::npos) break; @@ -120,55 +122,110 @@ return retval; } -void GsmModem::SendSms(string to, string message) + +void ModemTransceiver::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" { - Common::instance()->logMessage( string("SMS send to ") + to); + Logger::logMessage( string("SMS pdu send") ); - Command("AT+CMGF=1");///Allways telling the format makes the application more stable + Command("AT+CMGF=0"); Util::Sleep(2); string line1 = "AT+CMGS="; - line1.append(to); + line1.append( Util::str_formatint(len) ); line1.append("\r"); + Command(line1,"> "); - if (message.length() > 160) - { - message = message.substr(0,160); - Common::instance()->logMessage( "Trunkating message! "); - } + pdu.append("\032"); // \032 == Ctrl+Z + Command( pdu ); + Util::Sleep( 50 ); + Common::instance()->smsCounter.outgoing++; +} + +void ModemTransceiver::SendSms(string to, string message, bool allowMultipart) +{ + Logger::logMessage( string("SMS send to ") + to); - message.append("\032"); // \032 == Ctrl+Z + if (to.at(0) == '+') + to.erase(0,0); - Command( message ); //In textmode limit to 160 bytes + vector pdu_vec = SmsPdu::CreateSmsPdu(to, Util::str_latin2gsm(message), allowMultipart); + for (unsigned i=0; ismsCounter.outgoing++; } -void GsmModem::DeleteSms(std::string smsIndex) +void ModemTransceiver::DeleteSms(std::string smsIndex) { string cmd = "AT+CMGD="; cmd.append(smsIndex); Command(cmd); } -int GsmModem::DeleteAllSms() +int ModemTransceiver::DeleteAllSms() { vector 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 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("AT+CPIN=0067"); + 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) @@ -180,11 +237,13 @@ Command( "ATE0" ); //Disable echo - Command( "AT+CMGF=1" ); //Set SMS format to text - Command ("AT^SM20=0,0" ); //No SM20 compability - //Set RealTimeClock ?? - - //Enter pin code ?? + //Command("AT+CGATT=1"); //GPRS Attach + + //Command("AT+CGSMS=2"); //SMS over GPRS preferred + + HandlePincode(); } + +