/// <summary> /// Creates and initialize a <a href="http://libusb.sourceforge.net/api-1.0/index.html">Libusb-1.0</a> USB session handle. /// </summary> /// <remarks> /// <para>A <see cref="MonoUsbSessionHandle"/> instance must be created before calling any other <a href="http://libusb.sourceforge.net/api-1.0/index.html">Libusb-1.0 API</a> function.</para> /// </remarks> public MonoUsbSessionHandle() : base(IntPtr.Zero, true) { lock (sessionLOCK) { IntPtr pNewSession = IntPtr.Zero; try { mLastReturnCode = (MonoUsbError)MonoUsbApi.Init(ref pNewSession); } catch (DllNotFoundException dllNotFound) { if (Helper.IsLinux) { throw new DllNotFoundException(DLL_NOT_FOUND_LINUX, dllNotFound); } else { throw new DllNotFoundException(DLL_NOT_FOUND_WINDOWS, dllNotFound); } } if ((int)mLastReturnCode < 0) { mLastReturnString = MonoUsbApi.StrError(mLastReturnCode); SetHandleAsInvalid(); } else { SetHandle(pNewSession); mSessionCount++; } } }
///<summary> ///Closes the <see cref="MonoUsbDeviceHandle"/>. ///</summary> ///<returns> ///true if the <see cref="MonoUsbDeviceHandle"/> is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a ReleaseHandleFailed Managed Debugging Assistant. ///</returns> protected override bool ReleaseHandle() { if (!IsInvalid) { Debug.WriteLine(GetType().Name + ".ReleaseHandle() Before", "Libusb-1.0"); MonoUsbApi.Close(handle); Debug.WriteLine(GetType().Name + ".ReleaseHandle() After", "Libusb-1.0"); SetHandleAsInvalid(); } return(true); }
private static void HandleEventFn(object oHandle) { MonoUsbSessionHandle sessionHandle = oHandle as MonoUsbSessionHandle; mIsStoppedEvent.Reset(); while (mRunning) { MonoUsbApi.HandleEventsTimeout(sessionHandle, ref mWaitUnixNativeTimeval); } mIsStoppedEvent.Set(); }
internal static UsbError Error(ErrorCode errorCode, int ret, string description, object sender) { string win32Error = String.Empty; if (errorCode == ErrorCode.MonoApiError) { win32Error = ((Error)ret) + ":" + MonoUsbApi.StrError((Error)ret); } UsbError err = new UsbError(errorCode, ret, win32Error, description, sender); return(err); }
static void config_op(MonoUsbDeviceHandle handle, byte endpoint, byte[] data, int readcnt) { int actual_length = 0; int res; byte[] recv_buf = new byte[1024]; GCHandle data_gc = GCHandle.Alloc(data, GCHandleType.Pinned); GCHandle recv_buf_gc = GCHandle.Alloc(recv_buf, GCHandleType.Pinned); MonoUsbTransferDelegate d = noop_usb_callback; MonoUsbTransfer transfer = new MonoUsbTransfer(0); wait = 0; Console.WriteLine("data_gc addr = {0}", data_gc.AddrOfPinnedObject()); Console.WriteLine("recv_buf_gc addr = {0}", recv_buf_gc.AddrOfPinnedObject()); // Execute the write operation (asynchronous). transfer.FillBulk(handle, endpoint, data_gc.AddrOfPinnedObject(), data.Length, d, recv_buf_gc.AddrOfPinnedObject(), 4000); transfer.Submit(); Thread.Sleep(300); // Execute the specified number of read operations (synchronous). for (int x = 0; x < readcnt; ++x) { res = Usb.BulkTransfer(handle, (byte)(endpoint | 0x80), recv_buf_gc.AddrOfPinnedObject(), recv_buf.Length, out actual_length, 4000); if (res != 0) { throw new Exception("config_op Usb.BulkTransfer failure"); } // Should only be here once the above transfer completes. } // Wait for the first write asynchronous to return. // Do not poll forever. Abort if it takes too long. var st = DateTime.Now; if (readcnt > 0) { while (wait == 0 && (DateTime.Now - st).TotalSeconds < 30) { Thread.Sleep(100); } } data_gc.Free(); recv_buf_gc.Free(); }
/// <summary> /// /// </summary> /// <returns></returns> protected override bool ReleaseHandle() { if (!IsInvalid) { lock (sessionLOCK) { MonoUsbApi.Exit(handle); SetHandleAsInvalid(); mSessionCount--; Debug.Print(GetType().Name + " : ReleaseHandle #{0}", mSessionCount); } } return(true); }
/// <summary>Open a device handle from <paramref name="profileHandle"/>.</summary> /// <remarks> /// <para>A handle allows you to perform I/O on the device in question.</para> /// <para>To close a device handle call its <see cref="SafeHandle.Close"/> method.</para> /// <para>This is a non-blocking function; no requests are sent over the bus.</para> /// <note title="Libusb-1.0 API Note:" type="cpp">The <see cref="MonoUsbDeviceHandle(MonoUsbProfileHandle)"/> constructor is roughly equivalent to <a href="http://libusb.sourceforge.net/api-1.0/group__dev.html#ga8163100afdf933fabed0db7fa81c89d1">libusb_open()</a>.</note> /// </remarks> /// <param name="profileHandle">A device profile handle.</param> public MonoUsbDeviceHandle(MonoUsbProfileHandle profileHandle) : base(IntPtr.Zero) { IntPtr pDeviceHandle = IntPtr.Zero; int ret = MonoUsbApi.Open(profileHandle, ref pDeviceHandle); if (ret < 0 || pDeviceHandle == IntPtr.Zero) { lock (handleLOCK) { mLastReturnCode = (MonoUsbError)ret; mLastReturnString = MonoUsbApi.StrError(mLastReturnCode); } SetHandleAsInvalid(); } else { SetHandle(pDeviceHandle); } }
public static void ShowConfig(RichTextBox rtb) { // Initialize the context. sessionHandle = new MonoUsbSessionHandle(); if (sessionHandle.IsInvalid) { throw new Exception(String.Format("Failed intialized libusb context.\n{0}:{1}", MonoUsbSessionHandle.LastErrorCode, MonoUsbSessionHandle.LastErrorString)); } MonoUsbProfileList profileList = new MonoUsbProfileList(); // The list is initially empty. // Each time refresh is called the list contents are updated. int ret = profileList.Refresh(sessionHandle); if (ret < 0) { throw new Exception("Failed to retrieve device list."); } rtb.AppendText(string.Format("{0} device(s) found.\r\n", ret)); // Use the GetList() method to get a generic List of MonoUsbProfiles // Find all profiles that match in the MyVidPidPredicate. List <MonoUsbProfile> myVidPidList = profileList.GetList().FindAll(MyVidPidPredicate); // myVidPidList reresents a list of connected USB devices that matched // in MyVidPidPredicate. foreach (MonoUsbProfile profile in myVidPidList) { MonoUsbDeviceHandle h = profile.OpenDeviceHandle();// Usb.OpenDeviceWithVidPid(sessionHandle, 0x1915, 0x007B); if (h.IsInvalid) { throw new Exception(string.Format("Failed opening device handle.\r\n{0}: {1}", MonoUsbDeviceHandle.LastErrorCode, MonoUsbDeviceHandle.LastErrorString)); } Usb.SetConfiguration(h, 1); Usb.ClaimInterface(h, 1); MonoUsbProfileHandle ph = Usb.GetDevice(h); int packetSize = Usb.GetMaxIsoPacketSize(ph, 0x88); // Write the VendorID and ProductID to console output. rtb.AppendText(string.Format("[Device] Vid:{0:X4} Pid:{1:X4}\r\n", profile.DeviceDescriptor.VendorID, profile.DeviceDescriptor.ProductID)); // Loop through all of the devices configurations. for (byte i = 0; i < profile.DeviceDescriptor.ConfigurationCount; i++) { // Get a handle to the configuration. MonoUsbConfigHandle configHandle; if (MonoUsbApi.GetConfigDescriptor(profile.ProfileHandle, i, out configHandle) < 0) { continue; } if (configHandle.IsInvalid) { continue; } // Create a MonoUsbConfigDescriptor instance for this config handle. MonoUsbConfigDescriptor configDescriptor = new MonoUsbConfigDescriptor(configHandle); // Write the bConfigurationValue to console output. rtb.AppendText(string.Format(" [Config] bConfigurationValue:{0}\r\n", configDescriptor.bConfigurationValue)); // Interate through the InterfaceList foreach (MonoUsbInterface usbInterface in configDescriptor.InterfaceList) { // Interate through the AltInterfaceList foreach (MonoUsbAltInterfaceDescriptor usbAltInterface in usbInterface.AltInterfaceList) { // Write the bInterfaceNumber and bAlternateSetting to console output. rtb.AppendText(string.Format(" [Interface] bInterfaceNumber:{0} bAlternateSetting:{1}\r\n", usbAltInterface.bInterfaceNumber, usbAltInterface.bAlternateSetting)); // Interate through the EndpointList foreach (MonoUsbEndpointDescriptor endpoint in usbAltInterface.EndpointList) { // Write the bEndpointAddress, EndpointType, and wMaxPacketSize to console output. rtb.AppendText(string.Format(" [Endpoint] bEndpointAddress:{0:X2} EndpointType:{1} wMaxPacketSize:{2}\r\n", endpoint.bEndpointAddress, (EndpointType)(endpoint.bmAttributes & 0x3), endpoint.wMaxPacketSize)); if (endpoint.bEndpointAddress == 0x88) { } } } } // Not neccessary, but good programming practice. configHandle.Close(); } } // Not neccessary, but good programming practice. profileList.Close(); // Not neccessary, but good programming practice. sessionHandle.Close(); }
static bool HandleConfigModeDevice(MonoUsbProfile profile) { MonoUsbDeviceHandle handle; handle = profile.OpenDeviceHandle(); if (handle == null || handle.IsInvalid) { return(false); } Usb.ClaimInterface(handle, 0); var now = DateTime.Now.ToUniversalTime(); var date_string = String.Format("{0:d4}/{1:d2}/{2:d2}/{3:d2}/{4:d2}/{5:d2}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second ); var date_string_bytes = Encoding.ASCII.GetBytes(date_string); MemoryStream date_string_cmd = new MemoryStream(); date_string_cmd.WriteByte(19); date_string_cmd.WriteByte(0); date_string_cmd.WriteByte(0); date_string_cmd.WriteByte(0); date_string_cmd.Write(date_string_bytes, 0, date_string_bytes.Length); byte[] seq0 = new byte[] { 69, 58, 2, 103, 203, 15, 16, 15, 9, 0, 0, 0, 186, 197, 253, 152 }; byte[] seq1 = new byte[] { 69, 58, 2, 103, 6, 15, 16, 15, 10, 0, 0, 0, 186, 197, 253, 152 }; byte[] seq2 = new byte[] { 69, 58, 2, 103, 179, 15, 16, 15, 132, 0, 0, 0, 186, 197, 253, 152 }; byte[] seq3 = new byte[] { 69, 58, 2, 103, 17, 15, 16, 15, 12, 0, 0, 0, 186, 197, 253, 152 }; byte[] seq4 = new byte[] { 69, 58, 2, 103, 16, 15, 16, 15, 5, 0, 0, 0, 186, 197, 253, 152 }; byte[] seq5 = new byte[] { 69, 58, 2, 103, 212, 0, 0, 15, 23, 0, 0, 0, 186, 197, 253, 152 }; //byte[] seq6 = new byte[] { 19, 0, 0, 0, 50, 48, 49, 55, 47, 48, 54, 47, 50, 55, 47, 50, 48, 47, 51, 56, 47, 50, 56 }; byte[] seq6 = date_string_cmd.ToArray(); byte[] seq7 = new byte[] { 69, 58, 2, 103, 204, 15, 16, 15, 5, 0, 0, 0, 186, 197, 253, 152 }; byte[] seq8 = new byte[] { 69, 58, 2, 103, 160, 15, 16, 15, 10, 0, 0, 0, 186, 197, 253, 152 }; // These two packets alone will switch the device into mass storage mode. byte[] seq9 = new byte[] { 69, 58, 2, 103, 247, 0, 0, 15, 10, 0, 0, 0, 186, 197, 253, 152 }; byte[] seq10 = new byte[] { 6, 0, 0, 0, 88, 122, 84, 117, 86, 116 }; config_op(handle, 1, seq0, 2); config_op(handle, 1, seq1, 2); config_op(handle, 1, seq2, 2); config_op(handle, 1, seq3, 2); config_op(handle, 1, seq4, 2); config_op(handle, 1, seq5, 0); config_op(handle, 1, seq6, 1); config_op(handle, 1, seq7, 2); config_op(handle, 1, seq8, 2); config_op(handle, 1, seq9, 0); config_op(handle, 1, seq10, 1); handle.Close(); return(true); }