From f92e53b789cfa39ba64fa2bf33f72c262ac78894 Mon Sep 17 00:00:00 2001 From: xerrni Date: Fri, 16 Apr 2021 00:52:41 +0200 Subject: [PATCH] [Peripheral] Fix Dispose pattern in Tizen.Peripheral (#2737) * [Peripheral] Fix Dispose pattern in Tizen.Peripheral Signed-off-by: Ernest Borowski * [Peripheral] check if parameter is not null. Signed-off-by: Ernest Borowski --- .../src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs | 10 +++---- .../src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs | 33 +++++++++++----------- .../src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs | 13 +++++---- .../src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs | 9 +++--- .../src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs | 17 +++++++---- .../src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs | 13 +++++---- 6 files changed, 52 insertions(+), 43 deletions(-) diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs index b40f3ab..918198c 100644 --- a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs @@ -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 /// /// Native handle to ADC. /// - private IntPtr _handle; + private IntPtr _handle = IntPtr.Zero; private bool _disposed = false; /// @@ -38,11 +38,9 @@ namespace Tizen.Peripheral.Adc /// The ADC channel number to control. 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; } /// @@ -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; } } diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs index 7243d48..32b33ec 100644 --- a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs @@ -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 /// /// Native handle to Gpio. /// - private IntPtr _handle; + private IntPtr _handle = IntPtr.Zero; private bool _disposed = false; /// @@ -111,39 +111,41 @@ namespace Tizen.Peripheral.Gpio /// GPIO direction. 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; } } diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs index b408f87..8a18343 100644 --- a/internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs @@ -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 /// /// Native handle to I2c. /// - private IntPtr _handle; + private IntPtr _handle = IntPtr.Zero; private bool _disposed = false; /// @@ -38,11 +38,9 @@ namespace Tizen.Peripheral.I2c /// The address of the slave device. 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; } /// @@ -78,6 +76,7 @@ namespace Tizen.Peripheral.I2c } NativeI2c.Close(_handle); + _handle = IntPtr.Zero; _disposed = true; } @@ -87,6 +86,8 @@ namespace Tizen.Peripheral.I2c /// The output byte array. 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 /// 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) diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs index 6d6c6fc..d340a49 100644 --- a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs @@ -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 /// /// Native handle to PWM. /// - private IntPtr _handle; + private IntPtr _handle = IntPtr.Zero; /// /// Opens the PWM pin. @@ -61,11 +61,9 @@ namespace Tizen.Peripheral.Pwm /// The PWM pin (channel) number to control. 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; } /// @@ -101,6 +99,7 @@ namespace Tizen.Peripheral.Pwm } NativePwm.Close(_handle); + _handle = IntPtr.Zero; _disposed = true; } diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs index c6e47e7..42b8cde 100644 --- a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs @@ -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 /// /// Native handle to Spi. /// - private IntPtr _handle; + private IntPtr _handle = IntPtr.Zero; private bool _disposed = false; /// @@ -90,11 +90,9 @@ namespace Tizen.Peripheral.Spi /// The SPI chip select number. 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; } /// @@ -130,6 +128,7 @@ namespace Tizen.Peripheral.Spi } NativeSpi.Close(_handle); + _handle = IntPtr.Zero; _disposed = true; } @@ -139,6 +138,8 @@ namespace Tizen.Peripheral.Spi /// The Data buffer. 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 /// The data buffer to write. 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 /// Array containing data read from the dievice. 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"); diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs index 6b802d6..1249938 100644 --- a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs @@ -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 /// /// Native handle to I2c. /// - private IntPtr _handle; + private IntPtr _handle = IntPtr.Zero; /// /// Opens the UART slave device. @@ -176,11 +176,9 @@ namespace Tizen.Peripheral.Uart /// The UART port number that the slave device is connected. 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; } /// @@ -216,6 +214,7 @@ namespace Tizen.Peripheral.Uart } NativeUart.Close(_handle); + _handle = IntPtr.Zero; _disposed = true; } @@ -228,6 +227,8 @@ namespace Tizen.Peripheral.Uart /// The number of bytes read. 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 /// The number of bytes to write 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]; -- 2.7.4