Mercurial > hg > ucis.core
annotate USBLib/Communication/WindowsUsbDeviceRegistry.cs @ 52:d4778c3232ad
Prevent exception in HWLib
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 01 Oct 2013 00:24:39 +0200 |
parents | 556b4fb511bd |
children | df5534af12e9 |
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; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
21 foreach (String token in hwid.Split(new Char[] { '\\', '#', '&' }, StringSplitOptions.None)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
22 if (token.StartsWith("VID_", StringComparison.InvariantCultureIgnoreCase)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
23 if (!Int32.TryParse(token.Substring(4), NumberStyles.HexNumber, null, out vendorID)) vendorID = -1; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
24 } else if (token.StartsWith("PID_", StringComparison.InvariantCultureIgnoreCase)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
25 if (!Int32.TryParse(token.Substring(4), NumberStyles.HexNumber, null, out productID)) productID = -1; |
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("REV_", StringComparison.InvariantCultureIgnoreCase)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
27 if (!Int32.TryParse(token.Substring(4), NumberStyles.Integer, null, out revision)) revision = -1; |
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("MI_", StringComparison.InvariantCultureIgnoreCase)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
29 if (!Int32.TryParse(token.Substring(3), NumberStyles.HexNumber, null, out interfaceID)) interfaceID = -1; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
30 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
31 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
32 return vendorID != -1 && productID != -1; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
33 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
34 |
21 | 35 // Parsed out of the device ID |
36 private bool mIsDeviceIDParsed; | |
37 private byte mInterfaceID; | |
38 private ushort mVid; | |
39 private ushort mPid; | |
40 private ushort mRevision; | |
41 | |
42 private IDictionary<string, object> mDeviceProperties; | |
43 | |
44 public String DevicePath { get; private set; } | |
45 public String DeviceID { get; private set; } | |
46 public String SymbolicName { get { return DevicePath; } } | |
47 | |
48 protected WindowsUsbDeviceRegistry(DeviceNode device, String interfacepath) { | |
49 DeviceNode = device; | |
50 DeviceID = device.DeviceID; | |
51 DevicePath = interfacepath; | |
52 } | |
53 | |
54 public IDictionary<string, object> DeviceProperties { | |
55 get { | |
56 if (mDeviceProperties == null) mDeviceProperties = SetupApi.GetSPDRPProperties(DeviceNode); | |
57 return mDeviceProperties; | |
58 } | |
59 } | |
60 | |
61 private void parseDeviceID() { | |
62 if (mIsDeviceIDParsed) return; | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
63 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
|
64 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
|
65 mVid = (UInt16)vid; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
66 mPid = (UInt16)pid; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
67 mRevision = (UInt16)rev; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
68 mInterfaceID = (Byte)mid; |
21 | 69 mIsDeviceIDParsed = true; |
70 } | |
71 public int Vid { | |
72 get { | |
73 parseDeviceID(); | |
74 return mVid; | |
75 } | |
76 } | |
77 public int Pid { | |
78 get { | |
79 parseDeviceID(); | |
80 return mPid; | |
81 } | |
82 } | |
83 public byte InterfaceID { | |
84 get { | |
85 parseDeviceID(); | |
86 return (byte)mInterfaceID; | |
87 } | |
88 } | |
89 | |
90 public string Name { get { return DeviceNode.GetPropertyString(CMRDP.DEVICEDESC); } } | |
91 public string Manufacturer { get { return DeviceNode.GetPropertyString(CMRDP.MFG); } } | |
92 public string FullName { | |
93 get { | |
94 String desc = Name; | |
95 String mfg = Manufacturer; | |
96 if (mfg == null) return desc; | |
97 if (desc == null) return mfg; | |
98 return mfg + " - " + desc; | |
99 } | |
100 } | |
101 } | |
102 } |