--- smsdaemon/GsmModem.cpp 2008/06/12 12:43:29 63 +++ smsdaemon/GsmModem.cpp 2008/12/07 15:51:02 142 @@ -2,15 +2,13 @@ */ #include - #include #include -#include #include -#include "SerialPort.h" +#include "serialport/SerialPort.h" #include "GsmModem.h" @@ -18,6 +16,7 @@ #include "common.h" #include "SmsPdu.h" +#include "SmsHelper.h" using namespace std; @@ -74,6 +73,7 @@ if ( (now-start) > 10 ) { Common::instance()->logMessage( string("GsmModem::Command time out --") + command); + Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) ); _timeout = true; break; } @@ -81,8 +81,6 @@ Util::Sleep(5); -// cout << response.length() << ":" << response << endl; -// DumpString(response); return response; } @@ -90,12 +88,12 @@ vector GsmModem::ReadSms(bool readAll) { - Command( "AT+CMGF=1" ); //Set SMS format to text + 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); @@ -112,7 +110,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; @@ -151,7 +149,7 @@ 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; ilogMessage( "All PDU's send"); } @@ -175,13 +174,56 @@ for (unsigned int i= 0; i 10) + throw std::runtime_error("Sim card timed out:"); + Util::Sleep(100); + } + +} + +void GsmModem::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 GsmModem::Init() { Command( "AT" ); @@ -196,9 +238,29 @@ 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(); } + + +void DebugGsmModem::SendSms(std::string to, std::string message, bool allowMultipart) +{ + _to=to; + _message = message; + _multipart = allowMultipart; + + if (_print) + { + cout << "DebugGsmModem::SendSms --------------" << endl; + cout << "To: " << to << endl;; + cout << "Message: " << message << endl; + cout << "Multipart: " << allowMultipart << endl; + + } +} +