/*
 * 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/savu.h"

static gchar const * const info_name = "info";

SavuInfo *savu_info_read(RoccatDevice *savu, GError **error) {
	return (SavuInfo *)roccat_device_read(savu, info_name, sizeof(SavuInfo), error);
}

static void savu_info_finalize(SavuInfo *info) {
	info->command = SAVU_COMMAND_INFO;
	info->size = sizeof(SavuInfo);
}

static gboolean savu_info_write(RoccatDevice *savu, SavuInfo *info, GError **error) {
	savu_info_finalize(info);
	return roccat_device_write(savu, info_name, (gchar const *)info, sizeof(SavuInfo), error);
}

gboolean savu_reset(RoccatDevice *savu, GError **error) {
	SavuInfo info = { };
	info.firmware_version = SAVU_INFO_FUNCTION_RESET;
	return savu_info_write(savu, &info, error);
}

guint savu_firmware_version_read(RoccatDevice *savu, GError **error) {
	SavuInfo *info;
	guint result;

	info = savu_info_read(savu, error);
	if (!info)
		return 0;
	result = info->firmware_version;
	g_free(info);
	return result;
}

gchar *savu_firmware_version_to_string(guint firmware_version) {
	return g_strdup_printf("%i.%i", firmware_version / 100, firmware_version % 100);
}
