libsigrok
unreleased development snapshot
sigrok hardware access and backend library
|
#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"
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... | |
Check whether a file descriptor is readable (without blocking).
[in] | fd | The file descriptor to check for readability. |
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().
Connect to a remote TCP communication peer.
[in] | tcp | The TCP communication instance to connect. |
Definition at line 219 of file tcp.c.
References SR_ERR_ARG, SR_ERR_DATA, SR_ERR_IO, and SR_OK.
Release a TCP communication instance.
[in] | host_addr | The host name or IP address (a string). |
[in] | tcp_port | The TCP port number. |
Definition at line 160 of file tcp.c.
References sr_tcp_disconnect(), and void().
SR_PRIV struct sr_tcp_dev_inst* sr_tcp_dev_inst_new | ( | const char * | host_addr, |
const char * | tcp_port | ||
) |
Disconnect from a remote TCP communication peer.
[in] | tcp | The TCP communication instance to disconnect. |
Definition at line 277 of file tcp.c.
References SR_ERR_ARG, and SR_OK.
Referenced by sr_tcp_dev_inst_free().
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.
[in] | tcp | The TCP communication instance to print the name of. |
[in] | prefix | An optional prefix text, or #NULL. |
[in] | separator | An optional separator character, or NUL. |
[out] | path | The caller provided buffer to fill in. |
[in] | path_len | The buffer's maximum length to fill in. |
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.
[in] | tcp | The TCP communication instance to read from. |
[in] | data | Caller provided buffer for receive data. |
[in] | dlen | The maximum number of bytes to receive. |
[in] | nonblocking | Whether to block for receive data. |
Definition at line 344 of file tcp.c.
References SR_ERR_ARG, SR_ERR_IO, and sr_fd_is_readable().
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().
[in] | session | See sr_session_source_add(). |
[in] | tcp | The TCP communication instance to read from. |
[in] | events | See sr_session_source_add(). |
[in] | timeout | See sr_session_source_add(). |
[in] | cb | See sr_session_source_add(). |
[in] | cb_data | See sr_session_source_add(). |
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().
[in] | session | See sr_session_source_remove(). |
[in] | tcp | The TCP communication instance to unregister. |
Definition at line 411 of file tcp.c.
References SR_ERR_ARG.
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.
[in] | tcp | The TCP communication instance to send to. |
[in] | data | The data bytes to send. |
[in] | dlen | The number of bytes to send. |
Definition at line 306 of file tcp.c.
References SR_ERR_ARG, and SR_ERR_IO.