File: irlink.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 (311 lines) | stat: -rw-r--r-- 7,559 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
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
/*****************************************************************************
** hw_irlink.c **************************************************************
*****************************************************************************
* Routines for the IRLink VS Infrared receiver
*
* Copyright (C) 2007 Maxim Muratov <mumg at mail.ru>
*
*  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/ttyUSB0"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <errno.h>
#include <termios.h>

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

//Forwards:
static int irlink_init(void);
static int irlink_deinit(void);
static char* irlink_rec(struct ir_remote* remotes);
static lirc_t irlink_readdata(lirc_t timeout);
static int drvctl_func(unsigned int cmd, void* arg);


const struct driver hw_irlink = {
	.name		= "irlink",
	.device		= LIRC_IRTTY,
	.features	= LIRC_CAN_REC_MODE2,
	.send_mode	= 0,
	.rec_mode	= LIRC_MODE_MODE2,
	.code_length	= 0,
	.init_func	= irlink_init,
	.deinit_func	= irlink_deinit,
	.open_func	= default_open,
	.close_func	= default_close,
	.send_func	= NULL,
	.rec_func	= irlink_rec,
	.decode_func	= receive_decode,
	.drvctl_func	= drvctl_func,
	.readdata	= irlink_readdata,
	.api_version	= 3,
	.driver_version = "0.10.0",
	.info		= "No info available",
	.device_hint    = "drvctl",
};

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

typedef enum {
	BLOCK,
	NON_BLOCK
} IRLINK_READ_MODE_T;

#define IS_IRLINK_LONG_PULSE(code) (code == 0xFF)
#define IS_IRLINK_LONG_PAUSE(code) (code == 0xFE)

#define IS_IRLINK_PULSE(code) (code & 0x01)

#define IS_IRLINK_TIMESCALE_3600(code)  ((code & 0x80) > 0)
#define IS_IRLINK_TIMESCALE_14400(code) ((code & 0x80) == 0)

#define IRLINK_DETECT_CMD 0x81

#define IRLINK_PERIOD(value, timescale) \
	(((1000000 * ((unsigned int)value)) / timescale) & PULSE_MASK)

static const logchannel_t logchannel = LOG_DRIVER;

static int is_long_pause = 0;
static int is_long_pulse = 0;
static struct timeval last_time = { 0 };

static unsigned char pulse = 1;
static lirc_t last_code = 0;

static int drvctl_func(unsigned int cmd, void* arg)
{
	switch (cmd) {
	case DRVCTL_GET_DEVICES:
		return drv_enum_glob((glob_t*) arg, "/dev/ttyUSB*");
	case DRVCTL_FREE_DEVICES:
		drv_enum_free((glob_t*) arg);
		return 0;
	default:
		return DRV_ERR_NOT_IMPLEMENTED;
	}
}


static int irlink_close(const int port)
{
	if (port != -1) {
		tty_delete_lock();
		close(port);
		return 0;
	}
	return -1;
}

static int irlink_open(const char* portName)
{
	int port = -1;

	if (!tty_create_lock((char*)portName)) {
		log_error("could not create lock files");
		return -1;
	}
	port = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);
	if (port < 0) {
		log_error("could not open %s", drv.device);
		tty_delete_lock();
		return -1;
	}
	if (tty_reset(port) < 0 || tty_setbaud(port, 115200) < 0 || tty_setcsize(port, 8) < 0
	    || tty_setrtscts(port, 0) < 0) {
		irlink_close(port);
		port = -1;
	}
	return port;
}

static int irlink_read(const int port, unsigned char* buffer, int bufferSize)
{
	if (port == -1)
		return -1;
	return read(port, buffer, bufferSize);
}

static int irlink_write(const int port, unsigned char* buffer, int bufferSize)
{
	if (port == -1)
		return -1;
	return write(port, buffer, bufferSize);
}

static void irlink_read_flush(const int port)
{
	struct pollfd pfd = {.fd = port, .events = POLLIN, .revents = 0};
	lirc_t data = 0;

	/* FIXME: This loop makes no sense. */
	for (;; ) {
		if (curl_poll(&pfd, 1, 0) > 0) {
			if (read(port, &data, sizeof(data)) <= 0)
				break;
		} else {
			break;
		}
	}
}

static int irlink_detect(const int port)
{
	unsigned char detect_cmd[] = { IRLINK_DETECT_CMD };

	if (port == -1)
		return -1;
	irlink_read_flush(port);
	if (irlink_write(port, detect_cmd, sizeof(detect_cmd)) == sizeof(detect_cmd)) {
		unsigned char detect_response = 0;

		if (waitfordata(500000)
		    && irlink_read(port, &detect_response, sizeof(detect_response)) == sizeof(detect_response)) {
			if (detect_response == IRLINK_DETECT_CMD)
				return 0;
		}
	}
	return -1;
}

char* irlink_rec(struct ir_remote* remotes)
{
	if (!rec_buffer_clear())
		return NULL;
	return decode_all(remotes);
}

lirc_t irlink_readdata(lirc_t timeout)
{
	lirc_t data = 0;
	unsigned char rd_value = 0;
	struct timeval start_time = { 0 };

	gettimeofday(&start_time, NULL);
	lirc_t time_delta = 0;

	for (;; ) {
		if (last_code != 0) {
			data = last_code;
			last_code = 0;
			break;
		}
		if (timeout < time_delta) {
			log_error("timeout < time_delta");
			break;
		}
		if (!waitfordata(timeout - time_delta))
			break;
		if (irlink_read(drv.fd, &rd_value, sizeof(rd_value)) == sizeof(rd_value)) {
			if (IS_IRLINK_LONG_PULSE(rd_value) || IS_IRLINK_LONG_PAUSE(rd_value)) {
				struct timeval diff_time = { 0 };

				is_long_pulse = IS_IRLINK_LONG_PULSE(rd_value);
				is_long_pause = IS_IRLINK_LONG_PAUSE(rd_value);
				gettimeofday(&last_time, NULL);
				timersub(&last_time, &start_time, &diff_time);
				time_delta = diff_time.tv_sec * 1000000 + diff_time.tv_usec;
				continue;
			} else {
				lirc_t* code_ptr = &data;

				if (is_long_pulse != 0 || is_long_pause != 0) {
					struct timeval curr_time;
					struct timeval diff_time;

					gettimeofday(&curr_time, NULL);
					timersub(&curr_time, &last_time, &diff_time);

					if (diff_time.tv_sec >= 16)
						data = PULSE_MASK;
					else
						data = diff_time.tv_sec * 1000000 + diff_time.tv_usec;
					if (is_long_pause) {
						is_long_pause = 0;
						pulse = 1;
						data &= ~PULSE_BIT;
					}
					if (is_long_pulse) {
						pulse = 0;
						is_long_pulse = 0;
						data |= PULSE_BIT;
					}
					code_ptr = &last_code;
				}
				if (IS_IRLINK_TIMESCALE_3600(rd_value)) {
					rd_value = (rd_value & 0x7F) >> 1;
					*code_ptr = IRLINK_PERIOD(rd_value, 3600);
				} else if (IS_IRLINK_TIMESCALE_14400(rd_value)) {
					rd_value = (rd_value & 0x7F) >> 1;
					*code_ptr = IRLINK_PERIOD(rd_value, 14400);
				}
				if (pulse == 0)
					*code_ptr &= ~PULSE_BIT;
				else
					*code_ptr |= PULSE_BIT;
				pulse = !pulse;
				break;
			}
		} else {
			log_error("error reading from %s", drv.device);
			log_perror_err(NULL);
			irlink_deinit();
		}
	}
	return data;
}

int irlink_init(void)
{
	drv.fd = irlink_open(drv.device);
	if (drv.fd < 0) {
		log_error("Could not open the '%s' device", drv.device);
	} else {
		if (irlink_detect(drv.fd) == 0) {
			return 1;
		}
		log_error("Failed to detect IRLink on '%s' device", drv.device);
		irlink_deinit();
	}
	return 0;
}

int irlink_deinit(void)
{
	if (drv.fd != -1)
		irlink_close(drv.fd);
	drv.fd = -1;
	return 1;
}