Skip to content

Commit

Permalink
transport: Add initial TCP/IP support
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Schink <jaylink-dev@marcschink.de>
  • Loading branch information
Marc Schink committed Jul 12, 2017
1 parent 0f10b0a commit 062a600
Show file tree
Hide file tree
Showing 8 changed files with 1,396 additions and 564 deletions.
4 changes: 3 additions & 1 deletion Doxyfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,9 @@ EXCLUDE = @top_srcdir@/libjaylink/buffer.c \
@top_srcdir@/libjaylink/libjaylink-internal.h \
@top_srcdir@/libjaylink/list.c \
@top_srcdir@/libjaylink/socket.c \
@top_srcdir@/libjaylink/transport.c
@top_srcdir@/libjaylink/transport.c \
@top_srcdir@/libjaylink/transport_tcp.c \
@top_srcdir@/libjaylink/transport_usb.c

# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
Expand Down
2 changes: 2 additions & 0 deletions libjaylink/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ libjaylink_la_SOURCES = \
swo.c \
target.c \
transport.c \
transport_tcp.c \
transport_usb.c \
util.c \
version.c

Expand Down
4 changes: 0 additions & 4 deletions libjaylink/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@ static void free_device_handle(struct jaylink_device_handle *devh)
* @retval JAYLINK_ERR_ARG Invalid arguments.
* @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
* @retval JAYLINK_ERR_MALLOC Memory allocation error.
* @retval JAYLINK_ERR_NOT_SUPPORTED Operation not supported.
* @retval JAYLINK_ERR_IO Input/output error.
* @retval JAYLINK_ERR Other error conditions.
*
Expand All @@ -543,9 +542,6 @@ JAYLINK_API int jaylink_open(struct jaylink_device *dev,
if (!dev || !devh)
return JAYLINK_ERR_ARG;

if (dev->interface != JAYLINK_HIF_USB)
return JAYLINK_ERR_NOT_SUPPORTED;

handle = allocate_device_handle(dev);

if (!handle) {
Expand Down
59 changes: 51 additions & 8 deletions libjaylink/libjaylink-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,6 @@ struct jaylink_device {
struct jaylink_device_handle {
/** Device instance. */
struct jaylink_device *dev;
/** libusb device handle. */
struct libusb_device_handle *usb_devh;
/** USB interface number of the device. */
uint8_t interface_number;
/** USB interface IN endpoint of the device. */
uint8_t endpoint_in;
/** USB interface OUT endpoint of the device. */
uint8_t endpoint_out;
/**
* Buffer for write and read operations.
*
Expand Down Expand Up @@ -177,6 +169,21 @@ struct jaylink_device_handle {
* write operations only.
*/
size_t write_pos;
/** libusb device handle. */
struct libusb_device_handle *usb_devh;
/** USB interface number of the device. */
uint8_t interface_number;
/** USB interface IN endpoint of the device. */
uint8_t endpoint_in;
/** USB interface OUT endpoint of the device. */
uint8_t endpoint_out;
/**
* Socket descriptor.
*
* This field is used for devices with host interface #JAYLINK_HIF_TCP
* only.
*/
int sock;
};

struct list {
Expand Down Expand Up @@ -236,6 +243,10 @@ JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx,
JAYLINK_PRIV bool socket_close(int sock);
JAYLINK_PRIV bool socket_bind(int sock, const struct sockaddr *address,
size_t length);
JAYLINK_PRIV bool socket_send(int sock, const void *buffer, size_t *length,
int flags);
JAYLINK_PRIV bool socket_recv(int sock, void *buffer, size_t *length,
int flags);
JAYLINK_PRIV bool socket_sendto(int sock, const void *buffer, size_t *length,
int flags, const struct sockaddr *address,
size_t address_length);
Expand All @@ -259,4 +270,36 @@ JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh,
JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh,
uint8_t *buffer, size_t length);

/*--- transport_usb.c -------------------------------------------------------*/

JAYLINK_PRIV int transport_usb_open(struct jaylink_device_handle *devh);
JAYLINK_PRIV int transport_usb_close(struct jaylink_device_handle *devh);
JAYLINK_PRIV int transport_usb_start_write_read(
struct jaylink_device_handle *devh, size_t write_length,
size_t read_length, bool has_command);
JAYLINK_PRIV int transport_usb_start_write(struct jaylink_device_handle *devh,
size_t length, bool has_command);
JAYLINK_PRIV int transport_usb_start_read(struct jaylink_device_handle *devh,
size_t length);
JAYLINK_PRIV int transport_usb_write(struct jaylink_device_handle *devh,
const uint8_t *buffer, size_t length);
JAYLINK_PRIV int transport_usb_read(struct jaylink_device_handle *devh,
uint8_t *buffer, size_t length);

/*--- transport_tcp.c -------------------------------------------------------*/

JAYLINK_PRIV int transport_tcp_open(struct jaylink_device_handle *devh);
JAYLINK_PRIV int transport_tcp_close(struct jaylink_device_handle *devh);
JAYLINK_PRIV int transport_tcp_start_write_read(
struct jaylink_device_handle *devh, size_t write_length,
size_t read_length, bool has_command);
JAYLINK_PRIV int transport_tcp_start_write(struct jaylink_device_handle *devh,
size_t length, bool has_command);
JAYLINK_PRIV int transport_tcp_start_read(struct jaylink_device_handle *devh,
size_t length);
JAYLINK_PRIV int transport_tcp_write(struct jaylink_device_handle *devh,
const uint8_t *buffer, size_t length);
JAYLINK_PRIV int transport_tcp_read(struct jaylink_device_handle *devh,
uint8_t *buffer, size_t length);

#endif /* LIBJAYLINK_LIBJAYLINK_INTERNAL_H */
65 changes: 65 additions & 0 deletions libjaylink/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,71 @@ JAYLINK_PRIV bool socket_bind(int sock, const struct sockaddr *address,
* value is undefined on failure.
* @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
* specify multiple flags.
*
* @return Whether the message was sent successfully.
*/
JAYLINK_PRIV bool socket_send(int sock, const void *buffer, size_t *length,
int flags)
{
ssize_t ret;

ret = send(sock, buffer, *length, flags);
#ifdef _WIN32
if (ret == SOCKET_ERROR)
return false;
#else
if (ret < 0)
return false;
#endif
*length = ret;

return true;
}

/**
* Receive a message from a socket.
*
* @param[in] sock Socket descriptor.
* @param[out] buffer Buffer to store the received message on success. Its
* content is undefined on failure.
* @param[in,out] length Maximum length of the message in bytes. On success,
* the value gets updated with the actual number of
* received bytes. The value is undefined on failure.
* @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
* specify multiple flags.
*
* @return Whether a message was successfully received.
*/
JAYLINK_PRIV bool socket_recv(int sock, void *buffer, size_t *length,
int flags)
{
ssize_t ret;

ret = recv(sock, buffer, *length, flags);

#ifdef _WIN32
if (ret == SOCKET_ERROR)
return false;
#else
if (ret < 0)
return false;
#endif

*length = ret;

return true;
}

/**
* Send a message on a socket.
*
* @param[in] sock Socket descriptor.
* @param[in] buffer Buffer to send message from.
* @param[in,out] length Number of bytes to send. On success, the value gets
* updated with the actual number of bytes sent. The
* value is undefined on failure.
* @param[in] flags Flags to modify the function behaviour. Use bitwise OR to
* specify multiple flags.
* @param[in] address Destination address of the message.
* @param[in] address_length Length of the structure pointed to by @p address
* in bytes.
Expand Down
Loading

0 comments on commit 062a600

Please sign in to comment.