private USB_STRING_DESCRIPTOR ParseStringDescriptor(EusbPdu pdu)
        {
            if (pdu is EusbUrbCompletionNoDataPdu)
            {
                Site.Log.Add(
                    LogEntryKind.Debug,
                    "Unexpectedly received an EusbUrbCompletionNoDataPdu message: {0}",
                    pdu
                    );
                return(null);
            }

            Site.Assert.IsInstanceOfType(
                pdu,
                typeof(EusbUrbCompletionPdu),
                "Must receive an EusbUrbCompletionPdu message."
                );

            EusbUrbCompletionPdu completionPdu = (EusbUrbCompletionPdu)pdu;

            Site.Assert.IsSuccess(
                (int)completionPdu.HResult,
                "the HResult member of the EusbUrbCompletionPdu must be a successful code."
                );

            USB_STRING_DESCRIPTOR res = UsbStructParser.Parse <USB_STRING_DESCRIPTOR>(completionPdu);

            Site.Assert.IsNotNull(res, "USB_STRING_DESCRIPTOR cannot be parsed from EusbUrbCompletionPdu");
            return(res);
        }
Example #2
0
    public static string GetManufact(IntPtr h, USB_DEVICE_DESCRIPTOR PortDeviceDescriptor, int PortPortNumber)
    {
        if (PortDeviceDescriptor.iManufacturer > 0)
        {
            int nBytesReturned;
            int nBytes = BUFFER_SIZE;

            // build a request for string descriptor
            USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST();
            Request.ConnectionIndex     = PortPortNumber;
            Request.SetupPacket.wValue  = (short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iManufacturer);
            Request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(Request));
            Request.SetupPacket.wIndex  = 0x409; // Language Code

            // Geez, I wish C# had a Marshal.MemSet() method
            string NullString = new string((char)0, nBytes / Marshal.SystemDefaultCharSize);
            IntPtr ptrRequest = Marshal.StringToHGlobalAuto(NullString);
            Marshal.StructureToPtr(Request, ptrRequest, true);

            // Use an IOCTL call to request the String Descriptor
            if (DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes, ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
            {
                // The location of the string descriptor is immediately after
                // the Request structure.  Because this location is not "covered"
                // by the structure allocation, we're forced to zero out this
                // chunk of memory by using the StringToHGlobalAuto() hack above
                IntPtr ptrStringDesc             = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
                USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc, typeof(USB_STRING_DESCRIPTOR));
                return(StringDesc.bString);
            }
            Marshal.FreeHGlobal(ptrRequest);
        }

        return("");
    }
        public USBDevice GetDevice()
        {
            if (!this.PortIsDeviceConnected)
            {
                return(null);
            }
            USBDevice uSBDevice = new USBDevice();

            uSBDevice.DevicePortNumber    = this.PortPortNumber;
            uSBDevice.DeviceHubDevicePath = this.PortHubDevicePath;
            uSBDevice.DeviceDescriptor    = this.PortDeviceDescriptor;
            uSBDevice.DeviceVID           = this.PortDeviceDescriptor.idVendor;
            uSBDevice.DevicePID           = this.PortDeviceDescriptor.idProduct;
            IntPtr intPtr = Acer_USB_Library.CreateFile(this.PortHubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);

            if (intPtr.ToInt32() != -1)
            {
                int    num  = 2048;
                string s    = new string('\0', 2048 / Marshal.SystemDefaultCharSize);
                int    num2 = default(int);
                if (this.PortDeviceDescriptor.iManufacturer > 0)
                {
                    USB_DESCRIPTOR_REQUEST structure = default(USB_DESCRIPTOR_REQUEST);
                    structure.ConnectionIndex     = this.PortPortNumber;
                    structure.SetupPacket.wValue  = (short)(768 + this.PortDeviceDescriptor.iManufacturer);
                    structure.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure));
                    structure.SetupPacket.wIndex  = 1033;
                    IntPtr intPtr2 = Marshal.StringToHGlobalAuto(s);
                    Marshal.StructureToPtr(structure, intPtr2, true);
                    if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero))
                    {
                        USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr2.ToInt32() + Marshal.SizeOf(structure)), typeof(USB_STRING_DESCRIPTOR));
                        uSBDevice.DeviceManufacturer = uSB_STRING_DESCRIPTOR.bString;
                    }
                    Marshal.FreeHGlobal(intPtr2);
                }
                if (this.PortDeviceDescriptor.iProduct > 0)
                {
                    USB_DESCRIPTOR_REQUEST structure2 = default(USB_DESCRIPTOR_REQUEST);
                    structure2.ConnectionIndex     = this.PortPortNumber;
                    structure2.SetupPacket.wValue  = (short)(768 + this.PortDeviceDescriptor.iProduct);
                    structure2.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure2));
                    structure2.SetupPacket.wIndex  = 1033;
                    IntPtr intPtr3 = Marshal.StringToHGlobalAuto(s);
                    Marshal.StructureToPtr(structure2, intPtr3, true);
                    if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr3, num, intPtr3, num, out num2, IntPtr.Zero))
                    {
                        USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR2 = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr3.ToInt32() + Marshal.SizeOf(structure2)), typeof(USB_STRING_DESCRIPTOR));
                        uSBDevice.DeviceProduct = uSB_STRING_DESCRIPTOR2.bString;
                    }
                    Marshal.FreeHGlobal(intPtr3);
                }
                if (this.PortDeviceDescriptor.iSerialNumber > 0)
                {
                    USB_DESCRIPTOR_REQUEST structure3 = default(USB_DESCRIPTOR_REQUEST);
                    structure3.ConnectionIndex     = this.PortPortNumber;
                    structure3.SetupPacket.wValue  = (short)(768 + this.PortDeviceDescriptor.iSerialNumber);
                    structure3.SetupPacket.wLength = (short)(num - Marshal.SizeOf(structure3));
                    structure3.SetupPacket.wIndex  = 1033;
                    IntPtr intPtr4 = Marshal.StringToHGlobalAuto(s);
                    Marshal.StructureToPtr(structure3, intPtr4, true);
                    if (Acer_USB_Library.DeviceIoControl(intPtr, 2229264, intPtr4, num, intPtr4, num, out num2, IntPtr.Zero))
                    {
                        USB_STRING_DESCRIPTOR uSB_STRING_DESCRIPTOR3 = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(new IntPtr(intPtr4.ToInt32() + Marshal.SizeOf(structure3)), typeof(USB_STRING_DESCRIPTOR));
                        uSBDevice.DeviceSerialNumber = uSB_STRING_DESCRIPTOR3.bString;
                    }
                    Marshal.FreeHGlobal(intPtr4);
                }
                USB_NODE_CONNECTION_DRIVERKEY_NAME uSB_NODE_CONNECTION_DRIVERKEY_NAME = default(USB_NODE_CONNECTION_DRIVERKEY_NAME);
                uSB_NODE_CONNECTION_DRIVERKEY_NAME.ConnectionIndex = this.PortPortNumber;
                num = Marshal.SizeOf(uSB_NODE_CONNECTION_DRIVERKEY_NAME);
                IntPtr intPtr5 = Marshal.AllocHGlobal(num);
                Marshal.StructureToPtr(uSB_NODE_CONNECTION_DRIVERKEY_NAME, intPtr5, true);
                if (Acer_USB_Library.DeviceIoControl(intPtr, 2229280, intPtr5, num, intPtr5, num, out num2, IntPtr.Zero))
                {
                    uSB_NODE_CONNECTION_DRIVERKEY_NAME = (USB_NODE_CONNECTION_DRIVERKEY_NAME)Marshal.PtrToStructure(intPtr5, typeof(USB_NODE_CONNECTION_DRIVERKEY_NAME));
                    uSBDevice.DeviceDriverKey          = uSB_NODE_CONNECTION_DRIVERKEY_NAME.DriverKeyName;
                    uSBDevice.DeviceName       = Acer_USB_Library.GetDescriptionByKeyName(uSBDevice.DeviceDriverKey);
                    uSBDevice.DeviceInstanceID = Acer_USB_Library.GetInstanceIDByKeyName(uSBDevice.DeviceDriverKey);
                }
                Marshal.FreeHGlobal(intPtr5);
                Acer_USB_Library.CloseHandle(intPtr);
            }
            return(uSBDevice);
        }