public void SendRequest (ref IOUSBDeviceRequest request) { ThrowIfDisposed (); var result = Interface.DeviceRequest (InterfaceRef, ref request); IOObject.ThrowIfError (result); }
public Task<int> SendRequestAsync (IOUSBDeviceRequest request) { ThrowIfDisposed (); var completionSource = new TaskCompletionSource<int> (); GCHandle callbackHandle = new GCHandle (); IOAsyncCallback1 callback = (refCon, callbackResult, arg0) => { callbackHandle.Free (); if (callbackResult == IOReturn.Success) completionSource.TrySetResult ((int)arg0); else completionSource.TrySetException (callbackResult.ToNSErrorException ()); }; callbackHandle = GCHandle.Alloc (callback, GCHandleType.Pinned); var result = Interface.DeviceRequestAsync (InterfaceRef, request, callback, IntPtr.Zero); IOObject.ThrowIfError (result); return completionSource.Task; }
char[] GetStringDescriptor (byte index, int language) { // based on http://oroboro.com/usb-serial-number-osx/ const int maxUsbStringLength = 255; var buffer = Marshal.AllocHGlobal (maxUsbStringLength); try { var request = new IOUSBDeviceRequest () { Direction = EndpointDirection.In, DeviceRequestType = DeviceRequestType.Standard, Recipient = DeviceRequestRecipient.Device, RequestType = RequestType.GetDescriptor, Value = (ushort)((byte)DescriptorType.String << 8 | index), Index = (ushort)language, DataLength = maxUsbStringLength, Data = buffer }; var result = Interface.DeviceRequest (InterfaceRef, ref request); ThrowIfError (result); var header = (IOUSBDescriptorHeader)Marshal.PtrToStructure (buffer, typeof(IOUSBDescriptorHeader)); var dataLength = (header.Length - 1) / 2; var data = new char[dataLength]; Marshal.Copy (buffer + 2, data, 0, dataLength); return data; } finally { Marshal.FreeHGlobal (buffer); } }