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

Diff of /smsdaemon/ModemTransceiver.cpp

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

revision 36 by torben, Tue Jun 10 15:34:19 2008 UTC revision 75 by torben, Fri Jun 13 10:10:06 2008 UTC
# Line 2  Line 2 
2   */   */
3    
4  #include <iostream>  #include <iostream>
   
5  #include <string>  #include <string>
 #include <cstring>  
6  #include <stdexcept>  #include <stdexcept>
7    
8  #include <sys/time.h>  #include <sys/time.h>
# Line 18  Line 16 
16  #include "util.h"  #include "util.h"
17  #include "common.h"  #include "common.h"
18    
19    #include "SmsPdu.h"
20    
21  using namespace std;  using namespace std;
22    
# Line 26  using namespace std; Line 25  using namespace std;
25  GsmModem::GsmModem(SerialPort& serialport)  GsmModem::GsmModem(SerialPort& serialport)
26  : m_port(serialport)  : m_port(serialport)
27  {  {
         Init();  
28  }  }
29    
30    
# Line 49  string GsmModem::Command(string command, Line 47  string GsmModem::Command(string command,
47  {  {
48          time_t  start,now;          time_t  start,now;
49          start = time(0);          start = time(0);
50            _timeout = false;
51    
52          if (term != "> ")          if (term != "> ")
53                  command.append("\r"); //Dont append CarriageReturn if sending SMS                  command.append("\r"); //Dont append CarriageReturn if sending SMS
# Line 74  string GsmModem::Command(string command, Line 73  string GsmModem::Command(string command,
73          if ( (now-start) > 10 )          if ( (now-start) > 10 )
74          {          {
75                  Common::instance()->logMessage( string("GsmModem::Command time out --") + command);                  Common::instance()->logMessage( string("GsmModem::Command time out --") + command);
76                            Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) );
77                            _timeout = true;
78              break;              break;
79          }          }
80          }          }
81    
82          Util::Sleep(5);          Util::Sleep(5);
83    
 //      cout  << response.length() << ":" << response << endl;  
 //      DumpString(response);  
84    
85          return response;          return response;
86  }  }
87    
88  vector<SMS> GsmModem::ReadSms(bool readAll)  vector<SMS> GsmModem::ReadSms(bool readAll)
89  {  {
90    
91            Command( "AT+CMGF=0" ); //Set SMS format to PDU
92    
93          const string search = "+CMGL: ";          const string search = "+CMGL: ";
94          std::string cmd = "AT+CMGL";          std::string cmd = "AT+CMGL";
95          if (readAll)          if (readAll)
96                  cmd.append("=ALL");                  cmd.append("=4");
97    
98          string result = Command(cmd);          string result = Command(cmd);
99                                    
# Line 108  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::FromRawString(sms_entry) );;                  retval.push_back( SMS::FromPduString(sms_entry) );;
114    
115                  if (endpos == string::npos)                  if (endpos == string::npos)
116                          break;                          break;
# Line 119  vector<SMS> GsmModem::ReadSms(bool readA Line 121  vector<SMS> GsmModem::ReadSms(bool readA
121          return retval;          return retval;
122  }  }
123    
124  void GsmModem::SendSms(string to, string message)  
125    void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00"
126  {  {
127          Common::instance()->logMessage( string("SMS send to ") + to);          Common::instance()->logMessage( string("SMS pdu send") );
128    
129          Command("AT+CMGF=1");///Allways telling the format makes the application more stable          Command("AT+CMGF=0");
130          Util::Sleep(2);          Util::Sleep(2);
131    
132          string line1 = "AT+CMGS=";          string line1 = "AT+CMGS=";
133          line1.append(to);          line1.append( Util::str_formatint(len) );
134          line1.append("\r");          line1.append("\r");
135    
136    
137          Command(line1,"> ");          Command(line1,"> ");
138    
139            pdu.append("\032"); // \032 == Ctrl+Z
140            Command( pdu );
141            Util::Sleep( 50 );
142            Common::instance()->smsCounter.outgoing++;
143    }
144    
145    void GsmModem::SendSms(string to, string message, bool allowMultipart)
146    {
147            Common::instance()->logMessage( string("SMS send to ") + to);
148    
149          message.append("\032"); // \032 == Ctrl+Z          if (to.at(0) == '+')
150                    to.erase(0,0);
151    
152            vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart);
153    
154            for (unsigned i=0; i<pdu_vec.size(); ++i)
155            {
156                    PduInfo& pdu = pdu_vec[i];
157    
158                    SendSmsPdu(pdu.pdu, pdu.len);
159            }
160    
         Command(message);  
         Util::Sleep(50); //Give the modem some time to send the sms and be ready again  
         Common::instance()->smsCounter.outgoing++;  
161  }  }
162    
163  void GsmModem::DeleteSms(std::string smsIndex)  void GsmModem::DeleteSms(std::string smsIndex)
# Line 162  int  GsmModem::DeleteAllSms() Line 182  int  GsmModem::DeleteAllSms()
182    
183  void GsmModem::Init()  void GsmModem::Init()
184  {  {
185            Command( "AT" );
186            if (_timeout)
187                    throw std::runtime_error("Modem did not respond!");
188    
189          Command( "ATZ" ); //Reset any previous setup          Command( "ATZ" ); //Reset any previous setup
190    
191          Command( "AT\\Q3" ); //Hardware flow control          Command( "AT\\Q3" ); //Hardware flow control
192    
193          Command( "ATE0" ); //Disable echo          Command( "ATE0" ); //Disable echo
194    
         Command( "AT+CMGF=1" ); //Set SMS format to text  
   
195          Command ("AT^SM20=0,0" ); //No SM20 compability          Command ("AT^SM20=0,0" ); //No SM20 compability
196    
197          //Set RealTimeClock ??          //Set RealTimeClock ??
198                    
199          //Enter pin code ??          //Enter pin code ??
200  }  }
201    
202    
203    
204    
205    void DebugGsmModem::SendSms(std::string to, std::string message, bool allowMultipart)
206    {
207            _to=to;
208            _message = message;
209            _multipart = allowMultipart;
210    
211            if (_print)
212            {
213                    cout << "DebugGsmModem::SendSms --------------" << endl;
214                    cout << "To:        " << to << endl;;
215                    cout << "Message:   " << message << endl;
216                    cout << "Multipart: " << allowMultipart << endl;
217    
218            }
219    }
220    

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

  ViewVC Help
Powered by ViewVC 1.1.20