1 /* $Id: hw_default.c,v 5.37 2008/09/03 20:22:12 lirc Exp $ */
3 /****************************************************************************
4 ** hw_default.c ************************************************************
5 ****************************************************************************
7 * routines for hardware that supports ioctl() interface
9 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
29 /* disable daemonise if maintainer mode SIM_REC / SIM_SEND defined */
30 #if defined(SIM_REC) || defined (SIM_SEND)
35 #include "ir_remote.h"
39 #include "hw_default.h"
41 extern struct ir_remote
*repeat_remote
;
43 static unsigned long supported_send_modes
[]=
45 /* LIRC_CAN_SEND_STRING, I don't think there ever will be a driver
47 /* LIRC_CAN_SEND_LIRCCODE, */
48 /* LIRC_CAN_SEND_CODE, */
49 /* LIRC_CAN_SEND_MODE2, this one would be very easy */
51 /* LIRC_CAN_SEND_RAW, */
54 static unsigned long supported_rec_modes
[]=
57 LIRC_CAN_REC_LIRCCODE
,
60 /* LIRC_CAN_REC_PULSE, shouldn't be too hard */
61 /* LIRC_CAN_REC_RAW, */
65 struct hardware hw_default
=
67 LIRC_DRIVER_DEVICE
, /* default device */
73 default_init
, /* init_func */
74 default_config
, /* config_func */
75 default_deinit
, /* deinit_func */
76 default_send
, /* send_func */
77 default_rec
, /* rec_func */
78 receive_decode
, /* decode_func */
79 default_ioctl
, /* ioctl_func */
84 static unsigned int min_freq
=0,max_freq
=0;
86 /**********************************************************************
88 * internal function prototypes
90 **********************************************************************/
92 static int default_config_frequency();
93 static int write_send_buffer(int lirc
);
95 /**********************************************************************
99 **********************************************************************/
101 lirc_t
default_readdata(lirc_t timeout
)
106 if (!waitfordata((long) timeout
))
109 #if defined(SIM_REC) && !defined(DAEMONIZE)
114 ret
=fscanf(stdin
,"space %ld\n",&scan
);
120 ret
=fscanf(stdin
,"pulse %ld\n",&scan
);
123 data
=(lirc_t
) scan
|PULSE_BIT
;
126 ret
=fscanf(stdin
,"%*s\n");
133 ret
=read(hw
.fd
,&data
,sizeof(data
));
134 if(ret
!=sizeof(data
))
136 logprintf(LOG_ERR
, "error reading from %s", hw
.device
);
137 logperror(LOG_ERR
, NULL
);
143 static int data_warning
= 1;
147 logprintf(LOG_WARNING
,
148 "read invalid data from device %s",
164 #if defined(SIM_SEND) && !defined(DAEMONIZE)
166 hw
.features
=LIRC_CAN_SEND_PULSE
;
167 hw
.send_mode
=LIRC_MODE_PULSE
;
169 #elif defined(SIM_REC) && !defined(DAEMONIZE)
171 hw
.features
=LIRC_CAN_REC_MODE2
;
173 hw
.rec_mode
=LIRC_MODE_MODE2
;
178 /* FIXME: other modules might need this, too */
182 if(stat(hw
.device
,&s
)==-1)
184 logprintf(LOG_ERR
,"could not get file information for %s",
186 logperror(LOG_ERR
,"default_init()");
190 /* file could be unix socket, fifo and native lirc device */
191 if(S_ISSOCK(s
.st_mode
))
193 struct sockaddr_un addr
;
194 addr
.sun_family
=AF_UNIX
;
195 strncpy(addr
.sun_path
,hw
.device
,sizeof(addr
.sun_path
));
197 hw
.fd
=socket(AF_UNIX
,SOCK_STREAM
,0);
200 logprintf(LOG_ERR
,"could not create socket");
201 logperror(LOG_ERR
,"default_init()");
205 if(connect(hw
.fd
,(struct sockaddr
*) &addr
,sizeof(addr
))==-1)
207 logprintf(LOG_ERR
,"could not connect to unix socket %s",
209 logperror(LOG_ERR
,"default_init()");
215 LOGPRINTF(1,"using unix socket lirc device");
216 hw
.features
=LIRC_CAN_REC_MODE2
| LIRC_CAN_SEND_PULSE
;
217 hw
.rec_mode
=LIRC_MODE_MODE2
; /* this might change in future */
218 hw
.send_mode
=LIRC_MODE_PULSE
;
222 if((hw
.fd
=open(hw
.device
,O_RDWR
))<0)
224 logprintf(LOG_ERR
,"could not open %s",hw
.device
);
225 logperror(LOG_ERR
,"default_init()");
228 if(S_ISFIFO(s
.st_mode
))
230 LOGPRINTF(1,"using defaults for the Irman");
231 hw
.features
=LIRC_CAN_REC_MODE2
;
232 hw
.rec_mode
=LIRC_MODE_MODE2
; /* this might change in future */
235 else if(!S_ISCHR(s
.st_mode
))
238 logprintf(LOG_ERR
,"%s is not a character device!!!",
240 logperror(LOG_ERR
,"something went wrong during "
244 else if(default_ioctl(LIRC_GET_FEATURES
, &hw
.features
)==-1)
246 logprintf(LOG_ERR
,"could not get hardware features");
247 logprintf(LOG_ERR
,"this device driver does not "
248 "support the new LIRC interface");
249 if(major(s
.st_rdev
) != LIRC_MAJOR
)
251 logprintf(LOG_ERR
, "major number of %s is %lu",
253 (unsigned long) major(s
.st_rdev
));
254 logprintf(LOG_ERR
, "LIRC major number is %lu",
255 (unsigned long) LIRC_MAJOR
);
256 logprintf(LOG_ERR
, "check if %s is a LIRC device",
261 logprintf(LOG_ERR
,"make sure you use a current "
262 "version of the driver");
270 if(!(LIRC_CAN_SEND(hw
.features
) ||
271 LIRC_CAN_REC(hw
.features
)))
273 LOGPRINTF(1,"driver supports neither "
274 "sending nor receiving of IR signals");
276 if(LIRC_CAN_SEND(hw
.features
) && LIRC_CAN_REC(hw
.features
))
278 LOGPRINTF(1,"driver supports both sending and "
281 else if(LIRC_CAN_SEND(hw
.features
))
283 LOGPRINTF(1,"driver supports sending");
285 else if(LIRC_CAN_REC(hw
.features
))
287 LOGPRINTF(1,"driver supports receiving");
292 /* set send/receive method */
294 if(LIRC_CAN_SEND(hw
.features
))
296 for(i
=0;supported_send_modes
[i
]!=0;i
++)
298 if(hw
.features
&supported_send_modes
[i
])
302 mode
=LIRC_SEND2MODE(supported_send_modes
[i
]);
303 if(default_ioctl(LIRC_SET_SEND_MODE
, &mode
)==-1)
305 logprintf(LOG_ERR
,"could not set "
307 logperror(LOG_ERR
,"default_init()");
311 hw
.send_mode
=LIRC_SEND2MODE
312 (supported_send_modes
[i
]);
316 if(supported_send_modes
[i
]==0)
318 logprintf(LOG_NOTICE
,"the send method of the "
319 "driver is not yet supported by lircd");
323 if(LIRC_CAN_REC(hw
.features
))
325 for(i
=0;supported_rec_modes
[i
]!=0;i
++)
327 if(hw
.features
&supported_rec_modes
[i
])
331 mode
=LIRC_REC2MODE(supported_rec_modes
[i
]);
332 if(default_ioctl(LIRC_SET_REC_MODE
, &mode
)==-1)
334 logprintf(LOG_ERR
,"could not set "
336 logperror(LOG_ERR
,"default_init()");
339 hw
.rec_mode
=LIRC_REC2MODE
340 (supported_rec_modes
[i
]);
344 if(supported_rec_modes
[i
]==0)
346 logprintf(LOG_NOTICE
,"the receive method of the "
347 "driver is not yet supported by lircd");
350 if(hw
.rec_mode
==LIRC_MODE_MODE2
)
354 if((hw
.features
&LIRC_CAN_GET_REC_RESOLUTION
) &&
355 (default_ioctl(LIRC_GET_REC_RESOLUTION
, &hw
.resolution
)!=-1))
357 LOGPRINTF(1, "resolution of receiver: %d",
362 else if(hw
.rec_mode
==LIRC_MODE_CODE
)
366 else if(hw
.rec_mode
==LIRC_MODE_LIRCCODE
)
368 if(default_ioctl(LIRC_GET_LENGTH
, &hw
.code_length
)==-1)
370 logprintf(LOG_ERR
,"could not get code length");
371 logperror(LOG_ERR
,"default_init()");
375 if(hw
.code_length
>sizeof(ir_code
)*CHAR_BIT
)
377 logprintf(LOG_ERR
,"lircd can not handle %lu bit "
378 "codes",hw
.code_length
);
383 if(!(hw
.send_mode
|| hw
.rec_mode
))
388 if(min_freq
!=0 && max_freq
!=0)
390 (void) default_config_frequency();
396 int default_config(struct ir_remote
*remotes
)
398 get_frequency_range(remotes
,&min_freq
,&max_freq
);
401 return(default_config_frequency());
406 int default_deinit(void)
408 #if (!defined(SIM_SEND) || !defined(SIM_SEND)) || defined(DAEMONIZE)
418 static int write_send_buffer(int lirc
)
420 #if defined(SIM_SEND) && !defined(DAEMONIZE)
423 if(send_buffer
.wptr
==0)
425 LOGPRINTF(1,"nothing to send");
430 printf("pulse %lu\n",(unsigned long) send_buffer
.data
[i
++]);
431 if(i
>=send_buffer
.wptr
) break;
432 printf("space %lu\n",(unsigned long) send_buffer
.data
[i
++]);
434 return(send_buffer
.wptr
*sizeof(lirc_t
));
436 if(send_buffer
.wptr
==0)
438 LOGPRINTF(1,"nothing to send");
441 return(write(lirc
,send_buffer
.data
,
442 send_buffer
.wptr
*sizeof(lirc_t
)));
446 int default_send(struct ir_remote
*remote
,struct ir_ncode
*code
)
448 lirc_t remaining_gap
;
450 /* things are easy, because we only support one mode */
451 if(hw
.send_mode
!=LIRC_MODE_PULSE
)
454 #if !defined(SIM_SEND) || defined(DAEMONIZE)
455 if(hw
.features
&LIRC_CAN_SET_SEND_CARRIER
)
459 freq
=remote
->freq
==0 ? DEFAULT_FREQ
:remote
->freq
;
460 if(default_ioctl(LIRC_SET_SEND_CARRIER
, &freq
)==-1)
462 logprintf(LOG_ERR
,"could not set modulation "
464 logperror(LOG_ERR
,NULL
);
468 if(hw
.features
&LIRC_CAN_SET_SEND_DUTY_CYCLE
)
470 unsigned int duty_cycle
;
472 duty_cycle
=remote
->duty_cycle
==0 ? 50:remote
->duty_cycle
;
473 if(default_ioctl(LIRC_SET_SEND_DUTY_CYCLE
, &duty_cycle
)==-1)
475 logprintf(LOG_ERR
,"could not set duty cycle");
476 logperror(LOG_ERR
,NULL
);
481 remaining_gap
=remote
->min_remaining_gap
;
482 if(!init_send(remote
,code
)) return(0);
484 #if !defined(SIM_SEND) || defined(DAEMONIZE)
485 if(remote
->last_code
!=NULL
)
487 struct timeval current
;
490 gettimeofday(¤t
,NULL
);
491 usecs
=time_left(¤t
,&remote
->last_send
,remaining_gap
*2);
494 if(repeat_remote
==NULL
||
495 remote
!=repeat_remote
||
496 remote
->last_code
!=code
)
504 if(write_send_buffer(hw
.fd
)==-1)
506 logprintf(LOG_ERR
,"write failed");
507 logperror(LOG_ERR
,NULL
);
512 #if defined(SIM_SEND) && !defined(DAEMONIZE)
513 printf("space %lu\n",(unsigned long) remote
->min_remaining_gap
);
519 char *default_rec(struct ir_remote
*remotes
)
523 static char message
[PACKET_SIZE
+1];
526 if(hw
.rec_mode
==LIRC_MODE_STRING
)
530 /* inefficient but simple, fix this if you want */
534 if(read(hw
.fd
,&c
,1)!=1)
536 logprintf(LOG_ERR
,"reading in mode "
537 "LIRC_MODE_STRING failed");
549 if(failed
) return(NULL
);
554 if(!clear_rec_buffer()) return(NULL
);
555 return(decode_all(remotes
));
559 static int default_config_frequency()
563 if(!(hw
.features
&LIRC_CAN_SET_REC_CARRIER
))
567 if(hw
.features
&LIRC_CAN_SET_REC_CARRIER_RANGE
&&
570 if(default_ioctl(LIRC_SET_REC_CARRIER_RANGE
, &min_freq
)==-1)
572 logprintf(LOG_ERR
,"could not set receive carrier");
573 logperror(LOG_ERR
,"default_init()");
580 freq
=(min_freq
+max_freq
)/2;
582 if(default_ioctl(LIRC_SET_REC_CARRIER
, &freq
)==-1)
584 logprintf(LOG_ERR
,"could not set receive carrier");
585 logperror(LOG_ERR
,"default_init()");
591 int default_ioctl(unsigned int cmd
, void *arg
)
593 return ioctl(hw
.fd
, cmd
, arg
);