[USB] Fixed interop code for proper marshalling 83/157683/1
authorDinesh Dwivedi <dinesh.d@samsung.com>
Wed, 25 Oct 2017 18:11:23 +0000 (23:41 +0530)
committerDinesh Dwivedi <dinesh.d@samsung.com>
Wed, 25 Oct 2017 18:11:23 +0000 (23:41 +0530)
Change-Id: I525ab548eff46a81b64e59cc8a5b39c856702a80
Signed-off-by: Dinesh Dwivedi <dinesh.d@samsung.com>
src/Tizen.System.Usb/Interop/Interop.Configuration.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Interop/Interop.Context.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Interop/Interop.Device.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Interop/Interop.Interface.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Interop/Interop.SafeUsbHandle.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Usb/UsbConfiguration.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Usb/UsbDevice.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Usb/UsbEndpoint.cs [changed mode: 0644->0755]
src/Tizen.System.Usb/Usb/UsbInterface.cs
src/Tizen.System.Usb/Usb/UsbManager.cs [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 6f2d34c..99dc52f
@@ -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
     {
old mode 100644 (file)
new mode 100755 (executable)
index 4750414..6caaa4f
@@ -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()
old mode 100644 (file)
new mode 100755 (executable)
index 613c1e7..b5a387d
@@ -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);
old mode 100644 (file)
new mode 100755 (executable)
index 22727dd..ed1cb61
@@ -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);
old mode 100644 (file)
new mode 100755 (executable)
index 5c5b5e7..086b28e
@@ -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()
old mode 100644 (file)
new mode 100755 (executable)
index 9ef8c98..7fe8304
@@ -85,9 +85,9 @@ namespace Tizen.System.Usb
                     int count = Interop.NativeGet<int>(_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);
                     }
                 }
old mode 100644 (file)
new mode 100755 (executable)
index faf882a..df6e9d7
@@ -37,9 +37,9 @@ namespace Tizen.System.Usb
             int count = Interop.NativeGet<int>(_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<Interop.UsbConfigHandle>(_handle.GetActiveConfig);
+                IntPtr handle = Interop.NativeGet<IntPtr>(_handle.GetActiveConfig);
+                Interop.UsbConfigHandle configHandle = new Interop.UsbConfigHandle(handle);
                 return _configurations.Values.Where(config => config._handle == configHandle).First();
             }
         }
old mode 100644 (file)
new mode 100755 (executable)
index 022fafd..8eaba21
@@ -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;
index 80e77e2..f0f327c 100755 (executable)
@@ -73,9 +73,9 @@ namespace Tizen.System.Usb
                     int count = Interop.NativeGet<int>(_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);
                     }
                 }
old mode 100644 (file)
new mode 100755 (executable)
index f539174..cdaaaa1
@@ -34,10 +34,14 @@ namespace Tizen.System.Usb
         /// </summary>
         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);
         }
 
         /// <summary>
@@ -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)
             {