From 1c5aae9dc58995d202e30c356dcf25a8c1827558 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Sat, 23 Nov 2013 17:11:19 +0000 Subject: [PATCH] Make port structure opaque. --- libserialport.h.in | 28 ++++++++++++++++------------ serialport.c | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/libserialport.h.in b/libserialport.h.in index 3de68a9..2492f68 100644 --- a/libserialport.h.in +++ b/libserialport.h.in @@ -226,18 +226,7 @@ enum sp_signal { }; /** A serial port. */ -struct sp_port { - /** Name used to open the port. */ - char *name; -/** @cond 0 */ - /** OS-specific port handle. */ -#ifdef _WIN32 - HANDLE hdl; -#else - int fd; -#endif -/** @endcond */ -}; +struct sp_port; /** Configuration for a serial port. */ struct sp_port_config { @@ -281,6 +270,21 @@ struct sp_port_config { */ enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); +/** + * Get the name of a port. + * + * The name returned is whatever is normally used to refer to a port on the + * current operating system; e.g. for Windows it will usually be a "COMn" + * device name, and for Unix it will be a device path beginning with "/dev/". + * + * @param port Pointer to port structure. + * + * @return The port name, or NULL if an invalid port is passed. The name + * string is part of the port structure and may not be used after the + * port structure has been freed. + */ +char *sp_get_port_name(const struct sp_port *port); + /** * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port(). */ diff --git a/serialport.c b/serialport.c index f751888..0ec664a 100644 --- a/serialport.c +++ b/serialport.c @@ -58,6 +58,15 @@ #include "libserialport.h" +struct sp_port { + char *name; +#ifdef _WIN32 + HANDLE hdl; +#else + int fd; +#endif +}; + struct port_data { #ifdef _WIN32 DCB dcb; @@ -184,6 +193,16 @@ enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_p RETURN_OK(); } +char *sp_get_port_name(const struct sp_port *port) +{ + TRACE("%p", port); + + if (!port) + return NULL; + + RETURN_VALUE("%s", port->name); +} + enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr) { TRACE("%p, %p", port, copy_ptr);