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 |
|
|
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" ); |
240 |
|
|
241 |
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
242 |
|
|
243 |
//Set RealTimeClock ?? |
HandlePincode(); |
|
|
|
|
//Enter pin code ?? |
|
244 |
} |
} |
245 |
|
|
246 |
|
|