示例#1
0
// caller can use UsbSend() afterwards, and should
// finally do libusb_release_interface(handle, 0); libusb_close(handle);
libusb_device_handle *UsbInit(struct cutter_id *id)
{
	// Now do stuff with the handle.
	int r = 0;

	libusb_device_handle* handle = UsbOpen(id);
	if (!handle) return NULL;

	if (libusb_kernel_driver_active(handle, 0) == 1)
	{
		r = libusb_detach_kernel_driver(handle, 0);
		if (r != 0)
		{
			libusb_close(handle);
			id->msg = Error("Error detaching kernel USB driver: " + UsbError(r));
			return NULL;
		}
	}

	r = libusb_reset_device(handle);
	if (r != 0)
	{
		libusb_close(handle);
		id->msg = Error("Error resetting device: " + UsbError(r));
		return NULL;
	}

	cout << "Selecting configuration." << endl;
	r = libusb_set_configuration(handle, 1);
	if (r < 0)
	{
		libusb_close(handle);
		id->msg = Error("Error setting USB configuration: " + UsbError(r));
		return NULL;
	}


	cout << "Claiming main control interface." << endl;
	r = libusb_claim_interface(handle, 0);
	if (r < 0)
	{
		libusb_close(handle);
		id->msg = Error("Error claiming USB interface: " + UsbError(r));
		return NULL;
	}

	cout << "Setting alt interface." << endl;
	r = libusb_set_interface_alt_setting(handle, 0, 0); // Probably not really necessary.
	if (r < 0)
	{
		libusb_close(handle);
		id->msg = Error("Error setting alternate USB interface: " + UsbError(r));
		return NULL;
	}

	cout << "Initialisation successful." << endl;
	return handle;
}
示例#2
0
char CT_init ( USHORT Ctn,  USHORT pn )
{
  APIRET rc;
  int IretVal;         /* Return for this function */
  ULONG nlength;
  UCHAR pcbuffer[RESP_BUF_SIZE];

  IretVal = ERR_MEMORY;        /* Could not allocate port */
  if(Ctn< MAX_CTN && hUSBDevice[Ctn] == 0)
  {
    rc = UsbOpen( &hUSBDevice[Ctn],
                  VENDOR_GEMPLUS,
                  PRODUCT_GPC430,
                  USB_ANY_PRODUCTVERSION,
                  pn);
    if(0==rc)
      rc = UsbSetDeviceConfiguration(hUSBDevice[Ctn], 1);
    if(0==rc)
      IretVal = OK;

    nlength = sizeof(pcbuffer);
    if ( GCSendCommand( Ctn, sizeof(pcSetModeROSNOTLP),
                       (const PUCHAR)pcSetModeROSNOTLP,
                       &nlength, pcbuffer) != STATUS_SUCCESS )
    {
      printf("IFD_GemPC430 : Setmode failed\n");
      IretVal = ERR_HTSI;
    }
    else
    {
      // Check status returned by reader
      if ( pcbuffer[STATUS_OFFSET] != GCORE_OK )
      {
        // Could not set reader mode
        // For now, we consider this a "fatal" error
        printf("IFD_GemPC430 : Setmode failed\n");
        IretVal = ERR_HTSI;
      }
    }
  }

  return IretVal;
}
示例#3
0
struct cutter_id *Identify()
{
	static struct cutter_id id = { "?", 0, 0 };
	libusb_device_handle *handle;

	if (1)
	  {
	    handle = UsbOpen(&id);
	    if (handle)
	      libusb_close(handle);
	    else
	      id.msg = "no device found";
	  }
	else
	  {
	    id.msg = Error("Cannot Identify while cut thread is running");
	  }
	return &id;
}
示例#4
0
void main()
{
  APIRET rc;
  USBHANDLE hUSBDevice;
  ULONG ulNumDevices;
  UCHAR ucBuffer[640];

  rc = UsbQueryNumberDevices(&ulNumDevices);
  printf("Num devices = %d (rc=%d)\r\n",ulNumDevices,rc);

  rc = UsbOpen( &hUSBDevice,
                VENDOR_RAINBOW,
                PRODUCT_IKEY1000,
                USB_ANY_PRODUCTVERSION,
                USB_OPEN_FIRST_UNUSED);

  if(!rc && hUSBDevice!=0 &&
     0==OpenIKey(hUSBDevice))
  {

    BlinkLED(hUSBDevice);

    memset(ucBuffer,sizeof(ucBuffer),0);

    rc = UsbCtrlMessage( hUSBDevice,
                         0x41, 4,0,0,
                         0x02, ucBuffer,
                         0);
    if(!rc)
      ReadIKeyData( hUSBDevice, 0x0E, ucBuffer);

    UsbClose(hUSBDevice);
  }
  else
  {
    printf("USBOpen returned %d\r\n",rc);
  }

}
示例#5
0
int usb_os_open( usb_dev_handle *dev)
{
  int       rtn = 0;
  APIRET    rc;
  int       handle;

  rc = UsbOpen( (PUSBHANDLE)&handle,
                (USHORT)dev->device->descriptor.idVendor,
                (USHORT)dev->device->descriptor.idProduct,
                (USHORT)dev->device->descriptor.bcdDevice,
                (USHORT)USB_OPEN_FIRST_UNUSED);

  if (rc) {
    USB_ERROR_MSG( "unable to open device - id= %x/%x  rc= %x",
                   dev->device->descriptor.idVendor, 
                   dev->device->descriptor.idProduct, (int)rc);
    handle = -1;
    rtn = -1;
  }

  dev->fd = handle;

  return rtn;
}