public LibUSBAsyncTransfer(IntPtr device, byte endpoint, int bufferSize) { //Configure this.device = device; this.bufferSize = bufferSize; //Create buffer buffer = UnsafeBuffer.Create(bufferSize, out bufferPtr); //Allocate transfer transfer = (LibUSBTransfer *)LibUSBNative.libusb_alloc_transfer(0); //Get the GCHandle for ourself handle = GCHandle.Alloc(this); //Populate transfer (same as libusb_fill_bulk_transfer) transfer->dev_handle = device; transfer->endpoint = endpoint; transfer->type = LibUSBTransferType.LIBUSB_TRANSFER_TYPE_BULK; transfer->timeout = 0; transfer->buffer = bufferPtr; transfer->length = bufferSize; transfer->user_data = (IntPtr)handle; transfer->callback = Marshal.GetFunctionPointerForDelegate(TransferCallbackDelegate); }
public IUsbDevice[] FindDevices(ushort vid, ushort pid) { //Get USB devices IntPtr devicesRef = IntPtr.Zero; int count = LibUSBNative.libusb_get_device_list(ctx, ref devicesRef); IntPtr *devices = (IntPtr *)devicesRef.ToPointer(); //Loop through devices List <LibUSBDevice> found = new List <LibUSBDevice>(); for (int i = 0; i < count; i++) { //Get pointer to the device IntPtr device = devices[i]; //Get info LibUSBDeviceDescriptor info; if (LibUSBNative.libusb_get_device_descriptor(device, &info) != 0) { continue; } //Check if this is a match if (vid != info.idVendor || pid != info.idProduct) { continue; } //Construct object found.Add(new LibUSBDevice(device, info)); } return(found.ToArray()); }
private void EventLoop() { while (true) { LibUSBNative.libusb_handle_events(ctx); } }
public int BulkTransfer(UsbTransferDirection direction, byte endpoint, UsbBuffer buffer, int timeout) { int transferred; LibUSBNative.ThrowIfError(LibUSBNative.libusb_bulk_transfer(handle, (byte)(endpoint | (byte)direction), buffer.AsPtr(), buffer.ByteLength, &transferred, (uint)timeout)); return(transferred); }
public LibUSBProvider() { //Create the LibUSB instance LibUSBNative.ThrowIfError(LibUSBNative.libusb_init(ref ctx)); //Create event thread eventThread = new Thread(EventLoop); eventThread.Name = "LibUSB Provider Event Loop"; eventThread.IsBackground = true; eventThread.Start(); }
private string ReadStringDescriptor(byte index, int length = 255) { //Make sure it's open ThrowIfUnopened(); //Create buffer byte[] buffer = new byte[length]; //Get data fixed(byte *bufferPtr = buffer) length = LibUSBNative.libusb_get_string_descriptor_ascii(handle, index, bufferPtr, length); //Read return(Encoding.ASCII.GetString(buffer, 0, length)); }
private int ControlTransfer(byte flags, byte fieldRequest, ushort fieldValue, ushort fieldIndex, UsbBuffer buffer, uint timeout) { //Apply int code; if (buffer != null) { code = LibUSBNative.libusb_control_transfer(handle, flags, fieldRequest, fieldValue, fieldIndex, buffer.AsPtr(), (ushort)buffer.ByteLength, timeout); } else { code = LibUSBNative.libusb_control_transfer(handle, flags, fieldRequest, fieldValue, fieldIndex, null, 0, timeout); } //Valiate if (code < 0) { throw new Exception("Unknown USB transfer write error."); } return(code); }
public bool OpenDevice() { //If it's already opened, return true if (isOpened) { return(true); } //Request the device to be opened handle = IntPtr.Zero; isOpened = LibUSBNative.libusb_open(device, ref handle) == 0; if (!isOpened) { return(false); } //Set config LibUSBNative.ThrowIfError(LibUSBNative.libusb_set_configuration(handle, 1)); LibUSBNative.ThrowIfError(LibUSBNative.libusb_claim_interface(handle, 0)); return(true); }
public void SubmitTransfer() { LibUSBNative.ThrowIfError(LibUSBNative.libusb_submit_transfer(transfer)); }