Little fix.
[irreco.git] / lirc-0.8.4a / daemons / hw_default.c
bloba6302e84172695e9a9b4f60e01f830c452898aad
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
8 *
9 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
29 /* disable daemonise if maintainer mode SIM_REC / SIM_SEND defined */
30 #if defined(SIM_REC) || defined (SIM_SEND)
31 # undef DAEMONIZE
32 #endif
34 #include "hardware.h"
35 #include "ir_remote.h"
36 #include "lircd.h"
37 #include "receive.h"
38 #include "transmit.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
46 that supports that */
47 /* LIRC_CAN_SEND_LIRCCODE, */
48 /* LIRC_CAN_SEND_CODE, */
49 /* LIRC_CAN_SEND_MODE2, this one would be very easy */
50 LIRC_CAN_SEND_PULSE,
51 /* LIRC_CAN_SEND_RAW, */
54 static unsigned long supported_rec_modes[]=
56 LIRC_CAN_REC_STRING,
57 LIRC_CAN_REC_LIRCCODE,
58 LIRC_CAN_REC_CODE,
59 LIRC_CAN_REC_MODE2,
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 */
68 -1, /* fd */
69 0, /* features */
70 0, /* send_mode */
71 0, /* rec_mode */
72 0, /* code_length */
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 */
80 default_readdata,
81 "default"
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 /**********************************************************************
97 * decode stuff
99 **********************************************************************/
101 lirc_t default_readdata(lirc_t timeout)
103 lirc_t data;
104 int ret;
106 if (!waitfordata((long) timeout))
107 return 0;
109 #if defined(SIM_REC) && !defined(DAEMONIZE)
110 while(1)
112 unsigned long scan;
114 ret=fscanf(stdin,"space %ld\n",&scan);
115 if(ret==1)
117 data=(lirc_t) scan;
118 break;
120 ret=fscanf(stdin,"pulse %ld\n",&scan);
121 if(ret==1)
123 data=(lirc_t) scan|PULSE_BIT;
124 break;
126 ret=fscanf(stdin,"%*s\n");
127 if(ret==EOF)
129 dosigterm(SIGTERM);
132 #else
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);
138 default_deinit();
139 return 0;
141 if(data == 0)
143 static int data_warning = 1;
145 if(data_warning)
147 logprintf(LOG_WARNING,
148 "read invalid data from device %s",
149 hw.device);
150 data_warning = 0;
152 data = 1;
154 #endif
155 return(data);
159 interface functions
162 int default_init()
164 #if defined(SIM_SEND) && !defined(DAEMONIZE)
165 hw.fd=STDOUT_FILENO;
166 hw.features=LIRC_CAN_SEND_PULSE;
167 hw.send_mode=LIRC_MODE_PULSE;
168 hw.rec_mode=0;
169 #elif defined(SIM_REC) && !defined(DAEMONIZE)
170 hw.fd=STDIN_FILENO;
171 hw.features=LIRC_CAN_REC_MODE2;
172 hw.send_mode=0;
173 hw.rec_mode=LIRC_MODE_MODE2;
174 #else
175 struct stat s;
176 int i;
178 /* FIXME: other modules might need this, too */
179 init_rec_buffer();
180 init_send_buffer();
182 if(stat(hw.device,&s)==-1)
184 logprintf(LOG_ERR,"could not get file information for %s",
185 hw.device);
186 logperror(LOG_ERR,"default_init()");
187 return(0);
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);
198 if(hw.fd==-1)
200 logprintf(LOG_ERR,"could not create socket");
201 logperror(LOG_ERR,"default_init()");
202 return(0);
205 if(connect(hw.fd,(struct sockaddr *) &addr,sizeof(addr))==-1)
207 logprintf(LOG_ERR,"could not connect to unix socket %s",
208 hw.device);
209 logperror(LOG_ERR,"default_init()");
210 default_deinit();
211 close(hw.fd);
212 return(0);
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;
219 return(1);
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()");
226 return(0);
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 */
233 return(1);
235 else if(!S_ISCHR(s.st_mode))
237 default_deinit();
238 logprintf(LOG_ERR,"%s is not a character device!!!",
239 hw.device);
240 logperror(LOG_ERR,"something went wrong during "
241 "installation");
242 return(0);
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",
252 hw.device,
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",
257 hw.device);
259 else
261 logprintf(LOG_ERR,"make sure you use a current "
262 "version of the driver");
264 default_deinit();
265 return(0);
267 # ifdef DEBUG
268 else
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 "
279 "receiving");
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");
290 # endif
292 /* set send/receive method */
293 hw.send_mode=0;
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])
300 unsigned long mode;
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 "
306 "send mode");
307 logperror(LOG_ERR,"default_init()");
308 default_deinit();
309 return(0);
311 hw.send_mode=LIRC_SEND2MODE
312 (supported_send_modes[i]);
313 break;
316 if(supported_send_modes[i]==0)
318 logprintf(LOG_NOTICE,"the send method of the "
319 "driver is not yet supported by lircd");
322 hw.rec_mode=0;
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])
329 unsigned long mode;
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 "
335 "receive mode");
336 logperror(LOG_ERR,"default_init()");
337 return(0);
339 hw.rec_mode=LIRC_REC2MODE
340 (supported_rec_modes[i]);
341 break;
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)
352 /* get resolution */
353 hw.resolution=0;
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",
358 hw.resolution);
362 else if(hw.rec_mode==LIRC_MODE_CODE)
364 hw.code_length=8;
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()");
372 default_deinit();
373 return(0);
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);
379 default_deinit();
380 return(0);
383 if(!(hw.send_mode || hw.rec_mode))
385 default_deinit();
386 return(0);
388 if(min_freq!=0 && max_freq!=0)
390 (void) default_config_frequency();
392 #endif
393 return(1);
396 int default_config(struct ir_remote *remotes)
398 get_frequency_range(remotes,&min_freq,&max_freq);
399 if(hw.fd!=-1)
401 return(default_config_frequency());
403 return(1);
406 int default_deinit(void)
408 #if (!defined(SIM_SEND) || !defined(SIM_SEND)) || defined(DAEMONIZE)
409 if(hw.fd != -1)
411 close(hw.fd);
412 hw.fd=-1;
414 #endif
415 return(1);
418 static int write_send_buffer(int lirc)
420 #if defined(SIM_SEND) && !defined(DAEMONIZE)
421 int i;
423 if(send_buffer.wptr==0)
425 LOGPRINTF(1,"nothing to send");
426 return(0);
428 for(i=0;;)
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));
435 #else
436 if(send_buffer.wptr==0)
438 LOGPRINTF(1,"nothing to send");
439 return(0);
441 return(write(lirc,send_buffer.data,
442 send_buffer.wptr*sizeof(lirc_t)));
443 #endif
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)
452 return(0);
454 #if !defined(SIM_SEND) || defined(DAEMONIZE)
455 if(hw.features&LIRC_CAN_SET_SEND_CARRIER)
457 unsigned int freq;
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 "
463 "frequency");
464 logperror(LOG_ERR,NULL);
465 return(0);
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);
477 return(0);
480 #endif
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;
488 unsigned long usecs;
490 gettimeofday(&current,NULL);
491 usecs=time_left(&current,&remote->last_send,remaining_gap*2);
492 if(usecs>0)
494 if(repeat_remote==NULL ||
495 remote!=repeat_remote ||
496 remote->last_code!=code)
498 usleep(usecs);
502 #endif
504 if(write_send_buffer(hw.fd)==-1)
506 logprintf(LOG_ERR,"write failed");
507 logperror(LOG_ERR,NULL);
508 return(0);
510 else
512 #if defined(SIM_SEND) && !defined(DAEMONIZE)
513 printf("space %lu\n",(unsigned long) remote->min_remaining_gap);
514 #endif
516 return(1);
519 char *default_rec(struct ir_remote *remotes)
521 char c;
522 int n;
523 static char message[PACKET_SIZE+1];
526 if(hw.rec_mode==LIRC_MODE_STRING)
528 int failed=0;
530 /* inefficient but simple, fix this if you want */
531 n=0;
534 if(read(hw.fd,&c,1)!=1)
536 logprintf(LOG_ERR,"reading in mode "
537 "LIRC_MODE_STRING failed");
538 return(NULL);
540 if(n>=PACKET_SIZE-1)
542 failed=1;
543 n=0;
545 message[n++]=c;
547 while(c!='\n');
548 message[n]=0;
549 if(failed) return(NULL);
550 return(message);
552 else
554 if(!clear_rec_buffer()) return(NULL);
555 return(decode_all(remotes));
559 static int default_config_frequency()
561 unsigned int freq;
563 if(!(hw.features&LIRC_CAN_SET_REC_CARRIER))
565 return(1);
567 if(hw.features&LIRC_CAN_SET_REC_CARRIER_RANGE &&
568 min_freq!=max_freq)
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()");
574 return(0);
576 freq=max_freq;
578 else
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()");
586 return(0);
588 return(1);
591 int default_ioctl(unsigned int cmd, void *arg)
593 return ioctl(hw.fd, cmd, arg);