/*
 * $RCSfile: keypad_cfg.c,v $  $Revision: 1.7 $  $Name:  $
 * $Id: keypad_cfg.c,v 1.7 2007/02/22 03:46:59 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2007/02/22 03:46:59 $
 * ----------------------------------------------------------------------------
 *
 *  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 KeypadLinc configuration 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 "keypad_cfg.h"
#include "scenes.h"

int kpl_list[500];
int kpl_cnt;

int insteon_get_kpl_cfg(ilib_t *iplc, unsigned char *id, kpl_info_t *cfg);
int insteon_set_kpl_cfg(ilib_t *iplc, unsigned char *id, kpl_info_t *cfg);
extern void update_kpl_progress(int amount);

void initialize_keypadlinc_configuration(GladeXML *xml)
{
	kpl_dialog = glade_xml_get_widget(xml, "kpl_dialog");
	kpl_image = glade_xml_get_widget(xml, "kpl_image");
	kpl_progress = glade_xml_get_widget(xml, "kpl_progress");
	kpl_selector = glade_xml_get_widget(xml, "kpl_selector");
	kpl_six_button = glade_xml_get_widget(xml, "kpl_six_button");
	kpl_eight_button = glade_xml_get_widget(xml, "kpl_eight_button");
	kpl_resume_dim = glade_xml_get_widget(xml, "kpl_resume_dim");
	kpl_resume_level = glade_xml_get_widget(xml, "kpl_resume_level");
	kpl_toggle[0] = glade_xml_get_widget(xml, "kpl_1_toggle");
	kpl_toggle[1] = glade_xml_get_widget(xml, "kpl_2_toggle");
	kpl_toggle[2] = glade_xml_get_widget(xml, "kpl_3_toggle");
	kpl_toggle[3] = glade_xml_get_widget(xml, "kpl_4_toggle");
	kpl_toggle[4] = glade_xml_get_widget(xml, "kpl_5_toggle");
	kpl_toggle[5] = glade_xml_get_widget(xml, "kpl_6_toggle");
	kpl_toggle[6] = glade_xml_get_widget(xml, "kpl_7_toggle");
	kpl_toggle[7] = glade_xml_get_widget(xml, "kpl_8_toggle");

	kpl_on[0][0] = glade_xml_get_widget(xml, "kpl_1_1_on");
	kpl_on[0][1] = glade_xml_get_widget(xml, "kpl_1_2_on");
	kpl_on[0][2] = glade_xml_get_widget(xml, "kpl_1_3_on");
	kpl_on[0][3] = glade_xml_get_widget(xml, "kpl_1_4_on");
	kpl_on[0][4] = glade_xml_get_widget(xml, "kpl_1_5_on");
	kpl_on[0][5] = glade_xml_get_widget(xml, "kpl_1_6_on");
	kpl_on[0][6] = glade_xml_get_widget(xml, "kpl_1_7_on");
	kpl_on[0][7] = glade_xml_get_widget(xml, "kpl_1_8_on");
	kpl_on[1][0] = glade_xml_get_widget(xml, "kpl_2_1_on");
	kpl_on[1][1] = glade_xml_get_widget(xml, "kpl_2_2_on");
	kpl_on[1][2] = glade_xml_get_widget(xml, "kpl_2_3_on");
	kpl_on[1][3] = glade_xml_get_widget(xml, "kpl_2_4_on");
	kpl_on[1][4] = glade_xml_get_widget(xml, "kpl_2_5_on");
	kpl_on[1][5] = glade_xml_get_widget(xml, "kpl_2_6_on");
	kpl_on[1][6] = glade_xml_get_widget(xml, "kpl_2_7_on");
	kpl_on[1][7] = glade_xml_get_widget(xml, "kpl_2_8_on");
	kpl_on[2][0] = glade_xml_get_widget(xml, "kpl_3_1_on");
	kpl_on[2][1] = glade_xml_get_widget(xml, "kpl_3_2_on");
	kpl_on[2][2] = glade_xml_get_widget(xml, "kpl_3_3_on");
	kpl_on[2][3] = glade_xml_get_widget(xml, "kpl_3_4_on");
	kpl_on[2][4] = glade_xml_get_widget(xml, "kpl_3_5_on");
	kpl_on[2][5] = glade_xml_get_widget(xml, "kpl_3_6_on");
	kpl_on[2][6] = glade_xml_get_widget(xml, "kpl_3_7_on");
	kpl_on[2][7] = glade_xml_get_widget(xml, "kpl_3_8_on");
	kpl_on[3][0] = glade_xml_get_widget(xml, "kpl_4_1_on");
	kpl_on[3][1] = glade_xml_get_widget(xml, "kpl_4_2_on");
	kpl_on[3][2] = glade_xml_get_widget(xml, "kpl_4_3_on");
	kpl_on[3][3] = glade_xml_get_widget(xml, "kpl_4_4_on");
	kpl_on[3][4] = glade_xml_get_widget(xml, "kpl_4_5_on");
	kpl_on[3][5] = glade_xml_get_widget(xml, "kpl_4_6_on");
	kpl_on[3][6] = glade_xml_get_widget(xml, "kpl_4_7_on");
	kpl_on[3][7] = glade_xml_get_widget(xml, "kpl_4_8_on");
	kpl_on[4][0] = glade_xml_get_widget(xml, "kpl_5_1_on");
	kpl_on[4][1] = glade_xml_get_widget(xml, "kpl_5_2_on");
	kpl_on[4][2] = glade_xml_get_widget(xml, "kpl_5_3_on");
	kpl_on[4][3] = glade_xml_get_widget(xml, "kpl_5_4_on");
	kpl_on[4][4] = glade_xml_get_widget(xml, "kpl_5_5_on");
	kpl_on[4][5] = glade_xml_get_widget(xml, "kpl_5_6_on");
	kpl_on[4][6] = glade_xml_get_widget(xml, "kpl_5_7_on");
	kpl_on[4][7] = glade_xml_get_widget(xml, "kpl_5_8_on");
	kpl_on[5][0] = glade_xml_get_widget(xml, "kpl_6_1_on");
	kpl_on[5][1] = glade_xml_get_widget(xml, "kpl_6_2_on");
	kpl_on[5][2] = glade_xml_get_widget(xml, "kpl_6_3_on");
	kpl_on[5][3] = glade_xml_get_widget(xml, "kpl_6_4_on");
	kpl_on[5][4] = glade_xml_get_widget(xml, "kpl_6_5_on");
	kpl_on[5][5] = glade_xml_get_widget(xml, "kpl_6_6_on");
	kpl_on[5][6] = glade_xml_get_widget(xml, "kpl_6_7_on");
	kpl_on[5][7] = glade_xml_get_widget(xml, "kpl_6_8_on");
	kpl_on[6][0] = glade_xml_get_widget(xml, "kpl_7_1_on");
	kpl_on[6][1] = glade_xml_get_widget(xml, "kpl_7_2_on");
	kpl_on[6][2] = glade_xml_get_widget(xml, "kpl_7_3_on");
	kpl_on[6][3] = glade_xml_get_widget(xml, "kpl_7_4_on");
	kpl_on[6][4] = glade_xml_get_widget(xml, "kpl_7_5_on");
	kpl_on[6][5] = glade_xml_get_widget(xml, "kpl_7_6_on");
	kpl_on[6][6] = glade_xml_get_widget(xml, "kpl_7_7_on");
	kpl_on[6][7] = glade_xml_get_widget(xml, "kpl_7_8_on");
	kpl_on[7][0] = glade_xml_get_widget(xml, "kpl_8_1_on");
	kpl_on[7][1] = glade_xml_get_widget(xml, "kpl_8_2_on");
	kpl_on[7][2] = glade_xml_get_widget(xml, "kpl_8_3_on");
	kpl_on[7][3] = glade_xml_get_widget(xml, "kpl_8_4_on");
	kpl_on[7][4] = glade_xml_get_widget(xml, "kpl_8_5_on");
	kpl_on[7][5] = glade_xml_get_widget(xml, "kpl_8_6_on");
	kpl_on[7][6] = glade_xml_get_widget(xml, "kpl_8_7_on");
	kpl_on[7][7] = glade_xml_get_widget(xml, "kpl_8_8_on");

	kpl_off[0][0] = glade_xml_get_widget(xml, "kpl_1_1_off");
	kpl_off[0][1] = glade_xml_get_widget(xml, "kpl_1_2_off");
	kpl_off[0][2] = glade_xml_get_widget(xml, "kpl_1_3_off");
	kpl_off[0][3] = glade_xml_get_widget(xml, "kpl_1_4_off");
	kpl_off[0][4] = glade_xml_get_widget(xml, "kpl_1_5_off");
	kpl_off[0][5] = glade_xml_get_widget(xml, "kpl_1_6_off");
	kpl_off[0][6] = glade_xml_get_widget(xml, "kpl_1_7_off");
	kpl_off[0][7] = glade_xml_get_widget(xml, "kpl_1_8_off");
	kpl_off[1][0] = glade_xml_get_widget(xml, "kpl_2_1_off");
	kpl_off[1][1] = glade_xml_get_widget(xml, "kpl_2_2_off");
	kpl_off[1][2] = glade_xml_get_widget(xml, "kpl_2_3_off");
	kpl_off[1][3] = glade_xml_get_widget(xml, "kpl_2_4_off");
	kpl_off[1][4] = glade_xml_get_widget(xml, "kpl_2_5_off");
	kpl_off[1][5] = glade_xml_get_widget(xml, "kpl_2_6_off");
	kpl_off[1][6] = glade_xml_get_widget(xml, "kpl_2_7_off");
	kpl_off[1][7] = glade_xml_get_widget(xml, "kpl_2_8_off");
	kpl_off[2][0] = glade_xml_get_widget(xml, "kpl_3_1_off");
	kpl_off[2][1] = glade_xml_get_widget(xml, "kpl_3_2_off");
	kpl_off[2][2] = glade_xml_get_widget(xml, "kpl_3_3_off");
	kpl_off[2][3] = glade_xml_get_widget(xml, "kpl_3_4_off");
	kpl_off[2][4] = glade_xml_get_widget(xml, "kpl_3_5_off");
	kpl_off[2][5] = glade_xml_get_widget(xml, "kpl_3_6_off");
	kpl_off[2][6] = glade_xml_get_widget(xml, "kpl_3_7_off");
	kpl_off[2][7] = glade_xml_get_widget(xml, "kpl_3_8_off");
	kpl_off[3][0] = glade_xml_get_widget(xml, "kpl_4_1_off");
	kpl_off[3][1] = glade_xml_get_widget(xml, "kpl_4_2_off");
	kpl_off[3][2] = glade_xml_get_widget(xml, "kpl_4_3_off");
	kpl_off[3][3] = glade_xml_get_widget(xml, "kpl_4_4_off");
	kpl_off[3][4] = glade_xml_get_widget(xml, "kpl_4_5_off");
	kpl_off[3][5] = glade_xml_get_widget(xml, "kpl_4_6_off");
	kpl_off[3][6] = glade_xml_get_widget(xml, "kpl_4_7_off");
	kpl_off[3][7] = glade_xml_get_widget(xml, "kpl_4_8_off");
	kpl_off[4][0] = glade_xml_get_widget(xml, "kpl_5_1_off");
	kpl_off[4][1] = glade_xml_get_widget(xml, "kpl_5_2_off");
	kpl_off[4][2] = glade_xml_get_widget(xml, "kpl_5_3_off");
	kpl_off[4][3] = glade_xml_get_widget(xml, "kpl_5_4_off");
	kpl_off[4][4] = glade_xml_get_widget(xml, "kpl_5_5_off");
	kpl_off[4][5] = glade_xml_get_widget(xml, "kpl_5_6_off");
	kpl_off[4][6] = glade_xml_get_widget(xml, "kpl_5_7_off");
	kpl_off[4][7] = glade_xml_get_widget(xml, "kpl_5_8_off");
	kpl_off[5][0] = glade_xml_get_widget(xml, "kpl_6_1_off");
	kpl_off[5][1] = glade_xml_get_widget(xml, "kpl_6_2_off");
	kpl_off[5][2] = glade_xml_get_widget(xml, "kpl_6_3_off");
	kpl_off[5][3] = glade_xml_get_widget(xml, "kpl_6_4_off");
	kpl_off[5][4] = glade_xml_get_widget(xml, "kpl_6_5_off");
	kpl_off[5][5] = glade_xml_get_widget(xml, "kpl_6_6_off");
	kpl_off[5][6] = glade_xml_get_widget(xml, "kpl_6_7_off");
	kpl_off[5][7] = glade_xml_get_widget(xml, "kpl_6_8_off");
	kpl_off[6][0] = glade_xml_get_widget(xml, "kpl_7_1_off");
	kpl_off[6][1] = glade_xml_get_widget(xml, "kpl_7_2_off");
	kpl_off[6][2] = glade_xml_get_widget(xml, "kpl_7_3_off");
	kpl_off[6][3] = glade_xml_get_widget(xml, "kpl_7_4_off");
	kpl_off[6][4] = glade_xml_get_widget(xml, "kpl_7_5_off");
	kpl_off[6][5] = glade_xml_get_widget(xml, "kpl_7_6_off");
	kpl_off[6][6] = glade_xml_get_widget(xml, "kpl_7_7_off");
	kpl_off[6][7] = glade_xml_get_widget(xml, "kpl_7_8_off");
	kpl_off[7][0] = glade_xml_get_widget(xml, "kpl_8_1_off");
	kpl_off[7][1] = glade_xml_get_widget(xml, "kpl_8_2_off");
	kpl_off[7][2] = glade_xml_get_widget(xml, "kpl_8_3_off");
	kpl_off[7][3] = glade_xml_get_widget(xml, "kpl_8_4_off");
	kpl_off[7][4] = glade_xml_get_widget(xml, "kpl_8_5_off");
	kpl_off[7][5] = glade_xml_get_widget(xml, "kpl_8_6_off");
	kpl_off[7][6] = glade_xml_get_widget(xml, "kpl_8_7_off");
	kpl_off[7][7] = glade_xml_get_widget(xml, "kpl_8_8_off");

	gtk_image_set_from_file((GtkImage *)kpl_image, "kpl.jpg");
}

/*
 */
void on_kpl_configure_activate(GtkWidget *widget, gpointer user_data)
{
	int i;

	/* First we need to remove any existing entries from the list */
	for (i = 0; i <= last_device; i++) {
		if (device_array[i].device_type == 0x109) {
			gtk_combo_box_remove_text((GtkComboBox *)kpl_selector, 0);
		}
	}

	/* Rebuild the combo box list */
	kpl_cnt = 0;
	for (i = 0; i <= last_device; i++) {
		if (device_array[i].device_type == 0x109) {
			gtk_combo_box_append_text((GtkComboBox *)kpl_selector,
					device_array[i].device_name);
			kpl_list[kpl_cnt++] = i;
		}
	}
	gtk_widget_show(kpl_dialog);
}

/*
 * on_kpl_selector_changed
 *
 * Called when the user selects a specific KeypadLinc. This will have
 * to query the KeypadLinc and set all the widgets to the proper values.
 */
void on_kpl_selector_changed(GtkWidget *widget, gpointer user_data)
{
	int sel;
	int di;
	kpl_info_t kpl_cfg;
	int i, j;

	sel = gtk_combo_box_get_active((GtkComboBox *)kpl_selector);
	if (sel == -1) {
		printf("No Keypad selected!\n");
		return;
	}

	di = kpl_list[sel];

	gtk_progress_bar_set_fraction((GtkProgressBar *)kpl_progress, 0.0);
	gtk_progress_bar_set_text((GtkProgressBar *)kpl_progress,
			"Loading Config...");

	/* Need to PEEK all the information from the KPL and set all the widgets */
	if (insteon_get_kpl_cfg(iplc, device_array[di].device_id, &kpl_cfg) == 0) {
		gtk_toggle_button_set_active((GtkToggleButton *)kpl_resume_dim,
				(kpl_cfg.resume_dim_enable & 0x01));
		gtk_spin_button_set_value((GtkSpinButton *)kpl_resume_level,
				(gdouble)kpl_cfg.last_on_level);
		for(i = 0; i < 8; i++) {
			gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(kpl_toggle[i]),
					!((kpl_cfg.toggle_mask >> i) & 0x01));
			for(j = 0; j < 8; j++) {
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(kpl_on[i][j]),
						((kpl_cfg.on_mask[i] >> j) & 0x01));
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(kpl_off[i][j]),
						((kpl_cfg.off_mask[i] >> j) & 0x01));
			}
		}
	} else {
		printf("Failed to get keypad linc info\n");
	}
	gtk_progress_bar_set_fraction((GtkProgressBar *)kpl_progress, 1.0);
	gtk_progress_bar_set_text((GtkProgressBar *)kpl_progress, "Config Loaded");
}


void on_kpl_cancel_clicked(GtkWidget *widget, gpointer user_data)
{
	gtk_widget_hide(kpl_dialog);
}

void set_kpl(GtkWidget *widget)
{
	int sel;
	int di;
	kpl_info_t kpl_cfg;
	int i, j;

	sel = gtk_combo_box_get_active((GtkComboBox *)kpl_selector);
	if (sel == -1) {
		printf("No Keypad selected!\n");
		return;
	}

	di = kpl_list[sel];

	gtk_progress_bar_set_fraction((GtkProgressBar *)kpl_progress, 0.0);
	gtk_progress_bar_set_text((GtkProgressBar *)kpl_progress,
			"Saving Config...");

	/* What do the widgets say to set */
	if (gtk_toggle_button_get_active((GtkToggleButton *)kpl_resume_dim)) {
		kpl_cfg.resume_dim_enable |= 0x01;
	} else {
		kpl_cfg.resume_dim_enable &= 0xfe;
	}

	kpl_cfg.last_on_level = gtk_spin_button_get_value_as_int(
			(GtkSpinButton *)kpl_resume_level);

	kpl_cfg.toggle_mask = 0;
	for(i = 0; i < 8; i++) {
		if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(kpl_toggle[i]))) {
			kpl_cfg.toggle_mask |= (1 << i);
		}
		kpl_cfg.on_mask[i] = 0;
		kpl_cfg.off_mask[i] = 0;
		for(j = 0; j < 8; j++) {
			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(kpl_on[i][j]))){
				kpl_cfg.on_mask[i] |= (1 << j);
			}
			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(kpl_off[i][j]))){
				kpl_cfg.off_mask[i] |= (1 << j);
			}
		}
	}


	insteon_set_kpl_cfg(iplc, device_array[di].device_id, &kpl_cfg);

	gtk_progress_bar_set_fraction((GtkProgressBar *)kpl_progress, 1.0);
	gtk_progress_bar_set_text((GtkProgressBar *)kpl_progress, "Config Saved");

	salad_send_message(iplc, DO_READ_EE, 0x00, device_array[di].device_id);
}

void on_kpl_apply_clicked(GtkWidget *widget, gpointer user_data)
{
	set_kpl(widget);
	/* gtk_widget_hide(kpl_dialog); */
}

void on_kpl_ok_clicked(GtkWidget *widget, gpointer user_data)
{
	set_kpl(widget);
	gtk_widget_hide(kpl_dialog);
}


/*
 * insteon_get_kpl_cfg
 *
 * Query a KeypadLinc V2 for all it's configuration details.
 */
int insteon_get_kpl_cfg(ilib_t *iplc, unsigned char *id, kpl_info_t *cfg)
{
	unsigned short addr;
	int ret;
	int i;
	int progress = 1;

	if ((ret = insteon_read_byte(iplc, id, 0x0020)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	cfg->last_on_level = ret & 0xff;
	printf("last_on_level = 0x%02x\n", ret);
	update_kpl_progress(progress++);

	if ((ret = insteon_read_byte(iplc, id, 0x0022)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	cfg->resume_dim_enable = ret & 0xff;
	printf("resume_dim_enable = 0x%02x\n", ret);
	update_kpl_progress(progress++);

	/* X10 house code */
	if ((ret = insteon_read_byte(iplc, id, 0x0030)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	cfg->x10_house_code = ret & 0xff;
	update_kpl_progress(progress++);

	/* X10 unit code */
	if ((ret = insteon_read_byte(iplc, id, 0x0031)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	cfg->x10_unit_code = ret & 0xff;
	update_kpl_progress(progress++);

	/* Curent on level */
	if ((ret = insteon_read_byte(iplc, id, 0x0032)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	cfg->current_level = ret & 0xff;
	printf("current_level = 0x%02x\n", ret);
	update_kpl_progress(progress++);

	/* OnLEDMask */
	for (addr = 0x0241; addr <= 0x0248; addr++) {
		ret = insteon_read_byte(iplc, id, addr);
		cfg->on_mask[addr - 0x0241] = ret & 0xff;
		update_kpl_progress(progress);
		progress += 3;
	}

	/* OffLEDMask */
	for (addr = 0x024a; addr <= 0x0251; addr++) {
		ret = insteon_read_byte(iplc, id, addr);
		cfg->off_mask[addr - 0x024a] = ret & 0xff;
		update_kpl_progress(progress);
		progress += 3;
	}

	/* button toggle mask */
	if ((ret = insteon_read_byte(iplc, id, 0x0249)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	cfg->toggle_mask = ret & 0xff;
	printf("toggle mask = 0x%02x\n", ret);
	update_kpl_progress(progress++);

	/* current LED state */
	if ((ret = insteon_read_byte(iplc, id, 0x0262)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	cfg->led_state = ret & 0xff;
	printf("led state = 0x%02x\n", ret);
	update_kpl_progress(progress++);

	/* X10 group info */
	i = 0;
	for (addr = 0x0252; addr <= 0x0261; addr++) {
		ret = insteon_read_byte(iplc, id, addr);
		cfg->x10_house_codes[i] = ret & 0xff;
		addr++;
		update_kpl_progress(progress);
		progress += 3;
		ret = insteon_read_byte(iplc, id, addr);
		cfg->x10_unit_codes[i] = ret & 0xff;
		i++;
		update_kpl_progress(progress);
		progress += 3;
	}

	return 0;
}


/*
 * insteon_set_kpl_cfg
 *
 * Program a KeypadLinc.
 */
int insteon_set_kpl_cfg(ilib_t *iplc, unsigned char *id, kpl_info_t *cfg)
{
	unsigned short addr;
	int ret;
	int progress = 1;
	/* int i; */

	if ((ret = insteon_write_byte(iplc, id, 0x0020, cfg->last_on_level)) == -1) {
		printf("kpl: write failure\n");
		return -1;
	}
	update_kpl_progress(progress);
	progress += 6;

	if ((ret = insteon_write_byte(iplc, id, 0x0022, cfg->resume_dim_enable)) == -1){
		printf("kpl: write failure\n");
		return -1;
	}
	update_kpl_progress(progress);
	progress += 6;

	/* X10 house code */
	/*
	if ((ret = insteon_write_byte(iplc, id, 0x0030, cfg->x10_house_code)) == -1) {
		printf("kpl: write failure\n");
		return -1;
	}
	*/

	/* X10 unit code */
	/*
	if ((ret = insteon_write_byte(iplc, id, 0x0031, cfg->x10_unit_code)) == -1) {
		printf("kpl: read failure\n");
		return -1;
	}
	*/


	/* OnLEDMask */
	for (addr = 0x0241; addr <= 0x0248; addr++) {
		ret = insteon_write_byte(iplc, id, addr, cfg->on_mask[addr-0x0241]);
		update_kpl_progress(progress);
		progress += 6;
	}

	/* OffLEDMask */
	for (addr = 0x024a; addr <= 0x0251; addr++) {
		ret = insteon_write_byte(iplc, id, addr, cfg->off_mask[addr-0x024a]);
		update_kpl_progress(progress);
		progress += 6;
	}

	/* button toggle mask */
	if ((ret = insteon_write_byte(iplc, id, 0x0249, cfg->toggle_mask)) == -1) {
		printf("kpl: write failure\n");
		return -1;
	}
	update_kpl_progress(progress);
	progress += 5;

	/* X10 group info */
	/*
	i = 0;
	for (addr = 0x0252; addr <= 0x0261; addr++) {
		ret = insteon_write_byte(iplc, id, addr, cfg->x10_hose_codes[i]);
		addr++;
		ret = insteon_write_byte(iplc, id, addr, cfg->x10_unit_codes[i]);
		i++;
	}
	*/

	return 0;
}

