1 |
torben |
26 |
/* using http://sourceforge.net/projects/libserial/ |
2 |
|
|
*/ |
3 |
|
|
|
4 |
|
|
#include <iostream> |
5 |
|
|
#include <string> |
6 |
|
|
#include <stdexcept> |
7 |
|
|
|
8 |
|
|
#include <time.h> |
9 |
|
|
|
10 |
|
|
|
11 |
torben |
132 |
#include "serialport/SerialPort.h" |
12 |
torben |
26 |
|
13 |
|
|
#include "GsmModem.h" |
14 |
|
|
|
15 |
|
|
#include "util.h" |
16 |
|
|
#include "common.h" |
17 |
|
|
|
18 |
torben |
63 |
#include "SmsPdu.h" |
19 |
torben |
26 |
|
20 |
|
|
using namespace std; |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
GsmModem::GsmModem(SerialPort& serialport) |
25 |
|
|
: m_port(serialport) |
26 |
|
|
{ |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
string GsmModem::GetResponse() |
32 |
|
|
{ |
33 |
|
|
|
34 |
|
|
SerialPort::DataBuffer buf; |
35 |
|
|
m_port.Read(buf); |
36 |
|
|
|
37 |
|
|
buf.push_back(0); |
38 |
|
|
|
39 |
|
|
std::string str((char*) &buf[0]); |
40 |
|
|
|
41 |
|
|
return str; |
42 |
|
|
} |
43 |
|
|
|
44 |
torben |
34 |
|
45 |
torben |
26 |
string GsmModem::Command(string command, string term) |
46 |
|
|
{ |
47 |
|
|
time_t start,now; |
48 |
|
|
start = time(0); |
49 |
torben |
57 |
_timeout = false; |
50 |
torben |
26 |
|
51 |
torben |
34 |
if (term != "> ") |
52 |
|
|
command.append("\r"); //Dont append CarriageReturn if sending SMS |
53 |
|
|
|
54 |
torben |
26 |
m_port.Write(command); |
55 |
|
|
|
56 |
torben |
34 |
Util::Sleep(1); |
57 |
torben |
26 |
string response = GetResponse(); |
58 |
|
|
|
59 |
|
|
unsigned int tlen = term.length(); |
60 |
|
|
while ( 1 ) |
61 |
|
|
{ |
62 |
|
|
if (response.length() >= tlen) |
63 |
|
|
{ |
64 |
|
|
if (response.substr(response.length()-tlen,tlen) == term) |
65 |
|
|
break; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
response += GetResponse(); |
69 |
torben |
34 |
Util::Sleep(1); |
70 |
torben |
26 |
|
71 |
|
|
now = time(0); |
72 |
|
|
if ( (now-start) > 10 ) |
73 |
|
|
{ |
74 |
|
|
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
75 |
torben |
72 |
Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) ); |
76 |
torben |
57 |
_timeout = true; |
77 |
torben |
26 |
break; |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
Util::Sleep(5); |
82 |
|
|
|
83 |
|
|
|
84 |
|
|
return response; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
vector<SMS> GsmModem::ReadSms(bool readAll) |
88 |
|
|
{ |
89 |
torben |
63 |
|
90 |
torben |
75 |
Command( "AT+CMGF=0" ); //Set SMS format to PDU |
91 |
torben |
63 |
|
92 |
torben |
26 |
const string search = "+CMGL: "; |
93 |
|
|
std::string cmd = "AT+CMGL"; |
94 |
|
|
if (readAll) |
95 |
torben |
75 |
cmd.append("=4"); |
96 |
torben |
26 |
|
97 |
|
|
string result = Command(cmd); |
98 |
|
|
|
99 |
|
|
vector<SMS> retval; |
100 |
|
|
if (result.find(search) == string::npos) |
101 |
|
|
return retval; |
102 |
|
|
|
103 |
|
|
result = result.substr(2, result.length() - 8); //remove trailing "\r\nOK\r\n" and initial "\r\n" |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
while ( 1 ) |
107 |
|
|
{ |
108 |
|
|
unsigned int endpos = result.find(search,5); |
109 |
|
|
|
110 |
|
|
|
111 |
|
|
string sms_entry = result.substr(0,endpos); |
112 |
torben |
75 |
retval.push_back( SMS::FromPduString(sms_entry) );; |
113 |
torben |
26 |
|
114 |
|
|
if (endpos == string::npos) |
115 |
|
|
break; |
116 |
|
|
|
117 |
|
|
result = result.substr(endpos, result.length() - endpos); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
return retval; |
121 |
|
|
} |
122 |
|
|
|
123 |
torben |
59 |
|
124 |
|
|
void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" |
125 |
|
|
{ |
126 |
|
|
Common::instance()->logMessage( string("SMS pdu send") ); |
127 |
|
|
|
128 |
|
|
Command("AT+CMGF=0"); |
129 |
|
|
Util::Sleep(2); |
130 |
|
|
|
131 |
|
|
string line1 = "AT+CMGS="; |
132 |
|
|
line1.append( Util::str_formatint(len) ); |
133 |
|
|
line1.append("\r"); |
134 |
|
|
|
135 |
|
|
|
136 |
|
|
Command(line1,"> "); |
137 |
|
|
|
138 |
|
|
pdu.append("\032"); // \032 == Ctrl+Z |
139 |
|
|
Command( pdu ); |
140 |
|
|
Util::Sleep( 50 ); |
141 |
|
|
Common::instance()->smsCounter.outgoing++; |
142 |
|
|
} |
143 |
|
|
|
144 |
torben |
63 |
void GsmModem::SendSms(string to, string message, bool allowMultipart) |
145 |
torben |
26 |
{ |
146 |
|
|
Common::instance()->logMessage( string("SMS send to ") + to); |
147 |
|
|
|
148 |
torben |
63 |
if (to.at(0) == '+') |
149 |
|
|
to.erase(0,0); |
150 |
torben |
26 |
|
151 |
torben |
135 |
vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, Util::str_latin2gsm(message), allowMultipart); |
152 |
torben |
26 |
|
153 |
torben |
63 |
for (unsigned i=0; i<pdu_vec.size(); ++i) |
154 |
|
|
{ |
155 |
|
|
PduInfo& pdu = pdu_vec[i]; |
156 |
torben |
26 |
|
157 |
torben |
63 |
SendSmsPdu(pdu.pdu, pdu.len); |
158 |
torben |
50 |
} |
159 |
torben |
136 |
Common::instance()->logMessage( "All PDU's send"); |
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 |
torben |
105 |
void GsmModem::WaitForSimcard() |
184 |
|
|
{ |
185 |
|
|
int start = time(0); |
186 |
|
|
string result; |
187 |
|
|
|
188 |
|
|
|
189 |
|
|
while (result != "^SSIM READY") |
190 |
|
|
{ |
191 |
|
|
result += GetResponse(); |
192 |
|
|
result = Util::str_trim(result); |
193 |
|
|
|
194 |
|
|
if ( (time(0) - start) > 10) |
195 |
|
|
throw std::runtime_error("Sim card timed out:"); |
196 |
|
|
Util::Sleep(100); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
void GsmModem::HandlePincode() |
202 |
|
|
{ |
203 |
|
|
string result = Command("AT+CPIN?"); |
204 |
|
|
result = Util::str_trim(result); |
205 |
|
|
result.erase(result.length() -2, 2); //remove trailing ok |
206 |
|
|
result = Util::str_trim(result); |
207 |
|
|
if (result != "+CPIN: READY") |
208 |
|
|
{ |
209 |
|
|
if (result == "+CPIN: SIM PIN") |
210 |
|
|
{ |
211 |
|
|
Command("AT^SSET=1"); |
212 |
|
|
result = Command("AT+CPIN=0067"); |
213 |
|
|
if ( result.substr(result.length()-4, 4) != "OK\r\n") |
214 |
|
|
throw std::runtime_error(string("Illegal pincode: ") + result); |
215 |
|
|
|
216 |
|
|
WaitForSimcard(); |
217 |
|
|
} |
218 |
|
|
else |
219 |
|
|
{ |
220 |
|
|
throw std::runtime_error(string("AT+CPIN? returned unhandled code: ") + result); |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
} |
224 |
|
|
} |
225 |
|
|
|
226 |
torben |
26 |
void GsmModem::Init() |
227 |
|
|
{ |
228 |
torben |
58 |
Command( "AT" ); |
229 |
torben |
57 |
if (_timeout) |
230 |
|
|
throw std::runtime_error("Modem did not respond!"); |
231 |
|
|
|
232 |
torben |
58 |
Command( "ATZ" ); //Reset any previous setup |
233 |
|
|
|
234 |
torben |
33 |
Command( "AT\\Q3" ); //Hardware flow control |
235 |
torben |
26 |
|
236 |
|
|
Command( "ATE0" ); //Disable echo |
237 |
|
|
|
238 |
|
|
Command ("AT^SM20=0,0" ); //No SM20 compability |
239 |
|
|
|
240 |
torben |
83 |
//Command("AT+CGATT=1"); //GPRS Attach |
241 |
|
|
|
242 |
|
|
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
243 |
|
|
|
244 |
torben |
105 |
HandlePincode(); |
245 |
torben |
26 |
} |
246 |
torben |
59 |
|
247 |
|
|
|
248 |
torben |
67 |
|
249 |
|
|
|
250 |
|
|
void DebugGsmModem::SendSms(std::string to, std::string message, bool allowMultipart) |
251 |
|
|
{ |
252 |
|
|
_to=to; |
253 |
|
|
_message = message; |
254 |
|
|
_multipart = allowMultipart; |
255 |
|
|
|
256 |
|
|
if (_print) |
257 |
|
|
{ |
258 |
|
|
cout << "DebugGsmModem::SendSms --------------" << endl; |
259 |
|
|
cout << "To: " << to << endl;; |
260 |
|
|
cout << "Message: " << message << endl; |
261 |
|
|
cout << "Multipart: " << allowMultipart << endl; |
262 |
|
|
|
263 |
|
|
} |
264 |
|
|
} |