From 74c5e4ba010be459b9bb45f9636e0008fac0b7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Kr=C3=B3lik?= <66667989+Damian-Nordic@users.noreply.github.com> Date: Wed, 31 May 2023 04:25:44 +0200 Subject: [PATCH] [posix] fix setting non-standard baudrate on mac (#9090) tcsetattr() fails on mac OS when a non-standard baudrate, such as 1000000, is used. There is a separate ioct() that accepts non-standard baudrates though. --- src/posix/platform/hdlc_interface.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index a37427e5785..0f18c7b70dd 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -49,6 +49,7 @@ #endif #include #include +#include #include #include #include @@ -116,6 +117,10 @@ #define B4000000 4000000 #endif +#ifndef IOSSIOSPEED +#define IOSSIOSPEED 0x80045402 +#endif + #endif // __APPLE__ #if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_UART @@ -577,7 +582,21 @@ int HdlcInterface::OpenFile(const Url::Url &aRadioUrl) } VerifyOrExit((rval = cfsetspeed(&tios, static_cast(speed))) == 0, perror("cfsetspeed")); - VerifyOrExit((rval = tcsetattr(fd, TCSANOW, &tios)) == 0, perror("tcsetattr")); + rval = tcsetattr(fd, TCSANOW, &tios); + +#ifdef __APPLE__ + if (rval) + { + struct termios orig_tios; + VerifyOrExit((rval = tcgetattr(fd, &orig_tios)) == 0, perror("tcgetattr")); + VerifyOrExit((rval = cfsetispeed(&tios, cfgetispeed(&orig_tios))) == 0, perror("cfsetispeed")); + VerifyOrExit((rval = cfsetospeed(&tios, cfgetospeed(&orig_tios))) == 0, perror("cfsetospeed")); + VerifyOrExit((rval = tcsetattr(fd, TCSANOW, &tios)) == 0, perror("tcsetattr")); + VerifyOrExit((rval = ioctl(fd, IOSSIOSPEED, &speed)) == 0, perror("ioctl IOSSIOSPEED")); + } +#else // __APPLE__ + VerifyOrExit(rval == 0, perror("tcsetattr")); +#endif // __APPLE__ VerifyOrExit((rval = tcflush(fd, TCIOFLUSH)) == 0); }