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

LibUsbRegistry.cs « LibUsb « LibWinUsb - github.com/ClusterM/clovershell-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5204fd1c00feacbc42d445df3019162daeacdaca (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// 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.Runtime.InteropServices;
using System.Text;
using LibUsbDotNet.Internal.LibUsb;
using LibUsbDotNet.Main;
using Microsoft.Win32.SafeHandles;
using Debug=System.Diagnostics.Debug;

namespace LibUsbDotNet.LibUsb
{
    /// <summary> LibUsb specific members for device registry settings.
    /// </summary> 
    public class LibUsbRegistry : UsbRegistry
    {
        private readonly string mDeviceFilename;
        private readonly int mDeviceIndex;

        private LibUsbRegistry(SafeFileHandle usbHandle, string deviceFileName, int deviceIndex)
        {
            mDeviceFilename = deviceFileName;
            mDeviceIndex = deviceIndex;
            GetPropertiesSPDRP(usbHandle);
            string symbolicName;

            if (GetCustomDeviceKeyValue(usbHandle, SYMBOLIC_NAME_KEY, out symbolicName, 512) == ErrorCode.None)
            {
                mDeviceProperties.Add(SYMBOLIC_NAME_KEY, symbolicName);
            }

            // If the SymbolicName key does not exists, use the first HardwareID string.
            if (!mDeviceProperties.ContainsKey(SYMBOLIC_NAME_KEY) || String.IsNullOrEmpty(symbolicName))
            {
                string[] hwIDs = mDeviceProperties[SPDRP.HardwareId.ToString()] as string[];

                if ((hwIDs == null) || hwIDs.Length==0)
                {
                    LibUsbDevice usbDevice = new LibUsbDevice(UsbDevice.LibUsbApi, usbHandle, deviceFileName);
                    LegacyUsbRegistry.GetPropertiesSPDRP(usbDevice, mDeviceProperties);
                    return;
                }

                if (hwIDs.Length > 0)
                {
                    mDeviceProperties.Add(SYMBOLIC_NAME_KEY, hwIDs[0]);
                }
            }

            string deviceInterfaceGuids;
            if (GetCustomDeviceKeyValue(usbHandle, LIBUSB_INTERFACE_GUIDS, out deviceInterfaceGuids, 512) == ErrorCode.None)
            {
                string[] deviceInterfaceGuidsArray = deviceInterfaceGuids.Split(new char[] {'\0'}, StringSplitOptions.RemoveEmptyEntries);

                mDeviceProperties.Add(LIBUSB_INTERFACE_GUIDS, deviceInterfaceGuidsArray);
            }
        }

        /// <summary>
        /// Gets the 0 based index of this libusb device
        /// </summary>
        public int DeviceIndex
        {
            get { return mDeviceIndex; }
        }

        /// <summary>
        /// Gets a list of available LibUsb devices.
        /// </summary>
        public static List<LibUsbRegistry> DeviceList
        {
            get
            {
                List<LibUsbRegistry> deviceList = new List<LibUsbRegistry>();
                for (int i = 1; i < UsbConstants.MAX_DEVICES; i++)
                {
                    string deviceFileName = LibUsbDriverIO.GetDeviceNameString(i);

                    SafeFileHandle deviceHandle = LibUsbDriverIO.OpenDevice(deviceFileName);
                    if (deviceHandle != null && !deviceHandle.IsInvalid && !deviceHandle.IsClosed)
                    {
                        try
                        {
                            LibUsbRegistry regInfo = new LibUsbRegistry(deviceHandle, deviceFileName, i);
                            //System.Diagnostics.Debug.Print("Address:{0}, Index:{1}, {2}", regInfo[SPDRP.Address], i, regInfo[SPDRP.DeviceDesc]);
                            deviceList.Add(regInfo);
                        }
                        catch (Exception ex)
                        {
                            Debug.Print(ex.Message);
                        }
                    }
                    if (deviceHandle != null && !deviceHandle.IsClosed) deviceHandle.Close();
                }

                return deviceList;
            }
        }

        /// <summary>
        /// Check this value to determine if the usb device is still connected to the bus and ready to open.
        /// </summary>
        public override bool IsAlive
        {
            get
            {
                if (String.IsNullOrEmpty(SymbolicName)) throw new UsbException(this, "A symbolic name is required for this property.");
                List<LibUsbRegistry> deviceList = DeviceList;
                foreach (LibUsbRegistry registry in deviceList)
                {
                    if (String.IsNullOrEmpty(registry.SymbolicName)) continue;

                    if (registry.SymbolicName == SymbolicName)
                        return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Opens the USB device for communucation.
        /// </summary>
        /// <returns>Return a new instance of the <see cref="UsbDevice"/> class.
        /// If the device fails to open a null refrence is return. For extended error
        /// information use the <see cref="UsbDevice.UsbErrorEvent"/>.
        ///  </returns>
        public override UsbDevice Device
        {
            get
            {
                LibUsbDevice libUsbDevice;
                Open(out libUsbDevice);
                return libUsbDevice;
            }
        }

        /// <summary>
        /// Gets the DeviceInterfaceGuids for the WinUsb device.
        /// </summary>
        public override Guid[] DeviceInterfaceGuids
        {
            get
            {
                if (ReferenceEquals(mDeviceInterfaceGuids, null))
                {
                    if (!mDeviceProperties.ContainsKey(LIBUSB_INTERFACE_GUIDS)) return new Guid[0];

                    string[] saDeviceInterfaceGuids = mDeviceProperties[LIBUSB_INTERFACE_GUIDS] as string[];
                    if (ReferenceEquals(saDeviceInterfaceGuids, null)) return new Guid[0];

                    mDeviceInterfaceGuids = new Guid[saDeviceInterfaceGuids.Length];

                    for (int i = 0; i < saDeviceInterfaceGuids.Length; i++)
                    {
                        string sGuid = saDeviceInterfaceGuids[i].Trim(new char[] {' ', '{', '}', '[', ']', '\0'});
                        mDeviceInterfaceGuids[i] = new Guid(sGuid);
                    }
                }
                return mDeviceInterfaceGuids;
            }
        }

        /// <summary>
        /// Opens the USB device for communucation.
        /// </summary>
        /// <param name="usbDevice">The newly created UsbDevice.</param>
        /// <returns>True on success.</returns>
        public bool Open(out LibUsbDevice usbDevice)
        {
            bool bSuccess = LibUsbDevice.Open(mDeviceFilename, out usbDevice);
            if (bSuccess)
            {
                usbDevice.mUsbRegistry = this;
            }

            return bSuccess;
        }

        /// <summary>
        /// Opens the USB device for communucation.
        /// </summary>
        /// <param name="usbDevice">The newly created UsbDevice.</param>
        /// <returns>True on success.</returns>
        public override bool Open(out UsbDevice usbDevice)
        {
            usbDevice = null;
            LibUsbDevice libUsbDevice;
            bool bSuccess = Open(out libUsbDevice);
            if (bSuccess)
                usbDevice = libUsbDevice;
            return bSuccess;
        }

        internal ErrorCode GetCustomDeviceKeyValue(SafeFileHandle usbHandle, string key, out string propData, int maxDataLength)
        {
            byte[] propDataBytes;
            ErrorCode eReturn = GetCustomDeviceKeyValue(usbHandle, key, out propDataBytes, maxDataLength);
            if (eReturn == ErrorCode.None)
            {
                propData = Encoding.Unicode.GetString(propDataBytes);
                propData.TrimEnd(new char[] {'\0'});
            }
            else
            {
                propData = null;
            }

            return eReturn;
        }

        internal ErrorCode GetCustomDeviceKeyValue(SafeFileHandle usbHandle, string key, out byte[] propData, int maxDataLength)
        {
            ErrorCode eReturn = ErrorCode.None;
            int iReturnBytes;
            propData = null;
            byte[] bytesReq = LibUsbDeviceRegistryKeyRequest.RegGetRequest(key, maxDataLength);
            GCHandle gcbytesReq = GCHandle.Alloc(bytesReq, GCHandleType.Pinned);

            bool bSuccess = LibUsbDriverIO.UsbIOSync(usbHandle,
                                                     LibUsbIoCtl.GET_CUSTOM_REG_PROPERTY,
                                                     bytesReq,
                                                     bytesReq.Length,
                                                     gcbytesReq.AddrOfPinnedObject(),
                                                     bytesReq.Length,
                                                     out iReturnBytes);
            gcbytesReq.Free();
            if (bSuccess)
            {
                propData = new byte[iReturnBytes];
                Array.Copy(bytesReq, propData, iReturnBytes);
            }
            else
            {
                eReturn = ErrorCode.GetDeviceKeyValueFailed;
                // dont log this as an error; 
                //UsbError.Error(eReturn,0, "Failed getting device registry Key:" + key, this);
            }
            return eReturn;
        }

        private void GetPropertiesSPDRP(SafeHandle usbHandle)
        {
            byte[] propBuffer = new byte[1024];
            GCHandle gcPropBuffer = GCHandle.Alloc(propBuffer, GCHandleType.Pinned);

            LibUsbRequest req = new LibUsbRequest();
            Dictionary<string, int> allProps = Helper.GetEnumData(typeof (DevicePropertyType));
            foreach (KeyValuePair<string, int> prop in allProps)
            {
                object oValue = String.Empty;

                req.DeviceProperty.ID = prop.Value;
                int iReturnBytes;
                bool bSuccess = LibUsbDriverIO.UsbIOSync(usbHandle,
                                                         LibUsbIoCtl.GET_REG_PROPERTY,
                                                         req,
                                                         LibUsbRequest.Size,
                                                         gcPropBuffer.AddrOfPinnedObject(),
                                                         propBuffer.Length,
                                                         out iReturnBytes);
                if (bSuccess)
                {
                    switch ((DevicePropertyType) prop.Value)
                    {
                        case DevicePropertyType.PhysicalDeviceObjectName:
                        case DevicePropertyType.LocationInformation:
                        case DevicePropertyType.Class:
                        case DevicePropertyType.Mfg:
                        case DevicePropertyType.DeviceDesc:
                        case DevicePropertyType.Driver:
                        case DevicePropertyType.EnumeratorName:
                        case DevicePropertyType.FriendlyName:
                        case DevicePropertyType.ClassGuid:
                            oValue = GetAsString(propBuffer, iReturnBytes);
                            break;
                        case DevicePropertyType.HardwareId:
                        case DevicePropertyType.CompatibleIds:
                            oValue = GetAsStringArray(propBuffer, iReturnBytes);
                            break;
                        case DevicePropertyType.BusNumber:
                        case DevicePropertyType.InstallState:
                        case DevicePropertyType.LegacyBusType:
                        case DevicePropertyType.RemovalPolicy:
                        case DevicePropertyType.UiNumber:
                        case DevicePropertyType.Address:
                            oValue = GetAsStringInt32(propBuffer, iReturnBytes);
                            break;
                        case DevicePropertyType.BusTypeGuid:
                            oValue = GetAsGuid(propBuffer, iReturnBytes);
                            break;
                    }
                }
                else
                    oValue = String.Empty;

                mDeviceProperties.Add(prop.Key, oValue);
            }
            gcPropBuffer.Free();
        }
    }
}