/*
 * $RCSfile: device_discover.c,v $  $Revision: 1.2 $  $Name:  $
 * $Id: device_discover.c,v 1.2 2006/02/27 04:39:00 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2006/02/27 04:39:00 $
 * ----------------------------------------------------------------------------
 *
 *  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:
 *    Functions to handle the device discovery dialog box.
 * ----------------------------------------------------------------------------
 */

#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <glade/glade-build.h>

#ifdef WITH_GNOME
#  include <libgnomeui/libgnomeui.h>
#endif
#include "globals.h"
#include "device_discover.h"
#include "scenes.h"

static unsigned char dd_g_device_id[3];
static unsigned short dd_g_device_type;
static int dd_g_invalid;


void initialize_device_discovery(GladeXML *xml)
{
	dd_dialog = glade_xml_get_widget(xml, "dd_dialog");
	dd_device_id = glade_xml_get_widget(xml, "dd_device_id");
	dd_device_type = glade_xml_get_widget(xml, "dd_device_type");
	dd_firmware = glade_xml_get_widget(xml, "dd_firmware");
	dd_name = glade_xml_get_widget(xml, "dd_name");

	gtk_widget_hide(dd_dialog);
}

/*
 */
void on_device_discovery_activate(GtkWidget *widget, gpointer user_data)
{
	gtk_entry_set_text((GtkEntry *)dd_device_id, "");
	gtk_entry_set_text((GtkEntry *)dd_device_type, "");
	gtk_entry_set_text((GtkEntry *)dd_firmware, "");
	gtk_entry_set_text((GtkEntry *)dd_name, "");
	gtk_widget_show(dd_dialog);
	dd_g_invalid = 1;
}

/*
 * on_dd_start_clicked
 *
 * Called when the user clicks the start button. This puts the PLC into
 * enrollement mode for 4 minutes or until it recevies a device response.
 */
void on_dd_start_clicked(GtkWidget *widget, gpointer user_data)
{
	unsigned char buffer[100];
	char dtext[50];

	if (insteon_detect_device(iplc, buffer) != -1) {
		printf("Found device: %02x.%02x.%02x %s rev %d.%d\n",
				buffer[2], buffer[3], buffer[4],
				device_type_name(buffer[5], buffer[6]),
				(buffer[7] >> 4), (buffer[7] & 0x0f));

		memcpy(dd_g_device_id, &buffer[2], 3);
		dd_g_device_type = (buffer[5] * 256) + buffer[6];
		dd_g_invalid = 0;

		sprintf(dtext, "%02x.%02x.%02x", buffer[2], buffer[3], buffer[4]);
		gtk_entry_set_text((GtkEntry *)dd_device_id, dtext);

		sprintf(dtext, "%s", device_type_name(buffer[5], buffer[6]));
		gtk_entry_set_text((GtkEntry *)dd_device_type, dtext);

		sprintf(dtext, "%d.%d", (buffer[7] >> 4), (buffer[7] & 0x0f));
		gtk_entry_set_text((GtkEntry *)dd_firmware, dtext);
	} else {
		printf("Device discovery timed out!\n");
		gtk_entry_set_text((GtkEntry *)dd_device_id, "");
		gtk_entry_set_text((GtkEntry *)dd_device_type, "");
		gtk_entry_set_text((GtkEntry *)dd_firmware, "");
		memset(dd_g_device_id, 0, 3);
		dd_g_device_type = 0;
		dd_g_invalid = 1;
	}
}


void on_dd_add_clicked(GtkWidget *widget, gpointer user_data)
{
	char *name;

	/* Is the data on the form valid? */
	if (dd_g_invalid) {
		return;
	}

	/* Should check to make sure device id doesn't already exist */
	name = (char *)gtk_entry_get_text((GtkEntry *)dd_device_id);
	if (find_device((unsigned char *)name)) {
		printf("Device id already exist in device list, not adding.\n");
		return;
	}

	name = (char *)gtk_entry_get_text((GtkEntry *)dd_name);

	last_device++;
	device_array[last_device].device_index = last_device;
	device_array[last_device].num_of_entries = 0;
	device_array[last_device].device_name = (char *)malloc(strlen(name)+1);
	strcpy(device_array[last_device].device_name, name);
	device_array[last_device].device_type = dd_g_device_type;
	memcpy(device_array[last_device].device_id, dd_g_device_id, 3);

	/* Now add it to the combo boxes */
	ilink_add_device(last_device);
}

void on_dd_close_clicked(GtkWidget *widget, gpointer user_data)
{
	gtk_widget_hide(dd_dialog);
}
