/[projects]/smsdaemon/GsmModem.cpp
ViewVC logotype

Contents of /smsdaemon/GsmModem.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 59 - (show annotations) (download)
Wed Jun 11 19:42:24 2008 UTC (15 years, 11 months ago) by torben
File size: 3761 byte(s)
Implemented basic sms pdu support

1 /* using http://sourceforge.net/projects/libserial/
2 */
3
4 #include <iostream>
5
6 #include <string>
7 #include <stdexcept>
8
9 #include <sys/time.h>
10 #include <time.h>
11
12
13 #include "SerialPort.h"
14
15 #include "GsmModem.h"
16
17 #include "util.h"
18 #include "common.h"
19
20
21 using namespace std;
22
23
24
25 GsmModem::GsmModem(SerialPort& serialport)
26 : m_port(serialport)
27 {
28 }
29
30
31
32 string GsmModem::GetResponse()
33 {
34
35 SerialPort::DataBuffer buf;
36 m_port.Read(buf);
37
38 buf.push_back(0);
39
40 std::string str((char*) &buf[0]);
41
42 return str;
43 }
44
45
46 string GsmModem::Command(string command, string term)
47 {
48 time_t start,now;
49 start = time(0);
50 _timeout = false;
51
52 if (term != "> ")
53 command.append("\r"); //Dont append CarriageReturn if sending SMS
54
55 m_port.Write(command);
56
57 Util::Sleep(1);
58 string response = GetResponse();
59
60 unsigned int tlen = term.length();
61 while ( 1 )
62 {
63 if (response.length() >= tlen)
64 {
65 if (response.substr(response.length()-tlen,tlen) == term)
66 break;
67 }
68
69 response += GetResponse();
70 Util::Sleep(1);
71
72 now = time(0);
73 if ( (now-start) > 10 )
74 {
75 Common::instance()->logMessage( string("GsmModem::Command time out --") + command);
76 _timeout = true;
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
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)
144 {
145 Common::instance()->logMessage( string("SMS send to ") + to);
146
147 Command("AT+CMGF=1");///Allways telling the format makes the application more stable
148 Util::Sleep(2);
149
150 string line1 = "AT+CMGS=";
151 line1.append(to);
152 line1.append("\r");
153
154 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
163
164 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
168 Common::instance()->smsCounter.outgoing++;
169 }
170
171 void GsmModem::DeleteSms(std::string smsIndex)
172 {
173 string cmd = "AT+CMGD=";
174 cmd.append(smsIndex);
175 Command(cmd);
176 }
177
178 int GsmModem::DeleteAllSms()
179 {
180 vector<SMS> sms = ReadSms(true);
181
182 for (unsigned int i= 0; i<sms.size(); ++i)
183 {
184 DeleteSms( sms[i].sms_index);
185 }
186 return sms.size();
187 }
188
189
190
191 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
198
199 Command( "AT\\Q3" ); //Hardware flow control
200
201 Command( "ATE0" ); //Disable echo
202
203 Command( "AT+CMGF=1" ); //Set SMS format to text
204
205 Command ("AT^SM20=0,0" ); //No SM20 compability
206
207 //Set RealTimeClock ??
208
209 //Enter pin code ??
210 }

  ViewVC Help
Powered by ViewVC 1.1.20