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

Diff of /smsdaemon/ModemTransceiver.cpp

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

revision 58 by torben, Wed Jun 11 16:14:28 2008 UTC revision 93 by torben, Mon Jun 16 11:39:33 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    
 #include <sys/time.h>  
8  #include <time.h>  #include <time.h>
9    
10    
# Line 18  Line 15 
15  #include "util.h"  #include "util.h"
16  #include "common.h"  #include "common.h"
17    
18    #include "SmsPdu.h"
19    
20  using namespace std;  using namespace std;
21    
# Line 74  string GsmModem::Command(string command, Line 72  string GsmModem::Command(string command,
72          if ( (now-start) > 10 )          if ( (now-start) > 10 )
73          {          {
74                  Common::instance()->logMessage( string("GsmModem::Command time out --") + command);                  Common::instance()->logMessage( string("GsmModem::Command time out --") + command);
75                            Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) );
76                          _timeout = true;                          _timeout = true;
77              break;              break;
78          }          }
# Line 81  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  }  }
86    
87  vector<SMS> GsmModem::ReadSms(bool readAll)  vector<SMS> GsmModem::ReadSms(bool readAll)
88  {  {
89    
90            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 109  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 120  vector<SMS> GsmModem::ReadSms(bool readA Line 120  vector<SMS> GsmModem::ReadSms(bool readA
120          return retval;          return retval;
121  }  }
122    
123  void GsmModem::SendSms(string to, string message)  
124    void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00"
125  {  {
126          Common::instance()->logMessage( string("SMS send to ") + to);          Common::instance()->logMessage( string("SMS pdu send") );
127    
128          Command("AT+CMGF=1");///Allways telling the format makes the application more stable          Command("AT+CMGF=0");
129          Util::Sleep(2);          Util::Sleep(2);
130    
131          string line1 = "AT+CMGS=";          string line1 = "AT+CMGS=";
132          line1.append(to);          line1.append( Util::str_formatint(len) );
133          line1.append("\r");          line1.append("\r");
134    
135    
136          Command(line1,"> ");          Command(line1,"> ");
137    
138          if (message.length() > 160)          pdu.append("\032"); // \032 == Ctrl+Z
139          {          Command( pdu );
140                  message = message.substr(0,160);          Util::Sleep( 50 );
141                  Common::instance()->logMessage( "Trunkating message! ");          Common::instance()->smsCounter.outgoing++;
142          }  }
143    
144    void GsmModem::SendSms(string to, string message, bool allowMultipart)
145    {
146            Common::instance()->logMessage( string("SMS send to ") + to);
147    
148          message.append("\032"); // \032 == Ctrl+Z          if (to.at(0) == '+')
149                    to.erase(0,0);
150    
151          Command( message ); //In textmode limit to 160 bytes          vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart);
152    
153            for (unsigned i=0; i<pdu_vec.size(); ++i)
154            {
155                    PduInfo& pdu = pdu_vec[i];
156    
157                    SendSmsPdu(pdu.pdu, pdu.len);
158            }
159    
         Util::Sleep(50); //Give the modem some time to send the sms and be ready again  
         Common::instance()->smsCounter.outgoing++;  
160  }  }
161    
162  void GsmModem::DeleteSms(std::string smsIndex)  void GsmModem::DeleteSms(std::string smsIndex)
# Line 180  void GsmModem::Init() Line 191  void GsmModem::Init()
191    
192          Command( "ATE0" ); //Disable echo          Command( "ATE0" ); //Disable echo
193    
         Command( "AT+CMGF=1" ); //Set SMS format to text  
   
194          Command ("AT^SM20=0,0" ); //No SM20 compability          Command ("AT^SM20=0,0" ); //No SM20 compability
195    
196            //Command("AT+CGATT=1"); //GPRS Attach
197    
198            //Command("AT+CGSMS=2"); //SMS over GPRS preferred
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.58  
changed lines
  Added in v.93

  ViewVC Help
Powered by ViewVC 1.1.20