Skip to content

Commit 2e94549

Browse files
ChasonDeshoteljpieper
authored andcommitted
Guard Linux-specific serial code to enable macOS builds
This change makes the moteus C++ headers buildable on macOS by guarding Linux-specific serial includes and ioctls behind platform checks. No new transport functionality is added. On macOS, serial-based transports remain unavailable, but fdcanusb works as expected. Linux and Windows behavior is unchanged.
1 parent 2433a3e commit 2e94549

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/cpp/mjbots/moteus/moteus_transport.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
#include <fcntl.h>
1818
#include <glob.h>
19+
#ifdef __linux__
1920
#include <linux/can.h>
2021
#include <linux/can/raw.h>
2122
#include <linux/serial.h>
23+
#endif
2224
#include <net/if.h>
2325
#include <poll.h>
2426
#include <stdarg.h>
@@ -397,7 +399,13 @@ class TimeoutTransport : public Transport {
397399
tmo.tv_sec = to_sleep_ns / 1000000000;
398400
tmo.tv_nsec = to_sleep_ns % 1000000000;
399401

402+
403+
#ifdef __APPLE__
404+
const int poll_ret = ::poll(&fds[0], 1, static_cast<int>(to_sleep_ns / 1000000));
405+
#else
400406
const int poll_ret = ::ppoll(&fds[0], 1, &tmo, nullptr);
407+
#endif
408+
401409
if (poll_ret < 0) {
402410
if (errno == EINTR) {
403411
// Go back and try again.
@@ -513,7 +521,7 @@ class Fdcanusb : public details::TimeoutTransport {
513521
const int fd = ::open(device.c_str(), O_RDWR | O_NOCTTY);
514522
FailIfErrno(fd == -1);
515523

516-
#ifndef _WIN32
524+
#ifdef __linux__
517525
{
518526
struct serial_struct serial;
519527
FailIfErrno(::ioctl(fd, TIOCGSERIAL, &serial) < 0);
@@ -531,7 +539,16 @@ class Fdcanusb : public details::TimeoutTransport {
531539
FailIfErrno(::tcsetattr(fd, TCSANOW, &toptions) < 0);
532540
FailIfErrno(::tcsetattr(fd, TCSAFLUSH, &toptions) < 0);
533541
}
534-
#else // _WIN32
542+
#elif __APPLE__
543+
{
544+
termios toptions{};
545+
FailIfErrno(::tcgetattr(fd, &toptions) < 0);
546+
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
547+
toptions.c_oflag &= ~OPOST;
548+
FailIfErrno(::tcsetattr(fd, TCSANOW, &toptions) < 0);
549+
FailIfErrno(::tcsetattr(fd, TCSAFLUSH, &toptions) < 0);
550+
}
551+
#elif _WIN32
535552
{
536553
// Windows is likely broken for many other reasons, but if we do
537554
// fix all the other problems, this will be necessary.

0 commit comments

Comments
 (0)