/*
 * This file is part of roccat-tools.
 *
 * roccat-tools 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.
 *
 * roccat-tools 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 roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "roccat_device_hidraw.h"
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/hidraw.h>

static void roccat_device_hidraw_debug(RoccatDevice const *roccat_device, gboolean out, guint command, guchar const *data, gssize length) G_GNUC_UNUSED;

void roccat_device_hidraw_debug(RoccatDevice const *roccat_device, gboolean out, guint command, guchar const *data, gssize length) {
#ifndef NDEBUG
	gssize i;
	gchar *string;
	gchar *tmpstring;

	if (length == 0)
		return;

	string = g_strdup_printf("%s %04x/%02x",
			out ? "OUT" : "IN ",
			roccat_device_get_product_id(roccat_device),
			command);
	for (i = 0; i < length; ++i) {
		tmpstring = g_strdup_printf("%s %02x", string, data[i]);
		g_free(string);
		string = tmpstring;
	}
	g_debug("%s", string);
	g_free(string);
#endif
}

gchar *roccat_device_hidraw_read(RoccatDevice *roccat_device, guint command, gssize length, GError **error) {
#ifdef HIDIOCGFEATURE
	gchar *buffer;
	gint retval;
	gint fd;

	fd = roccat_device_open_hidraw_dev(roccat_device, error);
	if (fd == -1)
		return NULL;

	buffer = g_malloc(length);
	buffer[0] = command;
	
	roccat_device_lock(roccat_device);

	retval = ioctl(fd, HIDIOCGFEATURE(length), buffer);

	roccat_device_unlock(roccat_device);

	if (retval == -1) {
		g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), "Error in ioctl HIDIOCGFEATURE: %s", g_strerror(errno));
		g_free(buffer);
		return NULL;
	}

	roccat_device_hidraw_debug(roccat_device, FALSE, command, (guchar const *)buffer, length);

	return buffer;
#else
	g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_NOSYS, "ioctl HIDIOCGFEATURE not implemented");
	return NULL;
#endif
}

gboolean roccat_device_hidraw_write(RoccatDevice *roccat_device, gchar const *buffer, gssize length, GError **error) {
#ifdef HIDIOCSFEATURE
	int retval;
	int fd;

	fd = roccat_device_open_hidraw_dev(roccat_device, error);
	if (fd == -1)
		return FALSE;

	roccat_device_lock(roccat_device);

	retval = ioctl(fd, HIDIOCSFEATURE(length), buffer);

	roccat_device_unlock(roccat_device);

	if (retval == -1) {
		g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), "Error in ioctl HIDIOCSFEATURE: %s", g_strerror(errno));
		return FALSE;
	}

	roccat_device_hidraw_debug(roccat_device, TRUE, buffer[0], (guchar const *)buffer, length);

	return TRUE;
#else
	g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_NOSYS, "ioctl HIDIOCSFEATURE not implemented");
	return FALSE;
#endif
}
