File: file.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 (286 lines) | stat: -rw-r--r-- 7,072 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
/****************************************************************************
** file.c **********************************************************
****************************************************************************
*
*  File output driver which logs sent data to a disk file. The file used is
*  the regular drv.device.
*
*  Also, it supports the following drvctl options:
*    - 'set-input <path>' which makes is read data  from disk file and
*       deliver it as pulses from the remote.
*    - 'send-space <useconds>' which indeed sends a (typically long) space.
*
*  The exported file descriptor drv.fd reflects the input file, not the
*  output one.
*
*  Upon a request to send a LIRC_EOF pulse the driver will terminate the
*  running process using SIGUSR1.
*
*  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

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

#include "lirc_driver.h"


/* exported functions  */
static int init_func(void);
static int deinit_func(void);
static int send_func(struct ir_remote* remote, struct ir_ncode* code);
static char* receive_func(struct ir_remote* remotes);
static int open_func(const char* path);
static int close_func(void);
static int decode_func(struct ir_remote* remote, struct decode_ctx_t* ctx);
static lirc_t readdata(lirc_t timeout);
static int drvctl_func(unsigned int cmd, void* arg);


const struct driver drv_test = {
	.name		= "file",
	.device		= "testdata.sym",
	.features	= LIRC_CAN_REC_MODE2 | LIRC_CAN_SEND_PULSE,
	.send_mode	= LIRC_MODE_PULSE,
	.rec_mode	= LIRC_MODE_MODE2,
	.code_length	= 0,
	.init_func	= init_func,
	.deinit_func	= deinit_func,
	.open_func	= open_func,
	.close_func	= close_func,
	.send_func	= send_func,
	.rec_func	= receive_func,
	.decode_func	= decode_func,
	.drvctl_func	= drvctl_func,
	.readdata	= readdata,
	.api_version	= 3,
	.driver_version = "0.10.0",
	.info		= "See file://" PLUGINDOCS "/file.html",
	.device_hint    = "/tmp/*",
};


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

// private data

static const logchannel_t logchannel = LOG_DRIVER;

static FILE* infile = NULL;
static int outfile_fd = -1;
static int lineno = 1;
static int at_eof = 0;

static int decode_func(struct ir_remote* remote, struct decode_ctx_t* ctx)
{
	int res;

	log_trace("decode: enter");
	res = receive_decode(remote, ctx);
	log_trace("decode: %d", res);
	return res;
}


static lirc_t readdata(lirc_t timeout)
{
	char line[64];
	char what[16];
	int count;
	int data;
	const char* const close_msg =
		"# Closing infile file after %d lines (data still pending...)\n";

	if (infile == NULL || fgets(line, sizeof(line), infile) == NULL) {
		log_trace("No more input, timeout: %d", timeout);
		if (timeout > 0)
			usleep(timeout);
		if (infile != NULL) {
			fclose(infile);
			infile = NULL;
		}
		snprintf(line, sizeof(line), close_msg, lineno);
		chk_write(outfile_fd, line, strlen(line));
		drv.fd = -1;
		at_eof = 1;
		log_debug("Closing infile after  %d lines", lineno);
		lineno = 0;
		return LIRC_EOF | LIRC_MODE2_TIMEOUT | timeout;
	}
	count = sscanf(line, "%15s %d", what, &data);
	if (count != 2)
		return 0;
	data &= PULSE_MASK;
	if (strstr(what, "pulse") != NULL)
		data |= PULSE_BIT;
	lineno += 1;
	return data;
};


static int open_func(const char* device)
{
	if (device == NULL)
		device = drv.device;
	if (device == NULL) {
		log_error("Attempt to open NULL sink file");
		return 0;
	}
	outfile_fd = open(device, O_WRONLY | O_CREAT | O_APPEND, 0666);
	if (outfile_fd == -1) {
		log_warn(
			  "Cannot open path %s for write", drv.device);
		return 0;
	}
	send_buffer_init();
	return 1;
}


static int close_func(void)
{
	if (drv.fd == -1)
		return 1;
	if (close(drv.fd) == -1) {
		log_perror_warn("deinit: Cannot close");
		return 0;
	}
	drv.fd = -1;
	return 1;
}


static char* receive_func(struct ir_remote* remotes)
{
	static char* EOF_PACKET = "0000000008000000 00 __EOF lirc";

	if (at_eof) {
		log_trace("file.c: At eof");
		at_eof = 0;
		return EOF_PACKET;
	}
	if (!rec_buffer_clear()) {
		log_trace("file.c: At !rec_buffer_clear");
		if (at_eof) {
			at_eof = 0;
			return EOF_PACKET;
		} else {
			return NULL;
		}
	}
	return decode_all(remotes);
}


static void write_line(const char* type, int duration)
{
	char buffer[32];

	snprintf(buffer, sizeof(buffer), "%s %d\n", type, duration);
	chk_write(outfile_fd, buffer, strlen(buffer));
	if (duration & LIRC_EOF) {
		log_notice("Exiting on input EOF");
		raise(SIGUSR1);
	}
}


static int send_func(struct ir_remote* remote, struct ir_ncode* code)
{
	int i;

	log_trace("file.c: sending, code: %s", code->name);

	if (remote->pzero == 0 && remote->szero == 0
		&& ((remote->flags & RAW_CODES) == 0))
	{
		write_line("code", code->code);
		return 1;
	}
	if (!send_buffer_put(remote, code)) {
		log_debug("file.c: Cannot make send_buffer_put");
		return 0;
	}
	for (i = 0;; ) {
		write_line("pulse", send_buffer_data()[i++]);
		if (i >= send_buffer_length())
			break;
		write_line("space", send_buffer_data()[i++]);
	}
	write_line("space", remote->min_remaining_gap);
	return 1;
}


static int drvctl_func(unsigned int cmd, void* arg)
{
	struct option_t* opt;
	long value;
	char buff[256];
	const char* const open_msg = "# Reading from %s\n";

	switch (cmd) {
	case DRVCTL_SET_OPTION:
		at_eof = 0;    //FIXME! This should be a separate drvctl!
		opt = (struct option_t*)arg;
		if (strcmp(opt->key, "send-space") == 0) {
			value = strtol(opt->value, NULL, 10);
			if (value <= 0 || value > 100000000)
				return DRV_ERR_BAD_OPTION;
			snprintf(buff, sizeof(buff), "space %ld\n", value);
			chk_write(outfile_fd, buff, strlen(buff));
			return 0;
		} else if (strcmp(opt->key, "set-infile") == 0) {
			if (outfile_fd < 0)
				return DRV_ERR_BAD_STATE;
			infile = fopen(opt->value, "r");
			if (infile == NULL)
				return DRV_ERR_BAD_OPTION;
			drv.fd = fileno(infile);
			lineno = 1;
			snprintf(buff, sizeof(buff), open_msg, opt->value);
			chk_write(outfile_fd, buff, strlen(buff));
			return 0;
		} else {
			return DRV_ERR_BAD_OPTION;
		}
	default:
		return DRV_ERR_NOT_IMPLEMENTED;
	}
}


static int init_func(void)
{
	return 1;
};

static int deinit_func(void)
{
	return 1;
};