Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

bottom part of serial port support on Unix #29033

Merged
merged 11 commits into from
Jul 5, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal enum OpenFlags
O_EXCL = 0x0040,
O_TRUNC = 0x0080,
O_SYNC = 0x0100,
O_NOCTTY = 0x0200,
O_NONBLOCK = 0x0400,
}
}
}
55 changes: 55 additions & 0 deletions src/Common/src/Interop/Unix/System.Native/Interop.Terminal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO.Ports;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosReset", SetLastError = true)]
internal static extern int TerminalReset(SafeFileHandle fd, int speed, int data, StopBits stop, Parity parity, Handshake flow);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods should have the same name as the native counterpart, minus the "SystemNative_" prefix, so e.g. TermiosReset instead of TerminalReset.


[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosGetDcd", SetLastError = true)]
internal static extern int TerminalGetCd(SafeFileHandle fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing "D" in the function name - the function should end "GetDcd" instead of "GetCd".


[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosGetCts", SetLastError = true)]
internal static extern int TerminalGetCts(SafeFileHandle fd);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosGetRts", SetLastError = true)]
internal static extern int TerminalGetRts(SafeFileHandle fd);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosGetDsr", SetLastError = true)]
internal static extern int TerminalGetDsr(SafeFileHandle fd);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosGetDtr", SetLastError = true)]
internal static extern int TerminalGetDtr(SafeFileHandle fd);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosSetDtr", SetLastError = true)]
internal static extern int TerminalSetDtr(SafeFileHandle fd, int value);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosSetRts", SetLastError = true)]
internal static extern int TerminalSetRts(SafeFileHandle fd, int value);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosSetSpeed", SetLastError = true)]
internal static extern int TerminalSetSpeed(SafeFileHandle fd, int speed);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosGetSpeed", SetLastError = true)]
internal static extern int TerminalGetSpeed(SafeFileHandle fd);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosAvailableBytes", SetLastError = true)]
internal static extern int TerminalGetAvailableBytes(SafeFileHandle fd, int input);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosDiscard", SetLastError = true)]
internal static extern int TerminalDiscard(SafeFileHandle fd, int input);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosDrain", SetLastError = true)]
internal static extern int TerminalDrain(SafeFileHandle fd);

[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_TermiosSendBreak", SetLastError = true)]
internal static extern int TerminalSendBreak(SafeFileHandle fd, int duration);
}
}
1 change: 1 addition & 0 deletions src/Native/Unix/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(NATIVE_SOURCES
pal_uid.cpp
pal_datetime.cpp
pal_sysctl.cpp
pal_termios.cpp
)

if (CMAKE_SYSTEM_NAME STREQUAL Linux)
Expand Down
6 changes: 5 additions & 1 deletion src/Native/Unix/System.Native/pal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ static int32_t ConvertOpenFlags(int32_t flags)
return -1;
}

if (flags & ~(PAL_O_ACCESS_MODE_MASK | PAL_O_CLOEXEC | PAL_O_CREAT | PAL_O_EXCL | PAL_O_TRUNC | PAL_O_SYNC))
if (flags & ~(PAL_O_ACCESS_MODE_MASK | PAL_O_CLOEXEC | PAL_O_CREAT | PAL_O_EXCL | PAL_O_TRUNC | PAL_O_SYNC | PAL_O_NOCTTY | PAL_O_NONBLOCK))
{
assert_msg(false, "Unknown Open flag", (int)flags);
return -1;
Expand All @@ -249,6 +249,10 @@ static int32_t ConvertOpenFlags(int32_t flags)
ret |= O_TRUNC;
if (flags & PAL_O_SYNC)
ret |= O_SYNC;
if (flags & PAL_O_NOCTTY)
ret |= O_NOCTTY;
if (flags & PAL_O_NONBLOCK)
ret |= O_NONBLOCK;

assert(ret != -1);
return ret;
Expand Down
2 changes: 2 additions & 0 deletions src/Native/Unix/System.Native/pal_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ enum
PAL_O_EXCL = 0x0040, // When combined with CREAT, fails if file already exists
PAL_O_TRUNC = 0x0080, // Truncate file to length 0 if it already exists
PAL_O_SYNC = 0x0100, // Block writes call will block until physically written
PAL_O_NOCTTY = 0x0200, // Will not allocate controlling terminal when opening TTY device.
PAL_O_NONBLOCK = 0x0400, // Open file in non-blocking mode.
};

/**
Expand Down