1
0
mirror of git://sigrok.org/libserialport synced 2023-08-10 21:13:24 +03:00

Add sp_copy_port() function.

This commit is contained in:
Martin Ling 2013-11-04 13:42:55 +00:00 committed by Uwe Hermann
parent 99945a1fb5
commit 32b5ac05b4
3 changed files with 35 additions and 2 deletions

20
README
View File

@ -107,16 +107,32 @@ int sp_list_ports(struct sp_port ***list_ptr);
allocate a variable of type "struct sp_port **" and pass a pointer to this to allocate a variable of type "struct sp_port **" and pass a pointer to this to
receive the result. receive the result.
The result should be freed after use by calling sp_free_port_list(). The result should be freed after use by calling sp_free_port_list(). If a port
from the list is to be used after freeing the list, it must be copied first
using sp_copy_port().
Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
failure, or SP_ERR_ARG if an invalid pointer is passed. If any error failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
is returned, the variable pointed to by list_ptr will be set to NULL. is returned, the variable pointed to by list_ptr will be set to NULL.
Otherwise, it will be set to point to the newly allocated array. Otherwise, it will be set to point to the newly allocated array.
int sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
Makes a new copy of a sp_port structure. The user should allocate a variable
of type "struct sp_port *" and pass a pointer to this to receive the result.
The copy should be freed after use by calling sp_free_port().
Returns: SP_OK on success, SP_ERR_MEM on allocation failure, or SP_ERR_ARG
if an invalid port or pointer is passed. If any error is returned,
the variable pointed to by copy_ptr will be set to NULL. Otherwise,
it will be set to point to the newly allocated copy.
void sp_free_port_list(struct sp_port **list); void sp_free_port_list(struct sp_port **list);
Frees a port list obtained from sp_list_ports(). Frees a port list obtained from sp_list_ports(). This will also free all
the sp_port structures referred to from the list; any that are to be retained
must be copied first using sp_copy_port().
Opening and closing ports Opening and closing ports
------------------------- -------------------------

View File

@ -82,6 +82,7 @@ enum {
int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
void sp_free_port(struct sp_port *port); void sp_free_port(struct sp_port *port);
int sp_list_ports(struct sp_port ***list_ptr); int sp_list_ports(struct sp_port ***list_ptr);
int sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
void sp_free_port_list(struct sp_port **ports); void sp_free_port_list(struct sp_port **ports);
int sp_open(struct sp_port *port, int flags); int sp_open(struct sp_port *port, int flags);
int sp_close(struct sp_port *port); int sp_close(struct sp_port *port);

View File

@ -50,6 +50,9 @@ int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
struct sp_port *port; struct sp_port *port;
int len; int len;
if (!port_ptr)
return SP_ERR_ARG;
*port_ptr = NULL; *port_ptr = NULL;
if (!portname) if (!portname)
@ -73,6 +76,19 @@ int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
return SP_OK; return SP_OK;
} }
int sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
{
if (!copy_ptr)
return SP_ERR_ARG;
*copy_ptr = NULL;
if (!port || !port->name)
return SP_ERR_ARG;
return sp_get_port_by_name(port->name, copy_ptr);
}
void sp_free_port(struct sp_port *port) void sp_free_port(struct sp_port *port)
{ {
if (!port) if (!port)