[Bluetooth] Fix BluetoothDevice converting issue (#4176)
authorWootak Jung <wootak.jung@samsung.com>
Sun, 24 Apr 2022 23:33:50 +0000 (08:33 +0900)
committerGitHub <noreply@github.com>
Sun, 24 Apr 2022 23:33:50 +0000 (08:33 +0900)
* Fix unhandled exception in GetBondedDevices() (#2485)

I/DOTNET_LAUNCHER (P 2613, T 2617): Unhandled exception.
I/DOTNET_LAUNCHER (P 2613, T 2617): System.Runtime.InteropServices.COMException (0x8007007A):
The data area passed to a system call is too small.
I/DOTNET_LAUNCHER (P 2613, T 2617):  (0x8007007A)
I/DOTNET_LAUNCHER (P 2613, T 2617):    at System.StubHelpers.ValueClassMarshaler.ConvertToNative(IntPtr dst, IntPtr src,
IntPtr pMT, CleanupWorkListElement& pCleanupWorkList)
I/DOTNET_LAUNCHER (P 2613, T 2617):    at Tizen.Network.Bluetooth.BluetoothAdapterImpl.GetBondedDevices()
I/DOTNET_LAUNCHER (P 2613, T 2617):    at Tizen.Network.Bluetooth.BluetoothAdapter.GetBondedDevices()
I/DOTNET_LAUNCHER (P 2613, T 2617):    at Tizen.Network.Bluetooth.Tests.BluetoothAdapterTests.GetBondedDevices_RETURN_LIST_OF_DEVICES()
in /var/lib/jenkins/jobs/CsharpTCT/TCT_6.0/api/tct-suite-vs/Tizen.Bluetooth.Manual.Tests/testcase/TSBluetoothAdapter.cs:line 435
I/DOTNET_LAUNCHER (P 2613, T 2617): Fatal error.
I/DOTNET_LAUNCHER (P 2613, T 2617): Internal CLR error. (0x80131506)
I/DOTNET_LAUNCHER (P 2613, T 2617): DLOG_ERROR_NOMSG
I/DOTNET_LAUNCHER (P 2613, T 2617):    at Interop+Bluetooth.GetBondedDevices(BondedDeviceCallback, IntPtr)
I/DOTNET_LAUNCHER (P 2613, T 2617):    at Tizen.Network.Bluetooth.BluetoothAdapterImpl.GetBondedDevices()
I/DOTNET_LAUNCHER (P 2613, T 2617):    at Tizen.Network.Bluetooth.BluetoothAdapter.GetBondedDevices()

Change-Id: I04a0a89d75dc973a752950c9274b80d9988a11ed
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
Co-authored-by: Anupam Roy <anupam.r@samsung.com>
* Fix BluetoothDevice.Name converting issue (#2498)

Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
* Fix unhandled exception in GetBondedDevice() (#2514)

Device is not bonded : System.NullReferenceException: Object reference not set to an instance of an object.
    at System.SpanHelpers.IndexOf(Byte& searchSpace, Byte value, Int32 length)
    at System.String.Ctor(SByte* value)
    at System.Runtime.InteropServices.Marshal.PtrToStringAnsi(IntPtr ptr)
    at Tizen.Network.Bluetooth.BluetoothUtils.ConvertStructToDeviceClass(BluetoothDeviceStruct device)
    at Tizen.Network.Bluetooth.BluetoothAdapterImpl.GetBondedDevice(String address)
    at Tizen.Network.Bluetooth.BluetoothAdapter.GetBondedDevice(String address)

Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
* GetBondedDevice converting issue (#2517)

Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
Co-authored-by: dh79pyun <31202060+dh79pyun@users.noreply.github.com>
* Fix BluetoothDevice data converting issue (#2887)

Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
* Add device name null check logic (#3084)

Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
* Add log for converting devcie info (#3303)

Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
* Fix DeviceName converting issue (#3351)

Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
* Fix the crash in GetBondedDevices API (#1643)

The manufacture data can be filled without NULL termination.
Because it is the byte array type. So we should define it as
IntPrt instead of string to avoid the buffer overflow.

Exception !!!: System.Runtime.InteropServices.COMException (0x8007007A): The data area passed to a system call is too small.
at System.StubHelpers.ValueClassMarshaler.ConvertToNative(IntPtr dst, IntPtr src, IntPtr pMT, CleanupWorkListElement& pCleanupWorkList)
at Tizen.Network.Bluetooth.BluetoothAdapterImpl.GetBondedDevices()
at Tizen.Network.Bluetooth.BluetoothAdapter.GetBondedDevices()

Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
* Add NULL check logic for Marshal.PtrToStringAnsi (#1657)

This patchset is to avoid ArgumentNullException error. There are some
cases to come NULL variable from Native API.

BluetoothAdapterImpl.cs: RegisterDiscoveryStateChangedEvent(258) > Discovery state changed callback is called
Unhandled exception.
System.ArgumentNullException: Value cannot be null. (Parameter 'ptr')
at System.Runtime.InteropServices.Marshal.PtrToStringAnsi(IntPtr ptr, Int32 len)
at Tizen.Network.Bluetooth.BluetoothUtils.ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
at Tizen.Network.Bluetooth.BluetoothAdapterImpl.<RegisterDiscoveryStateChangedEvent>b__45_0(Int32 result, BluetoothDeviceDiscoveryState state, IntPtr deviceInfo, IntPtr user

Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
Co-authored-by: dh79pyun <31202060+dh79pyun@users.noreply.github.com>
Co-authored-by: Anupam Roy <anupam.r@samsung.com>
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs
src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothStructs.cs

index c5aaee36a48c957a7b35cdfbe99d3fb39c35913a..5c3e270827a2a660b09ea43656795abd9ef4c325 100644 (file)
@@ -491,6 +491,7 @@ namespace Tizen.Network.Bluetooth
         internal BluetoothDevice GetBondedDevice(string address)
         {
             IntPtr deviceInfo;
+            BluetoothDevice btDevice;
             int ret = Interop.Bluetooth.GetBondedDeviceByAddress(address, out deviceInfo);
             if(ret != (int)BluetoothError.None)
             {
@@ -498,9 +499,9 @@ namespace Tizen.Network.Bluetooth
                 BluetoothErrorFactory.ThrowBluetoothException(ret);
             }
             BluetoothDeviceStruct device = (BluetoothDeviceStruct)Marshal.PtrToStructure(deviceInfo, typeof(BluetoothDeviceStruct));
-
+            btDevice = BluetoothUtils.ConvertStructToDeviceClass(device);
             Interop.Bluetooth.FreeDeviceInfo(deviceInfo);
-            return BluetoothUtils.ConvertStructToDeviceClass(device);
+            return btDevice;
         }
 
         internal bool IsServiceUsed(string serviceUuid)
index 454d675dd73ae0157d8cd4d11daa718a9ccc017c..d287c77b6f1cedd655b4ccee38488a3328cea5b4 100644 (file)
@@ -59,8 +59,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The name of the device.
         /// </summary>
-        [MarshalAsAttribute(UnmanagedType.LPStr)]
-        internal string Name;
+        internal IntPtr Name;
 
         /// <summary>
         /// The class of the device.
@@ -103,8 +102,7 @@ namespace Tizen.Network.Bluetooth
         /// <summary>
         /// The manufacturer data.
         /// </summary>
-        [MarshalAsAttribute(UnmanagedType.LPStr)]
-        internal string ManufacturerData;
+        internal IntPtr ManufacturerData;
     }
 
     [StructLayout(LayoutKind.Sequential)]
@@ -113,8 +111,7 @@ namespace Tizen.Network.Bluetooth
         [MarshalAsAttribute(UnmanagedType.LPStr)]
         internal string Address;
 
-        [MarshalAsAttribute(UnmanagedType.LPStr)]
-        internal string Name;
+        internal IntPtr Name;
 
         internal BluetoothClassStruct Class;
 
@@ -131,8 +128,7 @@ namespace Tizen.Network.Bluetooth
 
         internal int ManufacturerDataLength;
 
-        [MarshalAsAttribute(UnmanagedType.LPStr)]
-        internal string ManufacturerData;
+        internal IntPtr ManufacturerData;
     }
 
     [StructLayout(LayoutKind.Sequential)]
@@ -228,14 +224,21 @@ namespace Tizen.Network.Bluetooth
                 IntPtr[] extensionList = new IntPtr[device.ServiceCount];
                 Marshal.Copy (device.ServiceUuidList, extensionList, 0, device.ServiceCount);
                 uuidList = new Collection<string> ();
+                Log.Info(Globals.LogTag, "UUID Count: " + device.ServiceCount);
                 foreach (IntPtr extension in extensionList) {
-                    string uuid = Marshal.PtrToStringAnsi (extension);
-                    uuidList.Add (uuid);
+                    if (extension != IntPtr.Zero) {
+                        string uuid = Marshal.PtrToStringAnsi (extension);
+                        Log.Info(Globals.LogTag, "UUID: " + uuid);
+                        uuidList.Add (uuid);
+                    }
                 }
             }
 
             resultDevice.RemoteDeviceAddress = device.Address;
-            resultDevice.RemoteDeviceName = device.Name;
+            if (device.Name != IntPtr.Zero) {
+                resultDevice.RemoteDeviceName = Marshal.PtrToStringAnsi(device.Name);
+                Log.Info(Globals.LogTag, "Device Name: " + resultDevice.RemoteDeviceName);
+            }
             resultDevice.RemoteDeviceClass = new BluetoothClass();
             resultDevice.Class.MajorType = device.Class.MajorDeviceClassType;
             resultDevice.Class.MinorType = device.Class.MinorDeviceClassType;
@@ -246,7 +249,9 @@ namespace Tizen.Network.Bluetooth
             resultDevice.RemoteDeviceService = uuidList;
             resultDevice.RemoteDeviceCount = device.ServiceCount;
             resultDevice.RemoteManufLength = device.ManufacturerDataLength;
-            resultDevice.RemoteManufData = device.ManufacturerData;
+
+            if (device.ManufacturerData != IntPtr.Zero)
+                resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(device.ManufacturerData, device.ManufacturerDataLength);
 
             return resultDevice;
         }
@@ -260,14 +265,21 @@ namespace Tizen.Network.Bluetooth
                 IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
                 Marshal.Copy (structDevice.ServiceUuidList, extensionList, 0, structDevice.ServiceCount);
                 uuidList = new Collection<string> ();
+                Log.Info(Globals.LogTag, "UUID Count: " + structDevice.ServiceCount);
                 foreach (IntPtr extension in extensionList) {
-                    string uuid = Marshal.PtrToStringAnsi (extension);
-                    uuidList.Add (uuid);
+                    if (extension != IntPtr.Zero) {
+                        string uuid = Marshal.PtrToStringAnsi(extension);
+                        Log.Info(Globals.LogTag, "UUID: " + uuid);
+                        uuidList.Add(uuid);
+                    }
                 }
             }
 
             resultDevice.RemoteDeviceAddress = structDevice.Address;
-            resultDevice.RemoteDeviceName = structDevice.Name;
+            if (structDevice.Name != IntPtr.Zero) {
+                resultDevice.RemoteDeviceName = Marshal.PtrToStringAnsi(structDevice.Name);
+                Log.Info(Globals.LogTag, "Device Name: " + resultDevice.RemoteDeviceName);
+            }
 
             resultDevice.RemoteDeviceClass = new BluetoothClass();
             resultDevice.Class.MajorType = structDevice.Class.MajorDeviceClassType;
@@ -283,8 +295,11 @@ namespace Tizen.Network.Bluetooth
             }
 
             resultDevice.RemotePaired = structDevice.IsPaired;
-            resultDevice.RemoteManufData = structDevice.ManufacturerData;
             resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
+
+            if (structDevice.ManufacturerData != IntPtr.Zero)
+                resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(structDevice.ManufacturerData, structDevice.ManufacturerDataLength);
+
             return resultDevice;
         }
 
@@ -297,8 +312,10 @@ namespace Tizen.Network.Bluetooth
                 IntPtr[] extensionList = new IntPtr[structData.ServiceCount];
                 Marshal.Copy (structData.ServiceUuid, extensionList, 0, structData.ServiceCount);
                 uuidList = new Collection<string> ();
+                Log.Info(Globals.LogTag, "UUID Count: " + structData.ServiceCount);
                 foreach (IntPtr extension in extensionList) {
                     string uuid = Marshal.PtrToStringAnsi (extension);
+                    Log.Info(Globals.LogTag, "UUID: " + uuid);
                     uuidList.Add (uuid);
                 }
             }