Exemplo n.º 1
0
        protected unsafe void controlTransfer(byte RequestType, byte Request, ushort Value, ushort Index)
        {
            int ret = libusbControlTransfer(deviceHandle, RequestType, Request,
                                            Value, Index, (byte *)0, 0, (ushort)5000);

            LibUsb.throwIfError(ret, "Control transfer failed");
        }
Exemplo n.º 2
0
        /// <returns>the device descriptor</returns>
        static LibusbDeviceDescriptor getDeviceDescriptorFromDevice(IntPtr device)
        {
            LibusbDeviceDescriptor descriptor;

            LibUsb.throwIfError(UsbDevice.libusbGetDeviceDescriptor(device, out descriptor),
                                "Failed to get device descriptor");
            return(descriptor);
        }
Exemplo n.º 3
0
        protected unsafe uint controlTransfer(byte RequestType, byte Request, ushort Value, ushort Index, void *data, ushort length)
        {
            int ret = libusbControlTransfer(deviceHandle, RequestType, Request,
                                            Value, Index, data, length, (ushort)5000);

            LibUsb.throwIfError(ret, "Control transfer failed");
            return((uint)ret);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Raises an exception if its argument is negative, with a
 /// message prefixed by the message parameter and describing
 /// which LIBUSB_ERROR it is.
 /// </summary>
 /// <returns>the code, if it is non-negative</returns>
 internal static int throwIfError(int code, string message)
 {
     try
     {
         return(LibUsb.throwIfError(code));
     }
     catch (Exception e)
     {
         throw new Exception(message, e);
     }
 }
Exemplo n.º 5
0
        /// <returns>the serial number</returns>
        internal static unsafe string getSerialNumber(IntPtr device_handle)
        {
            LibusbDeviceDescriptor descriptor = getDeviceDescriptor(device_handle);

            byte[] buffer = new byte[100];
            int    length;

            fixed(byte *p = buffer)
            {
                length = LibUsb.throwIfError(UsbDevice.libusbGetStringDescriptorASCII(device_handle, descriptor.iSerialNumber, p, buffer.Length), "Error getting serial number string from device (pid=" + descriptor.idProduct.ToString("x") + ", vid=" + descriptor.idVendor.ToString("x") + ").");
            }

            String serial_number = "";

            for (int i = 0; i < length; i++)
            {
                serial_number += (char)buffer[i];
            }
            return(serial_number);
        }
Exemplo n.º 6
0
        /// <summary>
        /// gets a list of devices by vendor and product ID
        /// </summary>
        /// <returns></returns>
        protected static unsafe List <DeviceListItem> getDeviceList(UInt16 vendorId, UInt16[] productIdArray)
        {
            var list = new List <DeviceListItem>();

            IntPtr *device_list;
            int     count = LibUsb.throwIfError(UsbDevice.libusbGetDeviceList(LibUsb.context, out device_list),
                                                "Error from libusb_get_device_list.");

            int i;

            for (i = 0; i < count; i++)
            {
                IntPtr device = device_list[i];

                foreach (UInt16 productId in productIdArray)
                {
                    if (LibUsb.deviceMatchesVendorProduct(device, vendorId, productId))
                    {
                        IntPtr device_handle;
                        LibUsb.throwIfError(UsbDevice.libusbOpen(device, out device_handle),
                                            "Error connecting to device to get serial number (" + (i + 1) + " of " + count + ", " + device.ToString("x8") + ").");

                        string serialNumber = LibUsb.getSerialNumber(device_handle);
                        list.Add(new DeviceListItem(device, "#" + serialNumber, serialNumber, productId));

                        UsbDevice.libusbClose(device_handle);
                    }
                }
            }


            // Free device list without unreferencing.
            // Unreference/free the individual devices in the
            // DeviceListItem destructor.
            UsbDevice.libusbFreeDeviceList(device_list, 0);

            return(list);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Create a usb device from a deviceListItem
 /// </summary>
 /// <param name="handles"></param>
 protected UsbDevice(DeviceListItem deviceListItem)
 {
     LibUsb.throwIfError(libusbOpen(deviceListItem.devicePointer, out privateDeviceHandle),
                         "Error connecting to device.");
 }
Exemplo n.º 8
0
 internal static void handleEvents()
 {
     LibUsb.throwIfError(libusb_handle_events(context));
 }