[USB] Add UsbConfiguration and related changes in UsbInterface 51/133851/3
authorDinesh Dwivedi <dinesh.d@samsung.com>
Wed, 21 Jun 2017 14:52:48 +0000 (20:22 +0530)
committerDinesh Dwivedi <dinesh.d@samsung.com>
Wed, 21 Jun 2017 14:52:48 +0000 (20:22 +0530)
Change-Id: I78a8ace9013b5a751a561b1aa33ed53854a4c884
Signed-off-by: Dinesh Dwivedi <dinesh.d@samsung.com>
Tizen.System.Usb/Interop/Interop.Configuration.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/UsbBulkEndpoint.cs
Tizen.System.Usb/Usb/UsbConfiguration.cs [new file with mode: 0644]
Tizen.System.Usb/Usb/UsbInterface.cs
Tizen.System.Usb/Usb/UsbInterruptEndpoint.cs

diff --git a/Tizen.System.Usb/Interop/Interop.Configuration.cs b/Tizen.System.Usb/Interop/Interop.Configuration.cs
new file mode 100644 (file)
index 0000000..9bf5d8f
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+    [DllImport(Libraries.Usb, EntryPoint = "usb_host_set_config")]
+    internal static extern ErrorCode SetAsActive(this UsbConfigHandle /* usb_host_config_h */ configuration);
+
+    [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_get_num_interfaces")]
+    internal static extern ErrorCode GetNumInterfaces(this UsbConfigHandle /* usb_host_config_h */ config, out int numInterfaces);
+
+    [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_is_self_powered")]
+    internal static extern ErrorCode IsSelfPowered(this UsbConfigHandle /* usb_host_config_h */ config, out bool selfPowered);
+
+    [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_support_remote_wakeup")]
+    internal static extern ErrorCode SupportRemoteWakeup(this UsbConfigHandle /* usb_host_config_h */ config, out bool remoteWakeup);
+
+    [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_get_max_power")]
+    internal static extern ErrorCode GetMaxPower(this UsbConfigHandle /* usb_host_config_h */ config, out int maxPower);
+
+    [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_config_str")]
+    internal static extern ErrorCode GetConfigStr(this UsbConfigHandle /* usb_host_config_h */ config, ref int length, byte[] data);
+
+    [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_get_interface")]
+    internal static extern ErrorCode GetInterface(this UsbConfigHandle /* usb_host_config_h */ config, int interfaceIndex, out UsbInterfaceHandle /* usb_host_interface_h */ usbInterface);
+
+    internal class UsbConfigHandle : SafeUsbHandle
+    {
+        [DllImport(Libraries.Usb, EntryPoint = "usb_host_config_destroy")]
+        internal static extern ErrorCode ConfigDestroy(UsbConfigHandle /* usb_host_config_h */ config);
+
+        public UsbConfigHandle(IntPtr handle) : base(handle) { }
+        public override void Destroy()
+        {
+            ConfigDestroy(this).ThrowIfFailed("Failed to destroy native HostConfig handle");
+        }
+    }
+}
\ No newline at end of file
index a9b0bf8..0637109 100644 (file)
@@ -36,7 +36,7 @@ namespace Tizen.System.Usb
         /// into the data buffer.
         /// </param>
         /// <param name="timeout">
-        /// Timeout (in milliseconds) that this function should wait before giving up due to no response being
+        /// Time (in milliseconds) that this function should wait for before giving up due to no response being
         /// received(for an unlimited timeout 0 value should be used).
         /// </param>
         /// <returns>Number of bytes actually transferred.</returns>
diff --git a/Tizen.System.Usb/Usb/UsbConfiguration.cs b/Tizen.System.Usb/Usb/UsbConfiguration.cs
new file mode 100644 (file)
index 0000000..294f905
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * 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;
+
+namespace Tizen.System.Usb
+{
+    /// <summary>
+    /// Class to manage USB Configuration.
+    /// </summary>
+    public class UsbConfiguration : IDisposable
+    {
+        internal readonly Interop.UsbConfigHandle _handle;
+        private Dictionary<int, UsbInterface> _interfaces;
+
+        internal UsbConfiguration(Interop.UsbConfigHandle handle)
+        {
+            _handle = handle;
+        }
+
+        /// <summary>
+        /// Checks if device is self-powered in given configuration.
+        /// </summary>
+        public bool IsSelfPowered
+        {
+            get
+            {
+                ThrowIfDisposed();
+                return Interop.NativeGet<bool>(_handle.IsSelfPowered);
+            }
+        }
+
+        /// <summary>
+        /// Checks if device in given configuration supports remote wakeup.
+        /// </summary>
+        public bool SupportRemoteWakeup
+        {
+            get
+            {
+                ThrowIfDisposed();
+                return Interop.NativeGet<bool>(_handle.SupportRemoteWakeup);
+            }
+        }
+
+        /// <summary>
+        /// Gets maximum power required in given configuration, in mA.
+        /// </summary>
+        public int MaximumPowerRequired
+        {
+            get
+            {
+                ThrowIfDisposed();
+                return Interop.NativeGet<int>(_handle.GetMaxPower);
+            }
+        }
+
+        /// <summary>
+        /// Dictionary mapping interfaces Ids to interface instances for given configuration.
+        /// </summary>
+        public IReadOnlyDictionary<int, UsbInterface> Interfaces
+        {
+            get
+            {
+                ThrowIfDisposed();
+                if (_interfaces == null)
+                {
+                    _interfaces = new Dictionary<int, UsbInterface>();
+                    int count = Interop.NativeGet<int>(_handle.GetNumInterfaces);
+                    for (int i = 0; i < count; ++i)
+                    {
+                        Interop.UsbInterfaceHandle handle;
+                        _handle.GetInterface(i, out handle);
+                        UsbInterface usbInterface = new UsbInterface(this, handle);
+                        _interfaces.Add(usbInterface.Id, usbInterface);
+                    }
+                }
+                return _interfaces;
+            }
+        }
+
+        /// <summary>
+        /// Configuration string.
+        /// </summary>
+        public string ConfigurationString
+        {
+            get
+            {
+                ThrowIfDisposed();
+                return Interop.DescriptorString(_handle.GetConfigStr);
+            }
+        }
+
+        /// <summary>
+        /// Set this configuration as active configuration for the device.
+        /// </summary>
+        /// <exception cref="InvalidOperationException">
+        /// Throws exception if device is disconnected or not opened for operation or busy as its interfaces are currently claimed.
+        /// </exception>
+        public void SetAsActive()
+        {
+            ThrowIfDisposed();
+            _handle.SetAsActive();
+        }
+
+        internal void ThrowIfDisposed()
+        {
+            if (disposedValue) throw new ObjectDisposedException("Configuration is already disposed");
+        }
+
+        #region IDisposable Support
+        private bool disposedValue = false;
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!disposedValue)
+            {
+                _handle.Dispose();
+                disposedValue = true;
+            }
+        }
+
+        ~UsbConfiguration()
+        {
+            Dispose(false);
+        }
+
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+        #endregion
+    }
+}
\ No newline at end of file
index 961b04e..59ead92 100644 (file)
@@ -24,11 +24,13 @@ namespace Tizen.System.Usb
     public class UsbInterface
     {
         internal readonly Interop.UsbInterfaceHandle _handle;
+        private readonly UsbConfiguration _parent;
         private Dictionary<int, UsbEndpoint> _endpoints;
         private bool _isClaimed;
 
-        internal UsbInterface(Interop.UsbInterfaceHandle handle)
+        internal UsbInterface(UsbConfiguration parent, Interop.UsbInterfaceHandle handle)
         {
+            _parent = parent;
             _handle = handle;
         }
 
@@ -122,6 +124,7 @@ namespace Tizen.System.Usb
 
         internal void ThrowIfDisposed()
         {
+            _parent.ThrowIfDisposed();
         }
     }
 }
index 29b48d5..b0c5300 100644 (file)
@@ -46,7 +46,7 @@ namespace Tizen.System.Usb
         /// into the data buffer.
         /// </param>
         /// <param name="timeout">
-        /// Timeout (in milliseconds) that this function should wait before giving up due to no response being
+        /// Time (in milliseconds) that this function should wait for before giving up due to no response being
         /// received(for an unlimited timeout 0 value should be used).
         /// </param>
         /// <returns>Number of bytes actually transferred.</returns>