From a7b96b506b330300a0444a1ee1ed9331481289b9 Mon Sep 17 00:00:00 2001 From: Sudha Bheemanna Date: Thu, 19 Jan 2017 10:46:47 +0530 Subject: [PATCH] [BT C#] Add checks in code to fix issues. 1> Added code to check data length before copying data to memory location. 2> Removed unwanted exception throw for optional data in the advertising and scan response data of Le packet. Change-Id: If456f3602fc72d8354f1026158991946c25b3028 Signed-off-by: Sudha Bheemanna --- .../BluetoothLeAdapterImpl.cs | 29 ++++++++-------- .../Tizen.Network.Bluetooth/BluetoothStructs.cs | 39 ++++++++++++++-------- 2 files changed, 39 insertions(+), 29 deletions(-) mode change 100644 => 100755 src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs mode change 100644 => 100755 src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothStructs.cs diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs old mode 100644 new mode 100755 index ae098cf..a7134af --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothLeAdapterImpl.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); @@ -164,7 +164,7 @@ namespace Tizen.Network.Bluetooth { _adapterLeScanResultChangedCallback = (int result, ref BluetoothLeScanDataStruct scanData, IntPtr userData) => { - Log.Info(Globals.LogTag, "Inside scan callback " ); + Log.Info(Globals.LogTag, "Inside Le scan callback " ); BluetoothLeScanData scanDataInfo = new BluetoothLeScanData(); scanDataInfo = BluetoothUtils.ConvertStructToLeScanData(scanData, scanDataInfo); @@ -210,21 +210,26 @@ namespace Tizen.Network.Bluetooth scanDataStruct.RemoteAddress = scanData.RemoteAddress; scanDataStruct.AddressType = scanData.AddressType; scanDataStruct.Rssi = scanData.Rssi; - scanDataStruct.AdvDataLength = scanData.AdvDataLength; - scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength); - Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength); + if (scanData.AdvDataLength > 0) + { + scanDataStruct.AdvDataLength = scanData.AdvDataLength; + scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength); + Marshal.Copy(scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength); + } - scanDataStruct.ScanDataLength = scanData.ScanDataLength; - scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength); - Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength); + if (scanData.ScanDataLength > 0) + { + scanDataStruct.ScanDataLength = scanData.ScanDataLength; + scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength); + Marshal.Copy(scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength); + } int ret = Interop.Bluetooth.GetScanResultServiceUuid(ref scanDataStruct, packetType, ref uuidListArray, ref count); if (ret != (int)BluetoothError.None) { Log.Info(Globals.LogTag, "Failed to service uuids list- " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException(ret); } Log.Info(Globals.LogTag, "count of uuids : " + count); @@ -252,7 +257,6 @@ namespace Tizen.Network.Bluetooth int ret = Interop.Bluetooth.GetLeScanResultDeviceName(ref scanDataStruct, packetType, out deviceName); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get Device name- " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException(ret); } Log.Info (Globals.LogTag, "Device name " + deviceName); return deviceName; @@ -267,7 +271,6 @@ namespace Tizen.Network.Bluetooth int ret = Interop.Bluetooth.GetScanResultTxPowerLevel(ref scanDataStruct, packetType, out powerLevel); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get tx power level- " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException(ret); } Log.Info (Globals.LogTag, "TxPowerLevel is --- " + powerLevel); return powerLevel; @@ -284,7 +287,6 @@ namespace Tizen.Network.Bluetooth int ret = Interop.Bluetooth.GetScaResultSvcSolicitationUuids(ref scanDataStruct, packetType, out uuidListArray, out count); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get service solicitation uuids " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException(ret); } IntPtr[] uuidList = new IntPtr[count]; @@ -312,7 +314,6 @@ namespace Tizen.Network.Bluetooth if(ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get Service Data List, Error - " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException(ret); } BluetoothLeServiceDataStruct[] serviceArray = new BluetoothLeServiceDataStruct[_serviceListCount]; @@ -369,7 +370,6 @@ namespace Tizen.Network.Bluetooth int ret = Interop.Bluetooth.GetScanResultAppearance(ref scanDataStruct, packetType, out appearance); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get Appearance value- " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException(ret); } return appearance; } @@ -390,7 +390,6 @@ namespace Tizen.Network.Bluetooth if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get Manufacturer data - " + (BluetoothError)ret); - BluetoothErrorFactory.ThrowBluetoothException(ret); } Marshal.Copy (manufData, data.Data, 0, data.DataLength); diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothStructs.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothStructs.cs old mode 100644 new mode 100755 index 05d0110..9a2e87a --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothStructs.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothStructs.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); @@ -274,14 +274,19 @@ namespace Tizen.Network.Bluetooth scanData.AddressType = structScanData.AddressType; scanData.Rssi = structScanData.Rssi; - scanData.AdvDataLength = structScanData.AdvDataLength; - scanData.AdvData = new byte[scanData.AdvDataLength]; - Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength); - - scanData.ScanDataLength = structScanData.ScanDataLength; - scanData.ScanData = new byte[scanData.ScanDataLength]; - Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength); + if (structScanData.AdvDataLength > 0) + { + scanData.AdvDataLength = structScanData.AdvDataLength; + scanData.AdvData = new byte[scanData.AdvDataLength]; + Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength); + } + if (structScanData.ScanDataLength > 0) + { + scanData.ScanDataLength = structScanData.ScanDataLength; + scanData.ScanData = new byte[scanData.ScanDataLength]; + Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength); + } return scanData; } @@ -291,13 +296,19 @@ namespace Tizen.Network.Bluetooth scanDataStruct.AddressType = scanData.AddressType; scanDataStruct.Rssi = scanData.Rssi; - scanDataStruct.AdvDataLength = scanData.AdvDataLength; - scanDataStruct.AdvData = Marshal.AllocHGlobal (scanData.AdvDataLength); - Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength); + if (scanData.AdvDataLength > 0) + { + scanDataStruct.AdvDataLength = scanData.AdvDataLength; + scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength); + Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength); + } - scanDataStruct.ScanDataLength = scanData.ScanDataLength; - scanDataStruct.ScanData = Marshal.AllocHGlobal (scanData.ScanDataLength); - Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength); + if (scanData.ScanDataLength > 0) + { + scanDataStruct.ScanDataLength = scanData.ScanDataLength; + scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength); + Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength); + } return scanDataStruct; } -- 2.7.4