[TCSACR-93] Add USB Host C# API
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Usb / UsbManager.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20
21 namespace Tizen.System.Usb
22 {
23     /// <summary>
24     /// USB Manager class.
25     /// </summary>
26     public class UsbManager : IDisposable
27     {
28         private readonly Interop.UsbContextHandle _context;
29         private readonly Interop.HostHotplugHandle _hotpluggedHandle;
30         private List<UsbDevice> _devices = new List<UsbDevice>();
31
32         /// <summary>
33         /// USB Manager Constructor.
34         /// </summary>
35         public UsbManager()
36         {
37             _context = new Interop.UsbContextHandle();
38             _devices = _context.GetDeviceList().Select(devHandle => new UsbDevice(this, devHandle)).ToList();
39             _context.SetHotplugCb(HostHotplugCallback, Interop.HotplugEventType.Any,
40                 IntPtr.Zero, out _hotpluggedHandle).ThrowIfFailed("Failed to set hot plugged callback");
41         }
42
43         /// <summary>
44         /// This function returns list of USB devices attached to system.
45         /// </summary>
46         /// <exception cref="NotSupportedException">Throws exception if USB host feature is not enabled.</exception>
47         /// <exception cref="OutOfMemoryException">Throws exception in case of insufficient memory.</exception>
48         /// <exception cref="UnauthorizedAccessException">Throws exception if user has insufficient permission on device.</exception>
49         public IEnumerable<UsbDevice> AvailableDevices
50         {
51             get
52             {
53                 ThrowIfDisposed();
54                 return _devices;
55             }
56         }
57
58         /// <summary>
59         /// Event handler for events when a USB device is attached or detached.
60         /// </summary>
61         public event EventHandler<HotPluggedEventArgs> DeviceHotPlugged;
62
63         internal void HostHotplugCallback(IntPtr devHandle, IntPtr userData)
64         {
65             Interop.HostDeviceHandle handle = new Interop.HostDeviceHandle(devHandle);
66             UsbDevice device = _devices.Where(dev => dev._handle == handle).FirstOrDefault();
67             if (device == null)
68             {
69                 device = new UsbDevice(this, handle);
70                 _devices.Add(device);
71
72                 if (DeviceHotPlugged != null)
73                 {
74                     DeviceHotPlugged.Invoke(this, new HotPluggedEventArgs(device, HotplugEventType.Attach));
75                 }
76             }
77             else
78             {
79                 if (DeviceHotPlugged != null)
80                 {
81                     DeviceHotPlugged.Invoke(this, new HotPluggedEventArgs(device, HotplugEventType.Detach));
82                 }
83
84                 _devices.Remove(device);
85                 // do we need to dispose device here ?
86             }
87         }
88
89         internal void ThrowIfDisposed()
90         {
91             if (disposedValue) throw new ObjectDisposedException("USB Context is already disposed");
92         }
93
94         #region IDisposable Support
95         private bool disposedValue = false;
96
97         protected virtual void Dispose(bool disposing)
98         {
99             if (!disposedValue)
100             {
101                 _hotpluggedHandle.UnsetHotplugCb();
102                 _context.Dispose();
103                 disposedValue = true;
104             }
105         }
106
107         ~UsbManager()
108         {
109             Dispose(false);
110         }
111
112         public void Dispose()
113         {
114             Dispose(true);
115             GC.SuppressFinalize(this);
116         }
117         #endregion
118     }
119 }