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

Contents of /smsdaemon/ModemTransceiver.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 49 - (show annotations) (download)
Wed Jun 11 10:21:47 2008 UTC (15 years, 11 months ago) by torben
Original Path: smsdaemon/GsmModem.cpp
File size: 3118 byte(s)
Make togplugin work

GsmModem.cpp - when sending SMS - truncate message body to 160 chars

1 /* using http://sourceforge.net/projects/libserial/
2 */
3
4 #include <iostream>
5
6 #include <string>
7 #include <cstring>
8 #include <stdexcept>
9
10 #include <sys/time.h>
11 #include <time.h>
12
13
14 #include "SerialPort.h"
15
16 #include "GsmModem.h"
17
18 #include "util.h"
19 #include "common.h"
20
21
22 using namespace std;
23
24
25
26 GsmModem::GsmModem(SerialPort& serialport)
27 : m_port(serialport)
28 {
29 Init();
30 }
31
32
33
34 string GsmModem::GetResponse()
35 {
36
37 SerialPort::DataBuffer buf;
38 m_port.Read(buf);
39
40 buf.push_back(0);
41
42 std::string str((char*) &buf[0]);
43
44 return str;
45 }
46
47
48 string GsmModem::Command(string command, string term)
49 {
50 time_t start,now;
51 start = time(0);
52
53 if (term != "> ")
54 command.append("\r"); //Dont append CarriageReturn if sending SMS
55
56 m_port.Write(command);
57
58 Util::Sleep(1);
59 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 Util::Sleep(1);
72
73 now = time(0);
74 if ( (now-start) > 10 )
75 {
76 Common::instance()->logMessage( string("GsmModem::Command time out --") + command);
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 void GsmModem::SendSms(string to, string message)
123 {
124 Common::instance()->logMessage( string("SMS send to ") + to);
125
126 Command("AT+CMGF=1");///Allways telling the format makes the application more stable
127 Util::Sleep(2);
128
129 string line1 = "AT+CMGS=";
130 line1.append(to);
131 line1.append("\r");
132
133 Command(line1,"> ");
134
135
136 message.append("\032"); // \032 == Ctrl+Z
137
138 Command( message.substr(0,160) ); //In textmode limit to 160 bytes
139
140
141 Util::Sleep(50); //Give the modem some time to send the sms and be ready again
142 Common::instance()->smsCounter.outgoing++;
143 }
144
145 void GsmModem::DeleteSms(std::string smsIndex)
146 {
147 string cmd = "AT+CMGD=";
148 cmd.append(smsIndex);
149 Command(cmd);
150 }
151
152 int GsmModem::DeleteAllSms()
153 {
154 vector<SMS> sms = ReadSms(true);
155
156 for (unsigned int i= 0; i<sms.size(); ++i)
157 {
158 DeleteSms( sms[i].sms_index);
159 }
160 return sms.size();
161 }
162
163
164
165 void GsmModem::Init()
166 {
167 Command( "ATZ" ); //Reset any previous setup
168 Command( "AT\\Q3" ); //Hardware flow control
169
170 Command( "ATE0" ); //Disable echo
171
172 Command( "AT+CMGF=1" ); //Set SMS format to text
173
174 Command ("AT^SM20=0,0" ); //No SM20 compability
175
176 //Set RealTimeClock ??
177
178 //Enter pin code ??
179 }

  ViewVC Help
Powered by ViewVC 1.1.20