/*
 * $RCSfile: monitor.c,v $  $Revision: 1.8 $  $Name:  $
 * $Id: monitor.c,v 1.8 2007/09/09 18:16:05 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2007/09/09 18:16:05 $
 * ----------------------------------------------------------------------------
 *
 *  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:
 *    Monitor Insteon traffic as seen by the PLC and display it on the screen
 *
 * ----------------------------------------------------------------------------
 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <memory.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include "ilib.h"

/*
 * FIXME:  This function is not exported by the library.  Need to add
 * a wrapper to the library that allows this.
 */
int plc_read_response(ilib_t *iplc, unsigned char *buf, int size,
		unsigned char match);


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

void usage(char *prog)
{
	printf("%s\n\n", VERSION);
	printf("This program will monitor any Insteon traffic that the PLC\n");
	printf("sees and reports. For best results, all devices should be\n");
	printf("inserted into the PLC's link database.\n");
	printf("\n");
	printf("usage: %s\n", prog);
}

int main (int argc, char **argv)
{
	ilib_t *iplc;
	char mtype[20];
	unsigned char buffer[256];
	time_t tm;
	int i, j;

	if (argc > 0) {
		for (i = 1; i < argc; i++) {
			if (argv[i][0] == '-') {
				switch (argv[i][1]) {
					case 'v':
						printf("%s\n", VERSION);

						printf("arg = %d\n", alpha_to_i(argv[++i]));
						return 0;
						break;
					case 'h':
						usage(argv[0]);
						return 0;
						break;
					default:
						break;
				}
			}
		}
	}

	printf("%s\n", VERSION);
	/* if ((iplc = ilib_open(USE_LIBHID, NULL)) != NULL) { */
	/* if ((iplc = ilib_open(USE_LIBUSB, NULL)) != NULL) { */
	/* if ((iplc = ilib_open(USE_IPLC, "/dev/usb/iplc0")) != NULL) { */
	if ((iplc = ilib_open(USE_HIDDEV, "/dev/hiddev0")) != NULL) {
		/* Go into monitor mode */
		while (1) {
			if ((j = plc_read_response(iplc, buffer, 256, 0x4f)) > 0) {

				time(&tm);
				strftime(mtype, 100, "%F %T", localtime(&tm));
				printf("%s: ", mtype);

				if (buffer[1] == 0x02) {
					strcpy(mtype, "**");
				} else if (buffer[1] == 0x03) {
					strcpy(mtype, "rr");
				} else {
					strcpy(mtype, "??");
				}
				printf(" %s %02x.%02x.%02x -> %02x.%02x.%02x  0x%02x "
						"%s %d %s\n", mtype,
						buffer[5], buffer[6], buffer[7],
						buffer[2], buffer[3], buffer[4],
						buffer[8], command(buffer[9]), buffer[10],
						flags(buffer[8]));
			}
			usleep(10000);
		}

		ilib_close(iplc);
	}

	return 0;
}
