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

Diff of /smsdaemon/ModemTransceiver.cpp

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

revision 158 by torben, Mon Dec 8 21:49:49 2008 UTC revision 210 by torben, Sun Dec 21 21:14:40 2008 UTC
# Line 15  Line 15 
15  #include "Util.h"  #include "Util.h"
16  #include "Common.h"  #include "Common.h"
17  #include "Logger.h"  #include "Logger.h"
18    #include "ConfigFile.h"
19    
20  #include "SmsPdu.h"  #include "SmsPdu.h"
21  #include "SmsHelper.h"  #include "Exceptions.h"
22    
23  using namespace std;  using namespace std;
24    
25    
26    
27  ModemTransceiver::ModemTransceiver(SerialPort& serialport)  ModemTransceiver::ModemTransceiver(SerialPort& serialport)
28  : m_port(serialport)                  : m_port(serialport)
29  {  {
30  }  }
31    
# Line 70  string ModemTransceiver::Command(string Line 71  string ModemTransceiver::Command(string
71                  response += GetResponse();                  response += GetResponse();
72                  Util::Sleep(1);                  Util::Sleep(1);
73    
74          now = time(0);                  now = time(0);
75          if ( (now-start) > 10 )                  if ( (now-start) > 10 )
76          {                  {
77                  Logger::logMessage( string("ModemTransceiver::Command time out --") + command);                          Logger::logMessage( string("ModemTransceiver::Command time out --") + command);
78                          Logger::logMessage( string("Modem responded: ") + Util::str_trim(response) );                          Logger::logMessage( string("Modem responded: ") + Util::str_trim(response) );
79                          _timeout = true;                          _timeout = true;
80              break;                          break;
81          }                  }
82          }          }
83    
84          Util::Sleep(5);          Util::Sleep(5);
# Line 97  vector<SMS> ModemTransceiver::ReadSms(bo Line 98  vector<SMS> ModemTransceiver::ReadSms(bo
98                  cmd.append("=4");                  cmd.append("=4");
99    
100          string result = Command(cmd);          string result = Command(cmd);
101                    
102          vector<SMS> retval;          vector<SMS> retval;
103          if (result.find(search) == string::npos)          if (result.find(search) == string::npos)
104                  return retval;                  return retval;
# Line 109  vector<SMS> ModemTransceiver::ReadSms(bo Line 110  vector<SMS> ModemTransceiver::ReadSms(bo
110          {          {
111                  unsigned int endpos = result.find(search,5);                  unsigned int endpos = result.find(search,5);
112    
113                    
114                  string sms_entry = result.substr(0,endpos);                  string sms_entry = result.substr(0,endpos);
115                  retval.push_back( SmsHelper::FromPduString(sms_entry) );;  
116                    try
117                    {
118                            SMS sms = FromPduString(sms_entry);
119    
120                            retval.push_back( sms );
121                    }
122                    catch (smsnotfoundexception& e) //do nothing
123                    {
124                    }
125    
126                  if (endpos == string::npos)                  if (endpos == string::npos)
127                          break;                          break;
128            
129                  result = result.substr(endpos, result.length() - endpos);                  result = result.substr(endpos, result.length() - endpos);
130          }          }
131    
# Line 169  void ModemTransceiver::DeleteSms(std::st Line 179  void ModemTransceiver::DeleteSms(std::st
179          Command(cmd);          Command(cmd);
180  }  }
181    
 int  ModemTransceiver::DeleteAllSms()  
 {  
         vector<SMS> sms = ReadSms(true);  
   
         for (unsigned int i= 0; i<sms.size(); ++i)  
         {  
                 DeleteSms( sms[i].GetIndex() );  
         }  
         return sms.size();  
 }  
   
   
182    
183  void ModemTransceiver::WaitForSimcard()  void ModemTransceiver::WaitForSimcard()
184  {  {
# Line 202  void ModemTransceiver::WaitForSimcard() Line 200  void ModemTransceiver::WaitForSimcard()
200    
201  void ModemTransceiver::HandlePincode()  void ModemTransceiver::HandlePincode()
202  {  {
203            string pin = Common::instance()->GetConfigfile()->GetValue("gsmmodem","pin");
204    
205          string result = Command("AT+CPIN?");          string result = Command("AT+CPIN?");
206          result = Util::str_trim(result);          result = Util::str_trim(result);
207          result.erase(result.length() -2, 2); //remove trailing ok          result.erase(result.length() -2, 2); //remove trailing ok
# Line 211  void ModemTransceiver::HandlePincode() Line 211  void ModemTransceiver::HandlePincode()
211                  if (result == "+CPIN: SIM PIN")                  if (result == "+CPIN: SIM PIN")
212                  {                  {
213                          Command("AT^SSET=1");                          Command("AT^SSET=1");
214                          result = Command("AT+CPIN=0067");                          result = Command( string("AT+CPIN=")+pin  );
215                          if ( result.substr(result.length()-4, 4) != "OK\r\n")                          if ( result.substr(result.length()-4, 4) != "OK\r\n")
216                                  throw std::runtime_error(string("Illegal pincode: ") + result);                                  throw std::runtime_error(string("Illegal pincode: ") + result);
217                            
218                          WaitForSimcard();                          WaitForSimcard();
219                  }                  }
220                  else                  else
# Line 247  void ModemTransceiver::Init() Line 247  void ModemTransceiver::Init()
247  }  }
248    
249    
250    SMS ModemTransceiver::FromRawString(const std::string& input)
251    {
252            std::string smsline = input.substr(7, input.length() -7); //strip "+CMGL: "
253    
254            std::vector<std::string> lines = Util::str_split(smsline, "\r\n");
255            std::vector<std::string> fields = Util::str_split(lines[0],",");
256    
257            std::string body;
258            for (unsigned i=1; i<lines.size(); ++i)
259            {
260                    std::string body_line = lines[i];
261    
262    
263                    if (body_line != "")
264                    {
265                            if (body.length() > 0)
266                                    body += "\r\n";
267                            body += body_line;
268                    }
269            }
270    
271            body = Util::str_trim(body);
272    
273            SMS sms;
274    
275            sms.SetIndex( fields[0] );
276    
277    
278            std::string sender = fields[2];
279            sender = Util::str_replace(sender, "\"");
280            sms.SetSender(sender);
281    
282            std::string timestamp = fields[4] + std::string(",") + fields[5];
283            timestamp = Util::str_replace(timestamp, "\"");
284            sms.SetTimestamp(timestamp);
285    
286            sms.SetMessage( Util::str_gsm2latin(body) );
287    
288            return sms;
289    }
290    
291    SMS ModemTransceiver::FromPduString(const std::string& input)
292    {
293            std::string smsline = input.substr(7, input.length() -7); //strip "+CMGL: "
294    
295            std::vector<std::string> lines = Util::str_split(smsline, "\r\n");
296            std::vector<std::string> fields = Util::str_split(lines[0],",");
297    
298            std::string index = fields[0];
299            DeleteSms(index);
300    
301            SMS sms = SmsPdu::ParseSmsPdu(lines[1]);
302    
303            sms.SetIndex(index);
304    
305            sms.SetMessage( Util::str_gsm2latin(sms.GetMessage()) ) ;
306    
307            return sms;
308    }
309    
310    
311    
312    

Legend:
Removed from v.158  
changed lines
  Added in v.210

  ViewVC Help
Powered by ViewVC 1.1.20