Little fix.
[irreco.git] / lirc-0.8.4a / daemons / hw_uirt2.c
blob04a0575fd3dc897d3ff22b24f3f6b45d9a822fc0
1 /* $Id: hw_uirt2.c,v 5.4 2007/07/29 18:20:11 lirc Exp $ */
3 /****************************************************************************
4 ** hw_uirt2.c **************************************************************
5 ****************************************************************************
7 * routines for UIRT2 receiver using the UIR mode
8 *
9 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
10 * modified for logitech receiver by Isaac Lauer <inl101@alumni.psu.edu>
11 * modified for UIRT2 receiver by
12 * Mikael Magnusson <mikma@users.sourceforge.net>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <signal.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
44 #include "hardware.h"
45 #include "serial.h"
46 #include "ir_remote.h"
47 #include "lircd.h"
49 #define NUMBYTES 6
50 #define TIMEOUT 20000
52 static unsigned char b[NUMBYTES];
53 static struct timeval start,end,last;
54 static ir_code code;
57 static int uirt2_decode(struct ir_remote *remote,
58 ir_code *prep,ir_code *codep,ir_code *postp,
59 int *repeat_flagp,
60 lirc_t *min_remaining_gapp,
61 lirc_t *max_remaining_gapp);
62 static int uirt2_init(void);
63 static int uirt2_deinit(void);
64 static char *uirt2_rec(struct ir_remote *remotes);
66 struct hardware hw_uirt2=
68 LIRC_DRIVER_DEVICE, /* default device */
69 -1, /* fd */
70 LIRC_CAN_REC_LIRCCODE, /* features */
71 0, /* send_mode */
72 LIRC_MODE_LIRCCODE, /* rec_mode */
73 8*NUMBYTES, /* code_length */
74 uirt2_init, /* init_func */
75 NULL, /* config_func */
76 uirt2_deinit, /* deinit_func */
77 NULL, /* send_func */
78 uirt2_rec, /* rec_func */
79 uirt2_decode, /* decode_func */
80 NULL, /* ioctl_func */
81 NULL, /* readdata */
82 "uirt2"
86 static int uirt2_decode(struct ir_remote *remote,
87 ir_code *prep,ir_code *codep,ir_code *postp,
88 int *repeat_flagp,
89 lirc_t *min_remaining_gapp,
90 lirc_t *max_remaining_gapp)
92 if(!map_code(remote,prep,codep,postp,
93 0,0,8*NUMBYTES,code,0,0))
95 return(0);
98 map_gap(remote, &start, &last, 0, repeat_flagp,
99 min_remaining_gapp, max_remaining_gapp);
101 return(1);
105 static int uirt2_init(void)
107 if(!tty_create_lock(hw.device))
109 logprintf(LOG_ERR,"uirt2: could not create lock files");
110 return(0);
112 if((hw.fd=open(hw.device,O_RDWR|O_NONBLOCK|O_NOCTTY))<0)
114 logprintf(LOG_ERR,"uirt2: could not open %s",hw.device);
115 logperror(LOG_ERR,"uirt2: ");
116 tty_delete_lock();
117 return(0);
119 if(!tty_reset(hw.fd))
121 logprintf(LOG_ERR,"uirt2: could not reset tty");
122 uirt2_deinit();
123 return(0);
125 if(!tty_setbaud(hw.fd,115200))
127 logprintf(LOG_ERR,"uirt2: could not set baud rate");
128 uirt2_deinit();
129 return(0);
131 return(1);
135 static int uirt2_deinit(void)
137 close(hw.fd);
138 tty_delete_lock();
139 return(1);
142 static char *uirt2_rec(struct ir_remote *remotes)
144 char *m;
145 int i;
147 last=end;
148 gettimeofday(&start,NULL);
149 for(i=0;i<NUMBYTES;i++)
151 if(i>0)
153 if(!waitfordata(TIMEOUT))
155 logprintf(LOG_ERR,
156 "uirt2: timeout reading byte %d",i);
157 return(NULL);
160 if(read(hw.fd,&b[i],1)!=1)
162 logprintf(LOG_ERR,
163 "uirt2: reading of byte %d failed",i);
164 logperror(LOG_ERR,NULL);
165 return(NULL);
167 LOGPRINTF(1,"byte %d: %02x",i,b[i]);
169 gettimeofday(&end,NULL);
171 /* mark as Irman */
172 code=0xffff;
173 code<<=16;
175 code=((ir_code) b[0]);
176 code=code<<8;
177 code|=((ir_code) b[1]);
178 code=code<<8;
179 code|=((ir_code) b[2]);
180 code=code<<8;
181 code|=((ir_code) b[3]);
182 code=code<<8;
183 code|=((ir_code) b[4]);
184 code=code<<8;
185 code|=((ir_code) b[5]);
187 LOGPRINTF(1,"code: %llx",(unsigned long long) code);
189 m=decode_all(remotes);
190 return(m);