1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
/* $Id: hw_default.c,v 5.28 2004/11/20 11:43:35 lirc Exp $ */
/****************************************************************************
** hw_default.c ************************************************************
****************************************************************************
*
* routines for hardware that supports ioctl() interface
*
* Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
/* disable daemonise if maintainer mode SIM_REC / SIM_SEND defined */
#if defined(SIM_REC) || defined (SIM_SEND)
# undef DAEMONIZE
#endif
#include "hardware.h"
#include "ir_remote.h"
#include "lircd.h"
#include "receive.h"
#include "transmit.h"
#include "hw_default.h"
extern struct ir_remote *repeat_remote;
static unsigned long supported_send_modes[]=
{
/* LIRC_CAN_SEND_STRING, I don't think there ever will be a driver
that supports that */
/* LIRC_CAN_SEND_LIRCCODE, */
/* LIRC_CAN_SEND_CODE, */
/* LIRC_CAN_SEND_MODE2, this one would be very easy */
LIRC_CAN_SEND_PULSE,
/* LIRC_CAN_SEND_RAW, */
0
};
static unsigned long supported_rec_modes[]=
{
LIRC_CAN_REC_STRING,
LIRC_CAN_REC_LIRCCODE,
LIRC_CAN_REC_CODE,
LIRC_CAN_REC_MODE2,
/* LIRC_CAN_REC_PULSE, shouldn't be too hard */
/* LIRC_CAN_REC_RAW, */
0
};
struct hardware hw_default=
{
LIRC_DRIVER_DEVICE, /* default device */
-1, /* fd */
0, /* features */
0, /* send_mode */
0, /* rec_mode */
0, /* code_length */
default_init, /* init_func */
default_config, /* config_func */
default_deinit, /* deinit_func */
default_send, /* send_func */
default_rec, /* rec_func */
default_decode, /* decode_func */
default_readdata,
"default"
};
unsigned int min_freq=0,max_freq=0;
/**********************************************************************
*
* internal function prototypes
*
**********************************************************************/
static int default_config_frequency();
static int write_send_buffer(int lirc);
/**********************************************************************
*
* decode stuff
*
**********************************************************************/
lirc_t default_readdata(lirc_t timeout)
{
lirc_t data;
int ret;
if (!waitfordata((long) timeout))
return 0;
#if defined(SIM_REC) && !defined(DAEMONIZE)
while(1)
{
unsigned long scan;
ret=fscanf(stdin,"space %ld\n",&scan);
if(ret==1)
{
data=(lirc_t) scan;
break;
}
ret=fscanf(stdin,"pulse %ld\n",&scan);
if(ret==1)
{
data=(lirc_t) scan|PULSE_BIT;
break;
}
ret=fscanf(stdin,"%*s\n");
if(ret==EOF)
{
dosigterm(SIGTERM);
}
}
#else
ret=read(hw.fd,&data,sizeof(data));
if(ret!=sizeof(data))
{
LOGPRINTF(1,"error reading from lirc");
LOGPERROR(1,NULL);
dosigterm(SIGTERM);
}
#endif
return(data);
}
/*
interface functions
*/
int default_init()
{
#if defined(SIM_SEND) && !defined(DAEMONIZE)
hw.fd=STDOUT_FILENO;
hw.features=LIRC_CAN_SEND_PULSE;
hw.send_mode=LIRC_MODE_PULSE;
hw.rec_mode=0;
#elif defined(SIM_REC) && !defined(DAEMONIZE)
hw.fd=STDIN_FILENO;
hw.features=LIRC_CAN_REC_MODE2;
hw.send_mode=0;
hw.rec_mode=LIRC_MODE_MODE2;
#else
struct stat s;
int i;
/* FIXME: other modules might need this, too */
init_rec_buffer();
init_send_buffer();
if(stat(hw.device,&s)==-1)
{
logprintf(LOG_ERR,"could not get file information for %s",
hw.device);
logperror(LOG_ERR,"default_init()");
return(0);
}
/* file could be unix socket, fifo and native lirc device */
if(S_ISSOCK(s.st_mode))
{
struct sockaddr_un addr;
addr.sun_family=AF_UNIX;
strncpy(addr.sun_path,hw.device,sizeof(addr.sun_path));
hw.fd=socket(AF_UNIX,SOCK_STREAM,0);
if(hw.fd==-1)
{
logprintf(LOG_ERR,"could not create socket");
logperror(LOG_ERR,"default_init()");
return(0);
}
if(connect(hw.fd,(struct sockaddr *) &addr,sizeof(addr))==-1)
{
logprintf(LOG_ERR,"could not connect to unix socket %s",
hw.device);
logperror(LOG_ERR,"default_init()");
default_deinit();
close(hw.fd);
return(0);
}
LOGPRINTF(1,"using unix socket lirc device");
hw.features=LIRC_CAN_REC_MODE2 | LIRC_CAN_SEND_PULSE;
hw.rec_mode=LIRC_MODE_MODE2; /* this might change in future */
hw.send_mode=LIRC_MODE_PULSE;
return(1);
}
if((hw.fd=open(hw.device,O_RDWR))<0)
{
logprintf(LOG_ERR,"could not open %s",hw.device);
logperror(LOG_ERR,"default_init()");
return(0);
}
if(S_ISFIFO(s.st_mode))
{
LOGPRINTF(1,"using defaults for the Irman");
hw.features=LIRC_CAN_REC_MODE2;
hw.rec_mode=LIRC_MODE_MODE2; /* this might change in future */
return(1);
}
else if(!S_ISCHR(s.st_mode))
{
default_deinit();
logprintf(LOG_ERR,"%s is not a character device!!!",
hw.device);
logperror(LOG_ERR,"something went wrong during "
"installation");
return(0);
}
else if(ioctl(hw.fd,LIRC_GET_FEATURES,&hw.features)==-1)
{
logprintf(LOG_ERR,"could not get hardware features");
logprintf(LOG_ERR,"this device driver does not "
"support the new LIRC interface");
if(major(s.st_rdev) != LIRC_MAJOR)
{
logprintf(LOG_ERR, "major number of %s is %lu",
hw.device,
(unsigned long) major(s.st_rdev));
logprintf(LOG_ERR, "LIRC major number is %lu",
(unsigned long) LIRC_MAJOR);
logprintf(LOG_ERR, "check if %s is a LIRC device",
hw.device);
}
else
{
logprintf(LOG_ERR,"make sure you use a current "
"version of the driver");
}
default_deinit();
return(0);
}
# ifdef DEBUG
else
{
if(!(LIRC_CAN_SEND(hw.features) ||
LIRC_CAN_REC(hw.features)))
{
LOGPRINTF(1,"driver supports neither "
"sending nor receiving of IR signals");
}
if(LIRC_CAN_SEND(hw.features) && LIRC_CAN_REC(hw.features))
{
LOGPRINTF(1,"driver supports both sending and "
"receiving");
}
else if(LIRC_CAN_SEND(hw.features))
{
LOGPRINTF(1,"driver supports sending");
}
else if(LIRC_CAN_REC(hw.features))
{
LOGPRINTF(1,"driver supports receiving");
}
}
# endif
/* set send/receive method */
hw.send_mode=0;
if(LIRC_CAN_SEND(hw.features))
{
for(i=0;supported_send_modes[i]!=0;i++)
{
if(hw.features&supported_send_modes[i])
{
unsigned long mode;
mode=LIRC_SEND2MODE(supported_send_modes[i]);
if(ioctl(hw.fd,LIRC_SET_SEND_MODE,&mode)==-1)
{
logprintf(LOG_ERR,"could not set "
"send mode");
logperror(LOG_ERR,"default_init()");
default_deinit();
return(0);
}
hw.send_mode=LIRC_SEND2MODE
(supported_send_modes[i]);
break;
}
}
if(supported_send_modes[i]==0)
{
logprintf(LOG_NOTICE,"the send method of the "
"driver is not yet supported by lircd");
}
}
hw.rec_mode=0;
if(LIRC_CAN_REC(hw.features))
{
for(i=0;supported_rec_modes[i]!=0;i++)
{
if(hw.features&supported_rec_modes[i])
{
unsigned long mode;
mode=LIRC_REC2MODE(supported_rec_modes[i]);
if(ioctl(hw.fd,LIRC_SET_REC_MODE,&mode)==-1)
{
logprintf(LOG_ERR,"could not set "
"receive mode");
logperror(LOG_ERR,"default_init()");
return(0);
}
hw.rec_mode=LIRC_REC2MODE
(supported_rec_modes[i]);
break;
}
}
if(supported_rec_modes[i]==0)
{
logprintf(LOG_NOTICE,"the receive method of the "
"driver is not yet supported by lircd");
}
}
if(hw.rec_mode==LIRC_MODE_CODE)
{
hw.code_length=8;
}
else if(hw.rec_mode==LIRC_MODE_LIRCCODE)
{
if(ioctl(hw.fd,LIRC_GET_LENGTH,&hw.code_length)==-1)
{
logprintf(LOG_ERR,"could not get code length");
logperror(LOG_ERR,"default_init()");
default_deinit();
return(0);
}
if(hw.code_length>sizeof(ir_code)*CHAR_BIT)
{
logprintf(LOG_ERR,"lircd can not handle %lu bit "
"codes",hw.code_length);
default_deinit();
return(0);
}
}
if(!(hw.send_mode || hw.rec_mode))
{
default_deinit();
return(0);
}
if(min_freq!=0 && max_freq!=0)
{
(void) default_config_frequency(min_freq,max_freq);
}
#endif
return(1);
}
int default_config(struct ir_remote *remotes)
{
get_frequency_range(remotes,&min_freq,&max_freq);
if(hw.fd!=-1)
{
return(default_config_frequency());
}
return(1);
}
int default_deinit(void)
{
#if (!defined(SIM_SEND) || !defined(SIM_SEND)) || defined(DAEMONIZE)
close(hw.fd);
hw.fd=-1;
#endif
return(1);
}
static int write_send_buffer(int lirc)
{
#if defined(SIM_SEND) && !defined(DAEMONIZE)
int i;
if(send_buffer.wptr==0)
{
LOGPRINTF(1,"nothing to send");
return(0);
}
for(i=0;;)
{
printf("pulse %lu\n",(unsigned long) send_buffer.data[i++]);
if(i>=send_buffer.wptr) break;
printf("space %lu\n",(unsigned long) send_buffer.data[i++]);
}
return(send_buffer.wptr*sizeof(lirc_t));
#else
if(send_buffer.wptr==0)
{
LOGPRINTF(1,"nothing to send");
return(0);
}
return(write(lirc,send_buffer.data,
send_buffer.wptr*sizeof(lirc_t)));
#endif
}
int default_send(struct ir_remote *remote,struct ir_ncode *code)
{
lirc_t remaining_gap;
/* things are easy, because we only support one mode */
if(hw.send_mode!=LIRC_MODE_PULSE)
return(0);
#if !defined(SIM_SEND) || defined(DAEMONIZE)
if(hw.features&LIRC_CAN_SET_SEND_CARRIER)
{
unsigned int freq;
freq=remote->freq==0 ? DEFAULT_FREQ:remote->freq;
if(ioctl(hw.fd,LIRC_SET_SEND_CARRIER,&freq)==-1)
{
logprintf(LOG_ERR,"could not set modulation "
"frequency");
logperror(LOG_ERR,NULL);
return(0);
}
}
if(hw.features&LIRC_CAN_SET_SEND_DUTY_CYCLE)
{
unsigned int duty_cycle;
duty_cycle=remote->duty_cycle==0 ? 50:remote->duty_cycle;
if(ioctl(hw.fd,LIRC_SET_SEND_DUTY_CYCLE,&duty_cycle)==-1)
{
logprintf(LOG_ERR,"could not set duty cycle");
logperror(LOG_ERR,NULL);
return(0);
}
}
#endif
remaining_gap=remote->remaining_gap;
if(!init_send(remote,code)) return(0);
#if !defined(SIM_SEND) || defined(DAEMONIZE)
if(remote->last_code!=NULL)
{
struct timeval current;
unsigned long usecs;
gettimeofday(¤t,NULL);
usecs=time_left(¤t,&remote->last_send,remaining_gap*2);
if(usecs>0)
{
if(repeat_remote==NULL ||
remote!=repeat_remote ||
remote->last_code!=code)
{
usleep(usecs);
}
}
}
#endif
if(write_send_buffer(hw.fd)==-1)
{
logprintf(LOG_ERR,"write failed");
logperror(LOG_ERR,NULL);
return(0);
}
else
{
gettimeofday(&remote->last_send,NULL);
remote->last_code=code;
#if defined(SIM_SEND) && !defined(DAEMONIZE)
if(is_raw(remote) && is_const(remote))
{
int i;
for(i=0;remote->remaining_gap>code->signals[i] &&
i<code->length;i++)
{
remote->remaining_gap-=code->signals[i];
}
}
printf("space %lu\n",(unsigned long) remote->remaining_gap);
#endif
}
return(1);
}
char *default_rec(struct ir_remote *remotes)
{
char c;
int n;
static char message[PACKET_SIZE+1];
if(hw.rec_mode==LIRC_MODE_STRING)
{
int failed=0;
/* inefficient but simple, fix this if you want */
n=0;
do
{
if(read(hw.fd,&c,1)!=1)
{
logprintf(LOG_ERR,"reading in mode "
"LIRC_MODE_STRING failed");
return(NULL);
}
if(n>=PACKET_SIZE-1)
{
failed=1;
n=0;
}
message[n++]=c;
}
while(c!='\n');
message[n]=0;
if(failed) return(NULL);
return(message);
}
else
{
if(!clear_rec_buffer()) return(NULL);
return(decode_all(remotes));
}
}
int default_decode(struct ir_remote *remote,
ir_code *prep,ir_code *codep,ir_code *postp,
int *repeat_flagp,lirc_t *remaining_gapp)
{
return(receive_decode(remote,prep,codep,postp,
repeat_flagp,remaining_gapp));
}
static int default_config_frequency()
{
unsigned int freq;
if(!(hw.features&LIRC_CAN_SET_REC_CARRIER))
{
return(1);
}
if(hw.features&LIRC_CAN_SET_REC_CARRIER_RANGE &&
min_freq!=max_freq)
{
if(ioctl(hw.fd,LIRC_SET_REC_CARRIER_RANGE,&min_freq)==-1)
{
logprintf(LOG_ERR,"could not set receive carrier");
logperror(LOG_ERR,"default_init()");
return(0);
}
freq=max_freq;
}
else
{
freq=(min_freq+max_freq)/2;
}
if(ioctl(hw.fd,LIRC_SET_REC_CARRIER,&freq)==-1)
{
logprintf(LOG_ERR,"could not set receive carrier");
logperror(LOG_ERR,"default_init()");
return(0);
}
return(1);
}
|