Example #1
0
        /// <summary>
        /// 增加端口节点
        /// </summary>
        /// <param name="HubNode">集线器节点</param>
        /// <param name="NodeConnectionInfo">USB设备节点连接信息</param>
        private static void AddPortNode(XElement HubNode, UsbNodeConnectionInformation NodeConnectionInfo)
        {
            String DevicePath      = NodeConnectionInfo.DevicePath;
            Int32  ConnectionIndex = NodeConnectionInfo.ConnectionIndex;
            USB_CONNECTION_STATUS ConnectionStatus = NodeConnectionInfo.ConnectionStatus;

            // 创建端口节点
            XElement PortNode = new XElement("Port" + ConnectionIndex,
                                             new XAttribute("DevicePath", DevicePath),
                                             new XAttribute("ConnectionIndex", ConnectionIndex),
                                             new XAttribute("ConnectionStatus", NodeConnectionInfo.ConnectionStatus)
                                             );

            if (ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected)
            {
                Boolean DeviceIsHub = NodeConnectionInfo.DeviceIsHub;
                PortNode.Add(new XAttribute("DeviceIsHub", DeviceIsHub),
                             new XAttribute("CurrentConfigurationValue", NodeConnectionInfo.CurrentConfigurationValue),
                             new XAttribute("Speed", NodeConnectionInfo.Speed),
                             new XAttribute("DeviceAddress", NodeConnectionInfo.DeviceAddress),
                             new XAttribute("NumberOfOpenPipes", NodeConnectionInfo.NumberOfOpenPipes)
                             );

                // 设备描述符信息
                AddDeviceDescriptorNode(PortNode, ref NodeConnectionInfo.DeviceDescriptor);

                // 管道信息
                AddPipeInfoNode(PortNode, ref NodeConnectionInfo.PipeList);

                // 外部集线器
                if (DeviceIsHub)
                {   // 获取外部Hub设备路径
                    String ExternalHubPath = GetExternalHubPath(DevicePath, ConnectionIndex);

                    // 增加外部集线器节点
                    AddHubNode(PortNode, ExternalHubPath, "ExternalHub");
                }
            }

            HubNode.Add(PortNode);
        }
        public ReadOnlyCollection <USBPort> GetPorts()
        {
            List <USBPort> list   = new List <USBPort>();
            IntPtr         intPtr = Acer_USB_Library.CreateFile(this.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);

            if (intPtr.ToInt32() != -1)
            {
                int    num     = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
                IntPtr intPtr2 = Marshal.AllocHGlobal(num);
                for (int i = 1; i <= this.HubPortCount; i++)
                {
                    USB_NODE_CONNECTION_INFORMATION_EX uSB_NODE_CONNECTION_INFORMATION_EX = default(USB_NODE_CONNECTION_INFORMATION_EX);
                    uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionIndex = i;
                    Marshal.StructureToPtr(uSB_NODE_CONNECTION_INFORMATION_EX, intPtr2, true);
                    int num2 = default(int);
                    if (Acer_USB_Library.DeviceIoControl(intPtr, 2229320, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero))
                    {
                        uSB_NODE_CONNECTION_INFORMATION_EX = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(intPtr2, typeof(USB_NODE_CONNECTION_INFORMATION_EX));
                        USBPort uSBPort = new USBPort();
                        uSBPort.PortPortNumber    = i;
                        uSBPort.PortHubDevicePath = this.HubDevicePath;
                        USB_CONNECTION_STATUS connectionStatus = (USB_CONNECTION_STATUS)uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus;
                        uSBPort.PortStatus = connectionStatus.ToString();
                        USB_DEVICE_SPEED speed = (USB_DEVICE_SPEED)uSB_NODE_CONNECTION_INFORMATION_EX.Speed;
                        uSBPort.PortSpeed             = speed.ToString();
                        uSBPort.PortIsDeviceConnected = (uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus == 1);
                        uSBPort.PortIsHub             = Convert.ToBoolean(uSB_NODE_CONNECTION_INFORMATION_EX.DeviceIsHub);
                        uSBPort.PortDeviceDescriptor  = uSB_NODE_CONNECTION_INFORMATION_EX.DeviceDescriptor;
                        list.Add(uSBPort);
                    }
                }
                Marshal.FreeHGlobal(intPtr2);
                Acer_USB_Library.CloseHandle(intPtr);
            }
            return(new ReadOnlyCollection <USBPort>(list));
        }