Skip to content

Commit

Permalink
add support for less standard serial port speeds on macOS (#44052)
Browse files Browse the repository at this point in the history
* add support for less standard serial port speeds on macOS

* s/ifdef/if
  • Loading branch information
wfurt authored Nov 24, 2020
1 parent 2e8c4c7 commit d73c65e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/libraries/Native/Unix/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
#cmakedefine01 HAVE_CFMAKERAW
#cmakedefine01 HAVE_GETGROUPLIST
#cmakedefine01 HAVE_SYS_PROCINFO_H
#cmakedefine01 HAVE_IOSS_H

// Mac OS X has stat64, but it is deprecated since plain stat now
// provides the same 64-bit aware struct when targeting OS X > 10.5
Expand Down
37 changes: 33 additions & 4 deletions src/libraries/Native/Unix/System.IO.Ports.Native/pal_termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#if HAVE_IOSS_H
#include <IOKit/serial/ioss.h>
#endif

/* This is dup of System/IO/Ports/NativeMethods.cs */
enum
Expand Down Expand Up @@ -352,6 +355,14 @@ int32_t SystemIoPortsNative_TermiosSetSpeed(intptr_t handle, int32_t speed)

if (brate == B0)
{
#if HAVE_IOSS_H
// Looks like custom speed out of POSIX. Let see if we can set it via specialized call.
brate = speed;
if (ioctl(fd, IOSSIOSPEED, &brate) != -1)
{
return speed;
}
#endif
errno = EINVAL;
return -1;
}
Expand Down Expand Up @@ -418,6 +429,7 @@ int32_t SystemIoPortsNative_TermiosReset(intptr_t handle, int32_t speed, int32_t
{
int fd = ToFileDescriptor(handle);
struct termios term;
speed_t brate;
int ret = 0;

if (tcgetattr(fd, &term) < 0)
Expand Down Expand Up @@ -503,24 +515,41 @@ int32_t SystemIoPortsNative_TermiosReset(intptr_t handle, int32_t speed, int32_t

if (speed)
{
speed_t brate = SystemIoPortsNative_TermiosSpeed2Rate(speed);
brate = SystemIoPortsNative_TermiosSpeed2Rate(speed);
if (brate == B0)
{
#if !HAVE_IOSS_H
// We can try to set non-standard speed after tcsetattr().
errno = EINVAL;
return -1;
#endif
}

else
{
#if HAVE_CFSETSPEED
ret = cfsetspeed(&term, brate);
ret = cfsetspeed(&term, brate);
#else
ret = cfsetispeed(&term, brate) & cfsetospeed(&term, brate);
ret = cfsetispeed(&term, brate) & cfsetospeed(&term, brate);
#endif
}
}

if ((ret != 0) || (tcsetattr(fd, TCSANOW, &term) < 0))
{
return -1;
}

#if HAVE_IOSS_H
if (speed && brate == B0)
{
// we have deferred non-standard speed.
brate = speed;
if (ioctl(fd, IOSSIOSPEED, &brate) == -1)
{
return -1;
}
}
#endif

return 0;
}
4 changes: 4 additions & 0 deletions src/libraries/Native/Unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,10 @@ check_include_files(
linux/can.h
HAVE_LINUX_CAN_H)

check_include_files(
IOKit/serial/ioss.h
HAVE_IOSS_H)

check_symbol_exists(
getpeereid
unistd.h
Expand Down

0 comments on commit d73c65e

Please sign in to comment.