File: irsend.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 (250 lines) | stat: -rw-r--r-- 6,320 bytes parent folder | download | duplicates (5)
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
/*

  irsend -  application for sending IR-codes via lirc

  Copyright (C) 1998 Christoph Bartelmus (lirc@bartelmus.de)

  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 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 <getopt.h>
#include <errno.h>
#include <limits.h>

#include "lirc_log.h"
#include "lirc_client.h"

static const logchannel_t logchannel = LOG_APP;

static const char* const help =
	"\nSynopsis:\n"
	"    irsend [options] SEND_ONCE remote code [code...]\n"
	"    irsend [options] SEND_START remote code\n"
	"    irsend [options] SEND_STOP remote code\n"
	"    irsend [options] LIST remote\n"
	"    irsend [options] SET_TRANSMITTERS remote num [num...]\n"
	"    irsend [options] SIMULATE \"scancode repeat keysym remote\"\n"
	"Options:\n"
	"    -h --help\t\t\tdisplay usage summary\n"
	"    -v --version\t\tdisplay version\n"
	"    -d --device=device\t\tuse given lircd socket [" LIRCD "]\n"
	"    -a --address=host[:port]\tconnect to lircd at this address\n"
	"    -# --count=n\t\tsend command n times\n";

const char* prog;

int send_packet(lirc_cmd_ctx* ctx, int fd)
{
	int r;

	do {
		r = lirc_command_run(ctx, fd);
		if (r != 0 && r != EAGAIN)
			fprintf(stderr,
				"Error running command: %s\n", strerror(r));
	} while (r == EAGAIN);
	return r == 0 ? 0 : -1;
}

void reformat_simarg(char* code, char buffer[])
{
	unsigned int scancode;
	unsigned int repeat;
	char keysym[32];
	char remote[64];
	char trash[32];
	int r;

	r = sscanf(code, "%x %x %32s %64s %32s",
		   &scancode, &repeat, keysym, remote, trash);
	if (r != 4) {
		fprintf(stderr, "Bad simulate argument: %s\n", code);
		exit(EXIT_FAILURE);
	}
	snprintf(buffer, PACKET_SIZE, "%016x %02x %s %s",
		 scancode, repeat, keysym, remote);
}





int main(int argc, char** argv)
{
	char* directive;
	char* remote;
	char* code;
	const char* lircd = NULL;
	char* address = NULL;
	unsigned short port = LIRC_INET_PORT;
	unsigned long count = 1;
	int fd;
	char buffer[PACKET_SIZE + 1];
	int r;
	lirc_cmd_ctx ctx;

	prog = "irsend";

	while (1) {
		int c;
		static struct option long_options[] = {
			{ "help",    no_argument,	NULL, 'h' },
			{ "version", no_argument,	NULL, 'v' },
			{ "device",  required_argument, NULL, 'd' },
			{ "address", required_argument, NULL, 'a' },
			{ "count",   required_argument, NULL, '#' },
			{ 0,	     0,			0,    0	  }
		};
		c = getopt_long(argc, argv, "hvd:a:#:", long_options, NULL);
		if (c == -1)
			break;
		switch (c) {
		case 'h':
			fputs(help, stdout);
			return EXIT_SUCCESS;
		case 'v':
			printf("%s %s\n", prog, VERSION);
			return EXIT_SUCCESS;
		case 'd':
			lircd = optarg;
			break;
		case 'a':
		{
			char* p;
			char* end;
			unsigned long val;

			address = strdup(optarg);
			if (!address) {
				fprintf(stderr, "%s: out of memory\n", prog);
				return EXIT_FAILURE;
			}
			p = strchr(address, ':');
			if (p != NULL) {
				val = strtoul(p + 1, &end, 10);
				if (!(*(p + 1)) || *end || val < 1 || val > USHRT_MAX) {
					fprintf(stderr, "%s: invalid port number: %s\n", prog, p + 1);
					return EXIT_FAILURE;
				}
				port = (unsigned short)val;
				*p = 0;
			}
			break;
		}
		case '#':
		{
			char* end;

			count = strtoul(optarg, &end, 10);
			if (!*optarg || *end) {
				fprintf(stderr, "%s: invalid count value: %s\n", prog, optarg);
				return EXIT_FAILURE;
			}
			break;
		}
		default:
			return EXIT_FAILURE;
		}
	}
	if (optind + 2 > argc) {
		fprintf(stderr, "%s: not enough arguments\n", prog);
		return EXIT_FAILURE;
	}
	if (lircd == NULL)
		lircd = LIRCD;
	if (address == NULL)
		fd = lirc_get_local_socket(lircd ? lircd : NULL, 0);
	else
		fd = lirc_get_remote_socket(address, port, 0);
	if (fd < 0) {
		lircd = lircd ? lircd : getenv("LIRC_SOCKET_PATH");
		lircd = lircd ? lircd : LIRCD;
		perrorf("Cannot open socket %s", lircd);
		exit(EXIT_FAILURE);
	}
	if (address)
		free(address);
	address = NULL;

	directive = argv[optind++];

	if (strcasecmp(directive, "set_transmitters") == 0) {
		code = argv[optind++];
		if (strlen(directive) + strlen(code) + 2 < PACKET_SIZE) {
			sprintf(buffer, "%s %s", directive, code);
		} else {
			fprintf(stderr, "%s: input too long\n", prog);
			exit(EXIT_FAILURE);
		}
		while (optind < argc) {
			code = argv[optind++];
			if (strlen(buffer) + strlen(code) + 2 < PACKET_SIZE) {
				sprintf(buffer + strlen(buffer), " %s", code);
			} else {
				fprintf(stderr, "%s: input too long\n", prog);
				exit(EXIT_FAILURE);
			}
		}
		strcat(buffer, "\n");
		lirc_command_init(&ctx, buffer);
		if (send_packet(&ctx, fd) == -1)
			exit(EXIT_FAILURE);
	}
	if (strcasecmp(directive, "simulate") == 0) {
		code = argv[optind++];
		if (optind != argc) {
			fprintf(stderr, "%s: invalid argument count\n", prog);
			exit(EXIT_FAILURE);
		}
		reformat_simarg(code, buffer);
		r = lirc_command_init(&ctx, "%s %s\n", directive, buffer);
		if (r != 0) {
			fprintf(stderr, "%s: %s\n", prog, strerror(r));
			exit(EXIT_FAILURE);
		}
		if (send_packet(&ctx, fd) == -1)
			exit(EXIT_FAILURE);
	} else {
		remote = argv[optind++];

		if (optind == argc) {
			fprintf(stderr, "%s: not enough arguments\n", prog);
			exit(EXIT_FAILURE);
		}
		while (optind < argc) {
			code = argv[optind++];
			if (strcasecmp(directive, "SEND_ONCE") == 0 && count > 1)
				r = lirc_command_init(&ctx, "%s %s %s %lu\n",
						      directive, remote, code, count);
			else
				r = lirc_command_init(&ctx, "%s %s %s\n",
						      directive, remote, code);
			if (r != 0) {
				fprintf(stderr, "%s: input too long\n", prog);
				exit(EXIT_FAILURE);
			}
			lirc_command_reply_to_stdout(&ctx);
			if (send_packet(&ctx, fd) == -1)
				exit(EXIT_FAILURE);
		}
	}
	close(fd);
	return EXIT_SUCCESS;
}