Identify ports by sp_port structure, not name.
This commit is contained in:
parent
24c1a4bb05
commit
d54e90047b
18
README
18
README
|
@ -61,9 +61,8 @@ API
|
|||
The API is simple, and designed to be a minimal wrapper around the serial port
|
||||
support in each OS.
|
||||
|
||||
Most functions take a pointer to a struct sp_port, which represents an open
|
||||
port. This structure should be allocated by the user and is populated by
|
||||
sp_open(). It can be freed safely after sp_close().
|
||||
Most functions take a pointer to a struct sp_port, which represents an serial
|
||||
port. This structure is obtained from the array returned by sp_list_ports().
|
||||
|
||||
All functions can return only three possible error values. SP_ERR_ARG indicates
|
||||
the function was called with invalid arguments. SP_ERR_FAIL indicates that the
|
||||
|
@ -83,27 +82,26 @@ The available functions are as follows:
|
|||
Enumeration
|
||||
-----------
|
||||
|
||||
char **sp_list_ports();
|
||||
struct sp_port **sp_list_ports();
|
||||
|
||||
Lists the serial ports available on the system. The value returned is an array
|
||||
of port names as C strings, terminated by a NULL. It should be freed after use
|
||||
by calling sp_free_port_list().
|
||||
of pointers to sp_port structures, terminated by a NULL. It should be freed after
|
||||
use by calling sp_free_port_list().
|
||||
|
||||
void sp_free_port_list(char **list);
|
||||
void sp_free_port_list(struct sp_port **list);
|
||||
|
||||
Frees the data structure returned by sp_list_ports().
|
||||
|
||||
Opening and closing ports
|
||||
-------------------------
|
||||
|
||||
int sp_open(struct sp_port *port, char *portname, int flags);
|
||||
int sp_open(struct sp_port *port, int flags);
|
||||
|
||||
Opens the specified serial port.
|
||||
|
||||
Parameters:
|
||||
|
||||
port: Pointer to empty port structure, allocated by caller.
|
||||
portname: Name of port to open.
|
||||
port: Pointer to port structure.
|
||||
flags: Flags to use when opening the serial port. Possible
|
||||
flags are: SP_MODE_RDWR, SP_MODE_RDONLY, and SP_MODE_NONBLOCK.
|
||||
|
||||
|
|
40
serialport.c
40
serialport.c
|
@ -45,17 +45,34 @@
|
|||
|
||||
#include "serialport.h"
|
||||
|
||||
static char **sp_list_append(char **list, void *data, size_t len)
|
||||
static struct sp_port *sp_port_new(char *portname, size_t len)
|
||||
{
|
||||
struct sp_port *port;
|
||||
|
||||
if (!(port = malloc(sizeof(struct sp_port))))
|
||||
return NULL;
|
||||
|
||||
if (!(port->name = malloc(len)))
|
||||
{
|
||||
free(port);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(port->name, portname, len);
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
static struct sp_port **sp_list_append(struct sp_port **list, char *portname, size_t len)
|
||||
{
|
||||
void *tmp;
|
||||
unsigned int count;
|
||||
for (count = 0; list[count]; count++);
|
||||
if (!(tmp = realloc(list, sizeof(char *) * (count + 2))))
|
||||
if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
|
||||
goto fail;
|
||||
list = tmp;
|
||||
if (!(list[count] = malloc(len)))
|
||||
if (!(list[count] = sp_port_new(portname, len)))
|
||||
goto fail;
|
||||
memcpy(list[count], data, len);
|
||||
list[count + 1] = NULL;
|
||||
return list;
|
||||
fail:
|
||||
|
@ -68,11 +85,11 @@ fail:
|
|||
*
|
||||
* @return A null-terminated array of port name strings.
|
||||
*/
|
||||
char **sp_list_ports(void)
|
||||
struct sp_port **sp_list_ports(void)
|
||||
{
|
||||
char **list;
|
||||
struct sp_port **list;
|
||||
|
||||
if (!(list = malloc(sizeof(char *))))
|
||||
if (!(list = malloc(sizeof(struct sp_port **))))
|
||||
return NULL;
|
||||
|
||||
list[0] = NULL;
|
||||
|
@ -224,7 +241,7 @@ out:
|
|||
/**
|
||||
* Free a port list returned by sp_list_ports.
|
||||
*/
|
||||
void sp_free_port_list(char **list)
|
||||
void sp_free_port_list(struct sp_port **list)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i = 0; list[i]; i++)
|
||||
|
@ -259,16 +276,11 @@ static int sp_validate_port(struct sp_port *port)
|
|||
* @return SP_OK on success, SP_ERR_FAIL on failure,
|
||||
* or SP_ERR_ARG if an invalid port or name is passed.
|
||||
*/
|
||||
int sp_open(struct sp_port *port, char *portname, int flags)
|
||||
int sp_open(struct sp_port *port, int flags)
|
||||
{
|
||||
if (!port)
|
||||
return SP_ERR_ARG;
|
||||
|
||||
if (!portname)
|
||||
return SP_ERR_ARG;
|
||||
|
||||
port->name = portname;
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD desired_access = 0, flags_and_attributes = 0;
|
||||
/* Map 'flags' to the OS-specific settings. */
|
||||
|
|
|
@ -79,9 +79,9 @@ enum {
|
|||
SP_FLOW_SOFTWARE = 2
|
||||
};
|
||||
|
||||
char **sp_list_ports(void);
|
||||
void sp_free_port_list(char **ports);
|
||||
int sp_open(struct sp_port *port, char *portname, int flags);
|
||||
struct sp_port **sp_list_ports(void);
|
||||
void sp_free_port_list(struct sp_port **ports);
|
||||
int sp_open(struct sp_port *port, int flags);
|
||||
int sp_close(struct sp_port *port);
|
||||
int sp_flush(struct sp_port *port);
|
||||
int sp_write(struct sp_port *port, const void *buf, size_t count);
|
||||
|
|
Loading…
Reference in New Issue