From 3ed08b68d4d4bd51d85334aa1a21690737b95cfe Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Thu, 30 Jun 2016 21:11:19 -0400 Subject: Start work to support client code on Mac OS X. Includes preliminary support for the magic Mac-specific ioctl() to see line speed, but has not yet been tested, that's waiting for some supporting tweaks to the RPC code from Paul. Includes some general cleanup which isn't really specific to Mac OS X per se but which needed doing and which simplifies adding the Mac code. --- rpc_serial.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'rpc_serial.c') diff --git a/rpc_serial.c b/rpc_serial.c index dc5821f..98a1d20 100644 --- a/rpc_serial.c +++ b/rpc_serial.c @@ -44,12 +44,28 @@ #include "hal_internal.h" #include "slip_internal.h" +/* + * Not thrilled about having OS-specific conditionals, but as such things + * go, this seems relatively safe: gcc and clang both define it on Mac OS X, + * and anything *not* on Mac OS X which defines it is begging for trouble. + */ + +#ifndef HAL_RPC_SERIAL_USE_MACOSX_IOCTL +#define HAL_RPC_SERIAL_USE_MACOSX_IOCTL (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)) +#endif + +#if HAL_RPC_SERIAL_USE_MACOSX_IOCTL +#include +#include +#endif + static int fd = -1; -hal_error_t hal_serial_init(const char * const device, const speed_t speed) +hal_error_t hal_serial_init(const char * const device, const uint32_t speed) { struct termios tty; - + speed_t termios_speed; + fd = open(device, O_RDWR | O_NOCTTY | O_SYNC); if (fd == -1) { fprintf(stderr, "open %s: ", device); @@ -59,9 +75,32 @@ hal_error_t hal_serial_init(const char * const device, const speed_t speed) if (tcgetattr (fd, &tty) != 0) return perror("tcgetattr"), HAL_ERROR_RPC_TRANSPORT; +#if HAL_RPC_SERIAL_USE_MACOSX_IOCTL + + termios_speed = speed; + + if (ioctl(fd, IOSSIOSPEED, &speed) < 0) + return perror("ioctl()"), HAL_ERROR_RPC_TRANSPORT; + +#else + + switch (speed) { + case 115200: + termios_speed = B115200; + break; + case 921600: + termios_speed = B921600; + break; + default: + fprintf(stderr, "invalid line speed %lu\n", (unsigned long) speed); + return HAL_ERROR_RPC_TRANSPORT; + } + cfsetospeed (&tty, speed); cfsetispeed (&tty, speed); +#endif + tty.c_cflag &= ~CSIZE; tty.c_cflag |= (CS8 | CLOCAL | CREAD); -- cgit v1.2.3