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