test_com.c 1.46 KB
#include <libusb-1.0/libusb.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
	// init
	libusb_context *context;
	int status = libusb_init(&context);
	if(status != 0){perror("libusb_init"); exit(-1);}

	// devices list
	libusb_device **devices;
	ssize_t devices_count = libusb_get_device_list(context, &devices);
	if(devices_count < 0){perror("libusb_get_device_list"); exit(-1);}
	for(int i=0; i<devices_count; i++){
		libusb_device *device = devices[i];

		// open connection
		libusb_device_handle *handle;
		status=libusb_open(device,&handle);
		if(status!=0){ perror("libusb_open"); exit(-1); }

		// init interface
		// TODO int interface = valueof("bInterfaceNumber");
		status = libusb_claim_interface(handle, interface);
		if(status != 0){perror("libusb_claim_interface"); exit(-1);}

		// detach kernel
		if(libusb_kernel_driver_active(handle, interface)){
			status = libusb_detach_kernel_driver(handle, interface);
			if(status != 0){perror("libusb_detach_kernel_driver"); exit(-1);}
		}

		// use config
		// TODO int configuration = valueof("bConfigurationValue");
		status = libusb_set_configuration(handle, configuration);
		if(status != 0){perror("libusb_set_configuration"); exit(-1);}

		/* ... some code ... */

		// close interface
		status = libusb_release_interface(handle, interface);
		if(status != 0){perror("libusb_release_interface"); exit(-1);}

		// close connection
		libusb_close(handle);
	}
	libusb_free_device_list(devices, 1);

	libusb_exit(context);
}