aboutsummaryrefslogtreecommitdiff
path: root/rpc_serial.c
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2016-06-30 21:11:19 -0400
committerRob Austein <sra@hactrn.net>2016-06-30 21:11:19 -0400
commit3ed08b68d4d4bd51d85334aa1a21690737b95cfe (patch)
treec6e96226598704a3b21148c63fee3b81a25eae85 /rpc_serial.c
parent03a407b83da294ff05d4f230437ec06c910b2e85 (diff)
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.
Diffstat (limited to 'rpc_serial.c')
-rw-r--r--rpc_serial.c43
1 files changed, 41 insertions, 2 deletions
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 <IOKit/serial/ioss.h>
+#include <sys/ioctl.h>
+#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);