/*
-* 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.
/// <summary>
/// Native handle to ADC.
/// </summary>
- private IntPtr _handle;
+ private IntPtr _handle = IntPtr.Zero;
private bool _disposed = false;
/// <summary>
/// <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>
var ret = NativeAdc.Read(_handle, out uint adcValue);
if (ret != Internals.Errors.ErrorCode.None)
throw ExceptionFactory.CreateException(ret);
-
return adcValue;
}
}
NativeAdc.Close(_handle);
+ _handle = IntPtr.Zero;
_disposed = true;
}
}
/*
-* 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.
/// <summary>
/// Native handle to Gpio.
/// </summary>
- private IntPtr _handle;
+ private IntPtr _handle = IntPtr.Zero;
private bool _disposed = false;
/// <summary>
/// <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;
}
}
_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)
{
return;
}
-
NativeGpio.UnsetInterruptedCb(_handle);
NativeGpio.Close(_handle);
+ _handle = IntPtr.Zero;
_disposed = true;
}
var ret = NativeGpio.SetEdgeMode(_handle, (NativeGpio.EdgeType)value);
if (ret != ErrorCode.None)
throw ExceptionFactory.CreateException(ret);
-
_polarityType = value;
}
}
/*
-* 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.
/// <summary>
/// Native handle to I2c.
/// </summary>
- private IntPtr _handle;
+ private IntPtr _handle = IntPtr.Zero;
private bool _disposed = false;
/// <summary>
/// <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>
}
NativeI2c.Close(_handle);
+ _handle = IntPtr.Zero;
_disposed = true;
}
/// <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)
/// <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)
/*
-* 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.
/// <summary>
/// Native handle to PWM.
/// </summary>
- private IntPtr _handle;
+ private IntPtr _handle = IntPtr.Zero;
/// <summary>
/// Opens the PWM pin.
/// <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>
}
NativePwm.Close(_handle);
+ _handle = IntPtr.Zero;
_disposed = true;
}
/*
-* 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.
/// <summary>
/// Native handle to Spi.
/// </summary>
- private IntPtr _handle;
+ private IntPtr _handle = IntPtr.Zero;
private bool _disposed = false;
/// <summary>
/// <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>
}
NativeSpi.Close(_handle);
+ _handle = IntPtr.Zero;
_disposed = true;
}
/// <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)
/// <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)
/// <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");
/*
-* 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.
/// <summary>
/// Native handle to I2c.
/// </summary>
- private IntPtr _handle;
+ private IntPtr _handle = IntPtr.Zero;
/// <summary>
/// Opens the UART slave device.
/// <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>
}
NativeUart.Close(_handle);
+ _handle = IntPtr.Zero;
_disposed = true;
}
/// <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];
/// <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];