Ejemplo n.º 1
0
                protected static List <USBPort> GetHubPorts(USBHub hub)
                {
                    List <USBPort> ports = new List <USBPort>();
                    // Collect the node information
                    IOCTLcommand ioctl = new IOCTLcommand();

                    ioctl.ioctlNo       = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_NODE_INFORMATION, 0, 0x0);
                    ioctl.inputBuffer   = null;
                    ioctl.outputMaxSize = 128;
                    try {
                        // Open Root hub
                        IntPtr   hHub   = UsbApi.OpenDevice(hub.devicePath, true);
                        IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl);
                        UsbApi.UsbNativeType.USB_NODE_INFORMATION hubInfo = (UsbApi.UsbNativeType.USB_NODE_INFORMATION)UsbApi.UsbNativeType.Deserialize(
                            status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_NODE_INFORMATION));
                        UsbApi.CloseDevice(hHub);

                        // Now Iterate through all the ports
                        for (uint i = 1; i <= hubInfo.NodeInfo.hubDescriptor.bNumberOfPorts; i++)
                        {
                            ports.Add(new USBPort(hub, i));
                        }
                    } catch (Exception) {}


                    return(ports);
                }
Ejemplo n.º 2
0
        // checks the system for the devices listed in the list
        private static void CheckSystem(List <UsbIdentifier> devicesId)
        {
            lock (devicesId)
            {
                foreach (UsbIdentifier usbId in devicesId)
                {
                    List <String> devPaths = UsbApi.GetDevices(usbId.Identifier, usbId.Guid);
                    foreach (String devicePath in devPaths)
                    {
                        bool found = false;
                        // check for duplicate devices
                        foreach (IUsbDevice device in devices)
                        {
                            if (device.DevicePath == devicePath)
                            {
                                found = true;
                                break;
                            }
                        }
                        if (found)
                        {
                            continue;
                        }

                        IUsbDevice dev = new WebServiceUsbDevice(devicePath);
                        dev.UsbId = usbId;
                        devices.Add(dev);
                        // call all the registered clients for USB events
                        UsbNotify(dev, true);
                    }
                }
            }
        }
Ejemplo n.º 3
0
 internal UsbDevice(string systemPath)
 {
     devicePath = systemPath;
     inHandle   = UsbApi.OpenDevice(devicePath + "\\PIPE00", false);
     inPipe     = new IOStream(inHandle, true, new StreamCallback(PipeReadCallback));
     outHandle  = UsbApi.OpenDevice(devicePath + "\\PIPE01", false);
     outPipe    = new IOStream(outHandle, false, new StreamCallback(PipeWriteCallback));
 }
Ejemplo n.º 4
0
 public void Dispose()
 {
     if (inHandle != null)
     {
         UsbApi.CloseDevice(inHandle);
     }
     if (outHandle != null)
     {
         UsbApi.CloseDevice(outHandle);
     }
 }
Ejemplo n.º 5
0
 internal WebServiceUsbDevice(string devicePath)
     : base(devicePath)
 {
     // intialize the function to get the wsdl file
     webServiceWsdl = new IOCTLcommand[1];
     webServiceWsdl[0].FunctionName  = "UDDI descovery";
     webServiceWsdl[0].inputBuffer   = null;
     webServiceWsdl[0].outputMaxSize = 4096;
     // FILE_DEVICE_USB2VIRT, IOCTL_INDEX, METHOD_BUFFERED, FILE_READ_ACCESS
     webServiceWsdl[0].ioctlNo = UsbApi.CTL_CODE(0x65500, 0x800, 0x0, 0x1);
 }
Ejemplo n.º 6
0
        public static List <USBController> GetUSBControllers()
        {
            // Get all host controllers active on the machine
            List <string>        devPaths    = UsbApi.GetDevices(null, UsbApi.GUID_CLASS_USB_HOST_CONTROLLER);
            List <USBController> controllers = new List <USBController>();

            foreach (string path in devPaths)
            {
                controllers.Add(new USBController(path));
            }
            return(controllers);
        }
Ejemplo n.º 7
0
                    protected unsafe static void GetPortInformation(USBPort port)
                    {
                        // Collect the node information
                        IOCTLcommand ioctl = new IOCTLcommand();

                        ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_NODE_CONNECTION_INFORMATION_EX, 0, 0x0);
                        port.connInfo = new UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION();
                        port.connInfo.ConnectionIndex = port.portIndex;
                        ioctl.inputBuffer             = UsbApi.UsbNativeType.Serialize(port.connInfo);
                        ioctl.outputMaxSize           = (uint)Marshal.SizeOf(port.connInfo);
                        try {
                            // Open Root hub
                            IntPtr   hHub   = UsbApi.OpenDevice(port.rootHub.devicePath, true);
                            IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl);
                            UsbApi.CloseDevice(hHub);
                            port.connInfo = (UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION)UsbApi.UsbNativeType.Deserialize(
                                status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION));
                        } catch (Exception) {}
                    }
Ejemplo n.º 8
0
            protected static string GetHCDDriverKeyName(string devicePath)
            {
                char[]       decoded = null;
                IOCTLcommand ioctl   = new IOCTLcommand();

                ioctl.ioctlNo       = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.HCD_GET_DRIVERKEY_NAME, 0, 0x0);
                ioctl.inputBuffer   = null;
                ioctl.outputMaxSize = 260;
                try
                {
                    IntPtr   hDevice = UsbApi.OpenDevice(devicePath, true);
                    IoStatus status  = UsbApi.DeviceIoControl(hDevice, ioctl);
                    UsbApi.CloseDevice(hDevice);
                    // struct {ULONG Length; WCHAR Name[256];} DriverKeyName;
                    decoded = Encoding.Unicode.GetChars(status.buffer, IntPtr.Size, (int)status.size - IntPtr.Size);
                }
                catch (Exception) { }

                return((decoded == null) ? null : new String(decoded));
            }
Ejemplo n.º 9
0
                protected static string GetRootHubDevicePath(USBController controller)
                {
                    // Now get the system name of it's root hub for interrogation
                    char[]       decoded = null;
                    IOCTLcommand ioctl   = new IOCTLcommand();

                    ioctl.ioctlNo       = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.HCD_GET_ROOT_HUB_NAME, 0, 0x0);
                    ioctl.inputBuffer   = null;
                    ioctl.outputMaxSize = 260;
                    try
                    {
                        IntPtr   hDevice = UsbApi.OpenDevice(controller.devicePath, true);
                        IoStatus status  = UsbApi.DeviceIoControl(hDevice, ioctl);
                        UsbApi.CloseDevice(hDevice);
                        // struct {ULONG Length; WCHAR Name[256];} DriverKeyName;
                        decoded = Encoding.Unicode.GetChars(status.buffer, IntPtr.Size, (int)status.size - IntPtr.Size);
                    }
                    catch (Exception) { }

                    return((decoded == null) ? null : "\\\\.\\" + new String(decoded));
                }
Ejemplo n.º 10
0
                    protected unsafe static string GetStringDescriptor(USBPort port, Byte stringIndex)
                    {
                        UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST();
                        descriptor.ConnectionIndex = port.portIndex;
                        // USBD will automatically initialize these fields:
                        //     bmRequest = 0x80
                        //     bRequest  = 0x06
                        //     wValue    = Descriptor Type (high) and Descriptor Index (low byte)
                        descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_STRING_DESCRIPTOR_TYPE << 8) | (ushort)stringIndex);
                        //     wIndex    = Zero (or Language ID for String Descriptors)
                        descriptor.wIndex = (ushort)0;
                        //     wLength   = Length of descriptor buffer
                        descriptor.wLength = (ushort)Marshal.SizeOf(typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR));
                        // Collect the information string
                        IOCTLcommand ioctl = new IOCTLcommand();

                        ioctl.ioctlNo       = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0);
                        ioctl.inputBuffer   = UsbApi.UsbNativeType.Serialize(descriptor);
                        ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor);
                        string result = null;

                        try {
                            // Open Root hub
                            IntPtr   hHub   = UsbApi.OpenDevice(port.rootHub.devicePath, true);
                            IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl);
                            UsbApi.CloseDevice(hHub);
                            descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize(
                                status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST));
                            UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR strDescr = (UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR));
                            result = new String(Encoding.Unicode.GetChars(strDescr.bString, 0, strDescr.bLength));
                        }
                        catch (Exception) { result = "Invalid string value"; }

                        return(result);
                    }
Ejemplo n.º 11
0
 public IoStatus DeviceIoControl(IOCTLcommand command)
 {
     return(UsbApi.DeviceIoControl(outHandle, command));
 }
Ejemplo n.º 12
0
                    internal unsafe static List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> GetConfigurationDescriptors(USBPort port, Byte configIndex)
                    {
                        UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST();
                        descriptor.ConnectionIndex = port.portIndex;
                        // USBD will automatically initialize these fields:
                        //     bmRequest = 0x80
                        //     bRequest  = 0x06
                        //     wValue    = Descriptor Type (high) and Descriptor Index (low byte)
                        descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | (ushort)configIndex);
                        //     wIndex    = Zero (or Language ID for String Descriptors)
                        descriptor.wIndex = (ushort)0;
                        //     wLength   = Length of descriptor buffer
                        descriptor.wLength = (ushort)512;
                        // Collect the information string
                        IOCTLcommand ioctl = new IOCTLcommand();

                        ioctl.ioctlNo       = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0);
                        ioctl.inputBuffer   = UsbApi.UsbNativeType.Serialize(descriptor);
                        ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor);
                        List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> descriptorList = new List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR>();

                        try
                        {
                            // Open Root hub
                            IntPtr   hHub   = UsbApi.OpenDevice(port.rootHub.devicePath, true);
                            IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl);
                            UsbApi.CloseDevice(hHub);
                            descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize(
                                status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST));
                            UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR confDescr = (UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR));
                            descriptorList.Add(confDescr);

                            // Now deserialize all returned descriptors
                            int descrStart = confDescr.bLength;
                            UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR common;
                            while (descrStart < confDescr.wTotalLength)
                            {
                                common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                    descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_COMMON_DESCRIPTOR));
                                switch (common.DescriptorType)
                                {
                                case UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE:
                                    common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                        descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR));
                                    break;

                                case UsbApi.UsbNativeType.UsbDescriptorType.USB_INTERFACE_DESCRIPTOR_TYPE:
                                    common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                        descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_INTERFACE_DESCRIPTOR));
                                    break;

                                case UsbApi.UsbNativeType.UsbDescriptorType.USB_ENDPOINT_DESCRIPTOR_TYPE:
                                    common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                        descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_ENDPOINT_DESCRIPTOR));
                                    break;
                                }
                                descrStart += common.Length;
                                descriptorList.Add(common);
                            }
                        }
                        catch (Exception) {}

                        return(descriptorList);
                    }