Added lirc.
[irreco.git] / lirc-0.8.4a / daemons / hw_ea65.c
blob21fb23c3ecbc8706fcb93137e252ae7c5ff33833
1 /*
2 * Support for builtin key panel and remote control on
3 * AOpen XC Cube EA65, EA65-II
4 *
5 * Copyright (C) 2004 Max Krasnyansky <maxk@qualcomm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * $Id: hw_ea65.c,v 5.4 2007/07/29 18:20:07 lirc Exp $
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <signal.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
39 #include "hardware.h"
40 #include "serial.h"
41 #include "ir_remote.h"
42 #include "lircd.h"
44 #include "hw_ea65.h"
46 #define TIMEOUT 60000
47 #define CODE_LENGTH 24
49 struct timeval start, end, last;
50 ir_code code;
52 struct hardware hw_ea65 =
54 LIRC_IRTTY, /* default device */
55 -1, /* fd */
56 LIRC_CAN_REC_LIRCCODE, /* features */
57 0, /* send_mode */
58 LIRC_MODE_LIRCCODE, /* rec_mode */
59 CODE_LENGTH, /* code_length */
60 ea65_init, /* init_func */
61 NULL, /* config_func */
62 ea65_release, /* deinit_func */
63 NULL, /* send_func */
64 ea65_receive, /* rec_func */
65 ea65_decode, /* decode_func */
66 NULL, /* ioctl_func */
67 NULL, /* readdata */
68 "ea65"
71 int ea65_decode(struct ir_remote *remote,
72 ir_code *prep,ir_code *codep,ir_code *postp,
73 int *repeat_flagp,
74 lirc_t *min_remaining_gapp,
75 lirc_t *max_remaining_gapp)
77 lirc_t d = 0;
79 if (!map_code(remote, prep, codep, postp,
80 0, 0, CODE_LENGTH, code, 0, 0))
81 return 0;
83 if (start.tv_sec - last.tv_sec >= 2) {
84 *repeat_flagp = 0;
85 } else {
86 d = (start.tv_sec - last.tv_sec) * 1000000 +
87 start.tv_usec - last.tv_usec;
88 if (d < 960000)
90 *repeat_flagp = 1;
92 else
94 *repeat_flagp = 0;
98 *min_remaining_gapp = 0;
99 *max_remaining_gapp = 0;
101 return 1;
104 int ea65_init(void)
106 logprintf(LOG_INFO, "EA65: device %s", hw.device);
108 if (!tty_create_lock(hw.device)) {
109 logprintf(LOG_ERR,"EA65: could not create lock files");
110 return 0;
113 hw.fd = open(hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY);
114 if (hw.fd < 0) {
115 logprintf(LOG_ERR,"EA65: could not open %s",hw.device);
116 tty_delete_lock();
117 return 0;
120 if (!tty_reset(hw.fd)) {
121 logprintf(LOG_ERR,"EA65: could not reset tty");
122 ea65_release();
123 return 0;
126 if (!tty_setbaud(hw.fd, 9600)) {
127 logprintf(LOG_ERR,"EA65: could not set baud rate");
128 ea65_release();
129 return 0;
132 return 1;
135 int ea65_release(void)
137 close(hw.fd);
138 tty_delete_lock();
140 return 1;
143 char *ea65_receive(struct ir_remote *remote)
145 uint8_t data[5];
146 int r;
148 last = end;
149 gettimeofday(&start, NULL);
151 if (!waitfordata(TIMEOUT)) {
152 logprintf(LOG_ERR, "EA65: timeout reading code data");
153 return NULL;
156 r = read(hw.fd, data, sizeof(data));
157 if (r < 4) {
158 logprintf(LOG_ERR, "EA65: read failed. %s(%d)",
159 strerror(r), r);
160 return NULL;
163 LOGPRINTF(1, "EA65: data(%d): %02x %02x %02x %02x %02x", r,
164 data[0], data[1], data[2], data[3], data[4]);
166 if (data[0] != 0xa0)
167 return NULL;
169 switch (data[1]) {
170 case 0x01:
171 if (r < 5)
172 return NULL;
173 code = (data[2] << 16) | (data[3] << 8) | data[4];
174 break;
176 case 0x04:
177 code = (0xff << 16) | (data[2] << 8) | data[3];
178 break;
180 logprintf(LOG_INFO, "EA65: receive code: %llx",
181 (unsigned long long) code);
183 gettimeofday(&end, NULL);
185 return decode_all(remote);