ClaimInterface() private method

private ClaimInterface ( [ deviceHandle, int interfaceNumber ) : int
deviceHandle [
interfaceNumber int
return int
Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        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);
        }