using System;
using System.Runtime.InteropServices;
#if NETSTANDARD
using System.Runtime.ConstrainedExecution;
#endif
namespace AMWD.Protocols.Modbus.Serial.Utils
{
///
/// Definitions of the unsafe system methods.
///
/// Found on https://stackoverflow.com/a/10388107
///
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static class UnsafeNativeMethods
{
///
/// A flag for .
///
internal const int O_RDWR = 2;
///
/// A flag for .
///
internal const int O_NOCTTY = 256;
///
/// A flag for .
///
internal const uint TIOCGRS485 = 0x542E;
///
/// A flag for .
///
internal const uint TIOCSRS485 = 0x542F;
///
/// Opens a handle to a defined path (serial port).
///
/// The path to open the handle.
/// The flags for the handle.
[DllImport("libc", EntryPoint = "open", SetLastError = true)]
internal static extern SafeUnixHandle Open(string path, uint flag);
///
/// Performs an ioctl request to the open handle.
///
/// The handle.
/// The request.
/// The serial rs485 data structure to use.
[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
internal static extern int IoCtl(SafeUnixHandle handle, uint request, ref SerialRS485 serialRs485);
///
/// Closes an open handle.
///
/// The handle.
#if NETSTANDARD
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
#endif
[DllImport("libc", EntryPoint = "close", SetLastError = true)]
internal static extern int Close(IntPtr handle);
///
/// Converts the given error number (errno) into a readable string.
///
/// The error number.
[DllImport("libc", EntryPoint = "strerror", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr StrError(int errno);
}
}