[Peripheral] Fix Dispose pattern in Tizen.Peripheral (#2737)
authorxerrni <e.borowski@samsung.com>
Thu, 15 Apr 2021 22:52:41 +0000 (00:52 +0200)
committerGitHub <noreply@github.com>
Thu, 15 Apr 2021 22:52:41 +0000 (07:52 +0900)
* [Peripheral] Fix Dispose pattern in Tizen.Peripheral

Signed-off-by: Ernest Borowski <e.borowski@samsung.com>
* [Peripheral] check if parameter is not null.

Signed-off-by: Ernest Borowski <e.borowski@samsung.com>
internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs
internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs
internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs
internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs
internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs
internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs

index b40f3ab..918198c 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2020 - 2021 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.
@@ -28,7 +28,7 @@ namespace Tizen.Peripheral.Adc
         /// <summary>
         /// Native handle to ADC.
         /// </summary>
-        private IntPtr _handle;
+        private IntPtr _handle = IntPtr.Zero;
         private bool _disposed = false;
 
         /// <summary>
@@ -38,11 +38,9 @@ namespace Tizen.Peripheral.Adc
         /// <param name="channel">The ADC channel number to control.</param>
         public AdcChannel(int device, int channel)
         {
-            var ret = NativeAdc.Open(device, channel, out IntPtr handle);
+            var ret = NativeAdc.Open(device, channel, out _handle);
             if (ret != Internals.Errors.ErrorCode.None)
                 throw ExceptionFactory.CreateException(ret);
-
-            _handle= handle;
         }
 
         /// <summary>
@@ -61,7 +59,6 @@ namespace Tizen.Peripheral.Adc
             var ret = NativeAdc.Read(_handle, out uint adcValue);
             if (ret != Internals.Errors.ErrorCode.None)
                 throw ExceptionFactory.CreateException(ret);
-
             return adcValue;
         }
 
@@ -90,6 +87,7 @@ namespace Tizen.Peripheral.Adc
             }
 
             NativeAdc.Close(_handle);
+            _handle = IntPtr.Zero;
             _disposed = true;
         }
     }
index 7243d48..32b33ec 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2020 - 2021 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.
@@ -96,7 +96,7 @@ namespace Tizen.Peripheral.Gpio
         /// <summary>
         /// Native handle to Gpio.
         /// </summary>
-        private IntPtr _handle;
+        private IntPtr _handle = IntPtr.Zero;
         private bool _disposed = false;
 
         /// <summary>
@@ -111,39 +111,41 @@ namespace Tizen.Peripheral.Gpio
         /// <param name="mode">GPIO direction.</param>
         public GpioPin(int pinNumber, GpioPinDriveMode mode)
         {
-            var ret = NativeGpio.Open(pinNumber, out IntPtr handle);
-            if (ret != ErrorCode.None)
-                throw ExceptionFactory.CreateException(ret);
-
-            _handle = handle;
-            PinNumber = pinNumber;
+            var ret = NativeGpio.Open(pinNumber, out _handle);
             try
             {
+                if (ret != ErrorCode.None)
+                    throw ExceptionFactory.CreateException(ret);
+
+                PinNumber = pinNumber;
                 switch (mode)
                 {
                     case GpioPinDriveMode.Input:
-                        ret = NativeGpio.SetEdgeMode(handle, NativeGpio.EdgeType.Both);
+                        ret = NativeGpio.SetEdgeMode(_handle, NativeGpio.EdgeType.Both);
                         if (ret != ErrorCode.None)
                             throw ExceptionFactory.CreateException(ret);
 
-                        ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.In);
+                        ret = NativeGpio.SetDirection(_handle, NativeGpio.Direction.In);
                         if (ret != ErrorCode.None)
                             throw ExceptionFactory.CreateException(ret);
                         SetIntteruptedCallback();
                         break;
                     case GpioPinDriveMode.OutputInitiallyLow:
-                        ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.OutLow);
+                        ret = NativeGpio.SetDirection(_handle, NativeGpio.Direction.OutLow);
                         if (ret != ErrorCode.None)
                             throw ExceptionFactory.CreateException(ret);
                         break;
                     case GpioPinDriveMode.OutputInitiallyHigh:
-                        ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.OutHigh);
+                        ret = NativeGpio.SetDirection(_handle, NativeGpio.Direction.OutHigh);
                         if (ret != ErrorCode.None)
                             throw ExceptionFactory.CreateException(ret);
                         break;
                 }
-            } finally {
+            }
+            catch (Exception err)
+            {
                 Dispose();
+                throw;
             }
         }
 
@@ -161,9 +163,7 @@ namespace Tizen.Peripheral.Gpio
             _interruptedEventCallback = OnInterrupted;
             var ret = NativeGpio.SetInterruptedCb(_handle, _interruptedEventCallback, IntPtr.Zero);
             if (ret != ErrorCode.None)
-            {
                 throw ExceptionFactory.CreateException(ret);
-            }
         }
 
         private void OnInterrupted(IntPtr handle, ErrorCode error, IntPtr data)
@@ -194,9 +194,9 @@ namespace Tizen.Peripheral.Gpio
             {
                 return;
             }
-
             NativeGpio.UnsetInterruptedCb(_handle);
             NativeGpio.Close(_handle);
+            _handle = IntPtr.Zero;
             _disposed = true;
         }
 
@@ -240,7 +240,6 @@ namespace Tizen.Peripheral.Gpio
                 var ret = NativeGpio.SetEdgeMode(_handle, (NativeGpio.EdgeType)value);
                 if (ret != ErrorCode.None)
                     throw ExceptionFactory.CreateException(ret);
-
                 _polarityType = value;
             }
         }
index b408f87..8a18343 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2020 - 2021 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.
@@ -28,7 +28,7 @@ namespace Tizen.Peripheral.I2c
         /// <summary>
         /// Native handle to I2c.
         /// </summary>
-        private IntPtr _handle;
+        private IntPtr _handle = IntPtr.Zero;
         private bool _disposed = false;
 
         /// <summary>
@@ -38,11 +38,9 @@ namespace Tizen.Peripheral.I2c
         /// <param name="address">The address of the slave device.</param>
         public I2cDevice(int bus, int address)
         {
-            var ret = NativeI2c.Open(bus, address, out IntPtr handle);
+            var ret = NativeI2c.Open(bus, address, out _handle);
             if (ret != Internals.Errors.ErrorCode.None)
                 throw ExceptionFactory.CreateException(ret);
-
-            _handle = handle;
         }
 
         /// <summary>
@@ -78,6 +76,7 @@ namespace Tizen.Peripheral.I2c
             }
 
             NativeI2c.Close(_handle);
+            _handle = IntPtr.Zero;
             _disposed = true;
         }
 
@@ -87,6 +86,8 @@ namespace Tizen.Peripheral.I2c
         /// <param name="dataOut">The output byte array.</param>
         public void Read(byte[] dataOut)
         {
+            if (dataOut == null)
+                throw new ArgumentNullException(nameof(dataOut));
             var length = Convert.ToUInt32(dataOut.Length);
             var ret = NativeI2c.Read(_handle, dataOut, length);
             if (ret != Internals.Errors.ErrorCode.None)
@@ -99,6 +100,8 @@ namespace Tizen.Peripheral.I2c
         /// <param name="data"></param>
         public void Write(byte[] data)
         {
+            if (data == null)
+                throw new ArgumentNullException(nameof(data));
             var length = Convert.ToUInt32(data.Length);
             var ret = NativeI2c.Write(_handle, data, length);
             if (ret != Internals.Errors.ErrorCode.None)
index 6d6c6fc..d340a49 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2020 - 2021 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.
@@ -52,7 +52,7 @@ namespace Tizen.Peripheral.Pwm
         /// <summary>
         /// Native handle to PWM.
         /// </summary>
-        private IntPtr _handle;
+        private IntPtr _handle = IntPtr.Zero;
 
         /// <summary>
         /// Opens the PWM pin.
@@ -61,11 +61,9 @@ namespace Tizen.Peripheral.Pwm
         /// <param name="pin">The PWM pin (channel) number to control.</param>
         public PwmPin(int chip, int pin)
         {
-            var ret = NativePwm.Open(chip, pin, out IntPtr handle);
+            var ret = NativePwm.Open(chip, pin, out _handle);
             if (ret != Internals.Errors.ErrorCode.None)
                 throw ExceptionFactory.CreateException(ret);
-
-            _handle = handle;
         }
 
         /// <summary>
@@ -101,6 +99,7 @@ namespace Tizen.Peripheral.Pwm
             }
 
             NativePwm.Close(_handle);
+            _handle = IntPtr.Zero;
             _disposed = true;
         }
 
index c6e47e7..42b8cde 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2020 - 2021 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.
@@ -80,7 +80,7 @@ namespace Tizen.Peripheral.Spi
         /// <summary>
         /// Native handle to Spi.
         /// </summary>
-        private IntPtr _handle;
+        private IntPtr _handle = IntPtr.Zero;
         private bool _disposed = false;
 
         /// <summary>
@@ -90,11 +90,9 @@ namespace Tizen.Peripheral.Spi
         /// <param name="chip">The SPI chip select number.</param>
         public SpiDevice(int bus, int chip)
         {
-            var ret = NativeSpi.Open(bus, chip, out IntPtr handle);
+            var ret = NativeSpi.Open(bus, chip, out _handle);
             if (ret != Internals.Errors.ErrorCode.None)
                 throw ExceptionFactory.CreateException(ret);
-
-            _handle = handle;
         }
 
         /// <summary>
@@ -130,6 +128,7 @@ namespace Tizen.Peripheral.Spi
             }
 
             NativeSpi.Close(_handle);
+            _handle = IntPtr.Zero;
             _disposed = true;
         }
 
@@ -139,6 +138,8 @@ namespace Tizen.Peripheral.Spi
         /// <param name="buffer">The Data buffer.</param>
         public void Read(byte[] buffer)
         {
+            if (buffer == null)
+                throw new ArgumentNullException(nameof(buffer));
             var length = Convert.ToUInt32(buffer.Length);
             var ret = NativeSpi.Read(_handle, buffer, length);
             if (ret != Internals.Errors.ErrorCode.None)
@@ -151,6 +152,8 @@ namespace Tizen.Peripheral.Spi
         /// <param name="data">The data buffer to write.</param>
         public void Write(byte[] data)
         {
+            if (data == null)
+                throw new ArgumentNullException(nameof(data));
             var length = Convert.ToUInt32(data.Length);
             var ret = NativeSpi.Write(_handle, data, length);
             if (ret != Internals.Errors.ErrorCode.None)
@@ -165,6 +168,10 @@ namespace Tizen.Peripheral.Spi
         /// <param name="readBuffer">Array containing data read from the dievice.</param>
         public void TransferSequential(byte[] writeBuffer, byte[] readBuffer)
         {
+            if (readBuffer == null)
+                throw new ArgumentNullException(nameof(readBuffer));
+            if (writeBuffer == null)
+                throw new ArgumentNullException(nameof(writeBuffer));
             if (writeBuffer.Length != readBuffer.Length)
                 throw new Exception("writeBuffer.Length is not equal to readBuffer.Length");
 
index 6b802d6..1249938 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2020 - 2021 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.
@@ -168,7 +168,7 @@ namespace Tizen.Peripheral.Uart
         /// <summary>
         /// Native handle to I2c.
         /// </summary>
-        private IntPtr _handle;
+        private IntPtr _handle = IntPtr.Zero;
 
         /// <summary>
         /// Opens the UART slave device.
@@ -176,11 +176,9 @@ namespace Tizen.Peripheral.Uart
         /// <param name="port">The UART port number that the slave device is connected.</param>
         public SerialPort(int port)
         {
-            var ret = NativeUart.Open(port, out IntPtr handle);
+            var ret = NativeUart.Open(port, out _handle);
             if (ret != Internals.Errors.ErrorCode.None)
                 throw ExceptionFactory.CreateException(ret);
-
-            _handle = handle;
         }
 
         /// <summary>
@@ -216,6 +214,7 @@ namespace Tizen.Peripheral.Uart
             }
 
             NativeUart.Close(_handle);
+            _handle = IntPtr.Zero;
             _disposed = true;
         }
 
@@ -228,6 +227,8 @@ namespace Tizen.Peripheral.Uart
         /// <returns>The number of bytes read.</returns>
         public int Read(byte[] buffer, int offset, int count)
         {
+            if (buffer == null)
+                throw new ArgumentNullException(nameof(buffer));
             if (count > buffer.Length - offset)
                 throw new Exception("Can not read more bytes than the buffer can hold.");
             byte[] tmpBuffer = new byte[count];
@@ -247,6 +248,8 @@ namespace Tizen.Peripheral.Uart
         /// <param name="count">The number of bytes to write</param>
         public int Write(byte[] buffer, int offset, int count)
         {
+            if (buffer == null)
+                throw new ArgumentNullException(nameof(buffer));
             if (count > buffer.Length - offset)
                 throw new Exception("Can not write more bytes than the buffer holds.");
             byte[] tmpBuffer = new byte[count];