/// <summary> /// Gets the USB devices active configuration value. /// </summary> /// <param name="config">The active configuration value. A zero value means the device is not configured and a non-zero value indicates the device is configured.</param> /// <returns>True on success.</returns> public virtual bool GetConfiguration(out byte config) { config = 0; byte[] buf = new byte[1]; int uTransferLength; UsbSetupPacket setupPkt = new UsbSetupPacket(); setupPkt.RequestType = (byte)UsbEndpointDirection.EndpointIn | (byte)UsbRequestType.TypeStandard | (byte)UsbRequestRecipient.RecipDevice; setupPkt.Request = (byte)UsbStandardRequest.GetConfiguration; setupPkt.Value = 0; setupPkt.Index = 0; setupPkt.Length = 1; bool bSuccess = ControlTransfer(ref setupPkt, buf, buf.Length, out uTransferLength); if (bSuccess && uTransferLength == 1) { config = buf[0]; mCurrentConfigValue = config; return(true); } UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetConfiguration", this); return(false); }
/// <summary> /// Gets a descriptor from the device. See <see cref="DescriptorType"/> for more information. /// </summary> /// <param name="descriptorType">The descriptor type ID to retrieve; this is usually one of the <see cref="DescriptorType"/> enumerations.</param> /// <param name="index">Descriptor index.</param> /// <param name="langId">Descriptor language id.</param> /// <param name="buffer">Memory to store the returned descriptor in.</param> /// <param name="bufferLength">Length of the buffer parameter in bytes.</param> /// <param name="transferLength">The number of bytes transferred to buffer upon success.</param> /// <returns>True on success.</returns> public virtual bool GetDescriptor(byte descriptorType, byte index, short langId, IntPtr buffer, int bufferLength, out int transferLength) { transferLength = 0; bool wasOpen = IsOpen; if (!wasOpen) { Open(); } if (!IsOpen) { return(false); } bool bSuccess = mUsbApi.GetDescriptor(mUsbHandle, descriptorType, index, (ushort)langId, buffer, bufferLength, out transferLength); if (!bSuccess) { UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetDescriptor", this); } if (!wasOpen && IsOpen) { Close(); } return(bSuccess); }
private bool StopReadThread() { Abort(); Thread.Sleep(1); #if !NETSTANDARD1_5 && !NETSTANDARD1_6 Application.DoEvents(); #endif DateTime dtStart = DateTime.Now; while (mReadThread.IsAlive && ((DateTime.Now - dtStart).TotalSeconds < 5)) // 5 sec fail-safe { Thread.Sleep(100); #if !NETSTANDARD1_5 && !NETSTANDARD1_6 Application.DoEvents(); #endif } if (mReadThread.IsAlive) { UsbError.Error(ErrorCode.ReceiveThreadTerminated, 0, "Failed stopping read thread.", this); #if !NETSTANDARD1_5 && !NETSTANDARD1_6 mReadThread.Abort(); #endif return(false); } return(true); }
internal static List <UsbConfigInfo> GetDeviceConfigs(UsbDevice usbDevice) { List <UsbConfigInfo> rtnConfigs = new List <UsbConfigInfo>(); byte[] cfgBuffer = new byte[UsbConstants.MAX_CONFIG_SIZE]; int iConfigs = usbDevice.Info.Descriptor.ConfigurationCount; for (int iConfig = 0; iConfig < iConfigs; iConfig++) { int iBytesTransmitted; bool bSuccess = usbDevice.GetDescriptor((byte)DescriptorType.Configuration, 0, 0, cfgBuffer, cfgBuffer.Length, out iBytesTransmitted); if (bSuccess) { if (iBytesTransmitted >= UsbConfigDescriptor.Size && cfgBuffer[1] == (byte)DescriptorType.Configuration) { UsbConfigDescriptor configDescriptor = new UsbConfigDescriptor(); Helper.BytesToObject(cfgBuffer, 0, Math.Min(UsbConfigDescriptor.Size, cfgBuffer[0]), configDescriptor); if (configDescriptor.TotalLength == iBytesTransmitted) { List <byte[]> rawDescriptorList = new List <byte[]>(); int iRawLengthPosition = configDescriptor.Length; while (iRawLengthPosition < configDescriptor.TotalLength) { byte[] rawDescriptor = new byte[cfgBuffer[iRawLengthPosition]]; if (iRawLengthPosition + rawDescriptor.Length > iBytesTransmitted) { throw new UsbException(usbDevice, "Descriptor length is out of range."); } Array.Copy(cfgBuffer, iRawLengthPosition, rawDescriptor, 0, rawDescriptor.Length); rawDescriptorList.Add(rawDescriptor); iRawLengthPosition += rawDescriptor.Length; } rtnConfigs.Add(new UsbConfigInfo(usbDevice, configDescriptor, ref rawDescriptorList)); } else { UsbError.Error(ErrorCode.InvalidConfig, 0, "GetDeviceConfigs: USB config descriptor length doesn't match the length received.", usbDevice); } } else { UsbError.Error(ErrorCode.InvalidConfig, 0, "GetDeviceConfigs: USB config descriptor is invalid.", usbDevice); } } else { UsbError.Error(ErrorCode.InvalidConfig, 0, "GetDeviceConfigs", usbDevice); } } return(rtnConfigs); }
internal static void FireUsbError(object sender, UsbError usbError) { EventHandler <UsbError> temp = UsbErrorEvent; if (!ReferenceEquals(null, temp)) { temp(sender, usbError); } }
private static void ReadData(object context) { UsbTransfer overlappedTransferContext = (UsbTransfer)context; UsbEndpointReader reader = (UsbEndpointReader)overlappedTransferContext.EndpointBase; reader.mDataReceivedEnabled = true; EventHandler <DataReceivedEnabledChangedEventArgs> dataReceivedEnabledChangedEvent; dataReceivedEnabledChangedEvent = reader.DataReceivedEnabledChanged; if (!ReferenceEquals(dataReceivedEnabledChangedEvent, null)) { dataReceivedEnabledChangedEvent(reader, new DataReceivedEnabledChangedEventArgs(reader.mDataReceivedEnabled)); } overlappedTransferContext.Reset(); byte[] buf = new byte[reader.mReadBufferSize]; try { while (!overlappedTransferContext.IsCancelled) { int iTransferLength; ErrorCode eReturn = reader.Transfer(buf, 0, buf.Length, Timeout.Infinite, out iTransferLength); if (eReturn == ErrorCode.None) { EventHandler <EndpointDataEventArgs> temp = reader.DataReceived; if (!ReferenceEquals(temp, null) && !overlappedTransferContext.IsCancelled) { temp(reader, new EndpointDataEventArgs(buf, iTransferLength)); } continue; } if (eReturn != ErrorCode.IoTimedOut) { break; } } } #if !NETSTANDARD1_5 && !NETSTANDARD1_6 catch (ThreadAbortException) { UsbError.Error(ErrorCode.ReceiveThreadTerminated, 0, "ReadData:Read thread aborted.", reader); } #endif finally { reader.Abort(); reader.mDataReceivedEnabled = false; dataReceivedEnabledChangedEvent = reader.DataReceivedEnabledChanged; if (!ReferenceEquals(dataReceivedEnabledChangedEvent, null)) { dataReceivedEnabledChangedEvent(reader, new DataReceivedEnabledChangedEventArgs(reader.mDataReceivedEnabled)); } } }
/// <summary> /// Transmits control data over a default control endpoint. /// </summary> /// <param name="setupPacket">An 8-byte setup packet which contains parameters for the control request. /// See section 9.3 USB Device Requests of the Universal Serial Bus Specification Revision 2.0 for more information. </param> /// <param name="buffer">Data to be sent/received from the device.</param> /// <param name="bufferLength">Length of the buffer param.</param> /// <param name="lengthTransferred">Number of bytes sent or received (depends on the direction of the control transfer).</param> /// <returns>True on success.</returns> public virtual bool ControlTransfer(ref UsbSetupPacket setupPacket, IntPtr buffer, int bufferLength, out int lengthTransferred) { bool bSuccess = mUsbApi.ControlTransfer(mUsbHandle, setupPacket, buffer, bufferLength, out lengthTransferred); if (!bSuccess) { UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "ControlTransfer", this); } return(bSuccess); }
/// <summary> /// Asking for the zero'th index is special - it returns a string /// descriptor that contains all the language IDs supported by the /// device. Typically there aren't many - often only one. The /// language IDs are 16 bit numbers, and they start at the third byte /// in the descriptor. See USB 2.0 specification, section 9.6.7, for /// more information on this. /// </summary> /// <returns>A collection of LCIDs that the current <see cref="UsbDevice"/> supports.</returns> public bool GetLangIDs(out short[] langIDs) { LangStringDescriptor sd = new LangStringDescriptor(UsbDescriptor.Size + (16 * sizeof(short))); int ret; bool bSuccess = GetDescriptor((byte)DescriptorType.String, 0, 0, sd.Ptr, sd.MaxSize, out ret); if (bSuccess && ret == sd.Length) { bSuccess = sd.Get(out langIDs); } else { langIDs = new short[0]; UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetLangIDs", this); } sd.Free(); return(bSuccess); }
/// <summary> /// Gets a <see cref="DescriptorType.String"/> descriptor from the device. /// </summary> /// <param name="stringData">Buffer to store the returned string in upon success.</param> /// <param name="langId">The language ID to retrieve the string in. (0x409 for english).</param> /// <param name="stringIndex">The string index to retrieve.</param> /// <returns>True on success.</returns> public bool GetString(out string stringData, short langId, byte stringIndex) { stringData = null; int iTransferLength; LangStringDescriptor sd = new LangStringDescriptor(255); bool bSuccess = GetDescriptor((byte)DescriptorType.String, stringIndex, langId, sd.Ptr, sd.MaxSize, out iTransferLength); if (bSuccess && iTransferLength > UsbDescriptor.Size && sd.Length == iTransferLength) { bSuccess = sd.Get(out stringData); } else if (!bSuccess) { UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetString:GetDescriptor", this); } else { stringData = String.Empty; } return(bSuccess); }
internal static UsbError Error(ErrorCode errorCode, int ret, string description, object sender) { string win32Error = String.Empty; if (errorCode == ErrorCode.Win32Error && !UsbDevice.IsLinux && ret != 0) { win32Error = Kernel32.FormatSystemMessage(ret); } else if (errorCode == ErrorCode.MonoApiError && ret != 0) { win32Error = ((MonoUsbError)ret) + ":" + MonoUsbApi.StrError((MonoUsbError)ret); } UsbError err = new UsbError(errorCode, ret, win32Error, description, sender); lock (mLastErrorString) { mLastErrorNumber = (int)err.ErrorCode; mLastErrorString = err.ToString(); } UsbDevice.FireUsbError(sender, err); return(err); }
public static UsbError Error(ErrorCode errorCode, int ret, string description, object sender) { string win32Error = String.Empty; if (errorCode == ErrorCode.Win32Error && !UsbDevice.IsLinux && ret != 0) { win32Error = Kernel32.FormatSystemMessage(ret); } else if (errorCode == ErrorCode.MonoApiError && ret != 0) { Debug.Assert(false, "libusb-1.0 error codes should be resolved within LibUsbDotNet.LibUsb"); // win32Error = ((MonoUsbError) ret) + ":" + MonoUsbApi.StrError((MonoUsbError) ret); } UsbError err = new UsbError(errorCode, ret, win32Error, description, sender); lock (mLastErrorString) { mLastErrorNumber = (int)err.ErrorCode; mLastErrorString = err.ToString(); } UsbDevice.FireUsbError(sender, err); return(err); }
private void OnUsbError(object sender, UsbError e) { SetStatus(e.ToString(), true); }
private void UsbDeviceOnUsbErrorEvent(object sender, UsbError usbError) { Main.SendDebug(String.Format("A USB Error Occured: {0}", usbError)); if(Device == null || !Device.IsOpen) throw new X360NANDManagerException(X360NANDManagerException.ErrorLevels.DeviceCrashed); }
private void UsbDevice_UsbErrorEvent(object sender, UsbError e) { LogLine("usb error: " + e.ToString()); MessageBox.Show("UsbError: " + e.ToString()); }
private void UsbGlobalErrorEvent(object sender, UsbError e) { ConsoleTextbox.AppendText(e + "\r\n"); }
private void UsbGlobalErrorEvent(object sender, UsbError e) { tRecv.AppendText(e + "\r\n"); }
internal static UsbError Error(ErrorCode errorCode, int ret, string description, object sender) { string win32Error = String.Empty; if (errorCode == ErrorCode.Win32Error && !UsbDevice.IsLinux && ret != 0) { win32Error = Kernel32.FormatSystemMessage(ret); } else if (errorCode == ErrorCode.MonoApiError && ret != 0) { win32Error = ((MonoUsbError) ret) + ":" + MonoUsbApi.StrError((MonoUsbError) ret); } UsbError err = new UsbError(errorCode, ret, win32Error, description, sender); lock (mLastErrorString) { mLastErrorNumber = (int) err.ErrorCode; mLastErrorString = err.ToString(); } UsbDevice.FireUsbError(sender, err); return err; }
private void OnUsbError(object sender, UsbError usbError) { try { string eventText = DateTime.Now.ToString("HH:mm:ss.fff - ") + "USB Error"; TreeNode tvEvent = treeErrors.Nodes.Insert(0, eventText); string s; try { s = usbError.Description; if (string.IsNullOrEmpty(s) == false && string.IsNullOrEmpty(s.Trim()) == false) tvEvent.Nodes.Add(s); } catch { } try { s = usbError.ErrorCode.ToString(); if (string.IsNullOrEmpty(s) == false && string.IsNullOrEmpty(s.Trim()) == false) tvEvent.Nodes.Add("Error Code" + s); } catch { } try { s = usbError.Sender.ToString(); if (string.IsNullOrEmpty(s) == false && string.IsNullOrEmpty(s.Trim()) == false) tvEvent.Nodes.Add("Sender: " + s); } catch { } try { s = usbError.Win32ErrorString; if (string.IsNullOrEmpty(s) == false && string.IsNullOrEmpty(s.Trim()) == false) tvEvent.Nodes.Add("Win32 Error: " + s); } catch { } try { s = usbError.Win32ErrorNumber.ToString(); if (string.IsNullOrEmpty(s) == false && string.IsNullOrEmpty(s.Trim()) == false) tvEvent.Nodes.Add("Win32 Error Number: " + s); } catch { } } catch (Exception ex) { //ErrorReportWindow.Show(ex, "Exception During USB Error Event"); string eventText = DateTime.Now.ToString("HH:mm:ss.fff - ") + "USB Error Exception"; TreeNode tvEvent = treeErrors.Nodes.Insert(0, eventText); treeErrors.Nodes.Add(ex.Message); } }
internal static void FireUsbError(object sender, UsbError usbError) { EventHandler<UsbError> temp = UsbErrorEvent; if (!ReferenceEquals(null, temp)) temp(sender, usbError); }
private void OnUsbError(object sender, UsbError usbError) { Console.WriteLine(usbError.ToString()); }
private void UsbGlobals_UsbErrorEvent(object sender, UsbError e) { Invoke(new UsbErrorEventDelegate(UsbGlobalErrorEvent), new object[] {sender, e}); }
private void UsbDevice_UsbErrorEvent(object sender, UsbError e) { this.LogLine("usb error: " + e); MessageBox.Show("UsbError: " + e); }