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 |
|
|
16 |
#include "common.h" |
#include "common.h" |
17 |
|
|
18 |
#include "SmsPdu.h" |
#include "SmsPdu.h" |
19 |
|
#include "SmsHelper.h" |
20 |
|
|
21 |
using namespace std; |
using namespace std; |
22 |
|
|
81 |
|
|
82 |
Util::Sleep(5); |
Util::Sleep(5); |
83 |
|
|
|
// cout << response.length() << ":" << response << endl; |
|
|
// DumpString(response); |
|
84 |
|
|
85 |
return response; |
return response; |
86 |
} |
} |
88 |
vector<SMS> GsmModem::ReadSms(bool readAll) |
vector<SMS> GsmModem::ReadSms(bool readAll) |
89 |
{ |
{ |
90 |
|
|
91 |
Command( "AT+CMGF=1" ); //Set SMS format to text |
Command( "AT+CMGF=0" ); //Set SMS format to PDU |
92 |
|
|
93 |
const string search = "+CMGL: "; |
const string search = "+CMGL: "; |
94 |
std::string cmd = "AT+CMGL"; |
std::string cmd = "AT+CMGL"; |
95 |
if (readAll) |
if (readAll) |
96 |
cmd.append("=ALL"); |
cmd.append("=4"); |
97 |
|
|
98 |
string result = Command(cmd); |
string result = Command(cmd); |
99 |
|
|
110 |
|
|
111 |
|
|
112 |
string sms_entry = result.substr(0,endpos); |
string sms_entry = result.substr(0,endpos); |
113 |
retval.push_back( SMS::FromRawString(sms_entry) );; |
retval.push_back( SmsHelper::FromPduString(sms_entry) );; |
114 |
|
|
115 |
if (endpos == string::npos) |
if (endpos == string::npos) |
116 |
break; |
break; |
149 |
if (to.at(0) == '+') |
if (to.at(0) == '+') |
150 |
to.erase(0,0); |
to.erase(0,0); |
151 |
|
|
152 |
vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart); |
vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, Util::str_latin2gsm(message), allowMultipart); |
153 |
|
|
154 |
for (unsigned i=0; i<pdu_vec.size(); ++i) |
for (unsigned i=0; i<pdu_vec.size(); ++i) |
155 |
{ |
{ |
157 |
|
|
158 |
SendSmsPdu(pdu.pdu, pdu.len); |
SendSmsPdu(pdu.pdu, pdu.len); |
159 |
} |
} |
160 |
|
Common::instance()->logMessage( "All PDU's send"); |
161 |
|
|
162 |
} |
} |
163 |
|
|
174 |
|
|
175 |
for (unsigned int i= 0; i<sms.size(); ++i) |
for (unsigned int i= 0; i<sms.size(); ++i) |
176 |
{ |
{ |
177 |
DeleteSms( sms[i].sms_index); |
DeleteSms( sms[i].GetIndex() ); |
178 |
} |
} |
179 |
return sms.size(); |
return sms.size(); |
180 |
} |
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
void GsmModem::WaitForSimcard() |
185 |
|
{ |
186 |
|
int start = time(0); |
187 |
|
string result; |
188 |
|
|
189 |
|
|
190 |
|
while (result != "^SSIM READY") |
191 |
|
{ |
192 |
|
result += GetResponse(); |
193 |
|
result = Util::str_trim(result); |
194 |
|
|
195 |
|
if ( (time(0) - start) > 10) |
196 |
|
throw std::runtime_error("Sim card timed out:"); |
197 |
|
Util::Sleep(100); |
198 |
|
} |
199 |
|
|
200 |
|
} |
201 |
|
|
202 |
|
void GsmModem::HandlePincode() |
203 |
|
{ |
204 |
|
string result = Command("AT+CPIN?"); |
205 |
|
result = Util::str_trim(result); |
206 |
|
result.erase(result.length() -2, 2); //remove trailing ok |
207 |
|
result = Util::str_trim(result); |
208 |
|
if (result != "+CPIN: READY") |
209 |
|
{ |
210 |
|
if (result == "+CPIN: SIM PIN") |
211 |
|
{ |
212 |
|
Command("AT^SSET=1"); |
213 |
|
result = Command("AT+CPIN=0067"); |
214 |
|
if ( result.substr(result.length()-4, 4) != "OK\r\n") |
215 |
|
throw std::runtime_error(string("Illegal pincode: ") + result); |
216 |
|
|
217 |
|
WaitForSimcard(); |
218 |
|
} |
219 |
|
else |
220 |
|
{ |
221 |
|
throw std::runtime_error(string("AT+CPIN? returned unhandled code: ") + result); |
222 |
|
} |
223 |
|
|
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
void GsmModem::Init() |
void GsmModem::Init() |
228 |
{ |
{ |
229 |
Command( "AT" ); |
Command( "AT" ); |
238 |
|
|
239 |
Command ("AT^SM20=0,0" ); //No SM20 compability |
Command ("AT^SM20=0,0" ); //No SM20 compability |
240 |
|
|
241 |
//Set RealTimeClock ?? |
//Command("AT+CGATT=1"); //GPRS Attach |
242 |
|
|
243 |
//Enter pin code ?? |
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
244 |
|
|
245 |
|
HandlePincode(); |
246 |
} |
} |
247 |
|
|
248 |
|
|
264 |
} |
} |
265 |
} |
} |
266 |
|
|
267 |
|
std::vector<SMS> DebugGsmModem::ReadSms(bool readAll) |
268 |
|
{ |
269 |
|
vector<SMS> result; |
270 |
|
return result; |
271 |
|
} |