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

LinuxDeviceNotifier.DevMonitor.cs « Linux « DeviceNotify « LibWinUsb - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bbdab1f169943dccde51add24a0d5331c7403c48 (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
// 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.IO;
using System.Text.RegularExpressions;
using LibUsbDotNet.Descriptors;

namespace LibUsbDotNet.DeviceNotify.Linux
{
    public partial class LinuxDeviceNotifier
    {
        private static Regex _RegParseDeviceInterface;
        private static string DeviceIntefaceMatchExpression = "usbdev(?<BusNumber>[0-9]+)\\.(?<DeviceAddress>[0-9]+)$";
        private readonly string mDevDir;
        private FileSystemWatcher mUsbFS;

        private static Regex RegParseDeviceInterface
        {
            get
            {
                if (ReferenceEquals(_RegParseDeviceInterface, null))
                {
                    _RegParseDeviceInterface = new Regex(DeviceIntefaceMatchExpression,
                                                         RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant |
                                                         RegexOptions.Singleline);
                }
                return _RegParseDeviceInterface;
            }
        }


        private static bool IsDeviceEnterface(string name, out byte busNumber, out byte deviceAddress)
        {
            busNumber = 0;
            deviceAddress = 0;

            Match match = RegParseDeviceInterface.Match(name);
            if (match.Success)
            {
                try
                {
                    busNumber = byte.Parse(match.Groups["BusNumber"].Value);
                    deviceAddress = byte.Parse(match.Groups["DeviceAddress"].Value);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return false;
        }

        private static bool ReadFileDescriptor(string fullPath, out byte[] deviceDescriptorBytes)
        {
            try
            {
                FileStream f = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                deviceDescriptorBytes = new byte[UsbDeviceDescriptor.Size];
                int iRead = f.Read(deviceDescriptorBytes, 0, UsbDeviceDescriptor.Size);
                f.Close();

                return iRead == UsbDeviceDescriptor.Size;
            }
            catch
            {
                deviceDescriptorBytes = null;
                return false;
            }
        }

        private void StartDevDirectoryMonitor()
        {
            if (!ReferenceEquals(mUsbFS, null)) return;

            BuildDevList();
            mUsbFS = new FileSystemWatcher(mDevDir);
            mUsbFS.IncludeSubdirectories = false;
            mUsbFS.Created += FileAdded;
            mUsbFS.Deleted += FileRemoved;
            mUsbFS.EnableRaisingEvents = true;
        }

        private void BuildDevList()
        {
            mLinuxDevItemList.Clear();
            string[] deviceInterfaceFiles = Directory.GetFiles(mDevDir, "usbdev*", SearchOption.TopDirectoryOnly);
            foreach (string deviceInterfaceFile in deviceInterfaceFiles)
            {
                byte busNumber;
                byte deviceAddress;
                string deviceFileName = Path.GetFileName(deviceInterfaceFile);
                if (IsDeviceEnterface(deviceFileName, out busNumber, out deviceAddress))
                {
                    byte[] descriptorBytes;
                    if (ReadFileDescriptor(deviceInterfaceFile, out descriptorBytes))
                    {
                        LinuxDevItem addedItem = new LinuxDevItem(deviceFileName, busNumber, deviceAddress, descriptorBytes);
                        mLinuxDevItemList.Add(addedItem);
                    }
                }
            }
            //Console.WriteLine("LinuxDeviceNotifier:BuildDevList Count:{0}",mLinuxDevItemList.Count);
        }

        private void FileRemoved(object sender, FileSystemEventArgs e)
        {
            byte busNumber;
            byte deviceAddress;
            if (IsDeviceEnterface(e.Name, out busNumber, out deviceAddress))
            {
                LinuxDevItem foundLinuxDevItem;
                if ((foundLinuxDevItem = mLinuxDevItemList.FindByName(e.Name)) == null) throw new Exception("FileRemoved:Invalid LinuxDevItem");

                //Console.WriteLine("Removed Vid:{0:X4} Pid:{1:X4}", foundLinuxDevItem.DeviceDescriptor.VendorID, foundLinuxDevItem.DeviceDescriptor.ProductID);
                //////////////////////
                // TODO:DEVICE REMOVAL
                //////////////////////

                mLinuxDevItemList.Remove(foundLinuxDevItem);
                EventHandler<DeviceNotifyEventArgs> deviceNotify = OnDeviceNotify;
                if (!ReferenceEquals(deviceNotify, null))
                {
                    deviceNotify(this, new LinuxDeviceNotifyEventArgs(foundLinuxDevItem, DeviceType.DeviceInterface, EventType.DeviceRemoveComplete));
                }
            }
        }

        private void FileAdded(object sender, FileSystemEventArgs e)
        {
            byte busNumber;
            byte deviceAddress;
            if (IsDeviceEnterface(e.Name, out busNumber, out deviceAddress))
            {
                byte[] descriptorBytes;
                if (ReadFileDescriptor(e.FullPath, out descriptorBytes))
                {
                    LinuxDevItem addedItem = new LinuxDevItem(e.Name, busNumber, deviceAddress, descriptorBytes);
                    if (mLinuxDevItemList.FindByName(e.Name) != null) throw new Exception("FileAdded:Invalid LinuxDevItem");
                    mLinuxDevItemList.Add(addedItem);

                    //Console.WriteLine("Added Vid:{0:X4} Pid:{1:X4}", addedItem.DeviceDescriptor.VendorID, addedItem.DeviceDescriptor.ProductID);
                    //////////////////////
                    // TODO:DEVICE ARRIVAL
                    //////////////////////

                    EventHandler<DeviceNotifyEventArgs> deviceNotify = OnDeviceNotify;
                    if (!ReferenceEquals(deviceNotify, null))
                    {
                        deviceNotify(this, new LinuxDeviceNotifyEventArgs(addedItem, DeviceType.DeviceInterface, EventType.DeviceArrival));
                    }
                }
            }
        }

        private void StopDevDirectoryMonitor()
        {
            if (!ReferenceEquals(mUsbFS, null))
            {
                mUsbFS.EnableRaisingEvents = false;
                mUsbFS.Created -= FileAdded;
                mUsbFS.Deleted -= FileRemoved;
                mUsbFS.Dispose();
                mUsbFS = null;
            }
        }
    }
}