[Peripheral] Fix Dispose pattern in Tizen.Peripheral (#2737)
[platform/core/csapi/tizenfx.git] / internals / src / Tizen.Peripheral / Tizen.Peripheral / Spi.cs
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");