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

Diff of /smsdaemon/ModemTransceiver.cpp

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

revision 72 by torben, Fri Jun 13 08:37:19 2008 UTC revision 136 by torben, Sun Dec 7 10:15:44 2008 UTC
# Line 2  Line 2 
2   */   */
3    
4  #include <iostream>  #include <iostream>
   
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 "GsmModem.h"
14    
# Line 82  string GsmModem::Command(string command, Line 80  string GsmModem::Command(string command,
80    
81          Util::Sleep(5);          Util::Sleep(5);
82    
 //      cout  << response.length() << ":" << response << endl;  
 //      DumpString(response);  
83    
84          return response;          return response;
85  }  }
# Line 91  string GsmModem::Command(string command, Line 87  string GsmModem::Command(string command,
87  vector<SMS> GsmModem::ReadSms(bool readAll)  vector<SMS> GsmModem::ReadSms(bool readAll)
88  {  {
89    
90          Command( "AT+CMGF=1" ); //Set SMS format to text          Command( "AT+CMGF=0" ); //Set SMS format to PDU
91    
92          const string search = "+CMGL: ";          const string search = "+CMGL: ";
93          std::string cmd = "AT+CMGL";          std::string cmd = "AT+CMGL";
94          if (readAll)          if (readAll)
95                  cmd.append("=ALL");                  cmd.append("=4");
96    
97          string result = Command(cmd);          string result = Command(cmd);
98                                    
# Line 113  vector<SMS> GsmModem::ReadSms(bool readA Line 109  vector<SMS> GsmModem::ReadSms(bool readA
109    
110                                    
111                  string sms_entry = result.substr(0,endpos);                  string sms_entry = result.substr(0,endpos);
112                  retval.push_back( SMS::FromRawString(sms_entry) );;                  retval.push_back( SMS::FromPduString(sms_entry) );;
113    
114                  if (endpos == string::npos)                  if (endpos == string::npos)
115                          break;                          break;
# Line 152  void GsmModem::SendSms(string to, string Line 148  void GsmModem::SendSms(string to, string
148          if (to.at(0) == '+')          if (to.at(0) == '+')
149                  to.erase(0,0);                  to.erase(0,0);
150    
151          vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart);          vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, Util::str_latin2gsm(message), allowMultipart);
152    
153          for (unsigned i=0; i<pdu_vec.size(); ++i)          for (unsigned i=0; i<pdu_vec.size(); ++i)
154          {          {
# Line 160  void GsmModem::SendSms(string to, string Line 156  void GsmModem::SendSms(string to, string
156    
157                  SendSmsPdu(pdu.pdu, pdu.len);                  SendSmsPdu(pdu.pdu, pdu.len);
158          }          }
159            Common::instance()->logMessage( "All PDU's send");
160    
161  }  }
162    
# Line 183  int  GsmModem::DeleteAllSms() Line 180  int  GsmModem::DeleteAllSms()
180    
181    
182    
183    void GsmModem::WaitForSimcard()
184    {
185            int start = time(0);
186            string result;
187    
188    
189            while (result != "^SSIM READY")
190            {
191                    result += GetResponse();
192                    result = Util::str_trim(result);
193    
194                    if ( (time(0) - start) > 10)
195                            throw std::runtime_error("Sim card timed out:");
196                    Util::Sleep(100);
197            }
198    
199    }
200    
201    void GsmModem::HandlePincode()
202    {
203            string result = Command("AT+CPIN?");
204            result = Util::str_trim(result);
205            result.erase(result.length() -2, 2); //remove trailing ok
206            result = Util::str_trim(result);
207            if (result != "+CPIN: READY")
208            {
209                    if (result == "+CPIN: SIM PIN")
210                    {
211                            Command("AT^SSET=1");
212                            result = Command("AT+CPIN=0067");
213                            if ( result.substr(result.length()-4, 4) != "OK\r\n")
214                                    throw std::runtime_error(string("Illegal pincode: ") + result);
215                            
216                            WaitForSimcard();
217                    }
218                    else
219                    {
220                            throw std::runtime_error(string("AT+CPIN? returned unhandled code: ") + result);
221                    }
222    
223            }
224    }
225    
226  void GsmModem::Init()  void GsmModem::Init()
227  {  {
228          Command( "AT" );          Command( "AT" );
# Line 197  void GsmModem::Init() Line 237  void GsmModem::Init()
237    
238          Command ("AT^SM20=0,0" ); //No SM20 compability          Command ("AT^SM20=0,0" ); //No SM20 compability
239    
240          //Set RealTimeClock ??          //Command("AT+CGATT=1"); //GPRS Attach
241            
242          //Enter pin code ??          //Command("AT+CGSMS=2"); //SMS over GPRS preferred
243    
244            HandlePincode();
245  }  }
246    
247    

Legend:
Removed from v.72  
changed lines
  Added in v.136

  ViewVC Help
Powered by ViewVC 1.1.20