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

Annotation of /smsdaemon/ModemTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (hide annotations) (download)
Fri Jun 13 10:10:06 2008 UTC (15 years, 11 months ago) by torben
Original Path: smsdaemon/GsmModem.cpp
File size: 3861 byte(s)
Make gsmmodem::readsms use pdu mode 

Cleaned up some unneeded cout and <iostream>


1 torben 26 /* using http://sourceforge.net/projects/libserial/
2     */
3    
4     #include <iostream>
5     #include <string>
6     #include <stdexcept>
7    
8     #include <sys/time.h>
9     #include <time.h>
10    
11    
12     #include "SerialPort.h"
13    
14     #include "GsmModem.h"
15    
16     #include "util.h"
17     #include "common.h"
18    
19 torben 63 #include "SmsPdu.h"
20 torben 26
21     using namespace std;
22    
23    
24    
25     GsmModem::GsmModem(SerialPort& serialport)
26     : m_port(serialport)
27     {
28     }
29    
30    
31    
32     string GsmModem::GetResponse()
33     {
34    
35     SerialPort::DataBuffer buf;
36     m_port.Read(buf);
37    
38     buf.push_back(0);
39    
40     std::string str((char*) &buf[0]);
41    
42     return str;
43     }
44    
45 torben 34
46 torben 26 string GsmModem::Command(string command, string term)
47     {
48     time_t start,now;
49     start = time(0);
50 torben 57 _timeout = false;
51 torben 26
52 torben 34 if (term != "> ")
53     command.append("\r"); //Dont append CarriageReturn if sending SMS
54    
55 torben 26 m_port.Write(command);
56    
57 torben 34 Util::Sleep(1);
58 torben 26 string response = GetResponse();
59    
60     unsigned int tlen = term.length();
61     while ( 1 )
62     {
63     if (response.length() >= tlen)
64     {
65     if (response.substr(response.length()-tlen,tlen) == term)
66     break;
67     }
68    
69     response += GetResponse();
70 torben 34 Util::Sleep(1);
71 torben 26
72     now = time(0);
73     if ( (now-start) > 10 )
74     {
75     Common::instance()->logMessage( string("GsmModem::Command time out --") + command);
76 torben 72 Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) );
77 torben 57 _timeout = true;
78 torben 26 break;
79     }
80     }
81    
82     Util::Sleep(5);
83    
84    
85     return response;
86     }
87    
88     vector<SMS> GsmModem::ReadSms(bool readAll)
89     {
90 torben 63
91 torben 75 Command( "AT+CMGF=0" ); //Set SMS format to PDU
92 torben 63
93 torben 26 const string search = "+CMGL: ";
94     std::string cmd = "AT+CMGL";
95     if (readAll)
96 torben 75 cmd.append("=4");
97 torben 26
98     string result = Command(cmd);
99    
100     vector<SMS> retval;
101     if (result.find(search) == string::npos)
102     return retval;
103    
104     result = result.substr(2, result.length() - 8); //remove trailing "\r\nOK\r\n" and initial "\r\n"
105    
106    
107     while ( 1 )
108     {
109     unsigned int endpos = result.find(search,5);
110    
111    
112     string sms_entry = result.substr(0,endpos);
113 torben 75 retval.push_back( SMS::FromPduString(sms_entry) );;
114 torben 26
115     if (endpos == string::npos)
116     break;
117    
118     result = result.substr(endpos, result.length() - endpos);
119     }
120    
121     return retval;
122     }
123    
124 torben 59
125     void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00"
126     {
127     Common::instance()->logMessage( string("SMS pdu send") );
128    
129     Command("AT+CMGF=0");
130     Util::Sleep(2);
131    
132     string line1 = "AT+CMGS=";
133     line1.append( Util::str_formatint(len) );
134     line1.append("\r");
135    
136    
137     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 torben 63 void GsmModem::SendSms(string to, string message, bool allowMultipart)
146 torben 26 {
147     Common::instance()->logMessage( string("SMS send to ") + to);
148    
149 torben 63 if (to.at(0) == '+')
150     to.erase(0,0);
151 torben 26
152 torben 63 vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart);
153 torben 26
154 torben 63 for (unsigned i=0; i<pdu_vec.size(); ++i)
155     {
156     PduInfo& pdu = pdu_vec[i];
157 torben 26
158 torben 63 SendSmsPdu(pdu.pdu, pdu.len);
159 torben 50 }
160 torben 26
161     }
162    
163     void GsmModem::DeleteSms(std::string smsIndex)
164     {
165     string cmd = "AT+CMGD=";
166     cmd.append(smsIndex);
167     Command(cmd);
168     }
169    
170     int GsmModem::DeleteAllSms()
171     {
172     vector<SMS> sms = ReadSms(true);
173    
174     for (unsigned int i= 0; i<sms.size(); ++i)
175     {
176     DeleteSms( sms[i].sms_index);
177     }
178     return sms.size();
179     }
180    
181    
182    
183     void GsmModem::Init()
184     {
185 torben 58 Command( "AT" );
186 torben 57 if (_timeout)
187     throw std::runtime_error("Modem did not respond!");
188    
189 torben 58 Command( "ATZ" ); //Reset any previous setup
190    
191 torben 33 Command( "AT\\Q3" ); //Hardware flow control
192 torben 26
193     Command( "ATE0" ); //Disable echo
194    
195     Command ("AT^SM20=0,0" ); //No SM20 compability
196    
197     //Set RealTimeClock ??
198    
199     //Enter pin code ??
200     }
201 torben 59
202    
203 torben 67
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     }

  ViewVC Help
Powered by ViewVC 1.1.20