--- smsdaemon/GsmModem.cpp 2008/06/13 08:37:19 72 +++ smsdaemon/GsmModem.cpp 2008/12/07 00:59:05 132 @@ -2,15 +2,13 @@ */ #include - #include #include -#include #include -#include "SerialPort.h" +#include "serialport/SerialPort.h" #include "GsmModem.h" @@ -82,8 +80,6 @@ Util::Sleep(5); -// cout << response.length() << ":" << response << endl; -// DumpString(response); return response; } @@ -91,12 +87,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); @@ -113,7 +109,7 @@ string sms_entry = result.substr(0,endpos); - retval.push_back( SMS::FromRawString(sms_entry) );; + retval.push_back( SMS::FromPduString(sms_entry) );; if (endpos == string::npos) break; @@ -183,6 +179,49 @@ +void GsmModem::WaitForSimcard() +{ + int start = time(0); + string result; + + + while (result != "^SSIM READY") + { + result += GetResponse(); + result = Util::str_trim(result); + + if ( (time(0) - start) > 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" ); @@ -197,9 +236,11 @@ 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(); }