예제 #1
0
파일: usbhid.c 프로젝트: dihmuzikien/ECE473
int usbhidOpenDevice(usbDevice_t **device, int vendor, char *vendorName, int product, char *productName)
{
    printf("Opening device...\n");
	struct usb_bus      *bus;
	struct usb_device   *dev;
	usb_dev_handle      *handle = NULL;
	int                 errorCode = USBOPEN_ERR_NOTFOUND;
	static int          didUsbInit = 0;

    if(!didUsbInit){
        usb_init();
        didUsbInit = 1;
    }
    printf("Busses = %d\n", usb_find_busses());
    printf("Devices = %d\n", usb_find_devices());
    for(bus=usb_get_busses(); bus; bus=bus->next){
        printf("bus...\n");
        for(dev=bus->devices; dev; dev=dev->next){
            printf("dev...\n");
            if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){
		printf("Device with matching ID's found!\n");
                char    string[256];
                int     len;
                handle = usb_open(dev); /* we need to open the device in order to query strings */
                if(!handle){
                    errorCode = USBOPEN_ERR_ACCESS;
                    fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror());
                    continue;
                }
                if(vendorName == NULL && productName == NULL){  /* name does not matter */
                    break;
                }
                /* now check whether the names match: */
                len = usbhidGetStringAscii(handle, dev->descriptor.iManufacturer, string, sizeof(string));
                if(len < 0){
                    errorCode = USBOPEN_ERR_IO;
                    fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
                }else{
                    errorCode = USBOPEN_ERR_NOTFOUND;
                    /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */
                    if(strcmp(string, vendorName) == 0){
                        len = usbhidGetStringAscii(handle, dev->descriptor.iProduct, string, sizeof(string));
                        if(len < 0){
                            errorCode = USBOPEN_ERR_IO;
                            fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror());
                        }else{
                            errorCode = USBOPEN_ERR_NOTFOUND;
                            /* fprintf(stderr, "seen product ->%s<-\n", string); */
                            if(strcmp(string, productName) == 0)
                                break;
                        }
                    }
                }
                usb_close(handle);
                handle = NULL;
            }
        }
        if(handle)
            break;
    }
    if(handle != NULL){
        errorCode = 0;
        *device = (void *)handle;
    }
    return errorCode;
}
예제 #2
0
파일: hiddev.cpp 프로젝트: nodep/nrfburn
int usbhidOpenDevice(usbDevice_t** device, int vendor, const char* vendorName, int product, const char* productName)
{
	struct usb_bus*     bus;
	struct usb_device*  dev;
	usb_dev_handle*     handle = NULL;
	int                 errorCode = USBOPEN_ERR_NOTFOUND;
	static int          didUsbInit = 0;

	if (!didUsbInit)
	{
		usb_init();
		didUsbInit = 1;
	}

	usb_find_busses();
	usb_find_devices();

	for (bus = usb_get_busses(); bus; bus = bus->next)
	{
		for (dev = bus->devices; dev; dev = dev->next)
		{
			if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product)
			{
				char    string[256];
				int     len;
				handle = usb_open(dev); // we need to open the device in order to query strings
				if (!handle)
				{
					errorCode = USBOPEN_ERR_ACCESS;
					DEBUG_PRINT(("Warning: cannot open USB device: %s\n", usb_strerror()));
					continue;
				}

				if (vendorName == NULL && productName == NULL)  // name does not matter
					break;

				// now check whether the names match:
				len = usbhidGetStringAscii(handle, dev->descriptor.iManufacturer, string, sizeof(string));
				if (len < 0)
				{
					errorCode = USBOPEN_ERR_IO;
					DEBUG_PRINT(("Warning: cannot query manufacturer for device: %s\n", usb_strerror()));
				} else {
					errorCode = USBOPEN_ERR_NOTFOUND;
					if (strcmp(string, vendorName) == 0)
					{
						len = usbhidGetStringAscii(handle, dev->descriptor.iProduct, string, sizeof(string));
						if (len < 0)
						{
							errorCode = USBOPEN_ERR_IO;
							DEBUG_PRINT(("Warning: cannot query product for device: %s\n", usb_strerror()));
						} else {
							errorCode = USBOPEN_ERR_NOTFOUND;
							if (strcmp(string, productName) == 0)
								break;
						}
					}
				}
				usb_close(handle);
				handle = NULL;
			}
		}

		if (handle)
			break;
	}

	if (handle != NULL)
	{
		errorCode = USBOPEN_SUCCESS;
		*device = (usbDevice_t*)handle;
	}

	return errorCode;
}
예제 #3
0
파일: libhid.c 프로젝트: samy-gh/avr_proj
const T_HID_HDL* HidOpen( const char* const my_manufacturer, const char* const my_product )
{
	struct usb_bus		*bus;
	struct usb_device	*dev;
	usb_dev_handle		*handle = NULL;
	T_HID_HDL_LOCAL*	libhid_handle = NULL;
	const int			my_product_id = MY_PID;

	usb_find_busses();
	usb_find_devices();

	for( bus = usb_get_busses(); bus; bus = bus->next ) {
		for( dev = bus->devices; dev; dev = dev->next ) {
			gdev = dev;		/* @@@ add by tanioka */
#if DEBUG
			printf("dev=%04x:%04x\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
#endif
			if( (dev->descriptor.idVendor == MY_VID)
			 && (dev->descriptor.idProduct == my_product_id) ) {
				char	string[256];
				int		len;
				handle = usb_open( dev ); /* we need to open the device in order to query strings */
				if(!handle){
					fprintf( stderr, "Warning: cannot open USB device: %s\n", usb_strerror() );
					continue;
				}
				if( usb_set_configuration( handle, dev->config->bConfigurationValue ) < 0 ) {
					fprintf( stderr, "!!usb_set_configuration Error.\n" );
				}
#if DEBUG
				printf( "!!usb_set_configuration.\n" );
#endif

				if( usb_claim_interface( handle, dev->config->interface->altsetting->bInterfaceNumber ) < 0 ) {
					fprintf( stderr, "!!usb_claim_interface Error.\n" );
				}
#if DEBUG
				printf( "usb_claim_interface.\n" );
#endif

				/* now check whether the names match: */
				len = usbhidGetStringAscii( handle, dev->descriptor.iManufacturer, string, sizeof(string) );
				if( len < 0 ) {
					fprintf( stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror() );
				} else {
#if DEBUG
					printf( "seen device from vendor [%s]\n", string );
#endif
					if( strcmp( string, my_manufacturer ) == 0 ) {
						len = usbhidGetStringAscii( handle, dev->descriptor.iProduct, string, sizeof(string) );
						if( len < 0 ) {
							fprintf( stderr, "Warning: cannot query product for device: %s\n", usb_strerror() );
						} else {
#if DEBUG
							fprintf( stderr, "seen product [%s]\n", string );
#endif
							if( strcmp( string, my_product ) == 0 ) {
								break;
							}
						}
					}
				}
				usb_close( handle );
				handle = NULL;
			}
		}
	}
	if(handle != NULL){
		libhid_handle = malloc( sizeof(T_HID_HDL_LOCAL) );
		if( libhid_handle ) {
			libhid_handle->handle = handle;
		}
		else {
//			usb_reset( (void*)handle );
			usb_close( (void*)handle );
		}
	}
#if LINUX
	setuid(getuid());	// @@@ by iruka
#endif
	return (T_HID_HDL*)libhid_handle;
}