-----------------------------------------------------------------
+-------------------------------------------------------------------------------
libserialport: cross-platform library for accessing serial ports
-----------------------------------------------------------------
+-------------------------------------------------------------------------------
libserialport is a minimal library written in C that is intended to take care
of the OS-specific details when writing software that uses serial ports.
error code or message is that provided by the OS; libserialport does not define
any error codes or messages of its own.
-Functions calls that succeed return SP_OK, which is equal to zero, or where
+Function calls that succeed return SP_OK, which is equal to zero, or where
otherwise documented a positive value.
The available functions are as follows:
LT_INIT
# Initialize pkg-config.
+# We require at least 0.22, as "Requires.private" behaviour changed there.
PKG_PROG_PKG_CONFIG([0.22])
# Library version for libserialport (NOT the same as the package version).
AC_SUBST(SP_LIB_LDFLAGS)
# Checks for header files.
-# These are already checked: inttypes.h stdint.h stdlib.h string.h unistd.h.
-AC_CHECK_HEADERS([sys/types.h sys/stat.h fcntl.h termios.h sys/ioctl.h errno.h])
+# These are already checked: inttypes.h dlfcn.h memory.h stdint.h stdlib.h
+# string.h strings.h sys/types.h sys/stat.h unistd.h
+AC_CHECK_HEADERS([errno.h fcntl.h stddef.h sys/ioctl.h termios.h])
case $target_os in
*linux*)
+ # On Linux libudev is currently a hard requirement.
PKG_CHECK_MODULES([libudev], [libudev >= 0],
[CFLAGS="$CFLAGS $libudev_CFLAGS"; LIBS="$LIBS $libudev_LIBS"],
[AC_MSG_ERROR([libudev.h not found])])
echo
echo "libserialport configuration summary:"
echo
-echo " - Package version (major.minor): $SP_PACKAGE_VERSION"
+echo " - Package version (major.minor): $SP_PACKAGE_VERSION"
echo " - Library version (current:revision:age): $SP_LIB_VERSION"
echo " - Prefix: $prefix"
echo " - Building on: $build"
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef SERIALPORT_H
-#define SERIALPORT_H
+#ifndef LIBSERIALPORT_H
+#define LIBSERIALPORT_H
#include <stddef.h>
#ifdef _WIN32
/* A system error occured while executing the operation. */
SP_ERR_FAIL = -2,
/* A memory allocation failed while executing the operation. */
- SP_ERR_MEM = -3
+ SP_ERR_MEM = -3,
};
/* Port access modes. */
/* Open port for read access only. */
SP_MODE_RDONLY = 2,
/* Open port in non-blocking mode. */
- SP_MODE_NONBLOCK = 4
+ SP_MODE_NONBLOCK = 4,
};
/* Parity settings. */
/* Even parity. */
SP_PARITY_EVEN = 1,
/* Odd parity. */
- SP_PARITY_ODD = 2
+ SP_PARITY_ODD = 2,
};
/* Flow control settings. */
/* Hardware (RTS/CTS) flow control. */
SP_FLOW_HARDWARE = 1,
/* Software (XON/XOFF) flow control. */
- SP_FLOW_SOFTWARE = 2
+ SP_FLOW_SOFTWARE = 2,
};
int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
char *sp_last_error_message(void);
void sp_free_error_message(char *message);
-#endif /* SERIALPORT_H */
+#endif /* LIBSERIALPORT_H */
{
void *tmp;
unsigned int count;
+
for (count = 0; list[count]; count++);
if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
goto fail;
goto fail;
list[count + 1] = NULL;
return list;
+
fail:
sp_free_port_list(list);
return NULL;
int ret = SP_OK;
if (!(list = malloc(sizeof(struct sp_port **))))
- return SP_ERR_MEM;;
+ return SP_ERR_MEM;
list[0] = NULL;
void sp_free_port_list(struct sp_port **list)
{
unsigned int i;
+
for (i = 0; list[i]; i++)
sp_free_port(list[i]);
free(list);
return SP_ERR_FAIL;
#else
int flags_local = 0;
+
/* Map 'flags' to the OS-specific settings. */
if (flags & SP_MODE_RDWR)
flags_local |= O_RDWR;
#ifdef _WIN32
DWORD written = 0;
+
/* Returns non-zero upon success, 0 upon failure. */
if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
return SP_ERR_FAIL;
#else
/* Returns the number of bytes written, or -1 upon failure. */
ssize_t written = write(port->fd, buf, count);
+
if (written < 0)
return SP_ERR_FAIL;
else
- return written;;
+ return written;
#endif
}
#ifdef _WIN32
DWORD bytes_read = 0;
+
/* Returns non-zero upon success, 0 upon failure. */
if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
return SP_ERR_FAIL;
return bytes_read;
#else
ssize_t bytes_read;
+
/* Returns the number of bytes read, or -1 upon failure. */
if ((bytes_read = read(port->fd, buf, count)) < 0)
return SP_ERR_FAIL;