Diffstat (limited to 'noncore/tools/remote/packetread.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/remote/packetread.h | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/noncore/tools/remote/packetread.h b/noncore/tools/remote/packetread.h new file mode 100644 index 0000000..5dc1ad4 --- a/dev/null +++ b/noncore/tools/remote/packetread.h | |||
@@ -0,0 +1,200 @@ | |||
1 | /* | ||
2 | Opie-Remote. emulates remote controlls on an iPaq (and maybe a Zaurus) in Opie. | ||
3 | Copyright (C) 2002 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 | file not used | ||
17 | */ | ||
18 | |||
19 | #include <stdio.h> | ||
20 | #include <stdlib.h> | ||
21 | #include <string.h> | ||
22 | #include <sys/socket.h> | ||
23 | #include <sys/types.h> | ||
24 | #include <sys/un.h> | ||
25 | #include <unistd.h> | ||
26 | #include <errno.h> | ||
27 | |||
28 | #define PACKET_SIZE 256 | ||
29 | #define TIMEOUT 3 | ||
30 | |||
31 | //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html | ||
32 | const char *readPacket(int fd) | ||
33 | { | ||
34 | static char buffer[PACKET_SIZE+1]=""; | ||
35 | char *end; | ||
36 | static int ptr=0,end_len=0; | ||
37 | ssize_t ret; | ||
38 | |||
39 | if(ptr>0) | ||
40 | { | ||
41 | memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1); | ||
42 | ptr=strlen(buffer); | ||
43 | end=strchr(buffer,'\n'); | ||
44 | } | ||
45 | else | ||
46 | { | ||
47 | end=NULL; | ||
48 | } | ||
49 | alarm(TIMEOUT); | ||
50 | while(end==NULL) | ||
51 | { | ||
52 | if(PACKET_SIZE<=ptr) | ||
53 | { | ||
54 | // fprintf(stderr,"%s: bad packet\n",progname); | ||
55 | ptr=0; | ||
56 | return(NULL); | ||
57 | } | ||
58 | ret=read(fd,buffer+ptr,PACKET_SIZE-ptr); | ||
59 | |||
60 | if(ret<=0 || timeout) | ||
61 | { | ||
62 | if(timeout) | ||
63 | { | ||
64 | // fprintf(stderr,"%s: timeout\n",progname); | ||
65 | } | ||
66 | else | ||
67 | { | ||
68 | alarm(0); | ||
69 | } | ||
70 | ptr=0; | ||
71 | return(NULL); | ||
72 | } | ||
73 | buffer[ptr+ret]=0; | ||
74 | ptr=strlen(buffer); | ||
75 | end=strchr(buffer,'\n'); | ||
76 | } | ||
77 | alarm(0);timeout=0; | ||
78 | |||
79 | end[0]=0; | ||
80 | ptr=strlen(buffer)+1; | ||
81 | //# ifdef DEBUG | ||
82 | //printf("buffer: -%s-\n",buffer); | ||
83 | //# endif | ||
84 | return(buffer); | ||
85 | } | ||
86 | QStringList getRemotes(int fd) | ||
87 | { | ||
88 | const char write_buffer[] = "LIST\n"; | ||
89 | const char *readbuffer; | ||
90 | int i, numlines; | ||
91 | QStringList list; | ||
92 | |||
93 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
94 | |||
95 | if(std::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | ||
96 | { | ||
97 | QMessageBox *mb = new QMessageBox("Error!", | ||
98 | "couldnt connect to socket", | ||
99 | QMessageBox::NoIcon, | ||
100 | QMessageBox::Ok, | ||
101 | QMessageBox::NoButton, | ||
102 | QMessageBox::NoButton); | ||
103 | mb->exec(); | ||
104 | perror("ConfigTab::GetRemotes"); | ||
105 | return NULL; | ||
106 | } | ||
107 | |||
108 | write(fd, write_buffer, strlen(write_buffer) ); | ||
109 | |||
110 | for(i=0; i<5; i++) | ||
111 | { | ||
112 | readbuffer = readPacket(int fd); | ||
113 | } | ||
114 | |||
115 | numlines = atoi(readbuffer); | ||
116 | |||
117 | for(i=0; i<numlines; i++) | ||
118 | { | ||
119 | list+=readPacket(); | ||
120 | } | ||
121 | |||
122 | if(strcasecmp(readPacket(), "END") != 0) | ||
123 | { | ||
124 | QMessageBox *mb = new QMessageBox("Error!", | ||
125 | "bad packet", | ||
126 | QMessageBox::NoIcon, | ||
127 | QMessageBox::Ok, | ||
128 | QMessageBox::NoButton, | ||
129 | QMessageBox::NoButton); | ||
130 | mb->exec(); | ||
131 | perror("ConfigTab::GetRemotes"); | ||
132 | return NULL; | ||
133 | } | ||
134 | |||
135 | std::close(fd); | ||
136 | return list; | ||
137 | } | ||
138 | |||
139 | QStringList getButtons(int fd, const char *remoteName) | ||
140 | { | ||
141 | QString write_buffer = "LIST "; | ||
142 | const char *readbuffer; | ||
143 | int i, j, numlines; | ||
144 | QStringList list; | ||
145 | QString string; | ||
146 | |||
147 | write_buffer += remoteName; | ||
148 | write_buffer += '\n'; | ||
149 | |||
150 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
151 | |||
152 | if(std::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1) | ||
153 | { | ||
154 | QMessageBox *mb = new QMessageBox("Error!", | ||
155 | "couldnt connect to socket", | ||
156 | QMessageBox::NoIcon, | ||
157 | QMessageBox::Ok, | ||
158 | QMessageBox::NoButton, | ||
159 | QMessageBox::NoButton); | ||
160 | mb->exec(); | ||
161 | perror("ConfigTab::GetRemotes"); | ||
162 | return NULL; | ||
163 | } | ||
164 | |||
165 | write(fd, write_buffer.latin1(), strlen(write_buffer) ); | ||
166 | |||
167 | for(i=0; i<5; i++) | ||
168 | { | ||
169 | readbuffer = readPacket(); | ||
170 | } | ||
171 | |||
172 | numlines = atoi(readbuffer); | ||
173 | |||
174 | for(i=0; i<numlines; i++) | ||
175 | { | ||
176 | list+=readPacket(); | ||
177 | for(j=0; j<list[i].length(); j++) | ||
178 | { | ||
179 | if(list[i][j] == ' ') | ||
180 | break; | ||
181 | } | ||
182 | list[i].remove(0, j+1); | ||
183 | } | ||
184 | |||
185 | if(strcasecmp(readPacket(), "END") != 0) | ||
186 | { | ||
187 | QMessageBox *mb = new QMessageBox("Error!", | ||
188 | "bad packet", | ||
189 | QMessageBox::NoIcon, | ||
190 | QMessageBox::Ok, | ||
191 | QMessageBox::NoButton, | ||
192 | QMessageBox::NoButton); | ||
193 | mb->exec(); | ||
194 | perror("ConfigTab::GetRemotes"); | ||
195 | return NULL; | ||
196 | } | ||
197 | |||
198 | std::close(fd); | ||
199 | return list; | ||
200 | } | ||