/*
 * 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 "roccat_helper.h"
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/hidraw.h>
#include <sys/time.h>

static void G_GNUC_UNUSED roccat_device_hidraw_debug(RoccatDevice const *roccat_device, gboolean out, guint command, guchar const *data, gssize length) {
#ifndef NDEBUG
	gchar *string;
	gchar *temp_string;
	struct timeval time;

	gettimeofday(&time, NULL);

	temp_string = roccat_data_to_string(data, length);
	string = g_strdup_printf("%llu.%06llu %s %04x/%02x  %s",
			(unsigned long long)time.tv_sec, (unsigned long long)time.tv_usec,
			out ? "OUT" : "IN ",
			gaminggear_device_get_product_id(GAMINGGEAR_DEVICE(roccat_device)),
			command,
			temp_string);

	g_debug("%s", string);

	g_free(temp_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_get_hidraw(roccat_device, error);
	if (fd == -1)
		return NULL;

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

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

	gaminggear_device_unlock(GAMINGGEAR_DEVICE(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_get_hidraw(roccat_device, error);
	if (fd == -1)
		return FALSE;

	gaminggear_device_lock(GAMINGGEAR_DEVICE(roccat_device));

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

	gaminggear_device_unlock(GAMINGGEAR_DEVICE(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
}
