/
UsbManager.cs
executable file
·153 lines (134 loc) · 5.94 KB
/
UsbManager.cs
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
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace Tizen.System.Usb
{
/// <summary>
/// The USB Manager class.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public class UsbManager : IDisposable
{
// It needs to be static as its destroy function must be called after closing all devices and before application close.
// It has to be called to clean the memory used by library.
private static readonly Interop.UsbContextHandle _context = null;
private readonly Interop.HostHotplugHandle _hotpluggedAttachHandle = null;
private readonly Interop.HostHotplugHandle _hotpluggedDetachHandle = null;
static UsbManager()
{
bool isSupported;
Information.TryGetValue("http://tizen.org/feature/usb.host", out isSupported);
if (isSupported)
{
_context = Interop.UsbContextHandle.GetContextHandle();
}
}
/// <summary>
/// The USB Manager constructor.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public UsbManager()
{
if (_context == null) Interop.ErrorCode.NotSupported.ThrowIfFailed("USB host operations are not supported in this device");
IntPtr hotpluggedAttachHandle, hotpluggedDetachHandle;
_context.SetHotplugCb(HostHotplugAttachCallback, Interop.HotplugEventType.Attach,
IntPtr.Zero, out hotpluggedAttachHandle).ThrowIfFailed("Failed to set hot plugged callback");
_context.SetHotplugCb(HostHotplugDetachCallback, Interop.HotplugEventType.Detach,
IntPtr.Zero, out hotpluggedDetachHandle).ThrowIfFailed("Failed to set hot plugged callback");
_hotpluggedAttachHandle = new Interop.HostHotplugHandle(hotpluggedAttachHandle);
_hotpluggedDetachHandle = new Interop.HostHotplugHandle(hotpluggedDetachHandle);
}
/// <summary>
/// This function returns a list of USB devices attached to the system.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
/// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
/// <since_tizen> 4 </since_tizen>
public IEnumerable<UsbDevice> AvailableDevices
{
get
{
ThrowIfDisposed();
return _context.GetDeviceList().Select(devHandle => new UsbDevice(this, devHandle)).ToList();
}
}
/// <summary>
/// An event handler for events when the USB device is attached or detached.
/// </summary>
/// <feature>http://tizen.org/feature/usb.host</feature>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
public event EventHandler<HotPluggedEventArgs> DeviceHotPlugged;
internal void HostHotplugAttachCallback(IntPtr devHandle, IntPtr userData)
{
Interop.HostDeviceHandle handle = new Interop.HostDeviceHandle(devHandle);
UsbDevice device = new UsbDevice(this, handle);
if (DeviceHotPlugged != null)
{
DeviceHotPlugged.Invoke(this, new HotPluggedEventArgs(device, HotplugEventType.Attach));
}
//AvailableDevices.Remove(device);
// do we need to dispose device here ?
}
internal void HostHotplugDetachCallback(IntPtr devHandle, IntPtr userData)
{
Interop.HostDeviceHandle handle = new Interop.HostDeviceHandle(devHandle);
UsbDevice device = new UsbDevice(this, handle);
if (DeviceHotPlugged != null)
{
DeviceHotPlugged.Invoke(this, new HotPluggedEventArgs(device, HotplugEventType.Detach));
}
//AvailableDevices.Remove(device);
// do we need to dispose device here ?
}
internal void ThrowIfDisposed()
{
if (disposedValue) throw new ObjectDisposedException("USB Context is already disposed");
}
#region IDisposable Support
private bool disposedValue = false;
internal virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (_hotpluggedAttachHandle != null) _hotpluggedAttachHandle.Dispose();
if (_hotpluggedDetachHandle != null) _hotpluggedDetachHandle.Dispose();
disposedValue = true;
}
}
/// <summary>
/// Finalizes an instance of the USB Manager Class.
/// </summary>
/// <since_tizen> 4 </since_tizen>
~UsbManager()
{
Dispose(false);
}
/// <since_tizen> 4 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}