summaryrefslogtreecommitdiffstats
path: root/command.c
blob: bd82551bfd1ccc7a39af906df5f7b1eac5976610 (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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*
 *  command.c - Onkyo receiver user commands code
 *
 *  Copyright (c) 2008-2010 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/>.
 */

#define _BSD_SOURCE 1 /* strdup */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h> /* toupper, isspace */
#include <errno.h>
#include <string.h>

#include "onkyo.h"

static struct command *command_list = NULL;

typedef int (cmd_handler) (struct receiver *, const struct command *, const char *);

struct command {
	unsigned long hash;
	const char *name;
	const char *prefix;
	cmd_handler *handler;
	int fake;
	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 = (char)toupper(*ptr);
		ptr++;
	}
	return(str);
}

/**
 * Queue a receiver command to be sent when the device file descriptor
 * is available for writing. Queueing and sending asynchronously allows
 * the program to backlog many commands at once without blocking on the
 * potentially slow receiver device. When queueing, we check if this command
 * is already in the queue- if so, we do not queue it again.
 * @param rcvr the receiver the command should be queued for
 * @param cmd the command struct for the first part of the receiver command
 * string, usually containing a prefix aka "PWR"
 * @param arg the second part of the receiver command string, aka "QSTN"
 * @return 0 on success, -1 on missing args
 */
static int cmd_attempt(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	char *fullcmd;
	struct cmdqueue *q;

	if(!cmd || !arg)
		return(-1);

	if(!cmd->fake && !cmd->prefix)
		return(-1);

	fullcmd = malloc(strlen(cmd->prefix) + strlen(arg) + 1);
	sprintf(fullcmd, "%s%s", cmd->prefix, arg);

	q = malloc(sizeof(struct cmdqueue));
	q->hash = hash_sdbm(fullcmd);
	q->cmd = fullcmd;
	q->next = NULL;

	if(rcvr->queue == NULL) {
		rcvr->queue = q;
	} else {
		struct cmdqueue *ptr = rcvr->queue;
		for(;;) {
			if(ptr->hash == q->hash) {
				/* command already in our queue, skip second copy */
				free(q);
				free(fullcmd);
				return(0);
			}
			if(!ptr->next)
				break;
			ptr = ptr->next;
		}
		ptr->next = q;
	}
	return(0);
}

static int cmd_attempt_raw(struct receiver *rcvr,
		const char *fake, const char *arg)
{
	struct command c;
	c.prefix = fake;
	c.fake = 0;
	return cmd_attempt(rcvr, &c, arg);
}

/**
 * Handle the standard question, up, and down operations if possible.
 * @param rcvr the receiver the command should be queued for
 * @param cmd the struct containing information on the command being called
 * @param arg the provided argument, e.g. "QSTN"
 * @return the return value of cmd_attempt() if we found a standard operation,
 * else -2
 */
static int handle_standard(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(rcvr, cmd, "QSTN");
	else if(strcmp(arg, "up") == 0)
		return cmd_attempt(rcvr, cmd, "UP");
	else if(strcmp(arg, "down") == 0)
		return cmd_attempt(rcvr, cmd, "DOWN");
	return(-2);
}

static int handle_boolean(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(rcvr, cmd, "QSTN");
	else if(strcmp(arg, "on") == 0)
		return cmd_attempt(rcvr, cmd, "01");
	else if(strcmp(arg, "off") == 0)
		return cmd_attempt(rcvr, cmd, "00");
	else if(strcmp(arg, "toggle") == 0) {
		const char *prefix = cmd->prefix;
		/* toggle is applicable for mute, not for power */
		if(strcmp(prefix, "AMT") == 0 || strcmp(prefix, "ZMT") == 0
				|| strcmp(prefix, "MT3") == 0)
			return cmd_attempt(rcvr, cmd, "TG");
	}

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

static int handle_ranged(struct receiver *rcvr,
		const struct command *cmd, const char *arg,
		int lower, int upper, int offset, const char *fmt)
{
	int ret;
	long level;
	char *test;
	char cmdstr[3]; /* "XX\0" */

	ret = handle_standard(rcvr, cmd, arg);
	if(ret != -2)
		return (ret);

	/* otherwise we probably have a number */
	level = strtol(arg, &test, 10);
	if(*test != '\0') {
		/* parse error, not a number */
		return(-1);
	}
	if(level < lower || level > upper) {
		/* range error */
		return(-1);
	}
	level += offset;
	/* create our command */
	sprintf(cmdstr, fmt, (unsigned long)level);
	/* send the command */
	return cmd_attempt(rcvr, cmd, cmdstr);
}

static int handle_volume(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	return handle_ranged(rcvr, cmd, arg, 0, 100, 0, "%02lX");
}

static int handle_dbvolume(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	return handle_ranged(rcvr, cmd, arg, -82, 18, 82, "%02lX");
}

static int handle_preset(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	return handle_ranged(rcvr, cmd, arg, 0, 40, 0, "%02lX");
}

static int handle_avsync(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	/* the extra '0' is an easy way to not have to multiply by 10 */
	return handle_ranged(rcvr, cmd, arg, 0, 250, 0, "%03ld0");
}

static int handle_swlevel(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	int ret;
	long level;
	char *test;
	char cmdstr[3]; /* "XX\0" */

	ret = handle_standard(rcvr, cmd, arg);
	if(ret != -2)
		return (ret);

	/* otherwise we probably have a number */
	level = strtol(arg, &test, 10);
	if(*test != '\0') {
		/* parse error, not a number */
		return(-1);
	}
	if(level < -15 || level > 12) {
		/* range error */
		return(-1);
	}
	/* create our command */
	if(level == 0) {
		sprintf(cmdstr, "00");
	} else if(level > 0) {
		sprintf(cmdstr, "+%1lX", (unsigned long)level);
	} else { /* level < 0 */
		sprintf(cmdstr, "-%1lX", (unsigned long)-level);
	}
	/* send the command */
	return cmd_attempt(rcvr, cmd, cmdstr);
}

static const char * const inputs[][2] = {
	{ "DVR",       "00" },
	{ "VCR",       "00" },
	{ "CABLE",     "01" },
	{ "SAT",       "01" },
	{ "TV",        "02" },
	{ "AUX",       "03" },
	{ "AUX2",      "04" },
	{ "PC",        "05" },
	{ "DVD",       "10" },
	{ "TAPE",      "20" },
	{ "PHONO",     "22" },
	{ "CD",        "23" },
	{ "FM",        "24" },
	{ "FM TUNER",  "24" },
	{ "AM",        "25" },
	{ "AM TUNER",  "25" },
	{ "TUNER",     "26" },
	{ "MUSIC SERVER", "27" },
	{ "SERVER",    "27" },
	{ "IRADIO",    "28" },
	{ "USB",       "29" },
	{ "USB REAR",  "2A" },
	{ "PORT",      "40" },
	{ "MULTICH",   "30" },
	{ "XM",        "31" },
	{ "SIRIUS",    "32" },
};

static int handle_input(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	unsigned int i, loopsize;
	int ret;
	char *dup;

	ret = handle_standard(rcvr, cmd, arg);
	if(ret != -2)
		return (ret);

	/* allow lower or upper names */
	dup = strtoupper(strdup(arg));
	ret = -1;

	/* compile-time constant */
	loopsize = sizeof(inputs) / sizeof(*inputs);
	for(i = 0; i < loopsize; i++) {
		if(strcmp(dup, inputs[i][0]) == 0) {
			ret = cmd_attempt(rcvr, cmd, inputs[i][1]);
			break;
		}
	}
	/* the following are only valid for zones */
	if(ret == -1 &&
			(strcmp(cmd->prefix, "SLZ") == 0 ||
			 strcmp(cmd->prefix, "SL3") == 0)) {
		if(strcmp(dup, "OFF") == 0)
			ret = cmd_attempt(rcvr, cmd, "7F");
		else if(strcmp(dup, "SOURCE") == 0)
			ret = cmd_attempt(rcvr, cmd, "80");
	}

	free(dup);
	return(ret);
}

static const char * const modes[][2] = {
	{ "STEREO",     "00" },
	{ "DIRECT",     "01" },
	{ "MONOMOVIE",  "07" },
	{ "ORCHESTRA",  "08" },
	{ "UNPLUGGED",  "09" },
	{ "STUDIOMIX",  "0A" },
	{ "TVLOGIC",    "0B" },
	{ "ACSTEREO",   "0C" },
	{ "THEATERD",   "0D" },
	{ "MONO",       "0F" },
	{ "PURE",       "11" },
	{ "FULLMONO",   "13" },
	{ "DTSSS",      "15" },
	{ "DSX",        "16" },
	{ "STRAIGHT",   "40" },
	{ "DOLBYEX",    "41" },
	{ "DTSES",      "41" },
	{ "THX",        "42" },
	{ "THXEX",      "43" },
	{ "THXMUSIC",   "44" },
	{ "THXGAMES",   "45" },
	{ "PLIIMOVIE",  "80" },
	{ "PLIIMUSIC",  "81" },
	{ "NEO6CINEMA", "82" },
	{ "NEO6MUSIC",  "83" },
	{ "PLIITHX",    "84" },
	{ "NEO6THX",    "85" },
	{ "PLIIGAME",   "86" },
	{ "NEURALTHX",  "88" },
};

static int handle_mode(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	unsigned int i, loopsize;
	int ret;
	char *dup;

	ret = handle_standard(rcvr, cmd, arg);
	if(ret != -2)
		return (ret);

	/* allow lower or upper names */
	dup = strtoupper(strdup(arg));
	ret = -1;

	/* compile-time constant */
	loopsize = sizeof(modes) / sizeof(*modes);
	for(i = 0; i < loopsize; i++) {
		if(strcmp(dup, modes[i][0]) == 0) {
			ret = cmd_attempt(rcvr, cmd, modes[i][1]);
			break;
		}
	}

	free(dup);
	return(ret);
}

static int handle_tune(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	int ret;
	char cmdstr[6]; /* "00000\0" */
	char *test;

	ret = handle_standard(rcvr, cmd, arg);
	if(ret != -2)
		return (ret);

	/* Otherwise we should have a frequency. It can be one of two formats:
	 * FM: (1)00.0 (possible hundreds spot, with decimal point)
	 * AM: (1)000 (possible thousands spot, with NO decimal point)
	 */
	if(strchr(arg, '.')) {
		long freq, frac_freq;
		/* attempt to parse as FM */
		errno = 0;
		freq = strtol(arg, &test, 10);
		/* this should start parsing after the '.' */
		frac_freq = strtol(test + 1, &test, 10);
		if(errno != 0) {
			/* parse error, not a number */
			return(-1);
		}
		/* allowed range: 87.5 to 107.9 inclusive */
		if(frac_freq < 0 || frac_freq > 9 ||
				(freq <= 87 && frac_freq < 5) ||
				freq >= 108) {
			/* range error */
			return(-1);
		}
		/* we want to print something like "TUN09790" */
		freq = freq * 100 + frac_freq * 10;
		sprintf(cmdstr, "%05ld", freq);
	} else {
		long freq;
		/* should be AM, single number with no decimal */
		errno = 0;
		freq = strtol(arg, &test, 10);
		if(errno != 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, "%05ld", freq);
	}
	return cmd_attempt(rcvr, cmd, cmdstr);
}

static int handle_sleep(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	long mins;
	char *test;
	char cmdstr[3]; /* "XX\0" */

	if(!arg || strcmp(arg, "status") == 0)
		return cmd_attempt(rcvr, cmd, "QSTN");
	else if(strcmp(arg, "off") == 0)
		return cmd_attempt(rcvr, cmd, "OFF");

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

static int handle_fakesleep(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	struct timeval now;
	long mins;
	char *test;
	char msg[128];
	char zone;

	gettimeofday(&now, NULL);
	zone = cmd->prefix[0];

	if(zone != '2' && zone != '3')
		return(-1);

	if(!arg || strcmp(arg, "status") == 0) {
		struct timeval when = { 0, 0 };

		if(zone == '2')
			when = rcvr->zone2_sleep;
		else if(zone == '3')
			when = rcvr->zone3_sleep;

		mins = when.tv_sec > now.tv_sec ?
			(when.tv_sec - now.tv_sec + 59) / 60 : 0;
	} else if(strcmp(arg, "off") == 0) {
		/* clear out any future receiver set sleep time */
		if(zone == '2') {
			rcvr->zone2_sleep.tv_sec = 0;
			rcvr->zone2_sleep.tv_usec = 0;
		} else if(zone == '3') {
			rcvr->zone3_sleep.tv_sec = 0;
			rcvr->zone3_sleep.tv_usec = 0;
		}
		mins = 0;
	} else {
		/* otherwise we probably have a number */
		mins = strtol(arg, &test, 10);
		if(*test != '\0') {
			/* parse error, not a number */
			return(-1);
		}
		if(mins < 0) {
			/* range error */
			return(-1);
		}
		if(zone == '2') {
			rcvr->zone2_sleep = now;
			rcvr->zone2_sleep.tv_sec += 60 * mins;
		} else if(zone == '3') {
			rcvr->zone3_sleep = now;
			rcvr->zone3_sleep.tv_sec += 60 * mins;
		}
	}

	snprintf(msg, sizeof(msg), "OK:zone%csleep:%ld\n", zone, mins);
	write_to_connections(rcvr, msg);
	return(0);
}

static int handle_memory(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	if(!arg)
		return(-1);
	if(strcmp(arg, "lock") == 0)
		return cmd_attempt(rcvr, cmd, "LOCK");
	else if(strcmp(arg, "unlock") == 0)
		return cmd_attempt(rcvr, cmd, "UNLK");
	return(-1);
}


static int handle_status(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	int ret = 0;

	/* this handler is a bit different in that we call
	 * multiple receiver commands */
	if(strcmp(cmd->name, "status") == 0 && (!arg || strcmp(arg, "main") == 0)) {
		ret += cmd_attempt_raw(rcvr, "PWR", "QSTN");
		ret += cmd_attempt_raw(rcvr, "MVL", "QSTN");
		ret += cmd_attempt_raw(rcvr, "AMT", "QSTN");
		ret += cmd_attempt_raw(rcvr, "SLI", "QSTN");
		ret += cmd_attempt_raw(rcvr, "LMD", "QSTN");
		ret += cmd_attempt_raw(rcvr, "TUN", "QSTN");
	} else if(strcmp(cmd->name, "zone2status") == 0 || (arg && strcmp(arg, "zone2") == 0)) {
		ret += cmd_attempt_raw(rcvr, "ZPW", "QSTN");
		ret += cmd_attempt_raw(rcvr, "ZVL", "QSTN");
		ret += cmd_attempt_raw(rcvr, "ZMT", "QSTN");
		ret += cmd_attempt_raw(rcvr, "SLZ", "QSTN");
		ret += cmd_attempt_raw(rcvr, "TUZ", "QSTN");
	} else if(strcmp(cmd->name, "zone3status") == 0 || (arg && strcmp(arg, "zone3") == 0)) {
		ret += cmd_attempt_raw(rcvr, "PW3", "QSTN");
		ret += cmd_attempt_raw(rcvr, "VL3", "QSTN");
		ret += cmd_attempt_raw(rcvr, "MT3", "QSTN");
		ret += cmd_attempt_raw(rcvr, "SL3", "QSTN");
		ret += cmd_attempt_raw(rcvr, "TU3", "QSTN");
	} else {
		return(-1);
	}

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

static int handle_raw(struct receiver *rcvr,
		const struct command *cmd, const char *arg)
{
	return cmd_attempt(rcvr, cmd, arg);
}

static int handle_quit(UNUSED struct receiver *rcvr,
		UNUSED const struct command *cmd, UNUSED const char *arg)
{
	return -2;
}

/**
 * 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 prefix the first part of the receiver command string, aka "PWR"
 * @param handler the function that will handle the command
 */
static void add_command(const char *name, const char *prefix,
		cmd_handler *handler, int fake)
{
	/* create our new command object */
	struct command *cmd = malloc(sizeof(struct command));
	cmd->hash = hash_sdbm(name);
	cmd->name = name;
	cmd->prefix = prefix;
	cmd->handler = handler;
	cmd->fake = fake;
	cmd->next = NULL;

	/* 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)
{
	struct command *ptr;
	unsigned int cmd_count = 0;
	/*
	add_command(name,       prefix, handle_func,real/fake); */
	add_command("power",    "PWR", handle_boolean, 0);
	add_command("volume",   "MVL", handle_volume,  0);
	add_command("dbvolume", "MVL", handle_dbvolume,0);
	add_command("mute",     "AMT", handle_boolean, 0);
	add_command("input",    "SLI", handle_input,   0);
	add_command("mode",     "LMD", handle_mode,    0);
	add_command("tune",     "TUN", handle_tune,    0);
	add_command("preset",   "PRS", handle_preset,  0);
	add_command("swlevel",  "SWL", handle_swlevel, 0);
	add_command("avsync",   "AVS", handle_avsync,  0);
	add_command("memory",   "MEM", handle_memory,  0);
	add_command("audyssey", "ADY", handle_boolean, 0); 
	add_command("dyneq",    "ADQ", handle_boolean, 0);

	add_command("status",   NULL,  handle_status,  0);

	add_command("zone2power",  "ZPW", handle_boolean, 0);
	add_command("zone2volume", "ZVL", handle_volume,  0);
	add_command("zone2dbvolume","ZVL",handle_dbvolume,0);
	add_command("zone2mute",   "ZMT", handle_boolean, 0);
	add_command("zone2input",  "SLZ", handle_input,   0);
	add_command("zone2tune",   "TUZ", handle_tune,    0);
	add_command("zone2preset", "PRZ", handle_preset,  0);

	add_command("zone2status", NULL,  handle_status,  0);

	add_command("zone3power",  "PW3", handle_boolean, 0);
	add_command("zone3volume", "VL3", handle_volume,  0);
	add_command("zone3dbvolume","VL3",handle_dbvolume,0);
	add_command("zone3mute",   "MT3", handle_boolean, 0);
	add_command("zone3input",  "SL3", handle_input,   0);
	add_command("zone3tune",   "TU3", handle_tune,    0);
	add_command("zone3preset", "PR3", handle_preset,  0);

	add_command("zone3status", NULL,  handle_status,  0);

	add_command("sleep",       "SLP", handle_sleep,     0);
	add_command("zone2sleep",  "2",   handle_fakesleep, 1);
	add_command("zone3sleep",  "3",   handle_fakesleep, 1);

	add_command("raw",      "",    handle_raw,     0);
	add_command("quit",     "",    handle_quit,    0);

	ptr = command_list;
	while(ptr) {
		cmd_count++;
		ptr = ptr->next;
	}
	printf("%u commands added to command list.\n", cmd_count);
}

/**
 * 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);
		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 rcvr the receiver to process the command for
 * @param str the full command string, e.g. "power on"
 * @return 0 if the command string was correct and sent, -1 on invalid command,
 * -2 if we should quit/close the connection
 * string
 */
int process_command(struct receiver *rcvr, const char *str)
{
	unsigned long hashval;
	char *cmdstr, *argstr;
	char *c;
	struct command *cmd;

	if(!str)
		return(-1);

	cmdstr = strdup(str);
	/* start by killing trailing whitespace of any sort */
	c = cmdstr + strlen(cmdstr) - 1;
	while(isspace(*c) && c >= cmdstr)
		*c-- = '\0';
	/* 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++;
	}

	hashval = hash_sdbm(cmdstr);
	cmd = command_list;
	while(cmd) {
		if(cmd->hash == hashval) {
			/* we found the handler, call it and return the result */
			int ret = cmd->handler(rcvr, cmd, argstr);
			free(cmdstr);
			return(ret);
		}
		cmd = cmd->next;
	}

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


/**
 * Determine if a command is related to the receiver power status. This can
 * be either a query or an explicit command to turn the power on or off.
 * @param cmd the command to process (e.g. "!1PWRQSTN\n")
 * @return whether the command is related to receiver power
 */
int is_power_command(const char *cmd) {
	if(strstr(cmd, "PWR") != NULL || strstr(cmd, "ZPW") != NULL
			|| strstr(cmd, "PW3") != NULL)
		return(1);
	return(0);
}

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