/[projects]/smsdaemon/ModemTransceiver.cpp
ViewVC logotype

Diff of /smsdaemon/ModemTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

smsdaemon/GsmModem.cpp revision 75 by torben, Fri Jun 13 10:10:06 2008 UTC smsdaemon/ModemTransceiver.cpp revision 149 by torben, Sun Dec 7 20:58:41 2008 UTC
# Line 5  Line 5 
5  #include <string>  #include <string>
6  #include <stdexcept>  #include <stdexcept>
7    
 #include <sys/time.h>  
8  #include <time.h>  #include <time.h>
9    
10    
11  #include "SerialPort.h"  #include "serialport/SerialPort.h"
12    
13  #include "GsmModem.h"  #include "ModemTransceiver.h"
14    
15  #include "util.h"  #include "util.h"
16  #include "common.h"  #include "common.h"
17    
18  #include "SmsPdu.h"  #include "SmsPdu.h"
19    #include "SmsHelper.h"
20    
21  using namespace std;  using namespace std;
22    
23    
24    
25  GsmModem::GsmModem(SerialPort& serialport)  ModemTransceiver::ModemTransceiver(SerialPort& serialport)
26  : m_port(serialport)  : m_port(serialport)
27  {  {
28  }  }
29    
30    
31    
32  string GsmModem::GetResponse()  string ModemTransceiver::GetResponse()
33  {  {
34    
35          SerialPort::DataBuffer buf;          SerialPort::DataBuffer buf;
# Line 43  string GsmModem::GetResponse() Line 43  string GsmModem::GetResponse()
43  }  }
44    
45    
46  string GsmModem::Command(string command, string term)  string ModemTransceiver::Command(string command, string term)
47  {  {
48          time_t  start,now;          time_t  start,now;
49          start = time(0);          start = time(0);
# Line 72  string GsmModem::Command(string command, Line 72  string GsmModem::Command(string command,
72          now = time(0);          now = time(0);
73          if ( (now-start) > 10 )          if ( (now-start) > 10 )
74          {          {
75                  Common::instance()->logMessage( string("GsmModem::Command time out --") + command);                  Common::instance()->logMessage( string("ModemTransceiver::Command time out --") + command);
76                          Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) );                          Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) );
77                          _timeout = true;                          _timeout = true;
78              break;              break;
# Line 85  string GsmModem::Command(string command, Line 85  string GsmModem::Command(string command,
85          return response;          return response;
86  }  }
87    
88  vector<SMS> GsmModem::ReadSms(bool readAll)  vector<SMS> ModemTransceiver::ReadSms(bool readAll)
89  {  {
90    
91          Command( "AT+CMGF=0" ); //Set SMS format to PDU          Command( "AT+CMGF=0" ); //Set SMS format to PDU
# Line 110  vector<SMS> GsmModem::ReadSms(bool readA Line 110  vector<SMS> GsmModem::ReadSms(bool readA
110    
111                                    
112                  string sms_entry = result.substr(0,endpos);                  string sms_entry = result.substr(0,endpos);
113                  retval.push_back( SMS::FromPduString(sms_entry) );;                  retval.push_back( SmsHelper::FromPduString(sms_entry) );;
114    
115                  if (endpos == string::npos)                  if (endpos == string::npos)
116                          break;                          break;
# Line 122  vector<SMS> GsmModem::ReadSms(bool readA Line 122  vector<SMS> GsmModem::ReadSms(bool readA
122  }  }
123    
124    
125  void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00"  void ModemTransceiver::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00"
126  {  {
127          Common::instance()->logMessage( string("SMS pdu send") );          Common::instance()->logMessage( string("SMS pdu send") );
128    
# Line 142  void GsmModem::SendSmsPdu(std::string pd Line 142  void GsmModem::SendSmsPdu(std::string pd
142          Common::instance()->smsCounter.outgoing++;          Common::instance()->smsCounter.outgoing++;
143  }  }
144    
145  void GsmModem::SendSms(string to, string message, bool allowMultipart)  void ModemTransceiver::SendSms(string to, string message, bool allowMultipart)
146  {  {
147          Common::instance()->logMessage( string("SMS send to ") + to);          Common::instance()->logMessage( string("SMS send to ") + to);
148    
149          if (to.at(0) == '+')          if (to.at(0) == '+')
150                  to.erase(0,0);                  to.erase(0,0);
151    
152          vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart);          vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, Util::str_latin2gsm(message), allowMultipart);
153    
154          for (unsigned i=0; i<pdu_vec.size(); ++i)          for (unsigned i=0; i<pdu_vec.size(); ++i)
155          {          {
# Line 157  void GsmModem::SendSms(string to, string Line 157  void GsmModem::SendSms(string to, string
157    
158                  SendSmsPdu(pdu.pdu, pdu.len);                  SendSmsPdu(pdu.pdu, pdu.len);
159          }          }
160            Common::instance()->logMessage( "All PDU's send");
161    
162  }  }
163    
164  void GsmModem::DeleteSms(std::string smsIndex)  void ModemTransceiver::DeleteSms(std::string smsIndex)
165  {  {
166          string cmd = "AT+CMGD=";          string cmd = "AT+CMGD=";
167          cmd.append(smsIndex);          cmd.append(smsIndex);
168          Command(cmd);          Command(cmd);
169  }  }
170    
171  int  GsmModem::DeleteAllSms()  int  ModemTransceiver::DeleteAllSms()
172  {  {
173          vector<SMS> sms = ReadSms(true);          vector<SMS> sms = ReadSms(true);
174    
175          for (unsigned int i= 0; i<sms.size(); ++i)          for (unsigned int i= 0; i<sms.size(); ++i)
176          {          {
177                  DeleteSms( sms[i].sms_index);                  DeleteSms( sms[i].GetIndex() );
178          }          }
179          return sms.size();          return sms.size();
180  }  }
181    
182    
183    
184  void GsmModem::Init()  void ModemTransceiver::WaitForSimcard()
185    {
186            int start = time(0);
187            string result;
188    
189    
190            while (result != "^SSIM READY")
191            {
192                    result += GetResponse();
193                    result = Util::str_trim(result);
194    
195                    if ( (time(0) - start) > 10)
196                            throw std::runtime_error("Sim card timed out:");
197                    Util::Sleep(100);
198            }
199    
200    }
201    
202    void ModemTransceiver::HandlePincode()
203    {
204            string result = Command("AT+CPIN?");
205            result = Util::str_trim(result);
206            result.erase(result.length() -2, 2); //remove trailing ok
207            result = Util::str_trim(result);
208            if (result != "+CPIN: READY")
209            {
210                    if (result == "+CPIN: SIM PIN")
211                    {
212                            Command("AT^SSET=1");
213                            result = Command("AT+CPIN=0067");
214                            if ( result.substr(result.length()-4, 4) != "OK\r\n")
215                                    throw std::runtime_error(string("Illegal pincode: ") + result);
216                            
217                            WaitForSimcard();
218                    }
219                    else
220                    {
221                            throw std::runtime_error(string("AT+CPIN? returned unhandled code: ") + result);
222                    }
223    
224            }
225    }
226    
227    void ModemTransceiver::Init()
228  {  {
229          Command( "AT" );          Command( "AT" );
230          if (_timeout)          if (_timeout)
# Line 194  void GsmModem::Init() Line 238  void GsmModem::Init()
238    
239          Command ("AT^SM20=0,0" ); //No SM20 compability          Command ("AT^SM20=0,0" ); //No SM20 compability
240    
241          //Set RealTimeClock ??          //Command("AT+CGATT=1"); //GPRS Attach
           
         //Enter pin code ??  
 }  
   
242    
243            //Command("AT+CGSMS=2"); //SMS over GPRS preferred
244    
245            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;  
   
         }  
246  }  }
247    
248    

Legend:
Removed from v.75  
changed lines
  Added in v.149

  ViewVC Help
Powered by ViewVC 1.1.20