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