/*
 * 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
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <error.h>
#include <signal.h>

#include <libusb-1.0/libusb.h>

#define SHUTTLE_INTERFACE 0

libusb_device_handle *devsh = 0;
int claimed = -1;

void usb_done()
{
	if( devsh ) {
		if( claimed > 0 ) {
			int sh_iface = SHUTTLE_INTERFACE;
			libusb_release_interface(devsh, sh_iface);
			libusb_attach_kernel_driver(devsh, sh_iface);
			claimed = 0;
		}
		libusb_close(devsh);
		devsh = 0;
	}
	if( claimed >= 0 ) {
		libusb_exit(0);
		claimed = -1;
	}
}

void usb_probe()
{
	int ret = libusb_init(0);
	if( ret < 0 ) return;
	claimed = 0;
	devsh = libusb_open_device_with_vid_pid(0, 0x0b33, 0x0030);
	if( devsh ) {
		int sh_iface = SHUTTLE_INTERFACE;
		libusb_detach_kernel_driver(devsh, sh_iface);
		ret = libusb_claim_interface(devsh, sh_iface);
		if( ret >= 0 ) claimed = 1;
	}
	if( !claimed )
		usb_done();
}

int done = 0;
void sigint(int n) { done = 1; }

int main(int ac, char **av)
{
	setbuf(stdout, 0);
	usb_probe();
	if( claimed > 0 ) {
		signal(SIGINT, sigint);
		while( !done ) {
			int len = 0;
			static const int IN_ENDPOINT = 0x81;
			unsigned char dat[5];
			int ret = libusb_interrupt_transfer(devsh,
					IN_ENDPOINT, dat, sizeof(dat), &len, 100);
			if( ret != 0 ) {
				if( ret == LIBUSB_ERROR_TIMEOUT ) continue;
				printf("error: %s\n", libusb_strerror((libusb_error)ret));
				sleep(1);  continue;
			}
			printf("shuttle: ");
			for( int i=0; i<len; ++i ) printf(" %02x", dat[i]);
			printf("\n");
		}
	}
	usb_done();
	return 0;
}

