4 |
#include <iostream> |
#include <iostream> |
5 |
|
|
6 |
#include <string> |
#include <string> |
|
#include <cstring> |
|
7 |
#include <stdexcept> |
#include <stdexcept> |
8 |
|
|
9 |
#include <sys/time.h> |
#include <sys/time.h> |
25 |
GsmModem::GsmModem(SerialPort& serialport) |
GsmModem::GsmModem(SerialPort& serialport) |
26 |
: m_port(serialport) |
: m_port(serialport) |
27 |
{ |
{ |
|
Init(); |
|
28 |
} |
} |
29 |
|
|
30 |
|
|
47 |
{ |
{ |
48 |
time_t start,now; |
time_t start,now; |
49 |
start = time(0); |
start = time(0); |
50 |
|
_timeout = false; |
51 |
|
|
52 |
if (term != "> ") |
if (term != "> ") |
53 |
command.append("\r"); //Dont append CarriageReturn if sending SMS |
command.append("\r"); //Dont append CarriageReturn if sending SMS |
73 |
if ( (now-start) > 10 ) |
if ( (now-start) > 10 ) |
74 |
{ |
{ |
75 |
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
76 |
|
_timeout = true; |
77 |
break; |
break; |
78 |
} |
} |
79 |
} |
} |
119 |
return retval; |
return retval; |
120 |
} |
} |
121 |
|
|
122 |
|
|
123 |
|
void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" |
124 |
|
{ |
125 |
|
Common::instance()->logMessage( string("SMS pdu send") ); |
126 |
|
|
127 |
|
Command("AT+CMGF=0"); |
128 |
|
Util::Sleep(2); |
129 |
|
|
130 |
|
string line1 = "AT+CMGS="; |
131 |
|
line1.append( Util::str_formatint(len) ); |
132 |
|
line1.append("\r"); |
133 |
|
|
134 |
|
|
135 |
|
Command(line1,"> "); |
136 |
|
|
137 |
|
pdu.append("\032"); // \032 == Ctrl+Z |
138 |
|
Command( pdu ); |
139 |
|
Util::Sleep( 50 ); |
140 |
|
Common::instance()->smsCounter.outgoing++; |
141 |
|
} |
142 |
|
|
143 |
void GsmModem::SendSms(string to, string message) |
void GsmModem::SendSms(string to, string message) |
144 |
{ |
{ |
145 |
Common::instance()->logMessage( string("SMS send to ") + to); |
Common::instance()->logMessage( string("SMS send to ") + to); |
153 |
|
|
154 |
Command(line1,"> "); |
Command(line1,"> "); |
155 |
|
|
156 |
|
if (message.length() > 160) |
157 |
|
{ |
158 |
|
message = message.substr(0,160); |
159 |
|
Common::instance()->logMessage( "Trunkating message! "); |
160 |
|
} |
161 |
|
|
162 |
message.append("\032"); // \032 == Ctrl+Z |
message.append("\032"); // \032 == Ctrl+Z |
163 |
|
|
164 |
Command(message); |
Command( message ); //In textmode limit to 160 bytes |
165 |
|
|
166 |
|
|
167 |
Util::Sleep(50); //Give the modem some time to send the sms and be ready again |
Util::Sleep(50); //Give the modem some time to send the sms and be ready again |
168 |
Common::instance()->smsCounter.outgoing++; |
Common::instance()->smsCounter.outgoing++; |
169 |
} |
} |
190 |
|
|
191 |
void GsmModem::Init() |
void GsmModem::Init() |
192 |
{ |
{ |
193 |
|
Command( "AT" ); |
194 |
|
if (_timeout) |
195 |
|
throw std::runtime_error("Modem did not respond!"); |
196 |
|
|
197 |
Command( "ATZ" ); //Reset any previous setup |
Command( "ATZ" ); //Reset any previous setup |
198 |
|
|
199 |
Command( "AT\\Q3" ); //Hardware flow control |
Command( "AT\\Q3" ); //Hardware flow control |
200 |
|
|
201 |
Command( "ATE0" ); //Disable echo |
Command( "ATE0" ); //Disable echo |
208 |
|
|
209 |
//Enter pin code ?? |
//Enter pin code ?? |
210 |
} |
} |
211 |
|
|
212 |
|
|