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 |
|
|
Init(); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
string GsmModem::GetResponse() |
35 |
|
|
{ |
36 |
|
|
|
37 |
|
|
SerialPort::DataBuffer buf; |
38 |
|
|
m_port.Read(buf); |
39 |
|
|
|
40 |
|
|
buf.push_back(0); |
41 |
|
|
|
42 |
|
|
std::string str((char*) &buf[0]); |
43 |
|
|
|
44 |
|
|
return str; |
45 |
|
|
} |
46 |
|
|
|
47 |
torben |
34 |
|
48 |
torben |
26 |
string GsmModem::Command(string command, string term) |
49 |
|
|
{ |
50 |
|
|
time_t start,now; |
51 |
|
|
start = time(0); |
52 |
|
|
|
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 |
|
|
break; |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
Util::Sleep(5); |
82 |
|
|
|
83 |
|
|
// cout << response.length() << ":" << response << endl; |
84 |
|
|
// DumpString(response); |
85 |
|
|
|
86 |
|
|
return response; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
vector<SMS> GsmModem::ReadSms(bool readAll) |
90 |
|
|
{ |
91 |
|
|
const string search = "+CMGL: "; |
92 |
|
|
std::string cmd = "AT+CMGL"; |
93 |
|
|
if (readAll) |
94 |
|
|
cmd.append("=ALL"); |
95 |
|
|
|
96 |
|
|
string result = Command(cmd); |
97 |
|
|
|
98 |
|
|
vector<SMS> retval; |
99 |
|
|
if (result.find(search) == string::npos) |
100 |
|
|
return retval; |
101 |
|
|
|
102 |
|
|
result = result.substr(2, result.length() - 8); //remove trailing "\r\nOK\r\n" and initial "\r\n" |
103 |
|
|
|
104 |
|
|
|
105 |
|
|
while ( 1 ) |
106 |
|
|
{ |
107 |
|
|
unsigned int endpos = result.find(search,5); |
108 |
|
|
|
109 |
|
|
|
110 |
|
|
string sms_entry = result.substr(0,endpos); |
111 |
|
|
retval.push_back( SMS::FromRawString(sms_entry) );; |
112 |
|
|
|
113 |
|
|
if (endpos == string::npos) |
114 |
|
|
break; |
115 |
|
|
|
116 |
|
|
result = result.substr(endpos, result.length() - endpos); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
return retval; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
void GsmModem::SendSms(string to, string message) |
123 |
|
|
{ |
124 |
|
|
Common::instance()->logMessage( string("SMS send to ") + to); |
125 |
|
|
|
126 |
|
|
Command("AT+CMGF=1");///Allways telling the format makes the application more stable |
127 |
|
|
Util::Sleep(2); |
128 |
|
|
|
129 |
|
|
string line1 = "AT+CMGS="; |
130 |
|
|
line1.append(to); |
131 |
|
|
line1.append("\r"); |
132 |
|
|
|
133 |
|
|
Command(line1,"> "); |
134 |
|
|
|
135 |
torben |
50 |
if (message.length() > 160) |
136 |
|
|
{ |
137 |
|
|
message = message.substr(0,160); |
138 |
|
|
Common::instance()->logMessage( "Trunkating message! "); |
139 |
|
|
} |
140 |
torben |
26 |
|
141 |
torben |
34 |
message.append("\032"); // \032 == Ctrl+Z |
142 |
torben |
26 |
|
143 |
torben |
50 |
Command( message ); //In textmode limit to 160 bytes |
144 |
torben |
49 |
|
145 |
|
|
|
146 |
torben |
26 |
Util::Sleep(50); //Give the modem some time to send the sms and be ready again |
147 |
torben |
36 |
Common::instance()->smsCounter.outgoing++; |
148 |
torben |
26 |
} |
149 |
|
|
|
150 |
|
|
void GsmModem::DeleteSms(std::string smsIndex) |
151 |
|
|
{ |
152 |
|
|
string cmd = "AT+CMGD="; |
153 |
|
|
cmd.append(smsIndex); |
154 |
|
|
Command(cmd); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
int GsmModem::DeleteAllSms() |
158 |
|
|
{ |
159 |
|
|
vector<SMS> sms = ReadSms(true); |
160 |
|
|
|
161 |
|
|
for (unsigned int i= 0; i<sms.size(); ++i) |
162 |
|
|
{ |
163 |
|
|
DeleteSms( sms[i].sms_index); |
164 |
|
|
} |
165 |
|
|
return sms.size(); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
void GsmModem::Init() |
171 |
|
|
{ |
172 |
|
|
Command( "ATZ" ); //Reset any previous setup |
173 |
torben |
33 |
Command( "AT\\Q3" ); //Hardware flow control |
174 |
torben |
26 |
|
175 |
|
|
Command( "ATE0" ); //Disable echo |
176 |
|
|
|
177 |
|
|
Command( "AT+CMGF=1" ); //Set SMS format to text |
178 |
|
|
|
179 |
|
|
Command ("AT^SM20=0,0" ); //No SM20 compability |
180 |
|
|
|
181 |
|
|
//Set RealTimeClock ?? |
182 |
|
|
|
183 |
|
|
//Enter pin code ?? |
184 |
|
|
} |