1 |
torben |
26 |
/* using http://sourceforge.net/projects/libserial/ |
2 |
|
|
*/ |
3 |
|
|
|
4 |
|
|
#include <iostream> |
5 |
|
|
|
6 |
|
|
#include <string> |
7 |
|
|
#include <cstring> |
8 |
|
|
#include <stdexcept> |
9 |
|
|
|
10 |
|
|
#include <sys/time.h> |
11 |
|
|
#include <time.h> |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
#include "SerialPort.h" |
15 |
|
|
|
16 |
|
|
#include "GsmModem.h" |
17 |
|
|
|
18 |
|
|
#include "util.h" |
19 |
|
|
#include "common.h" |
20 |
|
|
|
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 |
torben |
34 |
|
47 |
torben |
26 |
string GsmModem::Command(string command, string term) |
48 |
|
|
{ |
49 |
|
|
time_t start,now; |
50 |
|
|
start = time(0); |
51 |
torben |
57 |
_timeout = false; |
52 |
torben |
26 |
|
53 |
torben |
34 |
if (term != "> ") |
54 |
|
|
command.append("\r"); //Dont append CarriageReturn if sending SMS |
55 |
|
|
|
56 |
torben |
26 |
m_port.Write(command); |
57 |
|
|
|
58 |
torben |
34 |
Util::Sleep(1); |
59 |
torben |
26 |
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 |
torben |
34 |
Util::Sleep(1); |
72 |
torben |
26 |
|
73 |
|
|
now = time(0); |
74 |
|
|
if ( (now-start) > 10 ) |
75 |
|
|
{ |
76 |
|
|
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
77 |
torben |
57 |
_timeout = true; |
78 |
torben |
26 |
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 |
|
|
const string search = "+CMGL: "; |
93 |
|
|
std::string cmd = "AT+CMGL"; |
94 |
|
|
if (readAll) |
95 |
|
|
cmd.append("=ALL"); |
96 |
|
|
|
97 |
|
|
string result = Command(cmd); |
98 |
|
|
|
99 |
|
|
vector<SMS> retval; |
100 |
|
|
if (result.find(search) == string::npos) |
101 |
|
|
return retval; |
102 |
|
|
|
103 |
|
|
result = result.substr(2, result.length() - 8); //remove trailing "\r\nOK\r\n" and initial "\r\n" |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
while ( 1 ) |
107 |
|
|
{ |
108 |
|
|
unsigned int endpos = result.find(search,5); |
109 |
|
|
|
110 |
|
|
|
111 |
|
|
string sms_entry = result.substr(0,endpos); |
112 |
|
|
retval.push_back( SMS::FromRawString(sms_entry) );; |
113 |
|
|
|
114 |
|
|
if (endpos == string::npos) |
115 |
|
|
break; |
116 |
|
|
|
117 |
|
|
result = result.substr(endpos, result.length() - endpos); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
return retval; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
void GsmModem::SendSms(string to, string message) |
124 |
|
|
{ |
125 |
|
|
Common::instance()->logMessage( string("SMS send to ") + to); |
126 |
|
|
|
127 |
|
|
Command("AT+CMGF=1");///Allways telling the format makes the application more stable |
128 |
|
|
Util::Sleep(2); |
129 |
|
|
|
130 |
|
|
string line1 = "AT+CMGS="; |
131 |
|
|
line1.append(to); |
132 |
|
|
line1.append("\r"); |
133 |
|
|
|
134 |
|
|
Command(line1,"> "); |
135 |
|
|
|
136 |
torben |
50 |
if (message.length() > 160) |
137 |
|
|
{ |
138 |
|
|
message = message.substr(0,160); |
139 |
|
|
Common::instance()->logMessage( "Trunkating message! "); |
140 |
|
|
} |
141 |
torben |
26 |
|
142 |
torben |
34 |
message.append("\032"); // \032 == Ctrl+Z |
143 |
torben |
26 |
|
144 |
torben |
50 |
Command( message ); //In textmode limit to 160 bytes |
145 |
torben |
49 |
|
146 |
|
|
|
147 |
torben |
26 |
Util::Sleep(50); //Give the modem some time to send the sms and be ready again |
148 |
torben |
36 |
Common::instance()->smsCounter.outgoing++; |
149 |
torben |
26 |
} |
150 |
|
|
|
151 |
|
|
void GsmModem::DeleteSms(std::string smsIndex) |
152 |
|
|
{ |
153 |
|
|
string cmd = "AT+CMGD="; |
154 |
|
|
cmd.append(smsIndex); |
155 |
|
|
Command(cmd); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
int GsmModem::DeleteAllSms() |
159 |
|
|
{ |
160 |
|
|
vector<SMS> sms = ReadSms(true); |
161 |
|
|
|
162 |
|
|
for (unsigned int i= 0; i<sms.size(); ++i) |
163 |
|
|
{ |
164 |
|
|
DeleteSms( sms[i].sms_index); |
165 |
|
|
} |
166 |
|
|
return sms.size(); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
void GsmModem::Init() |
172 |
|
|
{ |
173 |
|
|
Command( "ATZ" ); //Reset any previous setup |
174 |
torben |
57 |
if (_timeout) |
175 |
|
|
throw std::runtime_error("Modem did not respond!"); |
176 |
|
|
|
177 |
torben |
33 |
Command( "AT\\Q3" ); //Hardware flow control |
178 |
torben |
26 |
|
179 |
|
|
Command( "ATE0" ); //Disable echo |
180 |
|
|
|
181 |
|
|
Command( "AT+CMGF=1" ); //Set SMS format to text |
182 |
|
|
|
183 |
|
|
Command ("AT^SM20=0,0" ); //No SM20 compability |
184 |
|
|
|
185 |
|
|
//Set RealTimeClock ?? |
186 |
|
|
|
187 |
|
|
//Enter pin code ?? |
188 |
|
|
} |