From ce97f25e9ab038f8a96b6d9c9f1b9d247aeec11c Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 18 May 2009 00:39:41 +0200 Subject: Added usbmuxd_recv, usbmuxd_recv_timeout, usbmuxd_send, and usbmuxd_disconnect --- src/libusbmuxd.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/usbmuxd.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/libusbmuxd.c b/src/libusbmuxd.c index c8acbf8..4cd0a6d 100644 --- a/src/libusbmuxd.c +++ b/src/libusbmuxd.c @@ -200,3 +200,49 @@ int usbmuxd_connect(const int handle, const unsigned short tcp_port) return -1; } + +int usbmuxd_disconnect(int sfd) +{ + return close(sfd); +} + +int usbmuxd_send(int sfd, const char *data, uint32_t len, uint32_t *sent_bytes) +{ + int num_sent; + + if (sfd < 0) { + return -EINVAL; + } + + num_sent = send(sfd, (void*)data, len, 0); + if (num_sent < 0) { + *sent_bytes = 0; + fprintf(stderr, "%s: Error %d when sending: %s\n", __func__, num_sent, strerror(errno)); + return num_sent; + } else if ((uint32_t)num_sent < len) { + fprintf(stderr, "%s: Warning: Did not send enough (only %d of %d)\n", __func__, num_sent, len); + } + + *sent_bytes = num_sent; + + return 0; +} + +int usbmuxd_recv_timeout(int sfd, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout) +{ + int num_recv = recv_buf_timeout(sfd, (void*)data, len, 0, timeout); + if (num_recv < 0) { + *recv_bytes = 0; + return num_recv; + } + + *recv_bytes = num_recv; + + return 0; +} + +int usbmuxd_recv(int sfd, char *data, uint32_t len, uint32_t *recv_bytes) +{ + return usbmuxd_recv_timeout(sfd, data, len, recv_bytes, 5000); +} + diff --git a/src/usbmuxd.h b/src/usbmuxd.h index 15e97ee..ba45ec3 100644 --- a/src/usbmuxd.h +++ b/src/usbmuxd.h @@ -42,4 +42,50 @@ int usbmuxd_scan(usbmuxd_scan_result **available_devices); */ int usbmuxd_connect(const int handle, const unsigned short tcp_port); +/** + * Disconnect. For now, this just closes the socket file descriptor. + * + * @param sfd socker file descriptor returned by usbmuxd_connect() + * + * @return 0 on success, -1 on error. + */ +int usbmuxd_disconnect(int sfd); + +/** + * Send data to the specified socket. + * + * @param sfd socket file descriptor returned by usbmuxd_connect() + * @param data buffer to send + * @param len size of buffer to send + * @param sent_bytes how many bytes sent + * + * @return 0 on success, a negative errno value otherwise. + */ +int usbmuxd_send(int sfd, const char *data, uint32_t len, uint32_t *sent_bytes); + +/** + * Receive data from the specified socket. + * + * @param sfd socket file descriptor returned by usbmuxd_connect() + * @param data buffer to put the data to + * @param len number of bytes to receive + * @param recv_bytes number of bytes received + * @param timeout how many milliseconds to wait for data + * + * @return 0 on success, a negative errno value otherwise. + */ +int usbmuxd_recv_timeout(int sfd, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout); + +/** + * Receive data from the specified socket with a default timeout. + * + * @param sfd socket file descriptor returned by usbmuxd_connect() + * @param data buffer to put the data to + * @param len number of bytes to receive + * @param recv_bytes number of bytes received + * + * @return 0 on success, a negative errno value otherwise. + */ +int usbmuxd_recv(int sfd, char *data, uint32_t len, uint32_t *recv_bytes); + #endif /* __USBMUXD_H */ -- cgit v1.1-32-gdbae