| Index: device/bluetooth/bluetooth_socket_experimental_chromeos.cc
|
| diff --git a/device/bluetooth/bluetooth_socket_experimental_chromeos.cc b/device/bluetooth/bluetooth_socket_experimental_chromeos.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2fab26f88ef3820be4c43ff15d5d108967e34ef4
|
| --- /dev/null
|
| +++ b/device/bluetooth/bluetooth_socket_experimental_chromeos.cc
|
| @@ -0,0 +1,117 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "device/bluetooth/bluetooth_socket_experimental_chromeos.h"
|
| +
|
| +#include <errno.h>
|
| +#include <unistd.h>
|
| +#include <sys/ioctl.h>
|
| +#include <sys/types.h>
|
| +#include <sys/socket.h>
|
| +
|
| +#include <string>
|
| +
|
| +#include "base/logging.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/safe_strerror_posix.h"
|
| +#include "base/threading/thread_restrictions.h"
|
| +#include "dbus/file_descriptor.h"
|
| +#include "device/bluetooth/bluetooth_socket.h"
|
| +#include "net/base/io_buffer.h"
|
| +
|
| +namespace chromeos {
|
| +
|
| +BluetoothSocketExperimentalChromeOS::BluetoothSocketExperimentalChromeOS(
|
| + int fd)
|
| + : fd_(fd) {
|
| + // Fetch the socket type so we read from it correctly.
|
| + int optval;
|
| + socklen_t opt_len = sizeof optval;
|
| + if (getsockopt(fd_, SOL_SOCKET, SO_TYPE, &optval, &opt_len) < 0) {
|
| + // Sequenced packet is the safest assumption since it won't result in
|
| + // truncated packets.
|
| + LOG(WARNING) << "Unable to get socket type: " << safe_strerror(errno);
|
| + optval = SOCK_SEQPACKET;
|
| + }
|
| +
|
| + if (optval == SOCK_DGRAM || optval == SOCK_SEQPACKET) {
|
| + socket_type_ = L2CAP;
|
| + } else {
|
| + socket_type_ = RFCOMM;
|
| + }
|
| +}
|
| +
|
| +BluetoothSocketExperimentalChromeOS::~BluetoothSocketExperimentalChromeOS() {
|
| + close(fd_);
|
| +}
|
| +
|
| +bool BluetoothSocketExperimentalChromeOS::Receive(
|
| + net::GrowableIOBuffer *buffer) {
|
| + base::ThreadRestrictions::AssertIOAllowed();
|
| +
|
| + if (socket_type_ == L2CAP) {
|
| + int count;
|
| + if (ioctl(fd_, FIONREAD, &count) < 0) {
|
| + LOG(WARNING) << "Unable to get waiting data size: "
|
| + << safe_strerror(errno);
|
| + return true;
|
| + }
|
| +
|
| + // TODO(keybuk): verify what happens on a closed socket
|
| + // read(0) always returns (0)
|
| +
|
| + buffer->SetCapacity(count);
|
| + } else {
|
| + buffer->SetCapacity(1024);
|
| + }
|
| +
|
| + ssize_t bytes_read;
|
| + do {
|
| + if (buffer->RemainingCapacity() == 0)
|
| + buffer->SetCapacity(buffer->capacity() * 2);
|
| + bytes_read = read(fd_, buffer->data(), buffer->RemainingCapacity());
|
| + if (bytes_read > 0)
|
| + buffer->set_offset(buffer->offset() + bytes_read);
|
| + } while (socket_type_ == RFCOMM && bytes_read > 0);
|
| +
|
| + if (bytes_read < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
|
| + error_message_ = safe_strerror(errno);
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +bool BluetoothSocketExperimentalChromeOS::Send(
|
| + net::DrainableIOBuffer *buffer) {
|
| + base::ThreadRestrictions::AssertIOAllowed();
|
| +
|
| + ssize_t bytes_written;
|
| + do {
|
| + bytes_written = write(fd_, buffer->data(), 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 BluetoothSocketExperimentalChromeOS::GetLastErrorMessage() const {
|
| + return error_message_;
|
| +}
|
| +
|
| +// static
|
| +scoped_refptr<device::BluetoothSocket>
|
| +BluetoothSocketExperimentalChromeOS::Create(dbus::FileDescriptor* fd) {
|
| + DCHECK(fd->is_valid());
|
| +
|
| + BluetoothSocketExperimentalChromeOS* bluetooth_socket =
|
| + new BluetoothSocketExperimentalChromeOS(fd->TakeValue());;
|
| + return scoped_refptr<BluetoothSocketExperimentalChromeOS>(bluetooth_socket);
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|