libsigrok  unreleased development snapshot
sigrok hardware access and backend library
Macros | Functions
tcp.c File Reference
#include "config.h"
#include <errno.h>
#include <glib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
+ Include dependency graph for tcp.c:

Go to the source code of this file.

Macros

#define LOG_PREFIX   "tcp"
 

Functions

SR_PRIV gboolean sr_fd_is_readable (int fd)
 Check whether a file descriptor is readable (without blocking). More...
 
SR_PRIV struct sr_tcp_dev_inst * sr_tcp_dev_inst_new (const char *host_addr, const char *tcp_port)
 Create a TCP communication instance. More...
 
SR_PRIV void sr_tcp_dev_inst_free (struct sr_tcp_dev_inst *tcp)
 Release a TCP communication instance. More...
 
SR_PRIV int sr_tcp_get_port_path (struct sr_tcp_dev_inst *tcp, const char *prefix, char separator, char *path, size_t path_len)
 Construct display name for a TCP communication instance. More...
 
SR_PRIV int sr_tcp_connect (struct sr_tcp_dev_inst *tcp)
 Connect to a remote TCP communication peer. More...
 
SR_PRIV int sr_tcp_disconnect (struct sr_tcp_dev_inst *tcp)
 Disconnect from a remote TCP communication peer. More...
 
SR_PRIV int sr_tcp_write_bytes (struct sr_tcp_dev_inst *tcp, const uint8_t *data, size_t dlen)
 Send transmit data to a TCP connection. More...
 
SR_PRIV int sr_tcp_read_bytes (struct sr_tcp_dev_inst *tcp, uint8_t *data, size_t dlen, gboolean nonblocking)
 Fetch receive data from a TCP connection. More...
 
SR_PRIV int sr_tcp_source_add (struct sr_session *session, struct sr_tcp_dev_inst *tcp, int events, int timeout, sr_receive_data_callback cb, void *cb_data)
 Register receive callback for a TCP connection. More...
 
SR_PRIV int sr_tcp_source_remove (struct sr_session *session, struct sr_tcp_dev_inst *tcp)
 Unregister receive callback for a TCP connection. More...
 

Macro Definition Documentation

#define LOG_PREFIX   "tcp"

Definition at line 64 of file tcp.c.

Function Documentation

SR_PRIV gboolean sr_fd_is_readable ( int  fd)

Check whether a file descriptor is readable (without blocking).

Parameters
[in]fdThe file descriptor to check for readability.
Returns
TRUE when readable, FALSE when read would block or when readability could not get determined.
Since
6.0

TODO Move to common code, applies to non-sockets as well.

Definition at line 78 of file tcp.c.

References void().

Referenced by sr_tcp_read_bytes().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

SR_PRIV int sr_tcp_connect ( struct sr_tcp_dev_inst *  tcp)

Connect to a remote TCP communication peer.

Parameters
[in]tcpThe TCP communication instance to connect.
Returns
SR_OK on success, SR_ERR_* otherwise.
Since
6.0

Definition at line 219 of file tcp.c.

References SR_ERR_ARG, SR_ERR_DATA, SR_ERR_IO, and SR_OK.

SR_PRIV void sr_tcp_dev_inst_free ( struct sr_tcp_dev_inst *  tcp)

Release a TCP communication instance.

Parameters
[in]host_addrThe host name or IP address (a string).
[in]tcp_portThe TCP port number.
Returns
A sr_tcp_dev_inst structure on success. #NULL otherwise.
Since
6.0

Definition at line 160 of file tcp.c.

References sr_tcp_disconnect(), and void().

+ Here is the call graph for this function:

SR_PRIV struct sr_tcp_dev_inst* sr_tcp_dev_inst_new ( const char *  host_addr,
const char *  tcp_port 
)

Create a TCP communication instance.

Parameters
[in]host_addrThe host name or IP address (a string).
[in]tcp_portThe TCP port number.
Returns
A sr_tcp_dev_inst structure on success. #NULL otherwise.
Since
6.0

Definition at line 128 of file tcp.c.

SR_PRIV int sr_tcp_disconnect ( struct sr_tcp_dev_inst *  tcp)

Disconnect from a remote TCP communication peer.

Parameters
[in]tcpThe TCP communication instance to disconnect.
Returns
SR_OK on success, SR_ERR_* otherwise.
Since
6.0

Definition at line 277 of file tcp.c.

References SR_ERR_ARG, and SR_OK.

Referenced by sr_tcp_dev_inst_free().

+ Here is the caller graph for this function:

SR_PRIV int sr_tcp_get_port_path ( struct sr_tcp_dev_inst *  tcp,
const char *  prefix,
char  separator,
char *  path,
size_t  path_len 
)

Construct display name for a TCP communication instance.

Parameters
[in]tcpThe TCP communication instance to print the name of.
[in]prefixAn optional prefix text, or #NULL.
[in]separatorAn optional separator character, or NUL.
[out]pathThe caller provided buffer to fill in.
[in]path_lenThe buffer's maximum length to fill in.
Returns
SR_OK on success, SR_ERR_* otherwise.
Since
6.0

Definition at line 184 of file tcp.c.

References SR_ERR_ARG, and SR_OK.

SR_PRIV int sr_tcp_read_bytes ( struct sr_tcp_dev_inst *  tcp,
uint8_t *  data,
size_t  dlen,
gboolean  nonblocking 
)

Fetch receive data from a TCP connection.

Does a single operating system call, can return with short receive byte counts. Will not continue after short reads, callers need to handle the condition.

Parameters
[in]tcpThe TCP communication instance to read from.
[in]dataCaller provided buffer for receive data.
[in]dlenThe maximum number of bytes to receive.
[in]nonblockingWhether to block for receive data.
Returns
Number of received bytes on success, SR_ERR_* otherwise.
Since
6.0

Definition at line 344 of file tcp.c.

References SR_ERR_ARG, SR_ERR_IO, and sr_fd_is_readable().

+ Here is the call graph for this function:

SR_PRIV int sr_tcp_source_add ( struct sr_session session,
struct sr_tcp_dev_inst *  tcp,
int  events,
int  timeout,
sr_receive_data_callback  cb,
void cb_data 
)

Register receive callback for a TCP connection.

The connection must have been established before. The callback gets invoked when receive data is available. Or when a timeout has expired.

This is a simple wrapper around sr_session_source_add().

Parameters
[in]sessionSee sr_session_source_add().
[in]tcpThe TCP communication instance to read from.
[in]eventsSee sr_session_source_add().
[in]timeoutSee sr_session_source_add().
[in]cbSee sr_session_source_add().
[in]cb_dataSee sr_session_source_add().
Returns
SR_OK on success, SR_ERR* otherwise.
Since
6.0

Definition at line 389 of file tcp.c.

References SR_ERR_ARG.

SR_PRIV int sr_tcp_source_remove ( struct sr_session session,
struct sr_tcp_dev_inst *  tcp 
)

Unregister receive callback for a TCP connection.

This is a simple wrapper around sr_session_source_remove().

Parameters
[in]sessionSee sr_session_source_remove().
[in]tcpThe TCP communication instance to unregister.
Returns
SR_OK on success, SR_ERR* otherwise.
Since
6.0

Definition at line 411 of file tcp.c.

References SR_ERR_ARG.

SR_PRIV int sr_tcp_write_bytes ( struct sr_tcp_dev_inst *  tcp,
const uint8_t *  data,
size_t  dlen 
)

Send transmit data to a TCP connection.

Does a single operating system call, can return with short transmit byte counts. Will not continue after short writes, callers need to handle the condition.

Parameters
[in]tcpThe TCP communication instance to send to.
[in]dataThe data bytes to send.
[in]dlenThe number of bytes to send.
Returns
Number of transmitted bytes on success, SR_ERR_* otherwise.
Since
6.0

Definition at line 306 of file tcp.c.

References SR_ERR_ARG, and SR_ERR_IO.