Exemple #1
0
//! Destructor, de-initializes, frees memory allocated for buffers, etc.
UsbInterface::~UsbInterface()
{
	try {
		closeUsb();	//lint !e534
#ifdef USE_WINUSB
		::CloseHandle(d->m_quitEvent);			//lint !e534
		for (int i = 0; i < d->m_oCount; ++i)
			::CloseHandle(d->m_waitEvents[i]);	//lint !e534
		::DeleteCriticalSection(&d->m_mutex);
#endif
		delete d;
	} catch(...)
	{}
}
Exemple #2
0
/*! \brief Close the USB communication port.
*/
XsResultValue UsbInterface::close(void)
{
	return closeUsb();
}
/**
 * try to find a android accessory device, and try to open it.
 *
 * @return handle of the accessory device, or null if none are found.
 */
libusb_device_handle* findAndInitAccessory(
		libusb_context* ctx,
		const char* manufacturer,
		const char* modelName,
		const char* description,
		const char* version,
		const char* uri,
		const char* serialNumber)
{
	libusb_device** list;
#ifdef DEBUG
	printf("libAndroidAccessory: Enumerating devices\n");
#endif
	ssize_t cnt = libusb_get_device_list(ctx, &list);

	int i, j;
	struct libusb_device_descriptor desc;
	libusb_device_handle* handle = NULL;
	for (i = 0; i < cnt; i++) {
		libusb_device* tmpdevice = list[i];

		if (libusb_get_device_descriptor(tmpdevice, &desc) < 0) {
			continue;
		}

		debugDescriptor(&desc);

		for (j = 0; j < (sizeof vendors / sizeof *vendors); ++j)
		{
			if (desc.idVendor == vendors[j]) {
#ifdef DEBUG
				printf("libAndroidAccessory: Supported vendor Id found: %04hx\n", desc.idVendor);
#endif
				// Check to see if the device is already in accessory mode
				if(desc.idVendor == GOOGLE)
				{
					switch(desc.idProduct)
					{
						case ACCESSORY:
						case ACCESSORY_ADB:
						case ACCESSORY_AUDIO:
						case ACCESSORY_AUDIO_ADB:
#ifdef DEBUG
							printf("libAndroidAccessory: Connected device already in accessory mode, continuing!\n");
#endif
							; // Do not remove this empty statement. Removing it might make gcc refuse to compile our project.
							int errorCode = libusb_open(tmpdevice, &handle);
							if ( errorCode != 0 )
							{
								fprintf(stderr, "libAndroidAccessory: %s %d -> %s\n",__FILE__, __LINE__, libusb_error_name(errorCode));
							}
							determineEndpoints(tmpdevice);
							libusb_release_interface(handle, 0);
							libusb_release_interface(handle, 1);
							libusb_free_device_list(list, 1);
							return handle;
						default:
							break;
					}
				}

				// We should in no circumstance try to put a accessory in accessory mode. This should filter most of them out.
				if (desc.idVendor == GOOGLE && (desc.idProduct >= ACCESSORY && desc.idProduct <= ACCESSORY_AUDIO_ADB) )
				{
					printf("libAndroidAccessory: Ignoring a device which is already in accessory mode\n");
					break;
				}

				// We're not already in accessory mode, try to send configuration commands.
				handle = openUsb(ctx, desc.idVendor, desc.idProduct); //try to open the USB interface

				if (handle != NULL) {
					int version_supported = -1;

					version_supported = checkAndroid(handle);

					switch(version_supported) {
					case 2:
#ifdef DEBUG
						printf("libAndroidAccessory: Android Open Accessory version 2 support detected\n");
#endif
						// Fallthrough to next case
						/* no break */
					case 1:
#ifdef DEBUG
						printf("libAndroidAccessory: Android Open Accessory version 1 support detected\n");
						printf("libAndroidAccessory: Trying to setup accessory mode\n");
#endif
						handle = setupAccessory(
								manufacturer,
								modelName,
								description,
								version,
								uri,
								serialNumber,
								handle); //try to set-up Accessory
						libusb_free_device_list(list, 1);
						closeUsb(handle);
						return handle = NULL;
						break;
					default:
#ifdef DEBUG
						printf("libAndroidAccessory: Unsupported or no Android device support\n");
#endif
						closeUsb(handle);
						handle = NULL;
						break;

					}
				}
			}
		}
	}
	libusb_free_device_list(list, 1);

	// Upon reaching this location we didn't find any supported android device, so we return NULL
	return NULL;
}