author | paule <paule> | 2007-01-28 04:04:14 (UTC) |
---|---|---|
committer | paule <paule> | 2007-01-28 04:04:14 (UTC) |
commit | ec2309cb69365847762db542c41951629cd06cbe (patch) (unidiff) | |
tree | 2f0101c1f0d6268207dbd15eb21f40c812a4fff5 /noncore/tools/remote/lirchandler.cpp | |
parent | 3cd37427b5c5f26f62cff583fbde914467ddafe3 (diff) | |
download | opie-ec2309cb69365847762db542c41951629cd06cbe.zip opie-ec2309cb69365847762db542c41951629cd06cbe.tar.gz opie-ec2309cb69365847762db542c41951629cd06cbe.tar.bz2 |
Refactored out all communication with LIRC into new LircHandler class
Diffstat (limited to 'noncore/tools/remote/lirchandler.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/remote/lirchandler.cpp | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/noncore/tools/remote/lirchandler.cpp b/noncore/tools/remote/lirchandler.cpp new file mode 100644 index 0000000..f44806e --- a/dev/null +++ b/noncore/tools/remote/lirchandler.cpp | |||
@@ -0,0 +1,270 @@ | |||
1 | /* | ||
2 | Opie-Remote. emulates remote controls on an iPaq (and maybe a Zaurus) in Opie. | ||
3 | Copyright (C) 2007 Paul Eggleton & Thomas Stephens | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public | ||
6 | License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later | ||
7 | version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | ||
10 | implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
11 | Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with this program; if not, write to the Free | ||
14 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
15 | */ | ||
16 | |||
17 | #include <stdio.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | #include <sys/socket.h> | ||
21 | #include <sys/types.h> | ||
22 | #include <sys/un.h> | ||
23 | #include <unistd.h> | ||
24 | #include <errno.h> | ||
25 | |||
26 | #include <qstring.h> | ||
27 | #include <qmessagebox.h> | ||
28 | #include <qobject.h> | ||
29 | #include <opie2/oprocess.h> | ||
30 | |||
31 | #include "lirchandler.h" | ||
32 | |||
33 | #define PACKET_SIZE 256 | ||
34 | #define TIMEOUT 3 | ||
35 | #define LIRCD_SOCKET "/dev/lircd" | ||
36 | #define LIRCD_SERVICECMD "/etc/init.d/lircd" | ||
37 | |||
38 | using namespace Opie::Core; | ||
39 | |||
40 | LircHandler::LircHandler(void) | ||
41 | { | ||
42 | fd = 0; | ||
43 | addr.sun_family=AF_UNIX; | ||
44 | strcpy(addr.sun_path, LIRCD_SOCKET); | ||
45 | } | ||
46 | |||
47 | bool LircHandler::connectLirc(void) | ||
48 | { | ||
49 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
50 | if(fd == -1) | ||
51 | { | ||
52 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | ||
53 | QObject::tr("Unable to create socket"), | ||
54 | QMessageBox::Critical, | ||
55 | QMessageBox::Ok, | ||
56 | QMessageBox::NoButton, | ||
57 | QMessageBox::NoButton); | ||
58 | mb->exec(); | ||
59 | perror("LircHandler::connectLirc"); | ||
60 | return false; | ||
61 | } | ||
62 | |||
63 | if(::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | ||
64 | { | ||
65 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | ||
66 | QObject::tr("Could not connect to lircd"), | ||
67 | QMessageBox::Critical, | ||
68 | QMessageBox::Ok, | ||
69 | QMessageBox::NoButton, | ||
70 | QMessageBox::NoButton); | ||
71 | mb->exec(); | ||
72 | perror("LircHandler::connectLirc"); | ||
73 | return false; | ||
74 | } | ||
75 | |||
76 | return true; | ||
77 | } | ||
78 | |||
79 | //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html | ||
80 | const char *LircHandler::readPacket() | ||
81 | { | ||
82 | static char buffer[PACKET_SIZE+1]=""; | ||
83 | char *end; | ||
84 | static int ptr=0; | ||
85 | ssize_t ret; | ||
86 | int timeout = 0; | ||
87 | |||
88 | if(ptr>0) | ||
89 | { | ||
90 | memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1); | ||
91 | ptr=strlen(buffer); | ||
92 | end=strchr(buffer,'\n'); | ||
93 | } | ||
94 | else | ||
95 | { | ||
96 | end=NULL; | ||
97 | } | ||
98 | alarm(TIMEOUT); | ||
99 | while(end==NULL) | ||
100 | { | ||
101 | if(PACKET_SIZE<=ptr) | ||
102 | { | ||
103 | fprintf(stderr,"readPacket: bad packet\n"); | ||
104 | ptr=0; | ||
105 | return(NULL); | ||
106 | } | ||
107 | ret=read(fd,buffer+ptr,PACKET_SIZE-ptr); | ||
108 | |||
109 | if(ret<=0 || timeout) | ||
110 | { | ||
111 | if(timeout) | ||
112 | { | ||
113 | fprintf(stderr,"readPacket: timeout\n"); | ||
114 | } | ||
115 | else | ||
116 | { | ||
117 | alarm(0); | ||
118 | } | ||
119 | ptr=0; | ||
120 | return(NULL); | ||
121 | } | ||
122 | buffer[ptr+ret]=0; | ||
123 | ptr=strlen(buffer); | ||
124 | end=strchr(buffer,'\n'); | ||
125 | } | ||
126 | alarm(0); | ||
127 | |||
128 | end[0]=0; | ||
129 | ptr=strlen(buffer)+1; | ||
130 | //# ifdef DEBUG | ||
131 | //printf("buffer: -%s-\n",buffer); | ||
132 | //# endif | ||
133 | return(buffer); | ||
134 | } | ||
135 | |||
136 | QStringList LircHandler::getRemotes(void) | ||
137 | { | ||
138 | const char write_buffer[] = "LIST\n"; | ||
139 | const char *readbuffer; | ||
140 | int i, numlines; | ||
141 | QStringList list; | ||
142 | |||
143 | if(connectLirc()) { | ||
144 | write(fd, write_buffer, strlen(write_buffer) ); | ||
145 | |||
146 | for(i=0; i<5; i++) | ||
147 | { | ||
148 | readbuffer = readPacket(); | ||
149 | } | ||
150 | |||
151 | numlines = atoi(readbuffer); | ||
152 | |||
153 | for(i=0; i<numlines; i++) | ||
154 | { | ||
155 | list+=readPacket(); | ||
156 | } | ||
157 | |||
158 | if(strcasecmp(readPacket(), "END") != 0) | ||
159 | { | ||
160 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | ||
161 | QObject::tr("Bad packet while communicating with lircd"), | ||
162 | QMessageBox::Critical, | ||
163 | QMessageBox::Ok, | ||
164 | QMessageBox::NoButton, | ||
165 | QMessageBox::NoButton); | ||
166 | mb->exec(); | ||
167 | perror("LircHandler::getRemotes"); | ||
168 | return NULL; | ||
169 | } | ||
170 | |||
171 | ::close(fd); | ||
172 | } | ||
173 | |||
174 | return list; | ||
175 | } | ||
176 | |||
177 | QStringList LircHandler::getButtons(const char *remoteName) | ||
178 | { | ||
179 | QString write_buffer = "LIST "; | ||
180 | const char *readbuffer; | ||
181 | int i, j, numlines; | ||
182 | QStringList list; | ||
183 | QString string; | ||
184 | |||
185 | write_buffer += remoteName; | ||
186 | write_buffer += '\n'; | ||
187 | |||
188 | if(connectLirc()) { | ||
189 | write(fd, write_buffer.latin1(), strlen(write_buffer) ); | ||
190 | |||
191 | for(i=0; i<5; i++) | ||
192 | { | ||
193 | readbuffer = readPacket(); | ||
194 | } | ||
195 | |||
196 | numlines = atoi(readbuffer); | ||
197 | |||
198 | for(i=0; i<numlines; i++) | ||
199 | { | ||
200 | list+=readPacket(); | ||
201 | for(j=0; j<list[i].length(); j++) | ||
202 | { | ||
203 | if(list[i][j] == ' ') | ||
204 | break; | ||
205 | } | ||
206 | list[i].remove(0, j+1); | ||
207 | } | ||
208 | |||
209 | if(strcasecmp(readPacket(), "END") != 0) | ||
210 | { | ||
211 | QMessageBox *mb = new QMessageBox(QObject::tr("Error"), | ||
212 | QObject::tr("Bad packet while communicating with lircd"), | ||
213 | QMessageBox::Critical, | ||
214 | QMessageBox::Ok, | ||
215 | QMessageBox::NoButton, | ||
216 | QMessageBox::NoButton); | ||
217 | mb->exec(); | ||
218 | perror("LircHandler::getRemotes"); | ||
219 | return NULL; | ||
220 | } | ||
221 | |||
222 | ::close(fd); | ||
223 | } | ||
224 | |||
225 | return list; | ||
226 | } | ||
227 | |||
228 | int LircHandler::sendIR(const char *irbutton) | ||
229 | { | ||
230 | const char *read_buffer; | ||
231 | bool done=false; | ||
232 | |||
233 | if(connectLirc()) { | ||
234 | printf("fd2: %d\n", fd); | ||
235 | printf("%s", irbutton); | ||
236 | |||
237 | printf("1\n"); | ||
238 | printf("%d\n", write(fd, irbutton, strlen(irbutton) ) ); | ||
239 | printf("2\n"); | ||
240 | while(!done) | ||
241 | { | ||
242 | read_buffer=readPacket(); | ||
243 | printf("%s\n", read_buffer); | ||
244 | if(strcasecmp(read_buffer, "END") == 0) | ||
245 | { | ||
246 | printf("done reading packet\n"); | ||
247 | done=true; | ||
248 | } | ||
249 | } | ||
250 | ::close(fd); | ||
251 | return 1; | ||
252 | } | ||
253 | else | ||
254 | return 0; | ||
255 | } | ||
256 | |||
257 | bool LircHandler::startLircd(void) | ||
258 | { | ||
259 | return (system(LIRCD_SERVICECMD " start") == 0); | ||
260 | } | ||
261 | |||
262 | bool LircHandler::stopLircd(void) | ||
263 | { | ||
264 | return (system(LIRCD_SERVICECMD " stop") == 0); | ||
265 | } | ||
266 | |||
267 | bool LircHandler::isLircdRunning(void) | ||
268 | { | ||
269 | return (OProcess::processPID("lircd") != 0); | ||
270 | } | ||