Added lirc.
[irreco.git] / lirc-0.8.4a / tools / mode2.c
blobb569984556ffef1707c02e939a09f0efa3b3d3ba
1 /* $Id: mode2.c,v 5.15 2008/07/01 20:51:43 lirc Exp $ */
3 /****************************************************************************
4 ** mode2.c *****************************************************************
5 ****************************************************************************
7 * mode2 - shows the pulse/space length of a remote button
9 * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
10 * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/ioctl.h>
27 #include <limits.h>
28 #include <errno.h>
29 #include <syslog.h>
30 #include <time.h>
32 #include "drivers/lirc.h"
33 #include "daemons/ir_remote.h"
34 #include "daemons/hardware.h"
35 #include "daemons/hw-types.h"
37 #ifdef DEBUG
38 int debug=10;
39 #else
40 int debug=0;
41 #endif
42 FILE *lf=NULL;
43 char *hostname="";
44 int daemonized=0;
45 char *progname;
47 void logprintf(int prio,char *format_str, ...)
49 va_list ap;
51 if(lf)
53 time_t current;
54 char *currents;
56 current=time(&current);
57 currents=ctime(&current);
59 fprintf(lf,"%15.15s %s %s: ",currents+4,hostname,progname);
60 va_start(ap,format_str);
61 if(prio==LOG_WARNING) fprintf(lf,"WARNING: ");
62 vfprintf(lf,format_str,ap);
63 fputc('\n',lf);fflush(lf);
64 va_end(ap);
66 if(!daemonized)
68 fprintf(stderr,"%s: ",progname);
69 va_start(ap,format_str);
70 if(prio==LOG_WARNING) fprintf(stderr,"WARNING: ");
71 vfprintf(stderr,format_str,ap);
72 fputc('\n',stderr);fflush(stderr);
73 va_end(ap);
77 void logperror(int prio,const char *s)
79 if(s!=NULL)
81 logprintf(prio,"%s: %s",s,strerror(errno));
83 else
85 logprintf(prio,"%s",strerror(errno));
89 int waitfordata(unsigned long maxusec)
91 fd_set fds;
92 int ret;
93 struct timeval tv;
95 while(1)
97 FD_ZERO(&fds);
98 FD_SET(hw.fd,&fds);
99 do{
101 if(maxusec>0)
103 tv.tv_sec=maxusec/1000000;
104 tv.tv_usec=maxusec%1000000;
105 ret=select(hw.fd+1,&fds,NULL,NULL,&tv);
106 if(ret==0) return(0);
108 else
110 ret=select(hw.fd+1,&fds,NULL,NULL,NULL);
113 while(ret==-1 && errno==EINTR);
114 if(ret==-1)
116 logprintf(LOG_ERR,"select() failed\n");
117 logperror(LOG_ERR,NULL);
118 continue;
121 while(ret==-1);
123 if(FD_ISSET(hw.fd,&fds))
125 /* we will read later */
126 return(1);
131 int main(int argc,char **argv)
133 int fd;
134 char buffer[sizeof(ir_code)];
135 lirc_t data;
136 unsigned long mode;
137 char *device=LIRC_DRIVER_DEVICE;
138 struct stat s;
139 int dmode=0;
140 unsigned long code_length;
141 size_t count=sizeof(lirc_t);
142 int i;
143 int use_raw_access = 0;
144 int have_device = 0;
146 progname="mode2";
147 hw_choose_driver(NULL);
148 while(1)
150 int c;
151 static struct option long_options[] =
153 {"help",no_argument,NULL,'h'},
154 {"version",no_argument,NULL,'v'},
155 {"device",required_argument,NULL,'d'},
156 {"driver",required_argument,NULL,'H'},
157 {"mode",no_argument,NULL,'m'},
158 {"raw",no_argument,NULL,'r'},
159 {0, 0, 0, 0}
161 c = getopt_long(argc,argv,"hvd:H:mr",long_options,NULL);
162 if(c==-1)
163 break;
164 switch (c)
166 case 'h':
167 printf("Usage: %s [options]\n",progname);
168 printf("\t -h --help\t\tdisplay usage summary\n");
169 printf("\t -v --version\t\tdisplay version\n");
170 printf("\t -d --device=device\tread from given device\n");
171 printf("\t -H --driver=driver\t\tuse given driver\n");
172 printf("\t -m --mode\t\tenable alternative display mode\n");
173 printf("\t -r --raw\t\taccess device directly\n");
174 return(EXIT_SUCCESS);
175 case 'H':
176 if(hw_choose_driver(optarg) != 0){
177 fprintf(stderr, "Driver `%s' not supported.\n",
178 optarg);
179 hw_print_drivers(stderr);
180 exit (EXIT_FAILURE);
182 break;
183 case 'v':
184 printf("%s %s\n",progname, VERSION);
185 return(EXIT_SUCCESS);
186 case 'd':
187 device=optarg;
188 have_device = 1;
189 break;
190 case 'm':
191 dmode=1;
192 break;
193 case 'r':
194 use_raw_access = 1;
195 break;
196 default:
197 printf("Usage: %s [options]\n",progname);
198 return(EXIT_FAILURE);
201 if (optind < argc)
203 fprintf(stderr,"%s: too many arguments\n",progname);
204 return(EXIT_FAILURE);
207 if(use_raw_access)
209 fd=open(device,O_RDONLY);
210 if(fd==-1) {
211 fprintf(stderr, "%s: error opening %s\n", progname,
212 device);
213 perror(progname);
214 exit(EXIT_FAILURE);
217 if ( (fstat(fd,&s)!=-1) && (S_ISFIFO(s.st_mode)) )
219 /* can't do ioctls on a pipe */
221 else if ( (fstat(fd,&s)!=-1) && (!S_ISCHR(s.st_mode)) )
223 fprintf(stderr, "%s: %s is not a character device\n",
224 progname, device);
225 fprintf(stderr, "%s: use the -d option to specify "
226 "the correct device\n", progname);
227 close(fd);
228 exit(EXIT_FAILURE);
230 else if(ioctl(fd,LIRC_GET_REC_MODE,&mode)==-1)
232 printf("This program is only intended for receivers "
233 "supporting the pulse/space layer.\n");
234 printf("Note that this is no error, but this program "
235 "simply makes no sense for your\n"
236 "receiver.\n");
237 printf("In order to test your setup run lircd with "
238 "the --nodaemon option and \n"
239 "then check if the remote works with the irw "
240 "tool.\n");
241 close(fd);
242 exit(EXIT_FAILURE);
245 else
247 if(have_device) hw.device = device;
248 if(!hw.init_func())
250 return EXIT_FAILURE;
252 fd = hw.fd; /* please compiler */
253 mode = hw.rec_mode;
254 if(mode != LIRC_MODE_MODE2)
256 if(strcmp(hw.name, "default") == 0)
258 printf("Please use the --raw option to access "
259 "the device directly instead through\n"
260 "the abstraction layer.\n");
262 else
264 printf("This program does not work for this "
265 "hardware yet\n");
267 exit(EXIT_FAILURE);
272 if(mode==LIRC_MODE_CODE)
274 count = 1;
276 else if(mode==LIRC_MODE_LIRCCODE)
278 if(use_raw_access)
280 if(ioctl(fd,LIRC_GET_LENGTH,&code_length)==-1)
282 fprintf(stderr,
283 "%s: could not get code length\n",
284 progname);
285 perror(progname);
286 close(fd);
287 exit(EXIT_FAILURE);
290 else
292 code_length = hw.code_length;
294 if(code_length>sizeof(ir_code)*CHAR_BIT)
296 fprintf(stderr, "%s: cannot handle %lu bit codes\n",
297 progname, code_length);
298 close(fd);
299 exit(EXIT_FAILURE);
301 count = (code_length+CHAR_BIT-1)/CHAR_BIT;
303 while(1)
305 int result;
307 if(use_raw_access)
309 result=read(fd,
310 (mode==LIRC_MODE_MODE2 ?
311 (void *) &data:buffer), count);
312 if(result!=count)
314 fprintf(stderr,"read() failed\n");
315 break;
318 else
320 if(mode == LIRC_MODE_MODE2)
322 data=hw.readdata(0);
323 if(data == 0)
325 fprintf(stderr,"readdata() failed\n");
326 break;
329 else
331 /* not implemented yet */
335 if(mode!=LIRC_MODE_MODE2)
337 printf("code: 0x");
338 for(i=0; i<count; i++)
340 printf("%02x", (unsigned char) buffer[i]);
342 printf("\n");
343 fflush(stdout);
344 continue;
347 if (!dmode)
349 printf("%s %lu\n",(data&PULSE_BIT)?"pulse":"space",
350 (unsigned long) (data&PULSE_MASK));
352 else
354 static int bitno = 1;
356 /* print output like irrecord raw config file data */
357 printf(" %8lu" , (unsigned long) data&PULSE_MASK);
358 ++bitno;
359 if (data&PULSE_BIT)
361 if ((bitno & 1) == 0)
363 /* not in expected order */
364 printf("-pulse");
367 else
369 if (bitno & 1)
371 /* not in expected order */
372 printf("-space");
374 if ( ((data&PULSE_MASK) > 50000) ||
375 (bitno >= 6) )
377 /* real long space or more
378 than 6 codes, start new line */
379 printf("\n");
380 if ((data&PULSE_MASK) > 50000)
381 printf("\n");
382 bitno = 0;
386 fflush(stdout);
388 return(EXIT_SUCCESS);