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

Contents of /smsdaemon/ModemTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 72 - (show annotations) (download)
Fri Jun 13 08:37:19 2008 UTC (15 years, 11 months ago) by torben
Original Path: smsdaemon/GsmModem.cpp
File size: 3948 byte(s)
Enable dumping of modem error messages.

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

  ViewVC Help
Powered by ViewVC 1.1.20