diff options
author | Sharvil Nanavati <sharvil@google.com> | 2014-08-27 19:05:40 -0700 |
---|---|---|
committer | Andre Eisenbach <eisenbach@google.com> | 2015-03-16 16:51:30 -0700 |
commit | 98bf85fea392258ba22a252eeeed1153dc3eb16f (patch) | |
tree | 3cd62bc3f8fbba739f9193d30e56d2ba6b41b254 | |
parent | 56f3460b37bb8d2c1f2717e7977d5803b80481d1 (diff) | |
download | android_system_bt-98bf85fea392258ba22a252eeeed1153dc3eb16f.tar.gz android_system_bt-98bf85fea392258ba22a252eeeed1153dc3eb16f.tar.bz2 android_system_bt-98bf85fea392258ba22a252eeeed1153dc3eb16f.zip |
Add a method to check for available bytes on a socket
The available bytes function shouldn't normally be required (and
if it is, the code design is probably bad and should be rethought) but
is introduced as a stop-gap until we can redesign the BTA code that
expects a priori knowledge of the number of bytes that can be read from
the socket.
This change also relaxes the requirement that on socket_register that
at least one of read_cb or write_cb must be specified.
-rw-r--r-- | osi/include/socket.h | 14 | ||||
-rw-r--r-- | osi/src/socket.c | 12 |
2 files changed, 23 insertions, 3 deletions
diff --git a/osi/include/socket.h b/osi/include/socket.h index aadfd5bc3..e2d0888c1 100644 --- a/osi/include/socket.h +++ b/osi/include/socket.h @@ -18,8 +18,10 @@ #pragma once +#include <stdbool.h> #include <stddef.h> #include <stdint.h> +#include <sys/types.h> typedef struct reactor_t reactor_t; typedef struct socket_t socket_t; @@ -78,11 +80,19 @@ ssize_t socket_write(const socket_t *socket, const void *buf, size_t count); // If |fd| is INVALID_FD, this function behaves the same as |socket_write|. ssize_t socket_write_and_transfer_fd(const socket_t *socket, const void *buf, size_t count, int fd); +// Returns the number of bytes that can be read from |socket| without blocking. On error, +// this function returns -1. |socket| may not be NULL. +// +// Note: this function should not be part of the socket interface. It is only provided as +// a stop-gap until we can refactor away code that depends on a priori knowledge of +// the byte count. Do not use this function unless you need it while refactoring +// legacy bluedroid code. +ssize_t socket_bytes_available(const socket_t *socket); + // Registers |socket| with the |reactor|. When the socket becomes readable, |read_cb| // will be called. When the socket becomes writeable, |write_cb| will be called. The // |context| parameter is passed, untouched, to each of the callback routines. Neither -// |socket| nor |reactor| may be NULL. |read_cb| or |write_cb|, but not both, may be NULL. -// |context| may be NULL. +// |socket| nor |reactor| may be NULL. |read_cb|, |write_cb|, and |context| may be NULL. void socket_register(socket_t *socket, reactor_t *reactor, void *context, socket_cb read_cb, socket_cb write_cb); // Unregisters |socket| from whichever reactor it is registered with, if any. This diff --git a/osi/src/socket.c b/osi/src/socket.c index 687a67062..6f419767b 100644 --- a/osi/src/socket.c +++ b/osi/src/socket.c @@ -18,10 +18,12 @@ #define LOG_TAG "bt_osi_socket" +#include <asm/ioctls.h> #include <assert.h> #include <errno.h> #include <netinet/in.h> #include <string.h> +#include <sys/ioctl.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> @@ -179,9 +181,17 @@ ssize_t socket_write_and_transfer_fd(const socket_t *socket, const void *buf, si return ret; } +ssize_t socket_bytes_available(const socket_t *socket) { + assert(socket != NULL); + + int size = 0; + if (ioctl(socket->fd, FIONREAD, &size) == -1) + return -1; + return size; +} + void socket_register(socket_t *socket, reactor_t *reactor, void *context, socket_cb read_cb, socket_cb write_cb) { assert(socket != NULL); - assert(read_cb || write_cb); // Make sure the socket isn't currently registered. socket_unregister(socket); |