comparison USBLib/Communication/WindowsUsbDeviceRegistry.cs @ 21:dcfec2be27c9

Added USBLib
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 15 Apr 2013 01:04:59 +0200
parents
children 556b4fb511bd
comparison
equal deleted inserted replaced
20:c873e3dd73fe 21:dcfec2be27c9
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
12 // Parsed out of the device ID
13 private bool mIsDeviceIDParsed;
14 private byte mInterfaceID;
15 private ushort mVid;
16 private ushort mPid;
17 private ushort mRevision;
18
19 private IDictionary<string, object> mDeviceProperties;
20
21 public String DevicePath { get; private set; }
22 public String DeviceID { get; private set; }
23 public String SymbolicName { get { return DevicePath; } }
24
25 private static Regex RegHardwareID = null;
26
27 protected WindowsUsbDeviceRegistry(DeviceNode device, String interfacepath) {
28 DeviceNode = device;
29 DeviceID = device.DeviceID;
30 DevicePath = interfacepath;
31 }
32
33 public IDictionary<string, object> DeviceProperties {
34 get {
35 if (mDeviceProperties == null) mDeviceProperties = SetupApi.GetSPDRPProperties(DeviceNode);
36 return mDeviceProperties;
37 }
38 }
39
40 private void parseDeviceID() {
41 if (mIsDeviceIDParsed) return;
42 if (RegHardwareID == null) {
43 RegexOptions OPTIONS = RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase;
44 string PATTERN = "(Vid_(?<Vid>[0-9A-F]{1,4}))|(Pid_(?<Pid>[0-9A-F]{1,4}))|(Rev_(?<Rev>[0-9]{1,4}))|(MI_(?<MI>[0-9A-F]{1,2}))";
45 RegHardwareID = new Regex(PATTERN, OPTIONS);
46 }
47 String[] HardwareIDs = DeviceNode.GetPropertyStringArray(CMRDP.HARDWAREID);
48 String HardwareID = null;
49 if (HardwareIDs == null || HardwareIDs.Length < 1 || HardwareIDs[0].Length == 0) {
50 HardwareID = DeviceID;
51 } else {
52 HardwareID = HardwareIDs[0];
53 }
54 foreach (Match match in RegHardwareID.Matches(HardwareID)) {
55 Group group = match.Groups["Vid"];
56 if (group.Success) ushort.TryParse(group.Value, NumberStyles.HexNumber, null, out mVid);
57 group = match.Groups["Pid"];
58 if (group.Success) ushort.TryParse(group.Value, NumberStyles.HexNumber, null, out mPid);
59 group = match.Groups["Rev"];
60 if (group.Success) ushort.TryParse(group.Value, NumberStyles.Integer, null, out mRevision);
61 group = match.Groups["MI"];
62 if (group.Success) Byte.TryParse(group.Value, NumberStyles.HexNumber, null, out mInterfaceID);
63 }
64 mIsDeviceIDParsed = true;
65 }
66 public int Vid {
67 get {
68 parseDeviceID();
69 return mVid;
70 }
71 }
72 public int Pid {
73 get {
74 parseDeviceID();
75 return mPid;
76 }
77 }
78 public byte InterfaceID {
79 get {
80 parseDeviceID();
81 return (byte)mInterfaceID;
82 }
83 }
84
85 public string Name { get { return DeviceNode.GetPropertyString(CMRDP.DEVICEDESC); } }
86 public string Manufacturer { get { return DeviceNode.GetPropertyString(CMRDP.MFG); } }
87 public string FullName {
88 get {
89 String desc = Name;
90 String mfg = Manufacturer;
91 if (mfg == null) return desc;
92 if (desc == null) return mfg;
93 return mfg + " - " + desc;
94 }
95 }
96 }
97 }