Mercurial > hg > ucis.core
annotate USBLib/Communication/WindowsUsbDeviceRegistry.cs @ 94:3c1bba376dca
HTTP: Added support for cookies, improved prefix selector
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sat, 21 Jun 2014 00:40:33 +0200 |
parents | 99ed461509fe |
children |
rev | line source |
---|---|
21 | 1 ???using System; |
2 using System.Collections.Generic; | |
3 using System.Globalization; | |
4 using System.Text.RegularExpressions; | |
5 using UCIS.HWLib.Windows.Devices; | |
6 using UCIS.USBLib.Internal.Windows; | |
7 | |
8 namespace UCIS.USBLib.Communication { | |
9 public abstract class WindowsUsbDeviceRegistry { | |
10 public DeviceNode DeviceNode { get; private set; } | |
11 | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
12 public static Boolean DecodeDeviceIDs(DeviceNode device, out int vendorID, out int productID, out int revision, out int interfaceID) { |
52 | 13 String[] hwids = device.HardwareID; |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
14 String hwid = null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
15 if (hwids == null || hwids.Length < 1 || hwids[0].Length == 0) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
16 hwid = device.DeviceID; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
17 } else { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
18 hwid = hwids[0]; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
19 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
20 vendorID = productID = revision = interfaceID = -1; |
53
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
21 String[] tokens = hwid.Split(new Char[] { '\\', '#', '&' }, StringSplitOptions.None); |
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
22 Boolean isUSB = tokens.Length > 0 && tokens[0] == "USB"; |
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
23 foreach (String token in tokens) { |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
24 if (token.StartsWith("VID_", StringComparison.InvariantCultureIgnoreCase)) { |
53
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
25 if (vendorID == -1) if (!Int32.TryParse(token.Substring(4), NumberStyles.HexNumber, null, out vendorID)) vendorID = -1; |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
26 } else if (token.StartsWith("PID_", StringComparison.InvariantCultureIgnoreCase)) { |
53
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
27 if (productID == -1) if (!Int32.TryParse(token.Substring(4), NumberStyles.HexNumber, null, out productID)) productID = -1; |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
28 } else if (token.StartsWith("REV_", StringComparison.InvariantCultureIgnoreCase)) { |
53
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
29 if (revision == -1) if (!Int32.TryParse(token.Substring(4), NumberStyles.Integer, null, out revision)) revision = -1; |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
30 } else if (token.StartsWith("MI_", StringComparison.InvariantCultureIgnoreCase)) { |
53
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
31 if (interfaceID == -1) if (!Int32.TryParse(token.Substring(3), NumberStyles.HexNumber, null, out interfaceID)) interfaceID = -1; |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
32 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
33 } |
53
df5534af12e9
Small improvements in USB device identification extraction
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
34 return isUSB && vendorID != -1 && productID != -1; |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
35 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
36 |
21 | 37 // Parsed out of the device ID |
38 private bool mIsDeviceIDParsed; | |
39 private byte mInterfaceID; | |
40 private ushort mVid; | |
41 private ushort mPid; | |
42 private ushort mRevision; | |
43 | |
44 private IDictionary<string, object> mDeviceProperties; | |
45 | |
46 public String DevicePath { get; private set; } | |
47 public String DeviceID { get; private set; } | |
48 public String SymbolicName { get { return DevicePath; } } | |
49 | |
50 protected WindowsUsbDeviceRegistry(DeviceNode device, String interfacepath) { | |
51 DeviceNode = device; | |
52 DeviceID = device.DeviceID; | |
53 DevicePath = interfacepath; | |
54 } | |
55 | |
56 public IDictionary<string, object> DeviceProperties { | |
57 get { | |
58 if (mDeviceProperties == null) mDeviceProperties = SetupApi.GetSPDRPProperties(DeviceNode); | |
59 return mDeviceProperties; | |
60 } | |
61 } | |
62 | |
63 private void parseDeviceID() { | |
64 if (mIsDeviceIDParsed) return; | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
65 int vid, pid, rev, mid; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
66 if (!DecodeDeviceIDs(DeviceNode, out vid, out pid, out rev, out mid)) return; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
67 mVid = (UInt16)vid; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
68 mPid = (UInt16)pid; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
69 mRevision = (UInt16)rev; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
70 mInterfaceID = (Byte)mid; |
21 | 71 mIsDeviceIDParsed = true; |
72 } | |
64
99ed461509fe
Fixed data type for USB vendor and product IDs
Ivo Smits <Ivo@UCIS.nl>
parents:
53
diff
changeset
|
73 public UInt16 Vid { |
21 | 74 get { |
75 parseDeviceID(); | |
76 return mVid; | |
77 } | |
78 } | |
64
99ed461509fe
Fixed data type for USB vendor and product IDs
Ivo Smits <Ivo@UCIS.nl>
parents:
53
diff
changeset
|
79 public UInt16 Pid { |
21 | 80 get { |
81 parseDeviceID(); | |
82 return mPid; | |
83 } | |
84 } | |
85 public byte InterfaceID { | |
86 get { | |
87 parseDeviceID(); | |
88 return (byte)mInterfaceID; | |
89 } | |
90 } | |
91 | |
92 public string Name { get { return DeviceNode.GetPropertyString(CMRDP.DEVICEDESC); } } | |
93 public string Manufacturer { get { return DeviceNode.GetPropertyString(CMRDP.MFG); } } | |
94 public string FullName { | |
95 get { | |
96 String desc = Name; | |
97 String mfg = Manufacturer; | |
98 if (mfg == null) return desc; | |
99 if (desc == null) return mfg; | |
100 return mfg + " - " + desc; | |
101 } | |
102 } | |
103 } | |
104 } |