File: default.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 (547 lines) | stat: -rw-r--r-- 13,502 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/****************************************************************************
** hw_default.c ************************************************************
****************************************************************************
*
* routines for hardware that supports ioctl() interface
*
* Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
*
*/

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

#ifdef HAVE_LIBUDEV_H
#include <libudev.h>
#endif

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>

#ifdef HAVE_KERNEL_LIRC_H
#include <linux/lirc.h>
#else
#include "media/lirc.h"
#endif

#include "lirc_driver.h"



static const logchannel_t logchannel = LOG_DRIVER;

static uint32_t supported_send_modes[] = {
	/* LIRC_CAN_SEND_LIRCCODE, */
	/* LIRC_CAN_SEND_MODE2, this one would be very easy */
	LIRC_CAN_SEND_PULSE,
	/* LIRC_CAN_SEND_RAW, */
	0
};

static uint32_t supported_rec_modes[] = {
	LIRC_CAN_REC_LIRCCODE,
	LIRC_CAN_REC_MODE2,
	/* LIRC_CAN_REC_PULSE, shouldn't be too hard */
	/* LIRC_CAN_REC_RAW, */
	0
};

//Forwards:
static int default_init(void);
static int default_deinit(void);
static int default_send(struct ir_remote* remote, struct ir_ncode* code);
static char* default_rec(struct ir_remote* remotes);
static int default_ioctl(unsigned int cmd, void* arg);
static lirc_t default_readdata(lirc_t timeout);
static int my_open(const char* path);
static int drvctl(unsigned int cmd, void* arg);



static const struct driver hw_default = {
	.name		= "default",
	.device		= LIRC_DRIVER_DEVICE,
	.features	= 0,
	.send_mode	= 0,
	.rec_mode	= 0,
	.code_length	= 0,
	.init_func	= default_init,
	.deinit_func	= default_deinit,
	.open_func	= my_open,
	.close_func	= default_close,
	.send_func	= default_send,
	.rec_func	= default_rec,
	.decode_func	= receive_decode,
	.drvctl_func	= drvctl,
	.readdata	= default_readdata,
	.api_version	= 3,
	.driver_version = "0.10.0",
	.info		= "See file://" PLUGINDOCS "/default.html",
	.device_hint    = "drvctl",
};


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


/**********************************************************************
*
* internal function prototypes
*
**********************************************************************/

static int write_send_buffer(int lirc);

/***************************************************
*
* /sys/class/rc  stuff.
*
****************************************************/

static int is_rc(const char* s)
{
	if (s == NULL)
		return -1;
	return s[0] == 'r' && s[1] == 'c' && s[2] >= '0' && s[2] <= '9';
}


/*
 * Given a directory in /sys/class/rc, check if it contains
 * a file called device. If so, write "lirc" to the protocols
 * file in same directory unless the 'lirc' protocol already
 * is enabled.
 *
 * rc_dir: directory specification like rc0, rc1, etc.
 * device: Device given to lirc,  like 'lirc0' (or /dev/lirc0).
 * Returns: 0 if OK, else -1.
 *
 */
static int visit_rc(const char* rc_dir, const char* device)
{
	char path[512];
	char buff[128];
	char* enabled = NULL;
	int fd;
	int r;

	snprintf(path, sizeof(path), "/sys/class/rc/%s", rc_dir);
	if (access(path, F_OK) != 0) {
		log_notice("Cannot open rc directory: %s", path);
		return -1;
	}
	snprintf(path, sizeof(path), "/sys/class/rc/%s/%s", rc_dir, device);
	if (access(path, F_OK) != 0) {
		log_debug("No device found: %s", path);
		return -1;
	}
	snprintf(path, sizeof(path), "/sys/class/rc/%s/protocols", rc_dir);
	fd = open(path, O_RDONLY);
	if (fd < 0) {
		log_debug(
			  "Cannot open protocol file: %s for read", path);
		return -1;
	}
	r = read(fd, buff, sizeof(buff));
	if (r < 0) {
		log_debug("Cannot read from %s", path);
		return -1;
	}
	if (strchr(buff, '[') != NULL) {
		enabled = strchr(buff, '[') + 1;
		if (strchr(buff, ']') != NULL)
			*strchr(buff, ']') = '\0';
		else
			enabled = NULL;
	}
	if (enabled == NULL) {
		log_warn("Cannot parse protocols %s", buff);
	} else  if (strcmp(enabled, "lirc") == 0) {
		log_info("[lirc] protocol is enabled");
		return 0;
	}
	fd = open(path, O_WRONLY);
	if (fd < 0) {
		log_debug(
			  "Cannot open protocol file for write: %s", path);
		return -1;
	}
	chk_write(fd, "lirc\n", 5);
	log_notice("'lirc' written to protocols file %s", path);
	close(fd);
	return 0;
}

/*
 * Try to set the 'lirc' protocol for the device  we are using. Returns
 * 0 on success for at least one device, otherwise -1.
 */
static int set_rc_protocol(const char* device)
{
	struct dirent* ent;
	DIR* dir;
	int r = -1;

	if (strrchr(device, '/') != NULL)
		device = strrchr(device, '/') + 1;
	dir = opendir("/sys/class/rc");
	if (dir == NULL) {
		log_notice("Cannot open /sys/class/rc\n");
		return -1;
	}
	while ((ent = readdir(dir)) != NULL) {
		if (!is_rc(ent->d_name))
			continue;
		if (visit_rc(ent->d_name, device) == 0)
			r = 0;
	}
	closedir(dir);
	return r;
}


/** Log diagnostics if device cannot be accessed. */
static void check_device(const char* device)
{
	if (device == NULL) {
		log_error("default driver: NULL device");
	} else {
		if (access(device, R_OK | W_OK) != 0)
			log_warn("Cannot access device: %s", drv.device);
	}
}

/** Try to use an "auto" device specification. */
static int get_auto_device(char* pathbuf, size_t size)
{
	const char* const PATTERN = "/dev/lirc*";
	glob_t globbuf;

	globbuf.gl_offs = 10;
	glob(PATTERN, 0, NULL, &globbuf);
	if (globbuf.gl_pathc == 0) {
		log_error("No matching /dev/lirc device found for \"auto\"");
		return  ENODEV;
	}
	if (globbuf.gl_pathc > 1) {
		log_warn("Multiple /dev/lirc devices found for \"auto\"");
	}
	strncpy(pathbuf, globbuf.gl_pathv[0], size - 1);
	globfree(&globbuf);
	return 0;
}


int my_open(const char* path)
{
	char pathbuf[128];
	int r;

	strncpy(pathbuf, path, sizeof(pathbuf) - 1);
	if (strcmp(pathbuf, "auto") == 0) {
		r = get_auto_device(pathbuf, sizeof(pathbuf));
		if (r != 0)
			return r;
	}
	default_open(pathbuf);
	check_device(drv.device);
	set_rc_protocol(drv.device);
	return 0;
}


/**********************************************************************
*
* decode stuff
*
**********************************************************************/
int default_readdata(lirc_t timeout)
{
	int data, ret;
	static int last_space = (lirc_t) 0;

	if (!waitfordata((long)timeout))
		return 0;

	ret = read(drv.fd, &data, sizeof(data));
	if (ret != sizeof(data)) {
		log_perror_err("error reading from %s (ret %d, expected %d)",
			  drv.device, ret, sizeof(data));
		default_deinit();

		return 0;
	}
	if (last_space == LIRC_SPACE(LIRC_VALUE_MASK) && LIRC_IS_SPACE(data)) {
		/* Work around #172. */
		if (!waitfordata((long)timeout))
			return 0;

		ret = read(drv.fd, &data, sizeof(data));
		if (ret != sizeof(data)) {
			log_perror_err(
				"error reading from %s (got %d, expected %d)",
				drv.device, ret, sizeof(data));
			default_deinit();
			return 0;
		}
	}
	if (data == 0) {
		static int data_warning = 1;

		if (data_warning) {
			log_warn("read invalid data from device %s",
				  drv.device);
			data_warning = 0;
		}
		data = 1;
	}
	last_space = data;
	return data;
}

/*
 * interface functions
 */
int default_init(void)
{
	struct stat s;
	int i;

	/* FIXME: other modules might need this, too */
	rec_buffer_init();
	send_buffer_init();

	if (set_rc_protocol(drv.device) != 0)
		log_info("Cannot configure the rc device for %s",
			  drv.device);

	if (stat(drv.device, &s) == -1) {
		log_error("could not get file information for %s", drv.device);
		log_perror_err("default_init()");
		return 0;
	}

	/* file could be unix socket, fifo and native lirc device */
	if (S_ISSOCK(s.st_mode)) {
		struct sockaddr_un addr;

		addr.sun_family = AF_UNIX;
		strncpy(addr.sun_path, drv.device, sizeof(addr.sun_path) - 1);

		drv.fd = socket(AF_UNIX, SOCK_STREAM, 0);
		if (drv.fd == -1) {
			log_error("could not create socket");
			log_perror_err("default_init()");
			return 0;
		}

		if (connect(drv.fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
			log_error("could not connect to unix socket %s", drv.device);
			log_perror_err("default_init()");
			default_deinit();
			close(drv.fd);
			return 0;
		}

		log_trace("using unix socket lirc device");
		drv.features = LIRC_CAN_REC_MODE2 | LIRC_CAN_SEND_PULSE;
		drv.rec_mode = LIRC_MODE_MODE2; /* this might change in future */
		drv.send_mode = LIRC_MODE_PULSE;
		return 1;
	}
	drv.fd = open(drv.device, O_RDWR);
	if (drv.fd < 0) {
		log_error("could not open %s", drv.device);
		log_perror_err("default_init()");
		return 0;
	}
	if (S_ISFIFO(s.st_mode)) {
		log_trace("using defaults for the Irman");
		drv.features = LIRC_CAN_REC_MODE2;
		drv.rec_mode = LIRC_MODE_MODE2; /* this might change in future */
		return 1;
	} else if (!S_ISCHR(s.st_mode)) {
		default_deinit();
		log_error("%s is not a character device!!!", drv.device);
		log_perror_err("something went wrong during installation");
		return 0;
	} else if (default_ioctl(LIRC_GET_FEATURES, &drv.features) == -1) {
		log_error("could not get hardware features");
		log_error("this device driver does not support the LIRC ioctl interface");
		if (major(s.st_rdev) == 13) {
			log_error("did you mean to use the devinput driver instead of the %s driver?",
				  drv.name);
		} else {
			log_error("major number of %s is %lu", drv.device, (uint32_t)major(s.st_rdev));
			log_error("make sure %s is a LIRC device and use a current version of the driver",
				  drv.device);
		}
		default_deinit();
		return 0;
	}
	if (!(LIRC_CAN_SEND(drv.features) || LIRC_CAN_REC(drv.features)))
		log_trace("driver supports neither sending nor receiving of IR signals");
	if (LIRC_CAN_SEND(drv.features) && LIRC_CAN_REC(drv.features)) {
		log_trace("driver supports both sending and receiving");
	} else if (LIRC_CAN_SEND(drv.features)) {
		log_trace("driver supports sending");
	} else if (LIRC_CAN_REC(drv.features)) {
		log_trace("driver supports receiving");
	}

	/* set send/receive method */
	drv.send_mode = 0;
	if (LIRC_CAN_SEND(drv.features)) {
		for (i = 0; supported_send_modes[i] != 0; i++) {
			if (LIRC_CAN_SEND(drv.features) == supported_send_modes[i]) {
				drv.send_mode = LIRC_SEND2MODE(supported_send_modes[i]);
				break;
			}
		}
		if (supported_send_modes[i] == 0)
			log_notice("the send method of the driver is not yet supported by lircd");
	}
	drv.rec_mode = 0;
	if (LIRC_CAN_REC(drv.features)) {
		for (i = 0; supported_rec_modes[i] != 0; i++) {
			if (LIRC_CAN_REC(drv.features) == supported_rec_modes[i]) {
				drv.rec_mode = LIRC_REC2MODE(supported_rec_modes[i]);
				break;
			}
		}
		if (supported_rec_modes[i] == 0)
			log_notice("the receive method of the driver is not yet supported by lircd");
	}
	if (drv.rec_mode == LIRC_MODE_MODE2) {
		/* get resolution */
		drv.resolution = 0;
		if ((drv.features & LIRC_CAN_GET_REC_RESOLUTION)
		    && (default_ioctl(LIRC_GET_REC_RESOLUTION, &drv.resolution) != -1))
			log_trace("resolution of receiver: %d", drv.resolution);

	} else if (drv.rec_mode == LIRC_MODE_LIRCCODE) {
		if (default_ioctl(LIRC_GET_LENGTH, (void*)&drv.code_length) == -1) {
			log_error("could not get code length");
			log_perror_err("default_init()");
			default_deinit();
			return 0;
		}
		if (drv.code_length > sizeof(ir_code) * CHAR_BIT) {
			log_error("lircd can not handle %lu bit codes", drv.code_length);
			default_deinit();
			return 0;
		}
	}
	if (!(drv.send_mode || drv.rec_mode)) {
		default_deinit();
		return 0;
	}
	return 1;
}

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

static int write_send_buffer(int lirc)
{
	if (send_buffer_length() == 0) {
		log_trace("nothing to send");
		return 0;
	}
	return write(lirc, send_buffer_data(), send_buffer_length() * sizeof(lirc_t));
}

int default_send(struct ir_remote* remote, struct ir_ncode* code)
{
	/* things are easy, because we only support one mode */
	if (drv.send_mode != LIRC_MODE_PULSE)
		return 0;

	if (drv.features & LIRC_CAN_SET_SEND_CARRIER) {
		unsigned int freq;

		freq = remote->freq;
		if (default_ioctl(LIRC_SET_SEND_CARRIER, &freq) == -1) {
			log_error("could not set modulation frequency");
			log_perror_err(NULL);
			return 0;
		}
	}
	if (drv.features & LIRC_CAN_SET_SEND_DUTY_CYCLE) {
		unsigned int duty_cycle;

		duty_cycle = get_duty_cycle(remote);
		if (default_ioctl(LIRC_SET_SEND_DUTY_CYCLE, &duty_cycle) == -1) {
			log_error("could not set duty cycle");
			log_perror_err(NULL);
			return 0;
		}
	}
	if (!send_buffer_put(remote, code))
		return 0;
	if (write_send_buffer(drv.fd) == -1) {
		log_error("write failed");
		log_perror_err(NULL);
		return 0;
	}
	return 1;
}

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

int default_ioctl(unsigned int cmd, void* arg)
{
	return ioctl(drv.fd, cmd, arg);
}


static int list_devices(glob_t* glob)
{
	static const struct drv_enum_udev_what what[] = {
		{.subsystem = "lirc" },
		{0}
	};
	return drv_enum_udev(glob, what);
}


static int drvctl(unsigned int cmd, void* arg)
{
	switch (cmd) {
	case DRVCTL_GET_DEVICES:
		return list_devices((glob_t*) arg);
	case DRVCTL_FREE_DEVICES:
		drv_enum_free((glob_t*) arg);
		return 0;
	case LIRC_SET_TRANSMITTER_MASK:
		return default_ioctl(LIRC_SET_TRANSMITTER_MASK, arg);
	default:
		return DRV_ERR_NOT_IMPLEMENTED;
	}
}