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

MonoUsbProfileList.cs « Profile « MonoLibUsb « LibWinUsb - github.com/ClusterM/clovershell-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05d4fc7e112cba25317857a4ec73416035b63be5 (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
// 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;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using LibUsbDotNet;
using LibUsbDotNet.Main;

namespace MonoLibUsb.Profile
{
    /// <summary>
    /// Manages the device list.  This class is thread safe.
    /// </summary>
    public class MonoUsbProfileList : IEnumerable<MonoUsbProfile>
    {
        private object LockProfileList = new object();

        private static bool FindDiscoveredFn(MonoUsbProfile check) { return check.mDiscovered; }
        private static bool FindUnDiscoveredFn(MonoUsbProfile check) { return !check.mDiscovered; }

        private List<MonoUsbProfile> mList=new List<MonoUsbProfile>();
        private void FireAddRemove(MonoUsbProfile monoUSBProfile, AddRemoveType addRemoveType)
        {
            EventHandler<AddRemoveEventArgs> temp = AddRemoveEvent;
            if (!ReferenceEquals(temp, null))
            {
                temp(this, new AddRemoveEventArgs(monoUSBProfile, addRemoveType));
            }
        }

        private void SetDiscovered(bool discovered)
        {
            foreach (MonoUsbProfile deviceProfile in this)
            {
                deviceProfile.mDiscovered = discovered;
            }
        }

        private void syncWith(MonoUsbProfileList newList)
        {
            SetDiscovered(false);
            newList.SetDiscovered(true);

            int iNewProfiles = newList.mList.Count;
            for (int iNewProfile = 0; iNewProfile < iNewProfiles; iNewProfile++)
            {
                MonoUsbProfile newProfile = newList.mList[iNewProfile];
                int iFoundOldIndex;
                if ((iFoundOldIndex = mList.IndexOf(newProfile)) == -1)
                {
                    //Console.WriteLine("DeviceDiscovery: Added: {0}", newProfile.ProfileHandle.DangerousGetHandle());
                    newProfile.mDiscovered = true;
                    mList.Add(newProfile);
                    FireAddRemove(newProfile, AddRemoveType.Added);
                }
                else
                {
                    //Console.WriteLine("DeviceDiscovery: Unchanged: Orig:{0} New:{1}", mList[iFoundOldIndex].ProfileHandle.DangerousGetHandle(), newProfile.ProfileHandle.DangerousGetHandle());
                    mList[iFoundOldIndex].mDiscovered = true;
                    newProfile.mDiscovered = false;
                   
                }
            }

            newList.mList.RemoveAll(FindDiscoveredFn);
            newList.Close();

            foreach (MonoUsbProfile deviceProfile in mList)
            {
                if (!deviceProfile.mDiscovered)
                {
                    // Close Unplugged device profiles.
                    //Console.WriteLine("DeviceDiscovery: Removed: {0}", deviceProfile.ProfileHandle.DangerousGetHandle());
                    FireAddRemove(deviceProfile, AddRemoveType.Removed);
                    deviceProfile.Close();
                }
            }

            // Remove Unplugged device profiles.
            mList.RemoveAll(FindUnDiscoveredFn);
        }


        /// <summary>
        /// Refreshes the <see cref="MonoUsbProfile"/> list.
        /// </summary>
        /// <remarks>
        /// <para>This is your entry point into finding a USB device to operate.</para>
        /// <para>This return value of this function indicates the number of devices in the resultant list.</para>
        /// <para>The <see cref="MonoUsbProfileList"/> has a crude form of built-in device notification that works on all platforms. By adding an event handler to the <see cref="AddRemoveEvent"/> changes in the device profile list are reported when <see cref="Refresh"/> is called.</para>
        /// </remarks>
        /// <param name="sessionHandle">A valid <see cref="MonoUsbSessionHandle"/>.</param>
        /// <returns>The number of devices in the outputted list, or <see cref="MonoUsbError.ErrorNoMem"/> on memory allocation failure.</returns>
        /// <example>
        /// <code source="..\MonoLibUsb\MonoUsb.ShowInfo\ShowInfo.cs" lang="cs"/>
        /// </example>
        public int Refresh(MonoUsbSessionHandle sessionHandle)
        {
            lock (LockProfileList)
            {
                MonoUsbProfileList newList = new MonoUsbProfileList();
                MonoUsbProfileListHandle monoUSBProfileListHandle;

                int ret = MonoUsbApi.GetDeviceList(sessionHandle, out monoUSBProfileListHandle);
                if (ret < 0 || monoUSBProfileListHandle.IsInvalid)
                {
#if LIBUSBDOTNET
                    UsbError.Error(ErrorCode.MonoApiError, ret, "Refresh:GetDeviceList Failed", this);
#else
                    System.Diagnostics.Debug.Print("libusb_get_device_list failed:{0} {1}",
                                                   (MonoUsbError) ret,
                                                   MonoUsbApi.StrError((MonoUsbError) ret));
#endif
                    return ret;
                }
                int stopCount = ret;
                foreach (MonoUsbProfileHandle deviceProfileHandle in monoUSBProfileListHandle)
                {
                    newList.mList.Add(new MonoUsbProfile(deviceProfileHandle));
                    stopCount--;
                    if (stopCount <= 0) break;
                }
                syncWith(newList);
                monoUSBProfileListHandle.Close();

                return ret;
            }
        }

        /// <summary>
        /// Frees all unreferenced profiles contained in the list.
        /// </summary>
        /// <remarks>
        /// <para>
        /// <see cref="MonoUsbProfileHandle"/>s that are in-use are never closed until all reference(s) have gone 
        /// out-of-scope or specifically been closed with the <see cref="SafeHandle.Close"/> method.
        /// </para>
        /// </remarks>
        public void Close()
        {
            lock (LockProfileList)
            {
                foreach (MonoUsbProfile profile in mList)
                    profile.Close();

                mList.Clear();
            }
        }

        /// <summary>
        /// Usb device arrival/removal notification handler. 
        /// This event only reports when the <see cref="Refresh"/> method is called.
        /// </summary>
        /// <remarks>
        /// <see cref="AddRemoveEvent"/> could be used for a crude form for receiving usb 
        /// device arrival/removal notification.
        /// </remarks>
        /// <example>
        /// <code>
        /// // Startup code
        /// MonoUsbProfileList profileList = new MonoUsbProfileList();
        /// profileList.AddRemoveEvent += OnDeviceAddRemove;
        /// 
        /// // Device AddRemove event template
        /// private void OnDeviceAddRemove(object sender, AddRemoveEventArgs addRemoveArgs)
        /// {
        /// // This method will only report when Refresh() is called.
        /// }
        /// 
        /// // Refresh profile list.
        /// // Any devices added or removed since the last call to Refresh() will be returned
        /// // in the OnDeviceAddRemove method.
        /// profileList.Refresh();
        /// </code>
        /// </example>
        public event EventHandler<AddRemoveEventArgs> AddRemoveEvent;

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<MonoUsbProfile> GetEnumerator() 
        { 
            lock(LockProfileList)
                return mList.GetEnumerator(); 
        }

        IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }

        /// <summary>
        /// Returns the number of <see cref="MonoUsbProfile"/> instances in the list.
        /// </summary>
        public int Count
        {
            get
            {
                lock (LockProfileList)
                    return mList.Count;
            }
        }
        /// <summary>
        /// Gets a <see cref="List{T}"/> of <see cref="MonoUsbProfile"/> instances.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The <see cref="MonoUsbProfileList"/> uses an internal list that is locked when changes must be made.
        /// The <see cref="GetList"/> method returns a copy of this list that can be searched and modified as needed by the user.
        /// </para>
        /// <para>
        /// The returned generic <see cref="List{T}"/> contains many more functions for finding devices.  
        /// It may be desirable to use these members, such as <see cref="List{T}.FindAll"/> or <see cref="List{T}.ForEach"/> to find a <see cref="MonoUsbProfile"/> instead of iterating through the <see cref="MonoUsbProfileList"/> one-by-one.
        /// </para>
        /// </remarks>
        /// <returns>A <see cref="List{T}"/> of <see cref="MonoUsbProfile"/> instances.</returns>
        public List<MonoUsbProfile> GetList()
        {
            lock (LockProfileList)
                return new List<MonoUsbProfile>(mList);
        }

        /// <summary>
        /// Gets the <see cref="MonoUsbProfile"/> at the specfied index.
        /// </summary>
        /// <param name="index">The index of the <see cref="MonoUsbProfile"/> to retrieve.</param>
        /// <returns>The <see cref="MonoUsbProfile"/> instance at the specified <paramref name="index"/>.</returns>
        /// <exception cref="ArgumentOutOfRangeException">If index is invalid.</exception>
        public MonoUsbProfile this[int index]
        {
            get
            {
                lock (LockProfileList)
                    return mList[index];
            }
        }
    }
}