Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SetupApiRegistry.cs « Main « LibWinUsb - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1878b3e7dcaa3ddbe736219db804d61e5893cf35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright © 2006-2010 Travis Robinson. All rights reserved.
// 
// website: http://sourceforge.net/projects/libusbdotnet
// e-mail:  libusbdotnet@gmail.com
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or 
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. or 
// visit www.gnu.org.
// 
// 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using LibUsbDotNet.Internal;
using LibUsbDotNet.LudnMonoLibUsb;
using Microsoft.Win32;
using MonoLibUsb;
using System.Runtime.InteropServices;

namespace LibUsbDotNet.Main
{
    internal static class SetupApiRegistry
    {
        private static readonly Object mLockSetupApiRegistry = new object();
        private static readonly MasterList mMasterSetupApiDeviceList = new MasterList();
        private static DateTime mLastRefreshTime = DateTime.MinValue;
        public const string DEVICE_ID_KEY = "DeviceInstanceID";

        public static bool NeedsRefresh
        {
            get
            {
                lock (mLockSetupApiRegistry)
                {
                    if ((DateTime.Now - mLastRefreshTime).TotalMilliseconds >= 1000)
                        return true;
                    return false;
                }
            }
        }

        private class MasterItem : Dictionary<string, object>
        {
            public Dictionary<Guid, List<string>> DevicePaths = new Dictionary<Guid, List<string>>();
        }

        private class MasterList : List<MasterItem>
        {
        }

        public static bool FillDeviceProperties(UsbRegistry usbRegistry, UsbDevice usbDevice)
        {

            lock (mLockSetupApiRegistry)
            {
                if (NeedsRefresh) BuildMasterList();
                if (usbDevice is MonoUsbDevice && !Helper.IsLinux)
                    return FillWindowsMonoUsbDeviceRegistry(usbRegistry, (MonoUsbDevice) usbDevice);
                
                string fakeHwId = LegacyUsbRegistry.GetRegistryHardwareID((ushort) usbDevice.Info.Descriptor.VendorID,
                                                                          (ushort) usbDevice.Info.Descriptor.ProductID,
                                                                          (ushort) usbDevice.Info.Descriptor.BcdDevice);
                bool bFound = false;
                string hwIdToFind = fakeHwId.ToLower();
                foreach (MasterItem masterItem in mMasterSetupApiDeviceList)
                {
                    string[] hwIds = masterItem[SPDRP.HardwareId.ToString()] as string[];
                    if (ReferenceEquals(hwIds, null)) continue;
                    foreach (string hwID in hwIds)
                    {
                        if (hwID.ToLower().Contains(hwIdToFind))
                        {
                            usbRegistry.mDeviceProperties = masterItem;
                            bFound = true;
                            break;
                        }
                    }
                    if (bFound) break;
                }
                return bFound;
            }
        }

        private static bool FillWindowsMonoUsbDeviceRegistry(UsbRegistry usbRegistry, MonoUsbDevice usbDevice) 
        {
            MonoLibUsb.MonoUsbApi.internal_windows_device_priv priv = MonoLibUsb.MonoUsbApi.GetWindowsPriv(usbDevice.Profile.ProfileHandle);
            string path;
            for (int i = 0; i < 32; i++)
            {
                if (priv.usb_interfaces[i].path == IntPtr.Zero) break;
                path = Marshal.PtrToStringAnsi(priv.usb_interfaces[i].path);
                //Debug.Print("Intf:{0} Path:{1}",i,path);
            }
            path = Marshal.PtrToStringAnsi(priv.path);

            bool bFound = false;

            //System.Diagnostics.Debug.WriteLine(sb.ToString());
            path = path.ToString().ToLower().Replace("#", "\\");
            foreach (MasterItem masterItem in mMasterSetupApiDeviceList)
            {
                if (path.Contains(masterItem[DEVICE_ID_KEY].ToString().ToLower()))
                {
                    usbRegistry.mDeviceProperties = masterItem;
                    bFound = true;
                    break;
                }
            }
            return bFound;
        }

        public static void BuildMasterList()
        {
            lock (mLockSetupApiRegistry)
            {
                mMasterSetupApiDeviceList.Clear();
                SetupApi.EnumClassDevs(null, SetupApi.DICFG.PRESENT | SetupApi.DICFG.ALLCLASSES, BuildMasterCallback, mMasterSetupApiDeviceList);
                mLastRefreshTime = DateTime.Now;
            }
        }

        private static bool BuildMasterCallback(IntPtr deviceInfoSet, int deviceindex, ref SetupApi.SP_DEVINFO_DATA deviceInfoData, object userData)
        {
            MasterList deviceList = userData as MasterList;
            MasterItem deviceItem = new MasterItem();
            StringBuilder sb=new StringBuilder(256);

            if (SetupApi.CM_Get_Device_ID(deviceInfoData.DevInst, sb, sb.Capacity, 0)!=SetupApi.CR.SUCCESS)
                return false;

            deviceItem.Add(DEVICE_ID_KEY,sb.ToString());
            deviceList.Add(deviceItem);


            RegistryValueKind propertyType;
            byte[] propBuffer = new byte[256];
            int requiredSize;
            bool bSuccess = SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                                    ref deviceInfoData,
                                                                    UsbRegistry.DEVICE_INTERFACE_GUIDS,
                                                                    SetupApi.DICUSTOMDEVPROP.NONE,
                                                                    out propertyType,
                                                                    propBuffer,
                                                                    propBuffer.Length,
                                                                    out requiredSize);
            if (bSuccess)
            {
                string[] devInterfaceGuids = UsbRegistry.GetAsStringArray(propBuffer, requiredSize);

                deviceItem.Add(UsbRegistry.DEVICE_INTERFACE_GUIDS, devInterfaceGuids);
                foreach (string s in devInterfaceGuids)
                {
                    Guid g = new Guid(s);
                    List<string> devicePathList;
                    if (WinUsb.WinUsbRegistry.GetDevicePathList(g, out devicePathList))
                    {
                        deviceItem.DevicePaths.Add(g, devicePathList);
                    }
                }
            }
            else
            {
                bSuccess = SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                                   ref deviceInfoData,
                                                                   UsbRegistry.LIBUSB_INTERFACE_GUIDS,
                                                                   SetupApi.DICUSTOMDEVPROP.NONE,
                                                                   out propertyType,
                                                                   propBuffer,
                                                                   propBuffer.Length,
                                                                   out requiredSize);
                if (bSuccess)
                {
                    string[] devInterfaceGuids = UsbRegistry.GetAsStringArray(propBuffer, requiredSize);

                    deviceItem.Add(UsbRegistry.LIBUSB_INTERFACE_GUIDS, devInterfaceGuids);
                }
            }

            bSuccess =
                SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                        ref deviceInfoData,
                                                        UsbRegistry.SYMBOLIC_NAME_KEY,
                                                        SetupApi.DICUSTOMDEVPROP.NONE,
                                                        out propertyType,
                                                        propBuffer,
                                                        propBuffer.Length,
                                                        out requiredSize);
            if (bSuccess)
            {
                string symbolicName = UsbRegistry.GetAsString(propBuffer, requiredSize);
                deviceItem.Add(UsbRegistry.SYMBOLIC_NAME_KEY, symbolicName);
            }
            SetupApi.getSPDRPProperties(deviceInfoSet, ref deviceInfoData, deviceItem);

            return false;
        }
    }
}