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

Diff of /smsdaemon/ModemTransceiver.cpp

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20