diff options
author | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-29 23:29:50 +0000 |
---|---|---|
committer | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-29 23:29:50 +0000 |
commit | 0fd66b503787c5982a2de8dfe8cb8b3b50024310 (patch) | |
tree | e9eac3a4890c39669c0cb0e763d1097a52fdc542 /device | |
parent | cd684ce6d022c4bb5cc3ba9e9a3a07df4813856d (diff) | |
download | chromium_src-0fd66b503787c5982a2de8dfe8cb8b3b50024310.zip chromium_src-0fd66b503787c5982a2de8dfe8cb8b3b50024310.tar.gz chromium_src-0fd66b503787c5982a2de8dfe8cb8b3b50024310.tar.bz2 |
Implemented BluetoothSocket::Receive() and BluetoothSocket::Write() on ChromeOS.
This made bluetooth_api.cc platform-independent.
BUG=135470
Review URL: https://chromiumcodereview.appspot.com/12039075
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@179459 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device')
-rw-r--r-- | device/bluetooth/DEPS | 1 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_socket.h | 21 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_socket_chromeos.cc | 48 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_socket_chromeos.h | 11 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_socket_win.cc | 15 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_socket_win.h | 13 | ||||
-rw-r--r-- | device/bluetooth/test/mock_bluetooth_socket.h | 12 | ||||
-rw-r--r-- | device/device.gyp | 1 |
8 files changed, 118 insertions, 4 deletions
diff --git a/device/bluetooth/DEPS b/device/bluetooth/DEPS index 726572a..7f93e42 100644 --- a/device/bluetooth/DEPS +++ b/device/bluetooth/DEPS @@ -2,6 +2,7 @@ include_rules = [ "+chromeos/dbus", "+dbus", "+grit", + "+net/base", "+ui/base/l10n", "+third_party/cros_system_api/dbus", "+third_party/libxml/chromium", diff --git a/device/bluetooth/bluetooth_socket.h b/device/bluetooth/bluetooth_socket.h index b444110..bb3ab31 100644 --- a/device/bluetooth/bluetooth_socket.h +++ b/device/bluetooth/bluetooth_socket.h @@ -5,8 +5,17 @@ #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ +#include <string> + #include "base/memory/ref_counted.h" +namespace net { + +class DrainableIOBuffer; +class GrowableIOBuffer; + +} // namespace net + namespace device { // BluetoothSocket represents a socket to a specific service on a @@ -20,6 +29,18 @@ class BluetoothSocket : public base::RefCounted<BluetoothSocket> { // linux-specific hence this method has to be renamed. virtual int fd() const = 0; + // Receives data from the socket and stores it in |buffer|. It returns whether + // the reception has been successful. If it fails, the caller can get the + // error message through |GetLastErrorMessage()|. + virtual bool Receive(net::GrowableIOBuffer* buffer) = 0; + + // Sends |buffer| to the socket. It returns whether the sending has been + // successful. If it fails, the caller can get the error message through + // |GetLastErrorMessage()|. + virtual bool Send(net::DrainableIOBuffer* buffer) = 0; + + virtual std::string GetLastErrorMessage() const = 0; + protected: friend class base::RefCounted<BluetoothSocket>; virtual ~BluetoothSocket() {} diff --git a/device/bluetooth/bluetooth_socket_chromeos.cc b/device/bluetooth/bluetooth_socket_chromeos.cc index 7e75a9d..44506fa 100644 --- a/device/bluetooth/bluetooth_socket_chromeos.cc +++ b/device/bluetooth/bluetooth_socket_chromeos.cc @@ -4,19 +4,22 @@ #include "device/bluetooth/bluetooth_socket_chromeos.h" -#include <vector> +#include <errno.h> +#include <string.h> +#include <unistd.h> + +#include <string> #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> -#include <errno.h> #include <sys/socket.h> #include <sys/types.h> -#include <string.h> -#include <unistd.h> #include "base/logging.h" +#include "base/safe_strerror_posix.h" #include "device/bluetooth/bluetooth_service_record.h" #include "device/bluetooth/bluetooth_utils.h" +#include "net/base/io_buffer.h" using device::BluetoothServiceRecord; using device::BluetoothSocket; @@ -68,4 +71,41 @@ int BluetoothSocketChromeOs::fd() const { return fd_; } +bool BluetoothSocketChromeOs::Receive(net::GrowableIOBuffer* buffer) { + buffer->SetCapacity(1024); + ssize_t bytes_read; + do { + if (buffer->RemainingCapacity() == 0) + buffer->SetCapacity(buffer->capacity() * 2); + bytes_read = read(fd_, buffer, buffer->RemainingCapacity()); + if (bytes_read > 0) + buffer->set_offset(buffer->offset() + bytes_read); + } while (bytes_read > 0); + + if (bytes_read < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { + error_message_ = safe_strerror(errno); + return false; + } + return true; +} + +bool BluetoothSocketChromeOs::Send(net::DrainableIOBuffer* buffer) { + ssize_t bytes_written; + do { + bytes_written = write(fd_, buffer, buffer->BytesRemaining()); + if (bytes_written > 0) + buffer->DidConsume(bytes_written); + } while (buffer->BytesRemaining() > 0 && bytes_written > 0); + + if (bytes_written < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { + error_message_ = safe_strerror(errno); + return false; + } + return true; +} + +std::string BluetoothSocketChromeOs::GetLastErrorMessage() const { + return error_message_; +} + } // namespace chromeos diff --git a/device/bluetooth/bluetooth_socket_chromeos.h b/device/bluetooth/bluetooth_socket_chromeos.h index 0685830..8d4b84a 100644 --- a/device/bluetooth/bluetooth_socket_chromeos.h +++ b/device/bluetooth/bluetooth_socket_chromeos.h @@ -16,6 +16,13 @@ class BluetoothServiceRecord; } // namespace device +namespace net { + +class DrainableIOBuffer; +class GrowableIOBuffer; + +} // namespace net + namespace chromeos { // This class is an implementation of BluetoothSocket class for Chrome OS @@ -27,6 +34,9 @@ class BluetoothSocketChromeOs : public device::BluetoothSocket { // BluetoothSocket override virtual int fd() const OVERRIDE; + virtual bool Receive(net::GrowableIOBuffer* buffer) OVERRIDE; + virtual bool Send(net::DrainableIOBuffer* buffer) OVERRIDE; + virtual std::string GetLastErrorMessage() const OVERRIDE; protected: virtual ~BluetoothSocketChromeOs(); @@ -36,6 +46,7 @@ class BluetoothSocketChromeOs : public device::BluetoothSocket { const std::string address_; const int fd_; + std::string error_message_; DISALLOW_COPY_AND_ASSIGN(BluetoothSocketChromeOs); }; diff --git a/device/bluetooth/bluetooth_socket_win.cc b/device/bluetooth/bluetooth_socket_win.cc index ab83a1f..dae1648 100644 --- a/device/bluetooth/bluetooth_socket_win.cc +++ b/device/bluetooth/bluetooth_socket_win.cc @@ -6,7 +6,10 @@ #include "device/bluetooth/bluetooth_socket_win.h" +#include <string> + #include "base/logging.h" +#include "net/base/io_buffer.h" namespace device { @@ -21,4 +24,16 @@ int BluetoothSocketWin::fd() const { return -1; } +bool BluetoothSocketWin::Receive(net::GrowableIOBuffer* buffer) { + return false; +} + +bool BluetoothSocketWin::Send(net::DrainableIOBuffer* buffer) { + return false; +} + +std::string BluetoothSocketWin::GetLastErrorMessage() const { + return ""; +} + } // namespace device diff --git a/device/bluetooth/bluetooth_socket_win.h b/device/bluetooth/bluetooth_socket_win.h index e9fe120..78bd78f 100644 --- a/device/bluetooth/bluetooth_socket_win.h +++ b/device/bluetooth/bluetooth_socket_win.h @@ -5,8 +5,17 @@ #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_WIN_H_ +#include <string> + #include "device/bluetooth/bluetooth_socket.h" +namespace net { + +class DrainableIOBuffer; +class GrowableIOBuffer; + +} // namespace net + namespace device { class BluetoothSocketWin : public BluetoothSocket { @@ -14,6 +23,10 @@ class BluetoothSocketWin : public BluetoothSocket { // BluetoothSocket override virtual int fd() const OVERRIDE; + virtual bool Receive(net::GrowableIOBuffer* buffer) OVERRIDE; + virtual bool Send(net::DrainableIOBuffer* buffer) OVERRIDE; + virtual std::string GetLastErrorMessage() const OVERRIDE; + private: BluetoothSocketWin(); virtual ~BluetoothSocketWin(); diff --git a/device/bluetooth/test/mock_bluetooth_socket.h b/device/bluetooth/test/mock_bluetooth_socket.h index a2a287f..8b5e1e2 100644 --- a/device/bluetooth/test/mock_bluetooth_socket.h +++ b/device/bluetooth/test/mock_bluetooth_socket.h @@ -5,15 +5,27 @@ #ifndef DEVICE_BLUETOOTH_TEST_MOCK_BLUETOOTH_SOCKET_H_ #define DEVICE_BLUETOOTH_TEST_MOCK_BLUETOOTH_SOCKET_H_ +#include <string> + #include "device/bluetooth/bluetooth_socket.h" #include "testing/gmock/include/gmock/gmock.h" +namespace net { + +class DrainableIOBuffer; +class GrowableIOBuffer; + +} // namespace net + namespace device { class MockBluetoothSocket : public BluetoothSocket { public: MockBluetoothSocket(); MOCK_CONST_METHOD0(fd, int()); + MOCK_METHOD1(Receive, bool(net::GrowableIOBuffer*)); + MOCK_METHOD1(Send, bool(net::DrainableIOBuffer*)); + MOCK_CONST_METHOD0(GetLastErrorMessage, std::string()); protected: virtual ~MockBluetoothSocket(); diff --git a/device/device.gyp b/device/device.gyp index 8537eee..4ef03bc 100644 --- a/device/device.gyp +++ b/device/device.gyp @@ -14,6 +14,7 @@ '../base/base.gyp:base', '../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', '../chrome/chrome_resources.gyp:chrome_strings', + '../net/net.gyp:net', '../third_party/libxml/libxml.gyp:libxml', '../ui/ui.gyp:ui' ], |