Added lirc.
[irreco.git] / lirc-0.8.4a / daemons / hw_usbx.c
blob3bbd1591d75353628543458866519ac039c17c39
1 /*****************************************************************************
2 ** hw_usbx.c ****************************************************************
3 *****************************************************************************
4 * Routines for the ADSTech USBX-707 USB IR Blaster
6 * Only receiving is implemented.
8 * It uses a baudrate of 300kps on a USB serial device which, currently, is
9 * only supported by Linux.
10 * If someone knows how to set such a baudrate under other OS's, please add
11 * that functionality to daemons/serial.c to make this driver work for those
12 * OS's.
14 * Information on how to send with this device is greatly appreciated...
16 * Copyright (C) 2007 Jelle Foks <jelle@foks.8m.com>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Library General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <limits.h>
42 #include <signal.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <errno.h>
47 #include <termios.h>
49 #include "hardware.h"
50 #include "receive.h"
51 #include "serial.h"
52 #include "ir_remote.h"
53 #include "lircd.h"
54 #include "hw_usbx.h"
56 static unsigned char b[6];
57 static ir_code code;
59 #define REPEAT_FLAG ((ir_code) 0x1)
60 #define CODE_LENGTH 48
62 struct hardware hw_usbx = {
63 LIRC_IRTTY, /* Default device */
64 -1, /* fd */
65 LIRC_CAN_REC_LIRCCODE, /* Features */
66 0, /* send_mode */
67 LIRC_MODE_LIRCCODE, /* rec_mode */
68 CODE_LENGTH, /* code_length */
69 usbx_init, /* init_func */
70 NULL, /* config_func */
71 usbx_deinit, /* deinit_func */
72 NULL, /* send_func */
73 usbx_rec, /* rec_func */
74 usbx_decode, /* decode_func */
75 NULL, /* ioctl_func */
76 NULL, /* readdata */
77 "usbx"
80 int usbx_decode (struct ir_remote *remote, ir_code *prep, ir_code *codep,
81 ir_code *postp, int *repeat_flagp,
82 lirc_t *min_remaining_gapp, lirc_t *max_remaining_gapp)
84 if( remote->flags&CONST_LENGTH ||
85 !map_code(remote, prep, codep, postp,
86 0, 0, CODE_LENGTH, code&(~REPEAT_FLAG), 0, 0))
88 return 0;
90 /* the lsb in the code is the repeat flag */
91 *repeat_flagp = code&REPEAT_FLAG ? 1:0;
92 *min_remaining_gapp=min_gap(remote);
93 *max_remaining_gapp=max_gap(remote);
95 LOGPRINTF(1, "repeat_flagp: %d",*repeat_flagp);
96 LOGPRINTF(1, "remote->gap range: %lu %lu\n",
97 (unsigned long) min_gap(remote),
98 (unsigned long) max_gap(remote));
99 LOGPRINTF(1,"rem: %lu %lu",
100 (unsigned long) remote->min_remaining_gap,
101 (unsigned long) remote->max_remaining_gap);
102 return 1;
105 int usbx_init(void)
107 if(!tty_create_lock(hw.device))
109 logprintf(LOG_ERR,"could not create lock files for '%s'",
110 hw.device);
111 return 0;
113 if ( (hw.fd = open (hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
115 tty_delete_lock ();
116 logprintf (LOG_ERR, "Could not open the '%s' device",
117 hw.device);
118 return 0;
120 LOGPRINTF(1, "device '%s' opened", hw.device);
122 if(!tty_reset(hw.fd) ||
123 !tty_setbaud(hw.fd, 300000) ||
124 !tty_setrtscts(hw.fd, 1))
126 logprintf(LOG_ERR,"could not configure the serial port for "
127 "'%s'", hw.device);
128 usbx_deinit();
129 return 0;
132 return 1;
135 int usbx_deinit (void)
137 close(hw.fd);
138 hw.fd = -1;
139 tty_delete_lock();
140 return 1;
143 char *usbx_rec (struct ir_remote *remotes)
145 char *m;
146 int i, x;
148 x = 0;
149 for (i = 0 ; i < 6; i++)
151 if (i > 0)
153 if (!waitfordata(20000))
155 LOGPRINTF(LOG_ERR,"timeout reading byte %d",i);
156 break;
159 if (read(hw.fd, &b[i], 1) != 1)
161 LOGPRINTF(LOG_ERR, "reading of byte %d failed.", i);
162 usbx_deinit();
163 return NULL;
165 LOGPRINTF(1, "byte %d: %02x", i, b[i]);
166 x++;
168 code = 0;
169 for ( i = 0 ; i < x ; i++ )
171 code = code << 8;
172 code |= ((ir_code) b[i]);
175 LOGPRINTF(1," -> %0llx",(unsigned long long) code);
177 m = decode_all(remotes);
178 return m;