2 |
*/ |
*/ |
3 |
|
|
4 |
#include <iostream> |
#include <iostream> |
|
|
|
5 |
#include <string> |
#include <string> |
6 |
#include <stdexcept> |
#include <stdexcept> |
7 |
|
|
|
#include <sys/time.h> |
|
8 |
#include <time.h> |
#include <time.h> |
9 |
|
|
10 |
|
|
11 |
#include "SerialPort.h" |
#include "serialport/SerialPort.h" |
12 |
|
|
13 |
#include "GsmModem.h" |
#include "GsmModem.h" |
14 |
|
|
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 |
|
|
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 |
} |
} |
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 |
|
|
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; |
141 |
Common::instance()->smsCounter.outgoing++; |
Common::instance()->smsCounter.outgoing++; |
142 |
} |
} |
143 |
|
|
144 |
void GsmModem::SendSms(string to, string message) |
void GsmModem::SendSms(string to, string message, bool allowMultipart) |
145 |
{ |
{ |
146 |
Common::instance()->logMessage( string("SMS send to ") + to); |
Common::instance()->logMessage( string("SMS send to ") + to); |
147 |
|
|
148 |
Command("AT+CMGF=1");///Allways telling the format makes the application more stable |
if (to.at(0) == '+') |
149 |
Util::Sleep(2); |
to.erase(0,0); |
|
|
|
|
string line1 = "AT+CMGS="; |
|
|
line1.append(to); |
|
|
line1.append("\r"); |
|
150 |
|
|
151 |
Command(line1,"> "); |
vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart); |
152 |
|
|
153 |
if (message.length() > 160) |
for (unsigned i=0; i<pdu_vec.size(); ++i) |
154 |
{ |
{ |
155 |
message = message.substr(0,160); |
PduInfo& pdu = pdu_vec[i]; |
|
Common::instance()->logMessage( "Trunkating message! "); |
|
|
} |
|
|
|
|
|
message.append("\032"); // \032 == Ctrl+Z |
|
|
|
|
|
Command( message ); //In textmode limit to 160 bytes |
|
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) |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
void GsmModem::WaitForSimcard() |
183 |
|
{ |
184 |
|
int start = time(0); |
185 |
|
string result; |
186 |
|
|
187 |
|
|
188 |
|
while (result != "^SSIM READY") |
189 |
|
{ |
190 |
|
result += GetResponse(); |
191 |
|
result = Util::str_trim(result); |
192 |
|
|
193 |
|
if ( (time(0) - start) > 10) |
194 |
|
throw std::runtime_error("Sim card timed out:"); |
195 |
|
Util::Sleep(100); |
196 |
|
} |
197 |
|
|
198 |
|
} |
199 |
|
|
200 |
|
void GsmModem::HandlePincode() |
201 |
|
{ |
202 |
|
string result = Command("AT+CPIN?"); |
203 |
|
result = Util::str_trim(result); |
204 |
|
result.erase(result.length() -2, 2); //remove trailing ok |
205 |
|
result = Util::str_trim(result); |
206 |
|
if (result != "+CPIN: READY") |
207 |
|
{ |
208 |
|
if (result == "+CPIN: SIM PIN") |
209 |
|
{ |
210 |
|
Command("AT^SSET=1"); |
211 |
|
result = Command("AT+CPIN=0067"); |
212 |
|
if ( result.substr(result.length()-4, 4) != "OK\r\n") |
213 |
|
throw std::runtime_error(string("Illegal pincode: ") + result); |
214 |
|
|
215 |
|
WaitForSimcard(); |
216 |
|
} |
217 |
|
else |
218 |
|
{ |
219 |
|
throw std::runtime_error(string("AT+CPIN? returned unhandled code: ") + result); |
220 |
|
} |
221 |
|
|
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
void GsmModem::Init() |
void GsmModem::Init() |
226 |
{ |
{ |
227 |
Command( "AT" ); |
Command( "AT" ); |
234 |
|
|
235 |
Command( "ATE0" ); //Disable echo |
Command( "ATE0" ); //Disable echo |
236 |
|
|
|
Command( "AT+CMGF=1" ); //Set SMS format to text |
|
|
|
|
237 |
Command ("AT^SM20=0,0" ); //No SM20 compability |
Command ("AT^SM20=0,0" ); //No SM20 compability |
238 |
|
|
239 |
//Set RealTimeClock ?? |
//Command("AT+CGATT=1"); //GPRS Attach |
240 |
|
|
241 |
//Enter pin code ?? |
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
242 |
|
|
243 |
|
HandlePincode(); |
244 |
} |
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
void DebugGsmModem::SendSms(std::string to, std::string message, bool allowMultipart) |
250 |
|
{ |
251 |
|
_to=to; |
252 |
|
_message = message; |
253 |
|
_multipart = allowMultipart; |
254 |
|
|
255 |
|
if (_print) |
256 |
|
{ |
257 |
|
cout << "DebugGsmModem::SendSms --------------" << endl; |
258 |
|
cout << "To: " << to << endl;; |
259 |
|
cout << "Message: " << message << endl; |
260 |
|
cout << "Multipart: " << allowMultipart << endl; |
261 |
|
|
262 |
|
} |
263 |
|
} |
264 |
|
|