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 "ModemTransceiver.h" |
14 |
|
|
15 |
#include "util.h" |
#include "Util.h" |
16 |
#include "common.h" |
#include "Common.h" |
17 |
|
#include "Logger.h" |
18 |
|
#include "ConfigFile.h" |
19 |
|
|
20 |
#include "SmsPdu.h" |
#include "SmsPdu.h" |
21 |
|
#include "SmsHelper.h" |
22 |
|
|
23 |
using namespace std; |
using namespace std; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
GsmModem::GsmModem(SerialPort& serialport) |
ModemTransceiver::ModemTransceiver(SerialPort& serialport) |
28 |
: m_port(serialport) |
: m_port(serialport) |
29 |
{ |
{ |
30 |
} |
} |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
string GsmModem::GetResponse() |
string ModemTransceiver::GetResponse() |
35 |
{ |
{ |
36 |
|
|
37 |
SerialPort::DataBuffer buf; |
SerialPort::DataBuffer buf; |
45 |
} |
} |
46 |
|
|
47 |
|
|
48 |
string GsmModem::Command(string command, string term) |
string ModemTransceiver::Command(string command, string term) |
49 |
{ |
{ |
50 |
time_t start,now; |
time_t start,now; |
51 |
start = time(0); |
start = time(0); |
74 |
now = time(0); |
now = time(0); |
75 |
if ( (now-start) > 10 ) |
if ( (now-start) > 10 ) |
76 |
{ |
{ |
77 |
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
Logger::logMessage( string("ModemTransceiver::Command time out --") + command); |
78 |
Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) ); |
Logger::logMessage( string("Modem responded: ") + Util::str_trim(response) ); |
79 |
_timeout = true; |
_timeout = true; |
80 |
break; |
break; |
81 |
} |
} |
87 |
return response; |
return response; |
88 |
} |
} |
89 |
|
|
90 |
vector<SMS> GsmModem::ReadSms(bool readAll) |
vector<SMS> ModemTransceiver::ReadSms(bool readAll) |
91 |
{ |
{ |
92 |
|
|
93 |
Command( "AT+CMGF=0" ); //Set SMS format to PDU |
Command( "AT+CMGF=0" ); //Set SMS format to PDU |
112 |
|
|
113 |
|
|
114 |
string sms_entry = result.substr(0,endpos); |
string sms_entry = result.substr(0,endpos); |
115 |
retval.push_back( SMS::FromPduString(sms_entry) );; |
retval.push_back( SmsHelper::FromPduString(sms_entry) );; |
116 |
|
|
117 |
if (endpos == string::npos) |
if (endpos == string::npos) |
118 |
break; |
break; |
124 |
} |
} |
125 |
|
|
126 |
|
|
127 |
void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" |
void ModemTransceiver::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" |
128 |
{ |
{ |
129 |
Common::instance()->logMessage( string("SMS pdu send") ); |
Logger::logMessage( string("SMS pdu send") ); |
130 |
|
|
131 |
Command("AT+CMGF=0"); |
Command("AT+CMGF=0"); |
132 |
Util::Sleep(2); |
Util::Sleep(2); |
144 |
Common::instance()->smsCounter.outgoing++; |
Common::instance()->smsCounter.outgoing++; |
145 |
} |
} |
146 |
|
|
147 |
void GsmModem::SendSms(string to, string message, bool allowMultipart) |
void ModemTransceiver::SendSms(string to, string message, bool allowMultipart) |
148 |
{ |
{ |
149 |
Common::instance()->logMessage( string("SMS send to ") + to); |
Logger::logMessage( string("SMS send to ") + to); |
150 |
|
|
151 |
if (to.at(0) == '+') |
if (to.at(0) == '+') |
152 |
to.erase(0,0); |
to.erase(0,0); |
153 |
|
|
154 |
vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart); |
vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, Util::str_latin2gsm(message), allowMultipart); |
155 |
|
|
156 |
for (unsigned i=0; i<pdu_vec.size(); ++i) |
for (unsigned i=0; i<pdu_vec.size(); ++i) |
157 |
{ |
{ |
159 |
|
|
160 |
SendSmsPdu(pdu.pdu, pdu.len); |
SendSmsPdu(pdu.pdu, pdu.len); |
161 |
} |
} |
162 |
|
Logger::logMessage( "All PDU's send"); |
163 |
|
|
164 |
} |
} |
165 |
|
|
166 |
void GsmModem::DeleteSms(std::string smsIndex) |
void ModemTransceiver::DeleteSms(std::string smsIndex) |
167 |
{ |
{ |
168 |
string cmd = "AT+CMGD="; |
string cmd = "AT+CMGD="; |
169 |
cmd.append(smsIndex); |
cmd.append(smsIndex); |
170 |
Command(cmd); |
Command(cmd); |
171 |
} |
} |
172 |
|
|
173 |
int GsmModem::DeleteAllSms() |
int ModemTransceiver::DeleteAllSms() |
174 |
{ |
{ |
175 |
vector<SMS> sms = ReadSms(true); |
vector<SMS> sms = ReadSms(true); |
176 |
|
|
177 |
for (unsigned int i= 0; i<sms.size(); ++i) |
for (unsigned int i= 0; i<sms.size(); ++i) |
178 |
{ |
{ |
179 |
DeleteSms( sms[i].sms_index); |
DeleteSms( sms[i].GetIndex() ); |
180 |
} |
} |
181 |
return sms.size(); |
return sms.size(); |
182 |
} |
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
void GsmModem::Init() |
void ModemTransceiver::WaitForSimcard() |
187 |
|
{ |
188 |
|
int start = time(0); |
189 |
|
string result; |
190 |
|
|
191 |
|
|
192 |
|
while (result != "^SSIM READY") |
193 |
|
{ |
194 |
|
result += GetResponse(); |
195 |
|
result = Util::str_trim(result); |
196 |
|
|
197 |
|
if ( (time(0) - start) > 10) |
198 |
|
throw std::runtime_error("Sim card timed out:"); |
199 |
|
Util::Sleep(100); |
200 |
|
} |
201 |
|
|
202 |
|
} |
203 |
|
|
204 |
|
void ModemTransceiver::HandlePincode() |
205 |
|
{ |
206 |
|
string pin = Common::instance()->GetConfigfile()->GetValue("gsmmodem","pin"); |
207 |
|
|
208 |
|
string result = Command("AT+CPIN?"); |
209 |
|
result = Util::str_trim(result); |
210 |
|
result.erase(result.length() -2, 2); //remove trailing ok |
211 |
|
result = Util::str_trim(result); |
212 |
|
if (result != "+CPIN: READY") |
213 |
|
{ |
214 |
|
if (result == "+CPIN: SIM PIN") |
215 |
|
{ |
216 |
|
Command("AT^SSET=1"); |
217 |
|
result = Command( string("AT+CPIN=")+pin ); |
218 |
|
if ( result.substr(result.length()-4, 4) != "OK\r\n") |
219 |
|
throw std::runtime_error(string("Illegal pincode: ") + result); |
220 |
|
|
221 |
|
WaitForSimcard(); |
222 |
|
} |
223 |
|
else |
224 |
|
{ |
225 |
|
throw std::runtime_error(string("AT+CPIN? returned unhandled code: ") + result); |
226 |
|
} |
227 |
|
|
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
|
void ModemTransceiver::Init() |
232 |
{ |
{ |
233 |
Command( "AT" ); |
Command( "AT" ); |
234 |
if (_timeout) |
if (_timeout) |
246 |
|
|
247 |
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
248 |
|
|
249 |
//Set RealTimeClock ?? |
HandlePincode(); |
|
|
|
|
//Enter pin code ?? |
|
250 |
} |
} |
251 |
|
|
252 |
|
|
|
|
|
|
|
|
|
void DebugGsmModem::SendSms(std::string to, std::string message, bool allowMultipart) |
|
|
{ |
|
|
_to=to; |
|
|
_message = message; |
|
|
_multipart = allowMultipart; |
|
|
|
|
|
if (_print) |
|
|
{ |
|
|
cout << "DebugGsmModem::SendSms --------------" << endl; |
|
|
cout << "To: " << to << endl;; |
|
|
cout << "Message: " << message << endl; |
|
|
cout << "Multipart: " << allowMultipart << endl; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|