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>
24 #include <sys/types.h>
26 #include <sys/ioctl.h>
32 #include "drivers/lirc.h"
33 #include "daemons/ir_remote.h"
34 #include "daemons/hardware.h"
35 #include "daemons/hw-types.h"
47 void logprintf(int prio
,char *format_str
, ...)
56 current
=time(¤t
);
57 currents
=ctime(¤t
);
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
);
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
);
77 void logperror(int prio
,const char *s
)
81 logprintf(prio
,"%s: %s",s
,strerror(errno
));
85 logprintf(prio
,"%s",strerror(errno
));
89 int waitfordata(unsigned long maxusec
)
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);
110 ret
=select(hw
.fd
+1,&fds
,NULL
,NULL
,NULL
);
113 while(ret
==-1 && errno
==EINTR
);
116 logprintf(LOG_ERR
,"select() failed\n");
117 logperror(LOG_ERR
,NULL
);
123 if(FD_ISSET(hw
.fd
,&fds
))
125 /* we will read later */
131 int main(int argc
,char **argv
)
134 char buffer
[sizeof(ir_code
)];
137 char *device
=LIRC_DRIVER_DEVICE
;
140 unsigned long code_length
;
141 size_t count
=sizeof(lirc_t
);
143 int use_raw_access
= 0;
147 hw_choose_driver(NULL
);
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'},
161 c
= getopt_long(argc
,argv
,"hvd:H:mr",long_options
,NULL
);
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
);
176 if(hw_choose_driver(optarg
) != 0){
177 fprintf(stderr
, "Driver `%s' not supported.\n",
179 hw_print_drivers(stderr
);
184 printf("%s %s\n",progname
, VERSION
);
185 return(EXIT_SUCCESS
);
197 printf("Usage: %s [options]\n",progname
);
198 return(EXIT_FAILURE
);
203 fprintf(stderr
,"%s: too many arguments\n",progname
);
204 return(EXIT_FAILURE
);
209 fd
=open(device
,O_RDONLY
);
211 fprintf(stderr
, "%s: error opening %s\n", progname
,
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",
225 fprintf(stderr
, "%s: use the -d option to specify "
226 "the correct device\n", progname
);
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"
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 "
247 if(have_device
) hw
.device
= device
;
252 fd
= hw
.fd
; /* please compiler */
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");
264 printf("This program does not work for this "
272 if(mode
==LIRC_MODE_CODE
)
276 else if(mode
==LIRC_MODE_LIRCCODE
)
280 if(ioctl(fd
,LIRC_GET_LENGTH
,&code_length
)==-1)
283 "%s: could not get code length\n",
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
);
301 count
= (code_length
+CHAR_BIT
-1)/CHAR_BIT
;
310 (mode
==LIRC_MODE_MODE2
?
311 (void *) &data
:buffer
), count
);
314 fprintf(stderr
,"read() failed\n");
320 if(mode
== LIRC_MODE_MODE2
)
325 fprintf(stderr
,"readdata() failed\n");
331 /* not implemented yet */
335 if(mode
!=LIRC_MODE_MODE2
)
338 for(i
=0; i
<count
; i
++)
340 printf("%02x", (unsigned char) buffer
[i
]);
349 printf("%s %lu\n",(data
&PULSE_BIT
)?"pulse":"space",
350 (unsigned long) (data
&PULSE_MASK
));
354 static int bitno
= 1;
356 /* print output like irrecord raw config file data */
357 printf(" %8lu" , (unsigned long) data
&PULSE_MASK
);
361 if ((bitno
& 1) == 0)
363 /* not in expected order */
371 /* not in expected order */
374 if ( ((data
&PULSE_MASK
) > 50000) ||
377 /* real long space or more
378 than 6 codes, start new line */
380 if ((data
&PULSE_MASK
) > 50000)
388 return(EXIT_SUCCESS
);