bool Communicator::openUsbDevice(uint16_t vendorId, uint16_t productId)
{
	mVendorId = 0;
	mProductId = 0;
	bool result = false;
	libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
	libusb_device_descriptor desc;
	int r = 0;
	ssize_t cnt; //holding number of devices in list

	if (isUsbContextInitialized()) {

		cnt = libusb_get_device_list(mCtx, &devs); //get the list of devices
		if (cnt > 0) {
			syslog(LOG_INFO, "USB Devices in");
			ssize_t i = 0; //for iterating through the list
			while(i < cnt) {
				libusb_device *device = devs[i];
				r = libusb_get_device_descriptor(device, &desc);
				if (r == 0) {
					syslog(LOG_INFO, "Checking device with vendorId: %04x  and productId: %04x", vendorId, productId);
					if ( (desc.idVendor == vendorId && desc.idProduct == productId) || (vendorId == 0 && productId == 0) ) {
						syslog(LOG_INFO, "Trying to open device");
						if (canOpenUsbImagingDevice(device, &desc)) {
							syslog(LOG_INFO, "Device  open success");
							result = initUsbDevice(device);
							break;
						}
					}
				}
				i++;
			}
		}
		else
			syslog(LOG_INFO, "Can't found USB devices");
	}

	libusb_free_device_list(devs, 1); //free the list, unref the devices in it

	return result;
}
static int __doUsbUpgrade(int nStorMode) 
{
	int nRet = FALSE;
	int nOldCtrlc = 0;
	int nReadLength = 0;
	PacketInfo_S * psPacketInfo = NULL;
	char acReadFileName[32]={0};
	char *pcDeviceName = NULL;

	unsigned int extension_id = get_extension_chip_id();
	switch (extension_id) {
	case ARCH_EXID_AT91SAM9G25:
		pcDeviceName = "sam9g25ek"; break;
	case ARCH_EXID_AT91SAM9G35:
		pcDeviceName = "sam9g35ek"; break;
	case ARCH_EXID_AT91SAM9X25:
		pcDeviceName = "sam9x25ek"; break;
	case ARCH_EXID_AT91SAM9X35:
		pcDeviceName = "sam9x35ek"; break;
	default:
		printf("Warning: extension_id[0x%X] not support! Take sam9x25ek.dtb as default.", extension_id);
		pcDeviceName = "sam9x25ek";
		break;
	}
	
	memset(acReadFileName, 0, sizeof(acReadFileName));
	sprintf(acReadFileName, "usbupgrad_%s.dt", pcDeviceName);

	if (nStorMode==1) {
		nRet = initMMCDevice();
		if (nRet == FALSE) {
			printf("MMC upgrade initialize MMC failed.\n");
			return FALSE;
		}
	} else {
		nRet = initUsbDevice();
		if (nRet == FALSE) {
			printf("USB upgrade initialize USB failed.\n"); 	
			return FALSE;						
		}
	}

	nRet = initFatFileSystem();
	if (nRet == FALSE) {
		printf("USB upgrade initialize FAT filesystem failed.\n");
		return FALSE;
	}
	
	nOldCtrlc = disable_ctrlc(0);
	nReadLength = 100*1024*1024;
	nRet = loadImageFromUsbToRam(acReadFileName, (void *)LOAD_RAM_ADDRESS, &nReadLength);
	if (nRet == FALSE) {
		printf("[doUsbUpgrade] loadImageFromUsbToRam upgrade usbupgrad.dt  failed.\n");
		return FALSE;
	}

	psPacketInfo = parsePacketInfo((const void *)LOAD_RAM_ADDRESS, nReadLength);
	if (psPacketInfo==NULL) {
		printf("[doUsbUpgrade] parsePacketInfo upgrade usbupgrad.dt  failed.\n");
		return FALSE;
	}

	nRet = writeImageFromRamToFlash(psPacketInfo, (void *)LOAD_RAM_ADDRESS, nReadLength);
	if (nRet == FALSE) {
		printf("[doUsbUpgrade] writeImageFromRamToFlash upgrade usbupgrad.dt (%p) failed.\n", (void *)LOAD_RAM_ADDRESS);
		free(psPacketInfo);
		return FALSE;
	}
	
	free(psPacketInfo);
	disable_ctrlc(nOldCtrlc);
	
	return 0;
}