/*
 * $RCSfile: icmd.c,v $  $Revision: 1.9 $  $Name:  $
 * $Id: icmd.c,v 1.9 2007/09/09 18:16:04 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2007/09/09 18:16:04 $
 * ----------------------------------------------------------------------------
 *
 *  Copyright (c) Bob Paauwe (2006)
 *
 *  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.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * ----------------------------------------------------------------------------
 *
 *  Description:
 *    A program to send Insteon commands to Insteon devices.
 * ----------------------------------------------------------------------------
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <time.h>
#include "ilib.h"

#define VERSION "icmd version 0.2.20a Copyright (C) 2006, 2007, Bob Paauwe"

void usage(char *prog)
{
	printf("usage: %s <xx.xx.xx> <cmd> <data>\n", prog);
	printf("   Valid Commands:\n");
	print_commands();
	printf("  OR\n");
	printf("       %s <x10 command> <x10 address>\n", prog);
}

int main (int argc, char **argv)
{
	ilib_t *iplc;
	unsigned char device_id[10];
	unsigned char buffer[10];
	unsigned char test[10];

	unsigned char cmd = 0x00;
	unsigned char data = 0x00;
	int cmd_num;
	int i;
	int verbose = 0;
	int group = 0;
	int x10 = 0;
	int ret;

	if (argc > 0) {
		for (i = 1; i < argc; i++) {
			if (argv[i][0] == '-') {
				switch (argv[i][1]) {
					case 'V':
						printf("%s\n", VERSION);
						return 0;
						break;
					case 'h':
						usage(argv[0]);
						return 0;
						break;
					case 'v':
						verbose = 1;
						break;
					case 'g':
						group = 1;
						break;
					case 'x':
						x10 = 1;
						cmd = alpha_to_i(argv[++i]);
						data = alpha_to_i(argv[++i]);
						break;
					default:
						usage(argv[0]);
						return 0;
				}
			} else {
				memcpy(&device_id[0], str_id_to_hex(argv[i]), 3);
				if (++i < argc) {
					cmd_num = command_to_num(argv[i]);
					if (cmd_num == -1) {
						cmd = alpha_to_i(argv[i]);
					} else {
						cmd = cmd_num;
					}
				}
				if (++i < argc) {
					data = alpha_to_i(argv[i]);
				}
			}
		}
	} else {
		usage(argv[0]);
	}


	if ((iplc = ilib_open(USE_HIDDEV, "/dev/hiddev0")) != NULL) {
	/* if ((iplc = ilib_open(USE_IPLC, "/dev/usb/iplc0")) != NULL) { */
		/* Send some test commands */
		if (ibios_get_version(iplc, &test[0], buffer) == 0) {
			if (verbose) {
				printf("PLC ID: %s   Firmware version %02x\n", buffer, test[0]);
			}
		} else {
			printf("Command failed\n");
		}

		if (cmd) {
			if (!x10) {
				if (verbose) {
					printf("Sending command 0x%02x 0x%02x to %02x.%02x.%02x\n",
							cmd, data, device_id[0], device_id[1], device_id[2]);
				}
				if (group) {
					ret = insteon_send_group_msg(iplc, cmd, data, &device_id[0]);
				} else {
					ret = insteon_send_msg(iplc, cmd, data, &device_id[0]);
				}
			} else {
				if (verbose) {
					printf("Sending command 0x%02x 0x%02x\n", cmd, data);
				}
				send_x10(iplc, data, cmd);
			}
		}

		ilib_close(iplc);
		printf("%d\n", (ret & 0xff));
	} else {
		printf("Failed to initialize PLC USB driver\n");
	}

	return 0;
}


/* Stub for insteon_msg.c file */
void update_kpl_progress(int amount)
{
	return;
}

