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

Annotation of /smsdaemon/ModemTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (hide annotations) (download)
Thu Jun 12 12:43:29 2008 UTC (15 years, 11 months ago) by torben
Original Path: smsdaemon/GsmModem.cpp
File size: 3484 byte(s)
Make GsmModem use the pdu way.

Added allowMultipart parameter to IGsmModem::SendSms()



1 torben 26 /* 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 torben 63 #include "SmsPdu.h"
21 torben 26
22     using namespace std;
23    
24    
25    
26     GsmModem::GsmModem(SerialPort& serialport)
27     : m_port(serialport)
28     {
29     }
30    
31    
32    
33     string GsmModem::GetResponse()
34     {
35    
36     SerialPort::DataBuffer buf;
37     m_port.Read(buf);
38    
39     buf.push_back(0);
40    
41     std::string str((char*) &buf[0]);
42    
43     return str;
44     }
45    
46 torben 34
47 torben 26 string GsmModem::Command(string command, string term)
48     {
49     time_t start,now;
50     start = time(0);
51 torben 57 _timeout = false;
52 torben 26
53 torben 34 if (term != "> ")
54     command.append("\r"); //Dont append CarriageReturn if sending SMS
55    
56 torben 26 m_port.Write(command);
57    
58 torben 34 Util::Sleep(1);
59 torben 26 string response = GetResponse();
60    
61     unsigned int tlen = term.length();
62     while ( 1 )
63     {
64     if (response.length() >= tlen)
65     {
66     if (response.substr(response.length()-tlen,tlen) == term)
67     break;
68     }
69    
70     response += GetResponse();
71 torben 34 Util::Sleep(1);
72 torben 26
73     now = time(0);
74     if ( (now-start) > 10 )
75     {
76     Common::instance()->logMessage( string("GsmModem::Command time out --") + command);
77 torben 57 _timeout = true;
78 torben 26 break;
79     }
80     }
81    
82     Util::Sleep(5);
83    
84     // cout << response.length() << ":" << response << endl;
85     // DumpString(response);
86    
87     return response;
88     }
89    
90     vector<SMS> GsmModem::ReadSms(bool readAll)
91     {
92 torben 63
93     Command( "AT+CMGF=1" ); //Set SMS format to text
94    
95 torben 26 const string search = "+CMGL: ";
96     std::string cmd = "AT+CMGL";
97     if (readAll)
98     cmd.append("=ALL");
99    
100     string result = Command(cmd);
101    
102     vector<SMS> retval;
103     if (result.find(search) == string::npos)
104     return retval;
105    
106     result = result.substr(2, result.length() - 8); //remove trailing "\r\nOK\r\n" and initial "\r\n"
107    
108    
109     while ( 1 )
110     {
111     unsigned int endpos = result.find(search,5);
112    
113    
114     string sms_entry = result.substr(0,endpos);
115     retval.push_back( SMS::FromRawString(sms_entry) );;
116    
117     if (endpos == string::npos)
118     break;
119    
120     result = result.substr(endpos, result.length() - endpos);
121     }
122    
123     return retval;
124     }
125    
126 torben 59
127     void GsmModem::SendSmsPdu(std::string pdu, int len) //pdu inclussive leading "00"
128     {
129     Common::instance()->logMessage( string("SMS pdu send") );
130    
131     Command("AT+CMGF=0");
132     Util::Sleep(2);
133    
134     string line1 = "AT+CMGS=";
135     line1.append( Util::str_formatint(len) );
136     line1.append("\r");
137    
138    
139     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 torben 63 void GsmModem::SendSms(string to, string message, bool allowMultipart)
148 torben 26 {
149     Common::instance()->logMessage( string("SMS send to ") + to);
150    
151 torben 63 if (to.at(0) == '+')
152     to.erase(0,0);
153 torben 26
154 torben 63 vector<PduInfo> pdu_vec = SmsPdu::CreateSmsPdu(to, message, allowMultipart);
155 torben 26
156 torben 63 for (unsigned i=0; i<pdu_vec.size(); ++i)
157     {
158     PduInfo& pdu = pdu_vec[i];
159 torben 26
160 torben 63 SendSmsPdu(pdu.pdu, pdu.len);
161 torben 50 }
162 torben 26
163     }
164    
165     void GsmModem::DeleteSms(std::string smsIndex)
166     {
167     string cmd = "AT+CMGD=";
168     cmd.append(smsIndex);
169     Command(cmd);
170     }
171    
172     int GsmModem::DeleteAllSms()
173     {
174     vector<SMS> sms = ReadSms(true);
175    
176     for (unsigned int i= 0; i<sms.size(); ++i)
177     {
178     DeleteSms( sms[i].sms_index);
179     }
180     return sms.size();
181     }
182    
183    
184    
185     void GsmModem::Init()
186     {
187 torben 58 Command( "AT" );
188 torben 57 if (_timeout)
189     throw std::runtime_error("Modem did not respond!");
190    
191 torben 58 Command( "ATZ" ); //Reset any previous setup
192    
193 torben 33 Command( "AT\\Q3" ); //Hardware flow control
194 torben 26
195     Command( "ATE0" ); //Disable echo
196    
197     Command ("AT^SM20=0,0" ); //No SM20 compability
198    
199     //Set RealTimeClock ??
200    
201     //Enter pin code ??
202     }

  ViewVC Help
Powered by ViewVC 1.1.20