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> |
17 |
#include "util.h" |
#include "util.h" |
18 |
#include "common.h" |
#include "common.h" |
19 |
|
|
20 |
|
#include "SmsPdu.h" |
21 |
|
|
22 |
using namespace std; |
using namespace std; |
23 |
|
|
26 |
GsmModem::GsmModem(SerialPort& serialport) |
GsmModem::GsmModem(SerialPort& serialport) |
27 |
: m_port(serialport) |
: m_port(serialport) |
28 |
{ |
{ |
|
Init(); |
|
29 |
} |
} |
30 |
|
|
31 |
|
|
48 |
{ |
{ |
49 |
time_t start,now; |
time_t start,now; |
50 |
start = time(0); |
start = time(0); |
51 |
|
_timeout = false; |
52 |
|
|
53 |
if (term != "> ") |
if (term != "> ") |
54 |
command.append("\r"); //Dont append CarriageReturn if sending SMS |
command.append("\r"); //Dont append CarriageReturn if sending SMS |
74 |
if ( (now-start) > 10 ) |
if ( (now-start) > 10 ) |
75 |
{ |
{ |
76 |
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
Common::instance()->logMessage( string("GsmModem::Command time out --") + command); |
77 |
|
_timeout = true; |
78 |
break; |
break; |
79 |
} |
} |
80 |
} |
} |
89 |
|
|
90 |
vector<SMS> GsmModem::ReadSms(bool readAll) |
vector<SMS> GsmModem::ReadSms(bool readAll) |
91 |
{ |
{ |
92 |
|
|
93 |
|
Command( "AT+CMGF=1" ); //Set SMS format to text |
94 |
|
|
95 |
const string search = "+CMGL: "; |
const string search = "+CMGL: "; |
96 |
std::string cmd = "AT+CMGL"; |
std::string cmd = "AT+CMGL"; |
97 |
if (readAll) |
if (readAll) |
123 |
return retval; |
return retval; |
124 |
} |
} |
125 |
|
|
126 |
void GsmModem::SendSms(string to, string message) |
|
127 |
|
void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00" |
128 |
{ |
{ |
129 |
Common::instance()->logMessage( string("SMS send to ") + to); |
Common::instance()->logMessage( string("SMS pdu send") ); |
130 |
|
|
131 |
Command("AT+CMGF=1");///Allways telling the format makes the application more stable |
Command("AT+CMGF=0"); |
132 |
Util::Sleep(2); |
Util::Sleep(2); |
133 |
|
|
134 |
string line1 = "AT+CMGS="; |
string line1 = "AT+CMGS="; |
135 |
line1.append(to); |
line1.append( Util::str_formatint(len) ); |
136 |
line1.append("\r"); |
line1.append("\r"); |
137 |
|
|
138 |
|
|
139 |
Command(line1,"> "); |
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 |
message.append("\032"); // \032 == Ctrl+Z |
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 |
|
|
|
Command(message); |
|
|
Util::Sleep(50); //Give the modem some time to send the sms and be ready again |
|
|
Common::instance()->smsCounter.outgoing++; |
|
163 |
} |
} |
164 |
|
|
165 |
void GsmModem::DeleteSms(std::string smsIndex) |
void GsmModem::DeleteSms(std::string smsIndex) |
184 |
|
|
185 |
void GsmModem::Init() |
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 |
Command( "ATZ" ); //Reset any previous setup |
192 |
|
|
193 |
Command( "AT\\Q3" ); //Hardware flow control |
Command( "AT\\Q3" ); //Hardware flow control |
194 |
|
|
195 |
Command( "ATE0" ); //Disable echo |
Command( "ATE0" ); //Disable echo |
196 |
|
|
|
Command( "AT+CMGF=1" ); //Set SMS format to text |
|
|
|
|
197 |
Command ("AT^SM20=0,0" ); //No SM20 compability |
Command ("AT^SM20=0,0" ); //No SM20 compability |
198 |
|
|
199 |
//Set RealTimeClock ?? |
//Set RealTimeClock ?? |
200 |
|
|
201 |
//Enter pin code ?? |
//Enter pin code ?? |
202 |
} |
} |
203 |
|
|
204 |
|
|