2 |
*/ |
*/ |
3 |
|
|
4 |
#include <iostream> |
#include <iostream> |
|
|
|
5 |
#include <string> |
#include <string> |
6 |
#include <stdexcept> |
#include <stdexcept> |
7 |
|
|
16 |
#include "util.h" |
#include "util.h" |
17 |
#include "common.h" |
#include "common.h" |
18 |
|
|
19 |
|
#include "SmsPdu.h" |
20 |
|
|
21 |
using namespace std; |
using namespace std; |
22 |
|
|
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 |
|
Common::instance()->logMessage( string("Modem responded: ") + Util::str_trim(response) ); |
77 |
_timeout = true; |
_timeout = true; |
78 |
break; |
break; |
79 |
} |
} |
81 |
|
|
82 |
Util::Sleep(5); |
Util::Sleep(5); |
83 |
|
|
|
// cout << response.length() << ":" << response << endl; |
|
|
// DumpString(response); |
|
84 |
|
|
85 |
return response; |
return response; |
86 |
} |
} |
87 |
|
|
88 |
vector<SMS> GsmModem::ReadSms(bool readAll) |
vector<SMS> GsmModem::ReadSms(bool readAll) |
89 |
{ |
{ |
90 |
|
|
91 |
|
Command( "AT+CMGF=0" ); //Set SMS format to PDU |
92 |
|
|
93 |
const string search = "+CMGL: "; |
const string search = "+CMGL: "; |
94 |
std::string cmd = "AT+CMGL"; |
std::string cmd = "AT+CMGL"; |
95 |
if (readAll) |
if (readAll) |
96 |
cmd.append("=ALL"); |
cmd.append("=4"); |
97 |
|
|
98 |
string result = Command(cmd); |
string result = Command(cmd); |
99 |
|
|
110 |
|
|
111 |
|
|
112 |
string sms_entry = result.substr(0,endpos); |
string sms_entry = result.substr(0,endpos); |
113 |
retval.push_back( SMS::FromRawString(sms_entry) );; |
retval.push_back( SMS::FromPduString(sms_entry) );; |
114 |
|
|
115 |
if (endpos == string::npos) |
if (endpos == string::npos) |
116 |
break; |
break; |
142 |
Common::instance()->smsCounter.outgoing++; |
Common::instance()->smsCounter.outgoing++; |
143 |
} |
} |
144 |
|
|
145 |
void GsmModem::SendSms(string to, string message) |
void GsmModem::SendSms(string to, string message, bool allowMultipart) |
146 |
{ |
{ |
147 |
Common::instance()->logMessage( string("SMS send to ") + to); |
Common::instance()->logMessage( string("SMS send to ") + to); |
148 |
|
|
149 |
Command("AT+CMGF=1");///Allways telling the format makes the application more stable |
if (to.at(0) == '+') |
150 |
Util::Sleep(2); |
to.erase(0,0); |
151 |
|
|
152 |
string line1 = "AT+CMGS="; |
vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart); |
|
line1.append(to); |
|
|
line1.append("\r"); |
|
153 |
|
|
154 |
Command(line1,"> "); |
for (unsigned i=0; i<pdu_vec.size(); ++i) |
|
|
|
|
if (message.length() > 160) |
|
155 |
{ |
{ |
156 |
message = message.substr(0,160); |
PduInfo& pdu = pdu_vec[i]; |
|
Common::instance()->logMessage( "Trunkating message! "); |
|
|
} |
|
|
|
|
|
message.append("\032"); // \032 == Ctrl+Z |
|
|
|
|
|
Command( message ); //In textmode limit to 160 bytes |
|
157 |
|
|
158 |
|
SendSmsPdu(pdu.pdu, pdu.len); |
159 |
|
} |
160 |
|
|
|
Util::Sleep(50); //Give the modem some time to send the sms and be ready again |
|
|
Common::instance()->smsCounter.outgoing++; |
|
161 |
} |
} |
162 |
|
|
163 |
void GsmModem::DeleteSms(std::string smsIndex) |
void GsmModem::DeleteSms(std::string smsIndex) |
192 |
|
|
193 |
Command( "ATE0" ); //Disable echo |
Command( "ATE0" ); //Disable echo |
194 |
|
|
|
Command( "AT+CMGF=1" ); //Set SMS format to text |
|
|
|
|
195 |
Command ("AT^SM20=0,0" ); //No SM20 compability |
Command ("AT^SM20=0,0" ); //No SM20 compability |
196 |
|
|
197 |
|
//Command("AT+CGATT=1"); //GPRS Attach |
198 |
|
|
199 |
|
//Command("AT+CGSMS=2"); //SMS over GPRS preferred |
200 |
|
|
201 |
//Set RealTimeClock ?? |
//Set RealTimeClock ?? |
202 |
|
|
203 |
//Enter pin code ?? |
//Enter pin code ?? |
204 |
} |
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
void DebugGsmModem::SendSms(std::string to, std::string message, bool allowMultipart) |
210 |
|
{ |
211 |
|
_to=to; |
212 |
|
_message = message; |
213 |
|
_multipart = allowMultipart; |
214 |
|
|
215 |
|
if (_print) |
216 |
|
{ |
217 |
|
cout << "DebugGsmModem::SendSms --------------" << endl; |
218 |
|
cout << "To: " << to << endl;; |
219 |
|
cout << "Message: " << message << endl; |
220 |
|
cout << "Multipart: " << allowMultipart << endl; |
221 |
|
|
222 |
|
} |
223 |
|
} |
224 |
|
|