* Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
* Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
* Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
+ * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
#ifdef __APPLE__
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
+#include <IOKit/serial/ioss.h>
#include <sys/syslimits.h>
#endif
#ifdef __linux__
#include "libudev.h"
#include "linux/serial.h"
+#include "linux_termios.h"
#endif
#include "libserialport.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
+#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
+
/* Helper functions. */
static enum sp_return validate_port(struct sp_port *port);
static struct sp_port **list_append(struct sp_port **list, const char *portname);
#endif
}
+#ifdef __linux__
+static enum sp_return get_baudrate(int fd, int *baudrate)
+{
+ void *data;
+
+ if (!(data = malloc(get_termios_size())))
+ return SP_ERR_MEM;
+
+ if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
+ return SP_ERR_FAIL;
+
+ *baudrate = get_termios_speed(data);
+
+ return SP_OK;
+}
+
+static enum sp_return set_baudrate(int fd, int baudrate)
+{
+ void *data;
+
+ if (!(data = malloc(get_termios_size())))
+ return SP_ERR_MEM;
+
+ if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
+ return SP_ERR_FAIL;
+
+ set_termios_speed(data, baudrate);
+
+ if (ioctl(fd, get_termios_set_ioctl(), data) < 0)
+ return SP_ERR_FAIL;
+
+ return SP_OK;
+}
+#endif
+
static enum sp_return get_config(struct sp_port *port, struct port_data *data,
struct sp_port_config *config)
{
}
}
- if (i == NUM_STD_BAUDRATES)
+ if (i == NUM_STD_BAUDRATES) {
+#ifdef __APPLE__
+ config->baudrate = (int)data->term.c_ispeed;
+#elif defined(__linux__)
+ TRY(get_baudrate(port->fd, &config->baudrate));
+#else
config->baudrate = -1;
+#endif
+ }
switch (data->term.c_cflag & CSIZE) {
case CS8:
config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
config->dsr = SP_DSR_IGNORE;
- /* FIXME: Set config->xon_xoff properly, depending on data->term. */
- config->xon_xoff = SP_XONXOFF_DISABLED;
+ if (data->term.c_iflag & IXOFF) {
+ if (data->term.c_iflag & IXON)
+ config->xon_xoff = SP_XONXOFF_INOUT;
+ else
+ config->xon_xoff = SP_XONXOFF_IN;
+ } else {
+ if (data->term.c_iflag & IXON)
+ config->xon_xoff = SP_XONXOFF_OUT;
+ else
+ config->xon_xoff = SP_XONXOFF_DISABLED;
+ }
#endif
return SP_OK;
}
-static enum sp_return set_config(struct sp_port *port, struct port_data *data,
+static enum sp_return set_config(struct sp_port *port, struct port_data *data,
const struct sp_port_config *config)
{
unsigned int i;
+#ifdef __APPLE__
+ BAUD_TYPE baud_nonstd;
+
+ baud_nonstd = B0;
+#endif
+#ifdef __linux__
+ int baud_nonstd = 0;
+#endif
#ifdef _WIN32
if (config->baudrate >= 0) {
if (!SetCommState(port->hdl, &data->dcb))
return SP_ERR_FAIL;
-#else // !_WIN32
+#else /* !_WIN32 */
+
+ int controlbits;
if (config->baudrate >= 0) {
for (i = 0; i < NUM_STD_BAUDRATES; i++) {
}
}
- if (i == NUM_STD_BAUDRATES)
+ /* Non-standard baud rate */
+ if (i == NUM_STD_BAUDRATES) {
+#ifdef __APPLE__
+ /* Set "dummy" baud rate */
+ if (cfsetspeed(&data->term, B9600) < 0)
+ return SP_ERR_FAIL;
+ baud_nonstd = config->baudrate;
+#elif defined(__linux__)
+ baud_nonstd = 1;
+#else
return SP_ERR_ARG;
+#endif
+ }
}
if (config->bits >= 0) {
if (config->rts == SP_RTS_FLOW_CONTROL) {
data->term.c_iflag |= CRTSCTS;
} else {
- int controlbits = TIOCM_RTS;
+ controlbits = TIOCM_RTS;
if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
&controlbits) < 0)
return SP_ERR_FAIL;
return SP_ERR_ARG;
if (config->dtr >= 0) {
- int controlbits = TIOCM_DTR;
+ controlbits = TIOCM_DTR;
if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
&controlbits) < 0)
return SP_ERR_FAIL;
if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
return SP_ERR_FAIL;
+
+#ifdef __APPLE__
+ if (baud_nonstd != B0) {
+ if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
+ return SP_ERR_FAIL;
+ /* Set baud rates in data->term to correct, but incompatible
+ * with tcsetattr() value, same as delivered by tcgetattr(). */
+ if (cfsetspeed(&data->term, baud_nonstd) < 0)
+ return SP_ERR_FAIL;
+ }
+#elif defined(__linux__)
+ if (baud_nonstd)
+ TRY(set_baudrate(port->fd, config->baudrate));
#endif
+#endif /* !_WIN32 */
+
return SP_OK;
}
-#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
-
enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
{
struct port_data data;