Skip to content

Commit

Permalink
Upgrade to libserialport 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepb committed Dec 12, 2014
1 parent 650e7a0 commit 898e591
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 58 deletions.
36 changes: 19 additions & 17 deletions libserialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ enum sp_return {
/** A memory allocation failed while executing the operation. */
SP_ERR_MEM = -3,
/** The requested operation is not supported by this system or device. */
SP_ERR_SUPP = -4,
SP_ERR_SUPP = -4
};

/** Port access modes. */
Expand All @@ -109,6 +109,8 @@ enum sp_mode {
SP_MODE_READ = 1,
/** Open port for write access. */
SP_MODE_WRITE = 2,
/** Open port for read and write access. */
SP_MODE_READ_WRITE = 3
};

/** Port events. */
Expand All @@ -118,7 +120,7 @@ enum sp_event {
/* Ready to transmit new data. */
SP_EVENT_TX_READY = 2,
/* Error occured. */
SP_EVENT_ERROR = 4,
SP_EVENT_ERROR = 4
};

/** Buffer selection. */
Expand All @@ -128,7 +130,7 @@ enum sp_buffer {
/** Output buffer. */
SP_BUF_OUTPUT = 2,
/** Both buffers. */
SP_BUF_BOTH = 3,
SP_BUF_BOTH = 3
};

/** Parity settings. */
Expand All @@ -144,7 +146,7 @@ enum sp_parity {
/** Mark parity. */
SP_PARITY_MARK = 3,
/** Space parity. */
SP_PARITY_SPACE = 4,
SP_PARITY_SPACE = 4
};

/** RTS pin behaviour. */
Expand All @@ -156,7 +158,7 @@ enum sp_rts {
/** RTS on. */
SP_RTS_ON = 1,
/** RTS used for flow control. */
SP_RTS_FLOW_CONTROL = 2,
SP_RTS_FLOW_CONTROL = 2
};

/** CTS pin behaviour. */
Expand All @@ -166,7 +168,7 @@ enum sp_cts {
/** CTS ignored. */
SP_CTS_IGNORE = 0,
/** CTS used for flow control. */
SP_CTS_FLOW_CONTROL = 1,
SP_CTS_FLOW_CONTROL = 1
};

/** DTR pin behaviour. */
Expand All @@ -178,7 +180,7 @@ enum sp_dtr {
/** DTR on. */
SP_DTR_ON = 1,
/** DTR used for flow control. */
SP_DTR_FLOW_CONTROL = 2,
SP_DTR_FLOW_CONTROL = 2
};

/** DSR pin behaviour. */
Expand All @@ -188,7 +190,7 @@ enum sp_dsr {
/** DSR ignored. */
SP_DSR_IGNORE = 0,
/** DSR used for flow control. */
SP_DSR_FLOW_CONTROL = 1,
SP_DSR_FLOW_CONTROL = 1
};

/** XON/XOFF flow control behaviour. */
Expand All @@ -202,7 +204,7 @@ enum sp_xonxoff {
/** XON/XOFF enabled for output only. */
SP_XONXOFF_OUT = 2,
/** XON/XOFF enabled for input and output. */
SP_XONXOFF_INOUT = 3,
SP_XONXOFF_INOUT = 3
};

/** Standard flow control combinations. */
Expand All @@ -214,7 +216,7 @@ enum sp_flowcontrol {
/** Hardware flow control using RTS/CTS signals. */
SP_FLOWCONTROL_RTSCTS = 2,
/** Hardware flow control using DTR/DSR signals. */
SP_FLOWCONTROL_DTRDSR = 3,
SP_FLOWCONTROL_DTRDSR = 3
};

/** Input signals. */
Expand All @@ -226,7 +228,7 @@ enum sp_signal {
/** Data carrier detect. */
SP_SIG_DCD = 4,
/** Ring indicator. */
SP_SIG_RI = 8,
SP_SIG_RI = 8
};

/** Transport types. */
Expand All @@ -235,8 +237,8 @@ enum sp_transport {
SP_TRANSPORT_NATIVE,
/** USB serial port adapter. */
SP_TRANSPORT_USB,
/** Bluetooh serial port adapter. */
SP_TRANSPORT_BLUETOOTH,
/** Bluetooth serial port adapter. */
SP_TRANSPORT_BLUETOOTH
};

/**
Expand Down Expand Up @@ -1196,13 +1198,13 @@ void sp_free_event_set(struct sp_event_set *event_set);
* the sp_signal enum.
*
* @param port Pointer to port structure.
* @param signals Pointer to variable to receive result.
* @param signal_mask Pointer to variable to receive result.
*
* @return SP_OK upon success, a negative error code otherwise.
*
* @since 0.1.0
*/
enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals);
enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signal_mask);

/**
* Put the port transmit line into the break state.
Expand Down Expand Up @@ -1321,13 +1323,13 @@ void sp_default_debug_handler(const char *format, ...);
#define SP_PACKAGE_VERSION_MAJOR 0

/** The libserialport package 'minor' version number. */
#define SP_PACKAGE_VERSION_MINOR 1
#define SP_PACKAGE_VERSION_MINOR 2

/** The libserialport package 'micro' version number. */
#define SP_PACKAGE_VERSION_MICRO 0

/** The libserialport package version ("major.minor.micro") as string. */
#define SP_PACKAGE_VERSION_STRING "0.1.0"
#define SP_PACKAGE_VERSION_STRING "0.2.0"

/*
* Library/libtool version macros (can be used for conditional compilation).
Expand Down
7 changes: 5 additions & 2 deletions libserialport_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#ifdef __linux__
#define _BSD_SOURCE // for timeradd, timersub, timercmp
#define _XOPEN_SOURCE 700 // for readlinkat
#endif

#include <string.h>
Expand All @@ -37,7 +36,11 @@
#include <tchar.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#undef DEFINE_GUID
#define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
static const GUID name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } }
#include <usbioctl.h>
#include <usbiodef.h>
#else
#include <limits.h>
#include <termios.h>
Expand Down Expand Up @@ -75,7 +78,7 @@
#endif

/* Non-standard baudrates are not available everywhere. */
#if defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)
#if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && defined(HAVE_BOTHER)
#define USE_TERMIOS_SPEED
#endif

Expand Down
6 changes: 0 additions & 6 deletions linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list)
struct serial_struct serial_info;
int ioctl_result;
#endif
#ifndef HAVE_READLINKAT
char buf[sizeof(entry.d_name) + 16];
#endif
int len, fd;
DIR *dir;
int ret = SP_OK;
Expand All @@ -180,12 +178,8 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list)

DEBUG("Iterating over results");
while (!readdir_r(dir, &entry, &result) && result) {
#ifdef HAVE_READLINKAT
len = readlinkat(dirfd(dir), entry.d_name, target, sizeof(target));
#else
snprintf(buf, sizeof(buf), "/sys/class/tty/%s", entry.d_name);
len = readlink(buf, target, sizeof(target));
#endif
if (len <= 0 || len >= (int) sizeof(target)-1)
continue;
target[len] = 0;
Expand Down
11 changes: 6 additions & 5 deletions linux_termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@

#ifdef __linux__

#include <stdlib.h>
#include <linux/termios.h>
#include "linux_termios.h"

SP_PRIV int get_termios_get_ioctl(void)
SP_PRIV unsigned long get_termios_get_ioctl(void)
{
#ifdef HAVE_TERMIOS2
return TCGETS2;
Expand All @@ -47,7 +48,7 @@ SP_PRIV int get_termios_get_ioctl(void)
#endif
}

SP_PRIV int get_termios_set_ioctl(void)
SP_PRIV unsigned long get_termios_set_ioctl(void)
{
#ifdef HAVE_TERMIOS2
return TCSETS2;
Expand All @@ -56,7 +57,7 @@ SP_PRIV int get_termios_set_ioctl(void)
#endif
}

SP_PRIV int get_termios_size(void)
SP_PRIV size_t get_termios_size(void)
{
#ifdef HAVE_TERMIOS2
return sizeof(struct termios2);
Expand All @@ -65,7 +66,7 @@ SP_PRIV int get_termios_size(void)
#endif
}

#if defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)
#if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && defined(HAVE_BOTHER)
SP_PRIV int get_termios_speed(void *data)
{
#ifdef HAVE_TERMIOS2
Expand Down Expand Up @@ -93,7 +94,7 @@ SP_PRIV void set_termios_speed(void *data, int speed)
#endif

#ifdef HAVE_TERMIOX
SP_PRIV int get_termiox_size(void)
SP_PRIV size_t get_termiox_size(void)
{
return sizeof(struct termiox);
}
Expand Down
10 changes: 6 additions & 4 deletions linux_termios.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#ifndef LIBSERIALPORT_LINUX_TERMIOS_H
#define LIBSERIALPORT_LINUX_TERMIOS_H

SP_PRIV int get_termios_get_ioctl(void);
SP_PRIV int get_termios_set_ioctl(void);
SP_PRIV int get_termios_size(void);
#include <stdlib.h>

SP_PRIV unsigned long get_termios_get_ioctl(void);
SP_PRIV unsigned long get_termios_set_ioctl(void);
SP_PRIV size_t get_termios_size(void);
SP_PRIV int get_termios_speed(void *data);
SP_PRIV void set_termios_speed(void *data, int speed);
SP_PRIV int get_termiox_size(void);
SP_PRIV size_t get_termiox_size(void);
SP_PRIV int get_termiox_flow(void *data, int *rts, int *cts, int *dtr, int *dsr);
SP_PRIV void set_termiox_flow(void *data, int rts, int cts, int dtr, int dsr);

Expand Down
5 changes: 3 additions & 2 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ const Debug = false

const (
// Port access modes
MODE_READ = C.SP_MODE_READ // Open port for read access
MODE_WRITE = C.SP_MODE_WRITE // Open port for write access
MODE_READ = C.SP_MODE_READ // Open port for read access
MODE_WRITE = C.SP_MODE_WRITE // Open port for write access
MODE_READ_WRITE = C.SP_MODE_READ_WRITE // Open port for read and write access.

// Port events.
EVENT_RX_READY = C.SP_EVENT_RX_READY // Data received and ready to read.
Expand Down
11 changes: 6 additions & 5 deletions serialport.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port *
memcpy(port->name, portname, len);

#ifdef _WIN32
port->usb_path = NULL;
port->hdl = INVALID_HANDLE_VALUE;
#else
port->fd = -1;
Expand Down Expand Up @@ -402,7 +403,7 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)

CHECK_PORT();

if (flags > (SP_MODE_READ | SP_MODE_WRITE))
if (flags > SP_MODE_READ_WRITE)
RETURN_ERROR(SP_ERR_ARG, "Invalid flags");

DEBUG_FMT("Opening port %s", port->name);
Expand Down Expand Up @@ -479,7 +480,7 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
int flags_local = O_NONBLOCK | O_NOCTTY;

/* Map 'flags' to the OS-specific settings. */
if (flags & (SP_MODE_READ | SP_MODE_WRITE))
if ((flags & SP_MODE_READ_WRITE) == SP_MODE_READ_WRITE)
flags_local |= O_RDWR;
else if (flags & SP_MODE_READ)
flags_local |= O_RDONLY;
Expand Down Expand Up @@ -618,11 +619,11 @@ SP_API enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
RETURN_FAIL("PurgeComm() failed");
#else
int flags = 0;
if (buffers & SP_BUF_BOTH)
if (buffers == SP_BUF_BOTH)
flags = TCIOFLUSH;
else if (buffers & SP_BUF_INPUT)
else if (buffers == SP_BUF_INPUT)
flags = TCIFLUSH;
else if (buffers & SP_BUF_OUTPUT)
else if (buffers == SP_BUF_OUTPUT)
flags = TCOFLUSH;

/* Returns 0 upon success, -1 upon failure. */
Expand Down
Loading

0 comments on commit 898e591

Please sign in to comment.