1 |
/* 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 |
string GsmModem::Command(string command, string term) |
48 |
{ |
49 |
time_t start,now; |
50 |
start = time(0); |
51 |
|
52 |
command.append("\r"); |
53 |
m_port.Write(command); |
54 |
|
55 |
Util::Sleep(25); |
56 |
string response = GetResponse(); |
57 |
|
58 |
|
59 |
unsigned int tlen = term.length(); |
60 |
while ( 1 ) |
61 |
{ |
62 |
if (response.length() >= tlen) |
63 |
{ |
64 |
if (response.substr(response.length()-tlen,tlen) == term) |
65 |
break; |
66 |
} |
67 |
|
68 |
response += GetResponse(); |
69 |
Util::Sleep(25); |
70 |
|
71 |
now = time(0); |
72 |
if ( (now-start) > 10 ) |
73 |
{ |
74 |
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
75 |
break; |
76 |
} |
77 |
|
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 |
|
136 |
message.append("\032\r"); // \032 == Ctrl+Z |
137 |
|
138 |
Command(message); |
139 |
Util::Sleep(50); //Give the modem some time to send the sms and be ready again |
140 |
} |
141 |
|
142 |
void GsmModem::DeleteSms(std::string smsIndex) |
143 |
{ |
144 |
string cmd = "AT+CMGD="; |
145 |
cmd.append(smsIndex); |
146 |
Command(cmd); |
147 |
} |
148 |
|
149 |
int GsmModem::DeleteAllSms() |
150 |
{ |
151 |
vector<SMS> sms = ReadSms(true); |
152 |
|
153 |
for (unsigned int i= 0; i<sms.size(); ++i) |
154 |
{ |
155 |
DeleteSms( sms[i].sms_index); |
156 |
} |
157 |
return sms.size(); |
158 |
} |
159 |
|
160 |
|
161 |
|
162 |
void GsmModem::Init() |
163 |
{ |
164 |
Command( "ATZ" ); //Reset any previous setup |
165 |
|
166 |
Command( "ATE0" ); //Disable echo |
167 |
|
168 |
Command( "AT+CMGF=1" ); //Set SMS format to text |
169 |
|
170 |
Command ("AT^SM20=0,0" ); //No SM20 compability |
171 |
|
172 |
//Set RealTimeClock ?? |
173 |
|
174 |
//Enter pin code ?? |
175 |
} |