From 7d4dd9c87e11e53b49b6f5709850bc37d252cc0f Mon Sep 17 00:00:00 2001 From: Dinesh Dwivedi Date: Wed, 25 Oct 2017 23:41:23 +0530 Subject: [PATCH] [USB] Fixed interop code for proper marshalling Change-Id: I525ab548eff46a81b64e59cc8a5b39c856702a80 Signed-off-by: Dinesh Dwivedi --- src/Tizen.System.Usb/Interop/Interop.Configuration.cs | 2 +- src/Tizen.System.Usb/Interop/Interop.Context.cs | 10 +++++++--- src/Tizen.System.Usb/Interop/Interop.Device.cs | 6 +++--- src/Tizen.System.Usb/Interop/Interop.Interface.cs | 2 +- src/Tizen.System.Usb/Interop/Interop.SafeUsbHandle.cs | 3 ++- src/Tizen.System.Usb/Usb/UsbConfiguration.cs | 4 ++-- src/Tizen.System.Usb/Usb/UsbDevice.cs | 7 ++++--- src/Tizen.System.Usb/Usb/UsbEndpoint.cs | 4 ++-- src/Tizen.System.Usb/Usb/UsbInterface.cs | 4 ++-- src/Tizen.System.Usb/Usb/UsbManager.cs | 10 +++++++--- 10 files changed, 31 insertions(+), 21 deletions(-) mode change 100644 => 100755 src/Tizen.System.Usb/Interop/Interop.Configuration.cs mode change 100644 => 100755 src/Tizen.System.Usb/Interop/Interop.Context.cs mode change 100644 => 100755 src/Tizen.System.Usb/Interop/Interop.Device.cs mode change 100644 => 100755 src/Tizen.System.Usb/Interop/Interop.Interface.cs mode change 100644 => 100755 src/Tizen.System.Usb/Interop/Interop.SafeUsbHandle.cs mode change 100644 => 100755 src/Tizen.System.Usb/Usb/UsbConfiguration.cs mode change 100644 => 100755 src/Tizen.System.Usb/Usb/UsbDevice.cs mode change 100644 => 100755 src/Tizen.System.Usb/Usb/UsbEndpoint.cs mode change 100644 => 100755 src/Tizen.System.Usb/Usb/UsbManager.cs diff --git a/src/Tizen.System.Usb/Interop/Interop.Configuration.cs b/src/Tizen.System.Usb/Interop/Interop.Configuration.cs old mode 100644 new mode 100755 index 6f2d34c..99dc52f --- a/src/Tizen.System.Usb/Interop/Interop.Configuration.cs +++ b/src/Tizen.System.Usb/Interop/Interop.Configuration.cs @@ -38,7 +38,7 @@ internal static partial class Interop 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 static extern ErrorCode GetInterface(this UsbConfigHandle /* usb_host_config_h */ config, int interfaceIndex, out IntPtr /* usb_host_interface_h */ usbInterface); internal class UsbConfigHandle : SafeUsbHandle { diff --git a/src/Tizen.System.Usb/Interop/Interop.Context.cs b/src/Tizen.System.Usb/Interop/Interop.Context.cs old mode 100644 new mode 100755 index 4750414..6caaa4f --- a/src/Tizen.System.Usb/Interop/Interop.Context.cs +++ b/src/Tizen.System.Usb/Interop/Interop.Context.cs @@ -32,7 +32,7 @@ internal static partial class Interop internal delegate void HostHotplugCallback(IntPtr /* usb_host_device_h */ dev, IntPtr /* void */ userData); [DllImport(Libraries.Usb, EntryPoint = "usb_host_set_hotplug_cb")] - internal static extern ErrorCode SetHotplugCb(this UsbContextHandle /* usb_host_context_h */ ctx, HostHotplugCallback cb, HotplugEventType /* usb_host_hotplug_event_e */ hostEvent, IntPtr /* void */ userData, out HostHotplugHandle /* usb_host_hotplug_h */ handle); + internal static extern ErrorCode SetHotplugCb(this UsbContextHandle /* usb_host_context_h */ ctx, HostHotplugCallback cb, HotplugEventType /* usb_host_hotplug_event_e */ hostEvent, IntPtr /* void */ userData, out IntPtr /* usb_host_hotplug_h */ handle); [DllImport(Libraries.Usb, EntryPoint = "usb_host_get_device_list")] internal static extern ErrorCode GetDeviceList(this UsbContextHandle /* usb_host_context_h */ ctx, out IntPtr /* usb_host_device_h */ devs, out int length); @@ -53,9 +53,13 @@ internal static partial class Interop [DllImport(Libraries.Usb, EntryPoint = "usb_host_free_device_list")] internal static extern ErrorCode FreeDeviceList(IntPtr deviceList, bool unrefDevices); - internal UsbContextHandle() + internal UsbContextHandle(IntPtr handle) : base(handle) { } + + internal static UsbContextHandle GetContextHandle() { - Create(out handle).ThrowIfFailed("Failed to create native context handle"); + IntPtr nativeHandle; + Create(out nativeHandle).ThrowIfFailed("Failed to create native context handle"); + return new UsbContextHandle(nativeHandle); } public override void Destroy() diff --git a/src/Tizen.System.Usb/Interop/Interop.Device.cs b/src/Tizen.System.Usb/Interop/Interop.Device.cs old mode 100644 new mode 100755 index 613c1e7..b5a387d --- a/src/Tizen.System.Usb/Interop/Interop.Device.cs +++ b/src/Tizen.System.Usb/Interop/Interop.Device.cs @@ -40,13 +40,13 @@ internal static partial class Interop internal static extern ErrorCode GetAddress(this HostDeviceHandle /* usb_host_device_h */ dev, out int deviceAddress); [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_port_numbers")] - internal static extern ErrorCode GetPortNumbers(this HostDeviceHandle /* usb_host_device_h */ dev, [MarshalAs(UnmanagedType.SysInt, SizeParamIndex = 2)] [In, Out] int[] portNumbers, int portNumbersLen, out int portsCount); + internal static extern ErrorCode GetPortNumbers(this HostDeviceHandle /* usb_host_device_h */ dev, [In, Out] int[] portNumbers, int portNumbersLen, out int portsCount); [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_config")] - internal static extern ErrorCode GetConfig(this HostDeviceHandle /* usb_host_device_h */ dev, int configIndex, out UsbConfigHandle /* usb_host_config_h */ config); + internal static extern ErrorCode GetConfig(this HostDeviceHandle /* usb_host_device_h */ dev, int configIndex, out IntPtr /* usb_host_config_h */ config); [DllImport(Libraries.Usb, EntryPoint = "usb_host_get_active_config")] - internal static extern ErrorCode GetActiveConfig(this HostDeviceHandle /* usb_host_device_h */ dev, out UsbConfigHandle /* usb_host_config_h */ config); + internal static extern ErrorCode GetActiveConfig(this HostDeviceHandle /* usb_host_device_h */ dev, out IntPtr /* usb_host_config_h */ config); [DllImport(Libraries.Usb, EntryPoint = "usb_host_device_get_bcd_usb")] internal static extern ErrorCode GetBcdUsb(this HostDeviceHandle /* usb_host_device_h */ dev, out int bcdUsb); diff --git a/src/Tizen.System.Usb/Interop/Interop.Interface.cs b/src/Tizen.System.Usb/Interop/Interop.Interface.cs old mode 100644 new mode 100755 index 22727dd..ed1cb61 --- a/src/Tizen.System.Usb/Interop/Interop.Interface.cs +++ b/src/Tizen.System.Usb/Interop/Interop.Interface.cs @@ -33,7 +33,7 @@ internal static partial class Interop internal static extern ErrorCode GetNumEndpoints(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, out int numEndpoints); [DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_get_endpoint")] - internal static extern ErrorCode GetEndpoint(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int epIndex, out UsbEndpointHandle /* usb_host_endpoint_h */ ep); + internal static extern ErrorCode GetEndpoint(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int epIndex, out IntPtr /* usb_host_endpoint_h */ ep); [DllImport(Libraries.Usb, EntryPoint = "usb_host_interface_set_altsetting")] internal static extern ErrorCode SetAltsetting(this UsbInterfaceHandle /* usb_host_interface_h */ usbInterface, int altsetting); diff --git a/src/Tizen.System.Usb/Interop/Interop.SafeUsbHandle.cs b/src/Tizen.System.Usb/Interop/Interop.SafeUsbHandle.cs old mode 100644 new mode 100755 index 5c5b5e7..086b28e --- a/src/Tizen.System.Usb/Interop/Interop.SafeUsbHandle.cs +++ b/src/Tizen.System.Usb/Interop/Interop.SafeUsbHandle.cs @@ -55,7 +55,8 @@ internal static partial class Interop internal abstract class SafeUsbHandle : SafeHandle { public abstract void Destroy(); - public SafeUsbHandle() : base(IntPtr.Zero, true) { } + + private SafeUsbHandle() : base(IntPtr.Zero, true) { } public SafeUsbHandle(IntPtr handle) : base(handle, true) { } public override bool IsInvalid { get { return handle == IntPtr.Zero; } } protected override bool ReleaseHandle() diff --git a/src/Tizen.System.Usb/Usb/UsbConfiguration.cs b/src/Tizen.System.Usb/Usb/UsbConfiguration.cs old mode 100644 new mode 100755 index 9ef8c98..7fe8304 --- a/src/Tizen.System.Usb/Usb/UsbConfiguration.cs +++ b/src/Tizen.System.Usb/Usb/UsbConfiguration.cs @@ -85,9 +85,9 @@ namespace Tizen.System.Usb int count = Interop.NativeGet(_handle.GetNumInterfaces); for (int i = 0; i < count; ++i) { - Interop.UsbInterfaceHandle handle; + IntPtr handle; _handle.GetInterface(i, out handle); - UsbInterface usbInterface = new UsbInterface(this, handle); + UsbInterface usbInterface = new UsbInterface(this, new Interop.UsbInterfaceHandle(handle)); _interfaces.Add(usbInterface.Id, usbInterface); } } diff --git a/src/Tizen.System.Usb/Usb/UsbDevice.cs b/src/Tizen.System.Usb/Usb/UsbDevice.cs old mode 100644 new mode 100755 index faf882a..df6e9d7 --- a/src/Tizen.System.Usb/Usb/UsbDevice.cs +++ b/src/Tizen.System.Usb/Usb/UsbDevice.cs @@ -37,9 +37,9 @@ namespace Tizen.System.Usb int count = Interop.NativeGet(_handle.GetNumConfigurations); for (int i = 0; i < count; ++i) { - Interop.UsbConfigHandle configHandle; + IntPtr configHandle; _handle.GetConfig(i, out configHandle); - _configurations.Add(i, new UsbConfiguration(this, configHandle)); + _configurations.Add(i, new UsbConfiguration(this, new Interop.UsbConfigHandle(configHandle))); } } @@ -112,7 +112,8 @@ namespace Tizen.System.Usb get { ThrowIfDisposed(); - Interop.UsbConfigHandle configHandle = Interop.NativeGet(_handle.GetActiveConfig); + IntPtr handle = Interop.NativeGet(_handle.GetActiveConfig); + Interop.UsbConfigHandle configHandle = new Interop.UsbConfigHandle(handle); return _configurations.Values.Where(config => config._handle == configHandle).First(); } } diff --git a/src/Tizen.System.Usb/Usb/UsbEndpoint.cs b/src/Tizen.System.Usb/Usb/UsbEndpoint.cs old mode 100644 new mode 100755 index 022fafd..8eaba21 --- a/src/Tizen.System.Usb/Usb/UsbEndpoint.cs +++ b/src/Tizen.System.Usb/Usb/UsbEndpoint.cs @@ -24,7 +24,7 @@ namespace Tizen.System.Usb public class UsbEndpoint { internal readonly Interop.UsbEndpointHandle _handle; - protected readonly UsbInterface _parent; + internal readonly UsbInterface _parent; internal UsbEndpoint(UsbInterface parent, Interop.UsbEndpointHandle handle) { @@ -74,7 +74,7 @@ namespace Tizen.System.Usb _parent?.ThrowIfDisposed(); } - protected int TransferImpl(byte[] buffer, int length, uint timeout) + internal int TransferImpl(byte[] buffer, int length, uint timeout) { ThrowIfDisposed(); int transferred; diff --git a/src/Tizen.System.Usb/Usb/UsbInterface.cs b/src/Tizen.System.Usb/Usb/UsbInterface.cs index 80e77e2..f0f327c 100755 --- a/src/Tizen.System.Usb/Usb/UsbInterface.cs +++ b/src/Tizen.System.Usb/Usb/UsbInterface.cs @@ -73,9 +73,9 @@ namespace Tizen.System.Usb int count = Interop.NativeGet(_handle.GetNumEndpoints); for (int i = 0; i < count; ++i) { - Interop.UsbEndpointHandle handle; + IntPtr handle; _handle.GetEndpoint(i, out handle); - UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, handle); + UsbEndpoint endpoint = UsbEndpoint.EndpointFactory(this, new Interop.UsbEndpointHandle(handle)); _endpoints.Add(endpoint.Id, endpoint); } } diff --git a/src/Tizen.System.Usb/Usb/UsbManager.cs b/src/Tizen.System.Usb/Usb/UsbManager.cs old mode 100644 new mode 100755 index f539174..cdaaaa1 --- a/src/Tizen.System.Usb/Usb/UsbManager.cs +++ b/src/Tizen.System.Usb/Usb/UsbManager.cs @@ -34,10 +34,14 @@ namespace Tizen.System.Usb /// public UsbManager() { - _context = new Interop.UsbContextHandle(); + _context = Interop.UsbContextHandle.GetContextHandle(); _devices = _context.GetDeviceList().Select(devHandle => new UsbDevice(this, devHandle)).ToList(); + + IntPtr hotpluggedHandle; _context.SetHotplugCb(HostHotplugCallback, Interop.HotplugEventType.Any, - IntPtr.Zero, out _hotpluggedHandle).ThrowIfFailed("Failed to set hot plugged callback"); + IntPtr.Zero, out hotpluggedHandle).ThrowIfFailed("Failed to set hot plugged callback"); + + _hotpluggedHandle = new Interop.HostHotplugHandle(hotpluggedHandle); } /// @@ -94,7 +98,7 @@ namespace Tizen.System.Usb #region IDisposable Support private bool disposedValue = false; - protected virtual void Dispose(bool disposing) + internal virtual void Dispose(bool disposing) { if (!disposedValue) { -- 2.7.4