summaryrefslogtreecommitdiffstats
path: root/command.c
blob: b1bbe75b803eb715f40d3602fd1a06dba5ae4718 (plain)
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
/*
 *  command.c - Onkyo receiver user commands code
 *
 *  Copyright (c) 2008 Dan McGee <dpmcgee@gmail.com>
 *
 *  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, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>

#include "onkyo.h"

/*
 * commands:
 * power on
 * power off
 *
 * volume up
 * volume down
 * volume 55 (0 to 100 range)
 *
 * mute on
 * mute off
 * mute toggle
 *
 * input DVD
 * input cd
 * input tv
 * input tuner
 *
 * z2power on
 * z2power off
 * z2mute on
 * z2mute off
 * z2mute toggle
 * z2volume up
 * z2volume down
 * z2volume 56 (0 to 100 range)
 *
 * status (on demand or async):
 * power status (return on/off)
 *   power:on
 * volume status (return 0-100)
 *   volume:44
 * mute status (return on/off)
 *   mute:off
 * input status (return string name)
 *   input:DVD
 * status:
 *   power:on
 *   volume:44
 *   mute:off
 *   input:DVD
 *   z2power:off
 *   ...
 */

static struct command *command_list = NULL;

typedef int (cmd_handler) (int, const char *);

struct command {
	char *name;
	cmd_handler *handler;
	struct command *next;
};

/**
 * Convert a string to uppercase.
 * @param str string to convert (in place)
 * @return pointer to the string
 */
static char *strtoupper(char *str)
{
	char *ptr = str;
	while(*ptr) {
		(*ptr) = toupper(*ptr);
		ptr++;
	}
	return(str);
}

/**
 * Array to hold all status messages that can easily be transposed into
 * one of our own status messages. We can't handle all of them this way,
 * but for those we can this is a code and time saver.
 */
static const char * const statuses[][2] = {
	{ "PWR00", "OK:power:off\n" },
	{ "PWR01", "OK:power:on\n" },

	{ "AMT00", "OK:mute:off\n" },
	{ "AMT01", "OK:mute:on\n" },

	{ "SLI01", "OK:input:Cable\n" },
	{ "SLI02", "OK:input:TV\n" },
	{ "SLI03", "OK:input:AUX\n" },
	{ "SLI10", "OK:input:DVD\n" },
	{ "SLI23", "OK:input:CD\n" },
	{ "SLI24", "OK:input:FM Tuner\n" },
	{ "SLI25", "OK:input:AM Tuner\n" },
	{ "SLI26", "OK:input:Tuner\n" },

	{ "LMD00", "OK:mode:Stereo\n" },
	{ "LMD01", "OK:mode:Direct\n" },
	{ "LMD0C", "OK:mode:All Channel Stereo\n" },
	{ "LMD11", "OK:mode:Pure Audio\n" },
	{ "LMD40", "OK:mode:Straight Decode\n" },
	{ "LMD42", "OK:mode:THX Cinema\n" },
	{ "LMD80", "OK:mode:Pro Logic IIx Movie\n" },
	{ "LMD81", "OK:mode:Pro Logic IIx Music\n" },
	{ "LMD86", "OK:mode:Pro Logic IIx Game\n" },
};

/**
 * Write a receiver status message out to our output channel.
 * @param status the receiver status message to make human readable
 * @return the human readable status message, must be freed
 */
static char *parse_status(const char *status)
{
	unsigned int i, loopsize;
	char *trim, *sptr, *eptr, *ret = NULL;
	/* copy the string so we can trim the start and end portions off */
	trim = strdup(status);
	sptr = trim + strlen(START_RECV);
	eptr = strstr(sptr, END_RECV);
	if(eptr)
		*eptr = '\0';

	/* compile-time constant, should be # rows in statuses */
	loopsize = sizeof(statuses) / sizeof(*statuses);
	for(i = 0; i < loopsize; i++) {
		if(strcmp(sptr, statuses[i][0]) == 0) {
			ret = strdup(statuses[i][1]);
			break;
		}
	}
	if(ret) {
		free(trim);
		return(ret);
	}

	/* We couldn't use our easy method of matching statuses to messages,
	 * so handle the special cases. */
	if(strncmp(sptr, "MVL", 3) == 0) {
		/* parse the volume number out */
		char *pos;
		/* read volume level in as a base 16 (hex) number */
		long level = strtol(sptr + 3, &pos, 16);
		/* TODO parse error, pos != '\0' */
		ret = calloc(10 + 3 + 1, sizeof(char));
		sprintf(ret, "OK:volume:%ld\n", level);
	}

	else if(strncmp(sptr, "TUN", 3) == 0) {
		/* parse the frequency number out */
		char *pos;
		/* read frequency in as a base 10 number */
		long freq = strtol(sptr + 3, &pos, 10);
		/* TODO parse error, pos != '\0' */
		if(freq > 8000) {
			/* FM frequency, something like 09790 was read */
			ret = calloc(8 + 5 + 5, sizeof(char));
			sprintf(ret, "OK:tune:%3.1f FM\n", (double)freq / 100.0);
		} else {
			/* AM frequency, something like 00780 was read */
			ret = calloc(8 + 4 + 5, sizeof(char));
			sprintf(ret, "OK:tune:%ld AM\n", freq);
		}
	}

	else {
		ret = calloc(8 + strlen(sptr) + 2, sizeof(char));
		sprintf(ret, "OK:todo:%s\n", sptr);
	}
	free(trim);
	return(ret);
}

/**
 * Attempt to write a receiver command out to the control channel.
 * This will take a in a simple receiver string minus any preamble or
 * ending characters, turn it into a valid command, and send it to the
 * receiver. It will then attempt to parse the status message returned
 * by the receiver and return a human-readable status message.
 * @param serialfd the fd used for sending commands to the receiver
 * @param cmd a receiver command string, minus preamble and end characters
 * @return 0 on success, -2 on receiver failure
 */
static int cmd_attempt(int serialfd, const char *cmd)
{
	int ret;
	char *fullcmd;

	fullcmd = malloc(strlen(START_SEND) + strlen(cmd)
			+ strlen(END_SEND) + 1);
	sprintf(fullcmd, START_SEND "%s" END_SEND, cmd);

	/* send the command to the receiver */
	ret = rcvr_send_command(serialfd, fullcmd);

	free(fullcmd);
	return(ret < 0 ? -2 : 0);
}


static int handle_power(int serialfd, const char *arg)
{
	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(serialfd, "PWRQSTN");
	else if(strcmp(arg, "on") == 0)
		return cmd_attempt(serialfd, "PWR01");
	else if(strcmp(arg, "off") == 0)
		return cmd_attempt(serialfd, "PWR00");

	/* unrecognized command */
	return(-1);
}

static int handle_volume(int serialfd, const char *arg)
{
	long int level;
	char *test;
	char cmdstr[6]; /* "MVLXX\0" */

	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(serialfd, "MVLQSTN");
	else if(strcmp(arg, "up") == 0)
		return cmd_attempt(serialfd, "MVLUP");
	else if(strcmp(arg, "down") == 0)
		return cmd_attempt(serialfd, "MVLDOWN");

	/* otherwise we probably have a number */
	level = strtol(arg, &test, 10);
	if(*test != '\0') {
		/* parse error, not a number */
		return(-1);
	}
	if(level < 0 || level > 100) {
		/* range error */
		return(-1);
	}
	/* create our command */
	sprintf(cmdstr, "MVL%lX", level);
	/* send the command */
	return cmd_attempt(serialfd, cmdstr);
}

static int handle_mute(int serialfd, const char *arg)
{
	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(serialfd, "AMTQSTN");
	else if(strcmp(arg, "on") == 0)
		return cmd_attempt(serialfd, "AMT01");
	else if(strcmp(arg, "off") == 0)
		return cmd_attempt(serialfd, "AMT00");
	else if(strcmp(arg, "toggle") == 0)
		return cmd_attempt(serialfd, "AMTTG");

	/* unrecognized command */
	return(-1);
}

static int handle_input(int serialfd, const char *arg)
{
	int ret;
	char *dup;

	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(serialfd, "SLIQSTN");
	/* allow lower or upper names */
	dup = strtoupper(strdup(arg));

	if(strcmp(dup, "CABLE") == 0)
		ret = cmd_attempt(serialfd, "SLI01");
	else if(strcmp(dup, "TV") == 0)
		ret = cmd_attempt(serialfd, "SLI02");
	else if(strcmp(dup, "AUX") == 0)
		ret = cmd_attempt(serialfd, "SLI03");
	else if(strcmp(dup, "DVD") == 0)
		ret = cmd_attempt(serialfd, "SLI10");
	else if(strcmp(dup, "CD") == 0)
		ret = cmd_attempt(serialfd, "SLI23");
	else if(strcmp(dup, "FM") == 0)
		ret = cmd_attempt(serialfd, "SLI24");
	else if(strcmp(dup, "AM") == 0)
		ret = cmd_attempt(serialfd, "SLI25");
	else if(strcmp(dup, "TUNER") == 0)
		ret = cmd_attempt(serialfd, "SLI26");
	else
		/* unrecognized command */
		ret = -1;

	free(dup);
	return(ret);
}

static int handle_mode(int serialfd, const char *arg)
{
	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(serialfd, "LMDQSTN");

	/* unrecognized command */
	return(-1);
}

static int handle_tune(int serialfd, const char *arg)
{
	char cmdstr[9]; /* "TUN00000\0" */

	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(serialfd, "TUNQSTN");
	else if(strcmp(arg, "up") == 0)
		return cmd_attempt(serialfd, "TUNUP");
	else if(strcmp(arg, "down") == 0)
		return cmd_attempt(serialfd, "TUNDOWN");

	/* Otherwise we should have a frequency. It can be one of two formats:
	 * FM: (1)00.0
	 * AM: (1)000
	 * TODO intelligent parsing */
	if(strchr(arg, '.')) {
		/* attempt to parse as FM */
		char *test;
		double freq = strtod(arg, &test);
		if(*test != '\0') {
			/* parse error, not a number */
			return(-1);
		}
		if(freq < 87.5 || freq > 107.9) {
			/* range error */
			return(-1);
		}
		/* we want to print something like "TUN09790" */
		sprintf(cmdstr, "TUN%05.0f", freq * 100.0);
	} else {
		/* should be AM, single number with no decimal */
		char *test;
		int freq = strtol(arg, &test, 10);
		if(*test != '\0') {
			/* parse error, not a number */
			return(-1);
		}
		if(freq < 530 || freq > 1710) {
			/* range error */
			return(-1);
		}
		/* we want to print something like "TUN00780" */
		sprintf(cmdstr, "TUN%05d", freq);
	}
	return cmd_attempt(serialfd, cmdstr);
}

static int handle_status(int serialfd, const char *arg)
{
	int ret = 0;

	/* this handler is a bit different in that we call
	 * multiple receiver commands */
	ret += cmd_attempt(serialfd, "PWRQSTN");
	ret += cmd_attempt(serialfd, "MVLQSTN");
	ret += cmd_attempt(serialfd, "AMTQSTN");
	ret += cmd_attempt(serialfd, "SLIQSTN");
	ret += cmd_attempt(serialfd, "LMDQSTN");
	ret += cmd_attempt(serialfd, "TUNQSTN");

	return(ret < 0 ? -2 : 0);
}

static int handle_raw(int serialfd, const char *arg)
{
	return cmd_attempt(serialfd, arg);
}

static int handle_unimplemented(int serialfd, const char *arg)
{
	return(-1);
}

/** 
 * Process a status message to be read from the receiver (one that the
 * receiver initiated). Return a human-readable status message.
 * @param serialfd the fd used for sending commands to the receiver
 * @return the human readable status message, must be freed
 */
char *process_incoming_message(int serialfd)
{
	int ret;
	char *msg, *status = NULL;

	/* send the command to the receiver */
	ret = rcvr_handle_status(serialfd, &status);
	if(ret != -1) {
		/* parse the return and output a status message */
		msg = parse_status(status);
	} else {
		msg = strdup("ERROR:Receiver Error\n");
	}

	free(status);
	return(msg);
}


/**
 * Add the command with the given name and handler to our command list. This
 * will allow process_command() to locate the correct handler for a command.
 * @param name the name of the command, e.g. "volume"
 * @param handler the function that will handle the command
 */
static void add_command(char *name, cmd_handler handler)
{
	/* create our new command object */
	struct command *cmd = calloc(1, sizeof(struct command));
	cmd->name = strdup(name);
	cmd->handler = handler;

	/* add it to our list, first item is special case */
	if(!command_list) {
		command_list = cmd;
	} else {
		struct command *ptr = command_list;
		while(ptr->next)
			ptr = ptr->next;
		ptr->next = cmd;
	}
}

/**
 * Initialize our list of commands. This must be called before the first
 * call to process_command().
 */
void init_commands(void)
{
	/*
	add_command(name,       handle_func); */
	add_command("power",    handle_power);
	add_command("volume",   handle_volume);
	add_command("mute",     handle_mute);
	add_command("input",    handle_input);
	add_command("mode",     handle_mode);
	add_command("tune",     handle_tune);
	add_command("status",   handle_status);
	add_command("raw",      handle_raw);

	add_command("z2power",  handle_unimplemented);
	add_command("z2volume", handle_unimplemented);
	add_command("z2mute",   handle_unimplemented);
}

/**
 * Free our list of commands.
 */
void free_commands(void)
{
	struct command *cmd = command_list;
	command_list = NULL;
	while(cmd) {
		struct command *cmdnext = cmd->next;
		free(cmd->name);
		free(cmd);
		cmd = cmdnext;
	}
}

/** 
 * Process an incoming command, parsing it into the standard "<cmd> <arg>"
 * format. Attempt to locate a handler for the given command and delegate
 * the work to it. If no handler is found, return an error; otherwise
 * return the relevant human-readable status message.
 * @param serialfd the fd used for sending commands to the receiver
 * @param str the full command string, e.g. "power on"
 * @return 0 if the command string was correct and sent, -1 on invalid command
 * string, -2 on failed receiver write
 */
int process_command(int serialfd, const char *str)
{
	char *cmdstr, *argstr;
	struct command *cmd;

	if(!str)
		return(-1);

	cmdstr = strdup(str);
	/* start by splitting the string after the cmd */
	argstr = strchr(cmdstr, ' ');
	/* if we had an arg, set our pointers correctly */
	if(argstr) {
		*argstr = '\0';
		argstr++;
	}

	cmd = command_list;
	while(cmd) {
		if(strcmp(cmd->name, cmdstr) == 0) {
			/* we found the handler, call it and return the result */
			int ret = cmd->handler(serialfd, argstr);
			free(cmdstr);
			return(ret);
		}
		cmd = cmd->next;
	}

	/* we didn't find a handler, must be an invalid command */
	free(cmdstr);
	return(-1);
}

/* vim: set ts=4 sw=4 noet: */