File: ea65.c

package info (click to toggle)
lirc 0.10.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,364 kB
  • sloc: ansic: 26,717; cpp: 9,066; sh: 5,521; python: 4,362; makefile: 1,034; xml: 63
file content (186 lines) | stat: -rw-r--r-- 4,111 bytes parent folder | download | duplicates (2)
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
/*
 * Support for builtin key panel and remote control on
 *      AOpen XC Cube EA65, EA65-II
 *
 * Copyright (C) 2004 Max Krasnyansky <maxk@qualcomm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifndef LIRC_IRTTY
#define LIRC_IRTTY "/dev/ttyS1"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "lirc_driver.h"
#include "lirc/serial.h"

#define TIMEOUT     60000
#define CODE_LENGTH 24

static const logchannel_t logchannel = LOG_DRIVER;

struct timeval start, end, last;
ir_code code;

//Forwards:
static int ea65_decode(struct ir_remote* remote, struct decode_ctx_t* ctx);
static int ea65_init(void);
static int ea65_release(void);
static char* ea65_receive(struct ir_remote* remote);


const struct driver hw_ea65 = {
	.name		= "ea65",
	.device		= LIRC_IRTTY,
	.features	= LIRC_CAN_REC_LIRCCODE,
	.send_mode	= 0,
	.rec_mode	= LIRC_MODE_LIRCCODE,
	.code_length	= CODE_LENGTH,
	.init_func	= ea65_init,
	.deinit_func	= ea65_release,
	.open_func	= default_open,
	.close_func	= default_close,
	.send_func	= NULL,
	.rec_func	= ea65_receive,
	.decode_func	= ea65_decode,
	.drvctl_func	= NULL,
	.readdata	= NULL,
	.api_version	= 3,
	.driver_version = "0.10.0",
	.info		= "No info available",
	.device_hint    = "/dev/tty[0-9]*",
};

const struct driver* hardwares[] = { &hw_ea65, (const struct driver*)NULL };


int ea65_decode(struct ir_remote* remote, struct decode_ctx_t* ctx)
{
	lirc_t d = 0;

	if (!map_code(remote, ctx, 0, 0, CODE_LENGTH, code, 0, 0))
		return 0;

	if (start.tv_sec - last.tv_sec >= 2) {
		ctx->repeat_flag = 0;
	} else {
		d = (start.tv_sec - last.tv_sec) * 1000000 + start.tv_usec - last.tv_usec;
		if (d < 960000)
			ctx->repeat_flag = 1;
		else
			ctx->repeat_flag = 0;
	}

	ctx->min_remaining_gap = 0;
	ctx->max_remaining_gap = 0;

	return 1;
}

int ea65_init(void)
{
	log_info("EA65: device %s", drv.device);

	if (!tty_create_lock(drv.device)) {
		log_error("EA65: could not create lock files");
		return 0;
	}

	drv.fd = open(drv.device, O_RDWR | O_NONBLOCK | O_NOCTTY);
	if (drv.fd < 0) {
		log_error("EA65: could not open %s", drv.device);
		tty_delete_lock();
		return 0;
	}

	if (!tty_reset(drv.fd)) {
		log_error("EA65: could not reset tty");
		ea65_release();
		return 0;
	}

	if (!tty_setbaud(drv.fd, 9600)) {
		log_error("EA65: could not set baud rate");
		ea65_release();
		return 0;
	}

	return 1;
}

int ea65_release(void)
{
	close(drv.fd);
	tty_delete_lock();

	return 1;
}

char* ea65_receive(struct ir_remote* remote)
{
	uint8_t data[5];
	int r;

	last = end;
	gettimeofday(&start, NULL);

	if (!waitfordata(TIMEOUT)) {
		log_error("EA65: timeout reading code data");
		return NULL;
	}

	r = read(drv.fd, data, sizeof(data));
	if (r < 4) {
		log_error("EA65: read failed. %s(%d)", strerror(r), r);
		return NULL;
	}

	log_trace("EA65: data(%d): %02x %02x %02x %02x %02x", r, data[0], data[1], data[2], data[3], data[4]);

	if (data[0] != 0xa0)
		return NULL;

	switch (data[1]) {
	case 0x01:
		if (r < 5)
			return NULL;
		code = (data[2] << 16) | (data[3] << 8) | data[4];
		break;

	case 0x04:
		code = (0xff << 16) | (data[2] << 8) | data[3];
		break;
	}
	log_info("EA65: receive code: %llx", (uint64_t)code);

	gettimeofday(&end, NULL);

	return decode_all(remote);
}