File: mode2.cpp

package info (click to toggle)
lirc 0.10.2-0.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,268 kB
  • sloc: ansic: 26,981; cpp: 9,187; sh: 5,875; python: 4,507; makefile: 1,049; xml: 63
file content (521 lines) | stat: -rw-r--r-- 13,835 bytes parent folder | download
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
/****************************************************************************
** mode2.c *****************************************************************
****************************************************************************
*
* mode2 - shows the pulse/space length or decoded values of remote buttons.
*
*
* Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
* Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
*
*/

/*
 * TODO: Close stuff (call curr_driver->close_func(), closing logs)
 * when a terminating signal arrives.
 */

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

#ifdef _CYGWIN_
#define __USE_LINUX_IOCTL_DEFS
#endif

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <errno.h>
#include <glob.h>
#include <syslog.h>
#include <time.h>
#include <pwd.h>

#include "lirc_private.h"
#include "drv_enum.h"

static const logchannel_t logchannel = LOG_APP;

static const char* opt_device = NULL;
static const char* opt_driver = NULL;
static int opt_dmode = 0;
static int t_div = 500;
static unsigned int opt_gap = 10000;
static int opt_raw_access = 0;
static int opt_list_devices = 0;

static const char* const help =
	"Usage: mode2 [options]\n"
	"\t -d --device=device\tRead from given device\n"
	"\t -H --driver=driver\tUse given driver\n"
	"\t -U --plugindir=path\tLoad plugins from path\n"
	"\t -k --keep-root\t\tKeep root privileges\n"
	"\t -m --mode\t\tEnable column display mode\n"
	"\t -l --list-devices\tList available devices\n"
	"\t -r --raw\t\tAccess device directly without driver\n"
	"\t -g --gap=time\t\tTreat spaces longer than time as the gap\n"
	"\t -s --scope=time\t'Scope' like display with time us per char\n"
	"\t -A --driver-options=key:value[|key:value...]\n"
	"\t\t\t\tSet driver options\n"
	"\t -D --loglevel=level\t'error', 'info', 'notice',... or 3..10\n"
	"\t -O --options-file\tUse alternative lirc_options.conf file\n"
	"\t -h --help\t\tDisplay usage summary\n"
	"\t -v --version\t\tDisplay version\n";


static const struct option options[] = {
	{"help",           no_argument,       NULL, 'h'},
	{"loglevel",       required_argument, NULL, 'D'},
	{"version",        no_argument,       NULL, 'v'},
	{"device",         required_argument, NULL, 'd'},
	{"driver",         required_argument, NULL, 'H'},
	{"keep-root",      no_argument,       NULL, 'k'},
	{"list-devices",   no_argument,       NULL, 'l'},
	{"mode",           no_argument,       NULL, 'm'},
	{"raw",            no_argument,       NULL, 'r'},
	{"gap",            required_argument, NULL, 'g'},
	{"scope",          required_argument, NULL, 's'},
	{"plugindir",      required_argument, NULL, 'U'},
	{"driver-options", required_argument, NULL, 'A'},
	{0,	           0,		      0,    0  }
};

const char* const MSG_NO_GETMODE =
"Problems: this device is not a LIRC kernel device (it does not\n"
"support LIRC_GET_REC_MODE ioctl). This is not necessarily a\n"
"problem, but mode2 will not work.  If you are using the --raw\n"
"option you might try using without it and select a driver\n"
"instead. Otherwise, try using lircd + irw to view the decoded\n"
"data - this might very well work even if mode2 doesn't.\n";

const char* const USE_RAW_MSG =
"Please use the --raw option to access the device directly instead"
" through the abstraction layer\n";

static void add_defaults(void)
{
        const char* tmp;
	tmp = options_getstring("mode2:device");
	const char* const device =
		tmp ? tmp : options_getstring("lircd:device");
	tmp = options_getstring("mode2:driver");
	const char* const driver =
		tmp ? tmp : options_getstring("lircd:driver");
	tmp = options_getstring("mode2:plugindir");
	const char* const plugindir =
		tmp ? tmp : options_getstring("lircd:plugindir");
	tmp = options_getstring("mode2:debug");
	const char* const lircd_level =
		tmp ? tmp : options_getstring("lircd:debug");
	tmp = options_getstring("mode2:output");
	const char* const output =
		tmp ? tmp : options_getstring("lircd:output");

	const char* const env_level = getenv("LIRC_LOGLEVEL");
	char level[12];

	if (env_level != NULL) {
		if (string2loglevel(env_level) == LIRC_BADLEVEL) {
			fprintf(stderr,
			        "Bad debug level: %s in LIRC_LOGLEVEL\n",
			        env_level);
			exit(EXIT_FAILURE);
		}
		strncpy(level, env_level, sizeof(level) - 1);
	} else if (lircd_level != NULL) {
		strncpy(level, lircd_level, sizeof(level) - 1);
	} else {
		snprintf(level, sizeof(level), " %d", lirc_log_defaultlevel());
	}
	const char* const defaults[] = {
		"mode2:driver",     driver ? driver : "default",
		"mode2:lircdfile",  output ? output : LIRCD,
		"lircd:logfile",    "syslog",
		"mode2:plugindir",  plugindir ? plugindir : PLUGINDIR,
		"lircd:configfile", LIRCDCFGFILE,
		"mode2:debug",	    level,
		"mode2:device",     device ? device : LIRC_DRIVER_DEVICE,
		(const char*)NULL,  (const char*)NULL
	};
	options_add_defaults(defaults);
}


static void parse_options(int argc, char** argv)
{
	int c;
	static const char* const optstring = "hvD:d:H:mklrg:s:U:A:";

	add_defaults();
	while ((c = getopt_long(argc, argv, optstring, options, NULL)) != -1) {
		switch (c) {
		case 'D':
			if (string2loglevel(optarg) == LIRC_BADLEVEL) {
				fprintf(stderr, "Bad debug level: %s\n", optarg);
				exit(EXIT_FAILURE);
			}
			options_set_opt("mode2:debug", optarg);
			break;
		case 'h':
			puts(help);
			exit(EXIT_SUCCESS);
		case 'H':
			options_set_opt("mode2:driver", optarg);
			break;
		case 'v':
			printf("%s %s\n", "mode2 ", VERSION);
			exit(EXIT_SUCCESS);
		case 'U':
			options_set_opt("mode2:plugindir", optarg);
			break;
		case 'k':
			unsetenv("SUDO_USER");
			break;
		case 'l':
			opt_list_devices = 1;
			break;
		case 'd':
			options_set_opt("mode2:device", optarg);
			break;
		case 's':
			opt_dmode = 2;
			t_div = atoi(optarg);
			break;
		case 'm':
			opt_dmode = 1;
			break;
		case 'r':
			opt_raw_access = 1;
			break;
		case 'g':
			opt_gap = atoi(optarg);
			break;
		case 'A':
			options_set_opt("lircd:driver-options", optarg);
			break;
		default:
			fprintf(stderr, "Usage: mode2 [options]\n");
			exit(EXIT_FAILURE);
		}
	}
	if (optind < argc) {
		fputs("Too many arguments\n", stderr);
		exit(EXIT_FAILURE);
	}
	opt_device = options_getstring("mode2:device");
	if (opt_raw_access && opt_device == NULL) {
		fprintf(stderr, "The --raw option requires a --device\n");
		exit(EXIT_FAILURE);
	}
	options_set_opt("lircd:plugindir",
			options_getstring("mode2:plugindir"));
	opt_driver = options_getstring("mode2:driver");
	if (hw_choose_driver(opt_driver) != 0) {
		fprintf(stderr, "Driver `%s' not found.", opt_driver);
		fputs(" (Missing -U/--plugins option?)\n", stderr);
		fputs("\nAvailable drivers:\n", stderr);
		hw_print_drivers(stderr);
		exit(EXIT_FAILURE);
	}
}


/** Open device using curr_driver->open_func() and curr_driver->init_func().*/
int open_device(int opt_raw_access, const char* device)
{
	struct stat s;

	uint32_t mode;
	int fd;
	const char* opt;

	log_info("Opening device: %s", device);
	if (opt_raw_access) {
		fd = open(device, O_RDONLY);
		if (fd == -1) {
			device = device ? device : "(Null)";
			perrorf("Error while opening device: %s", device);
			exit(EXIT_FAILURE);
		}
		;
		if ((fstat(fd, &s) != -1) && (S_ISFIFO(s.st_mode))) {
			/* can't do ioctls on a pipe */
		} else if ((fstat(fd, &s) != -1) && (!S_ISCHR(s.st_mode))) {
			fprintf(stderr, "%s is not a character device\n",
				device);
			fputs("Use the -d option to specify device\n",
			      stderr);
			close(fd);
			exit(EXIT_FAILURE);
		} else if (ioctl(fd, LIRC_GET_REC_MODE, &mode) == -1) {
			fputs(MSG_NO_GETMODE, stderr);
			close(fd);
			exit(EXIT_FAILURE);
		}
	} else {
		curr_driver->open_func(device);
		opt = options_getstring("lircd:driver-options");
		if (drv_handle_options(opt) != 0) {
			fprintf(stderr,
				"Cannot set driver (%s) options (%s)\n",
				curr_driver->name, opt);
			exit(EXIT_FAILURE);
		}
		if (!curr_driver->init_func || !curr_driver->init_func()) {
			fprintf(stderr, "Cannot initiate device %s\n",
				curr_driver->device);
			exit(EXIT_FAILURE);
		}
		fprintf(stderr, "Trying device: %s\n", curr_driver->device);
		fd = curr_driver->fd;   /* please compiler */
		mode = curr_driver->rec_mode;
		if (mode != LIRC_MODE_MODE2) {
			if (strcmp(curr_driver->name, "default") == 0) {
				fputs(USE_RAW_MSG, stderr);
				exit(EXIT_FAILURE);
			} else if (mode != LIRC_MODE_LIRCCODE) {
				fputs("Internal error: bad receive mode\n",
				      stderr);
				exit(EXIT_FAILURE);
			}
		}
	}
	if (opt_device && strcmp(opt_device, LIRCD) == 0) {
		fputs("Refusing to connect to lircd socket\n", stderr);
		exit(EXIT_FAILURE);
	}
	fprintf(stderr, "Using device: %s\n", curr_driver->device);
	return fd;
}


/** Get the codelength (bits per decoded value) for lirccode data. */
unsigned int get_codelength(int fd, int use_raw_access)
{
	unsigned int code_length;
	int r = 1;

	if (use_raw_access) {
		if (ioctl(fd, LIRC_GET_LENGTH, &code_length) == -1) {
			perror("Could not get code length");
			close(fd);
			exit(EXIT_FAILURE);
		}
		return code_length;
	}
	if (curr_driver->drvctl_func) {
		r = curr_driver->drvctl_func(DRVCTL_GET_RAW_CODELENGTH,
					     &code_length);
	}
	if (r != 0)
		code_length = curr_driver->code_length;
	return code_length;
}


/** Print mode2 data as pulse/space durations, one per line. */
void print_mode2_data(unsigned int data)
{
	static int bitno = 1;
	static bool leading_space = true;
	unsigned int msg = data & LIRC_MODE2_MASK;
	unsigned int value = data & LIRC_VALUE_MASK;

	if (leading_space && msg == LIRC_MODE2_SPACE) {
		return;
	} else {
		leading_space = (msg == LIRC_MODE2_TIMEOUT);
	}

	switch (opt_dmode) {
	case 0:
		switch (msg) {
		case LIRC_MODE2_PULSE:
			printf("pulse %u\n", value);
			break;
		case LIRC_MODE2_SPACE:
			printf("space %u\n", value);
			break;
		case LIRC_MODE2_TIMEOUT:
			printf("timeout %u\n", value);
			break;
		case LIRC_MODE2_FREQUENCY:
			printf("carrier %u\n", value);
			break;
		case LIRC_MODE2_OVERFLOW:
			printf("overflow\n");
			break;
		}
		break;
	case 1: {
		/* print output like irrecord raw config file data */
		printf(" %8u", value);
		++bitno;
		if (is_pulse(data)) {
			if ((bitno & 1) == 0)
				/* not in expected order */
				fputs("-pulse", stdout);
		} else {
			if (bitno & 1)
				/* not in expected order */
				fputs("-space", stdout);
			if ((value > opt_gap) || (bitno >= 6)) {
				/* real long space or more
				 * than 6 codes, start new line */
				puts("");
				if (value > opt_gap)
					puts("");
				bitno = 0;
			}
		}
		break;
	}
	case 2:
		if (value > opt_gap) {
			fputs("_\n\n_", stdout);
		} else {
			printf("%.*s",
			       (value + t_div / 2) / t_div,
			       is_pulse(data) ?
			       "------------" : "____________");
		}
		break;
	}
	fflush(stdout);
}


/** Print lirccode data as a decoded value (an integer) per line. */
void print_lirccode_data(char* buffer, size_t count)
{
	size_t i;

	fputs("code: 0x", stdout);
	for (i = 0; i < count; i++)
		printf("%02x", (unsigned char)buffer[i]);
	puts("");
	fflush(stdout);
}


/**
 * Process next button press and print a dump, return boolean OK/FAIL.
 */
int next_press(int fd, int mode, int bytes)
{
	char buffspace[bytes];
	int r;
        union {
		char* buffer;
		lirc_t data;
	} input;

	input.buffer = buffspace;
	if (opt_raw_access || mode != LIRC_MODE_MODE2) {
		r = read(fd, input.buffer, bytes);
		if (r == -1)
			perrorf("read() error on %s", opt_device);
		else if (r != (int)bytes)
			fprintf(stderr, "Partial read %d bytes on %s",
				r, opt_device);
		if (r != (int)bytes)
			return 0;
		if (mode == LIRC_MODE_MODE2)
			print_mode2_data(input.data);
		else
			print_lirccode_data(input.buffer, bytes);
	} else {
		input.data = curr_driver->readdata(0);
		if (input.data == 0) {
			fputs("readdata() failed\n", stderr);
			return 0;
		}
		print_mode2_data(input.data);
	}
	return 1;
}


static void list_devices(void)
{
	glob_t glob;
	unsigned i;
	int r;

	r = curr_driver->drvctl_func ? 0 : DRV_ERR_NOT_IMPLEMENTED;
	if (r == 0) {
		r = curr_driver->drvctl_func(DRVCTL_GET_DEVICES, &glob);
	}
	switch (r) {
		case DRV_ERR_ENUM_EMPTY:
		break;
	case DRV_ERR_NOT_IMPLEMENTED:
		fputs("Device enumeration is not supported by this driver\n",
			stderr);
		exit(1);
	case 0:
		for (i = 0; i < glob.gl_pathc; i += 1)
			puts(glob.gl_pathv[i]);
		curr_driver->drvctl_func(DRVCTL_FREE_DEVICES, &glob);
		break;
	default:
		fputs("Error running enumerate command", stderr);
		exit(1);
	}
}


int main(int argc, char** argv)
{
	int fd;
	uint32_t mode;
	size_t bytes = sizeof(lirc_t);
	char logpath[256];
	/**
	 * Was hard coded to 50000 but this is too long, the shortest gap in the
	 * supplied .conf files is 10826, the longest space defined for any one,
	 * zero or header is 7590
	 */
	uint32_t code_length;
	const loglevel_t level = options_get_app_loglevel("mode2");

	hw_choose_driver(NULL);
	options_load(argc, argv, NULL, parse_options);

	lirc_log_get_clientlog("mode2", logpath, sizeof(logpath));
	(void)unlink(logpath);
	lirc_log_set_file(logpath);
	lirc_log_open("mode2", 1, level);
	if (opt_list_devices) {
		list_devices();
		return 0;
	}
        if (opt_raw_access) {
		fprintf(stderr, "Using raw access on device %s\n", opt_device);
	} else {
		fprintf(stderr, "Using driver %s on device %s\n",
		       options_getstring("mode2:driver"),
		       opt_device);
	}
	fd = open_device(opt_raw_access, opt_device);
	if (geteuid() == 0)
		drop_root_cli(setuid);

	mode = curr_driver->rec_mode;
	if (mode == LIRC_MODE_LIRCCODE) {
		code_length = get_codelength(fd, opt_raw_access);
		bytes = (code_length + CHAR_BIT - 1) / CHAR_BIT;
	}
	while (next_press(fd, mode, bytes))
		;
	return EXIT_SUCCESS;
}