OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/bluetooth/bluetooth_socket_experimental_chromeos.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <poll.h> |
| 9 #include <unistd.h> |
| 10 #include <sys/ioctl.h> |
| 11 #include <sys/types.h> |
| 12 #include <sys/socket.h> |
| 13 |
| 14 #include <string> |
| 15 |
| 16 #include "base/logging.h" |
| 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/safe_strerror_posix.h" |
| 20 #include "base/threading/thread_restrictions.h" |
| 21 #include "dbus/file_descriptor.h" |
| 22 #include "device/bluetooth/bluetooth_socket.h" |
| 23 #include "net/base/io_buffer.h" |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 BluetoothSocketExperimentalChromeOS::BluetoothSocketExperimentalChromeOS( |
| 28 int fd) |
| 29 : fd_(fd) { |
| 30 // Fetch the socket type so we read from it correctly. |
| 31 int optval; |
| 32 socklen_t opt_len = sizeof optval; |
| 33 if (getsockopt(fd_, SOL_SOCKET, SO_TYPE, &optval, &opt_len) < 0) { |
| 34 // Sequenced packet is the safest assumption since it won't result in |
| 35 // truncated packets. |
| 36 LOG(WARNING) << "Unable to get socket type: " << safe_strerror(errno); |
| 37 optval = SOCK_SEQPACKET; |
| 38 } |
| 39 |
| 40 if (optval == SOCK_DGRAM || optval == SOCK_SEQPACKET) { |
| 41 socket_type_ = L2CAP; |
| 42 } else { |
| 43 socket_type_ = RFCOMM; |
| 44 } |
| 45 } |
| 46 |
| 47 BluetoothSocketExperimentalChromeOS::~BluetoothSocketExperimentalChromeOS() { |
| 48 HANDLE_EINTR(close(fd_)); |
| 49 } |
| 50 |
| 51 bool BluetoothSocketExperimentalChromeOS::Receive( |
| 52 net::GrowableIOBuffer *buffer) { |
| 53 base::ThreadRestrictions::AssertIOAllowed(); |
| 54 |
| 55 if (socket_type_ == L2CAP) { |
| 56 int count; |
| 57 if (ioctl(fd_, FIONREAD, &count) < 0) { |
| 58 error_message_ = safe_strerror(errno); |
| 59 LOG(WARNING) << "Unable to get waiting data size: " << error_message_; |
| 60 return true; |
| 61 } |
| 62 |
| 63 // No bytes waiting can mean either nothing to read, or the other end has |
| 64 // been closed, and reading zero bytes always returns zero. |
| 65 // |
| 66 // We can't do a short read for fear of a race where data arrives between |
| 67 // calls and we trunctate it. So use poll() to check for the POLLHUP flag. |
| 68 if (count == 0) { |
| 69 struct pollfd pollfd; |
| 70 |
| 71 pollfd.fd = fd_; |
| 72 pollfd.events = 0; |
| 73 pollfd.revents = 0; |
| 74 |
| 75 // Timeout parameter set to 0 so this call will not block. |
| 76 if (HANDLE_EINTR(poll(&pollfd, 1, 0)) < 0) { |
| 77 error_message_ = safe_strerror(errno); |
| 78 LOG(WARNING) << "Unable to check whether socket is closed: " |
| 79 << error_message_; |
| 80 return false; |
| 81 } |
| 82 |
| 83 if (pollfd.revents & POLLHUP) { |
| 84 // TODO(keybuk, youngki): Agree a common way to flag disconnected. |
| 85 error_message_ = "Disconnected"; |
| 86 return false; |
| 87 } |
| 88 } |
| 89 |
| 90 buffer->SetCapacity(count); |
| 91 } else { |
| 92 buffer->SetCapacity(1024); |
| 93 } |
| 94 |
| 95 ssize_t bytes_read; |
| 96 do { |
| 97 if (buffer->RemainingCapacity() == 0) |
| 98 buffer->SetCapacity(buffer->capacity() * 2); |
| 99 bytes_read = |
| 100 HANDLE_EINTR(read(fd_, buffer->data(), buffer->RemainingCapacity())); |
| 101 if (bytes_read > 0) |
| 102 buffer->set_offset(buffer->offset() + bytes_read); |
| 103 } while (socket_type_ == RFCOMM && bytes_read > 0); |
| 104 |
| 105 // Ignore an error if at least one read() call succeeded; it'll be returned |
| 106 // the next read() call. |
| 107 if (buffer->offset() > 0) |
| 108 return true; |
| 109 |
| 110 if (bytes_read < 0) { |
| 111 if (errno == ECONNRESET || errno == ENOTCONN) { |
| 112 // TODO(keybuk, youngki): Agree a common way to flag disconnected. |
| 113 error_message_ = "Disconnected"; |
| 114 return false; |
| 115 } else if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 116 error_message_ = safe_strerror(errno); |
| 117 return false; |
| 118 } |
| 119 } |
| 120 |
| 121 if (bytes_read == 0 && socket_type_ == RFCOMM) { |
| 122 // TODO(keybuk, youngki): Agree a common way to flag disconnected. |
| 123 error_message_ = "Disconnected"; |
| 124 return false; |
| 125 } |
| 126 |
| 127 return true; |
| 128 } |
| 129 |
| 130 bool BluetoothSocketExperimentalChromeOS::Send( |
| 131 net::DrainableIOBuffer *buffer) { |
| 132 base::ThreadRestrictions::AssertIOAllowed(); |
| 133 |
| 134 ssize_t bytes_written; |
| 135 do { |
| 136 bytes_written = |
| 137 HANDLE_EINTR(write(fd_, buffer->data(), buffer->BytesRemaining())); |
| 138 if (bytes_written > 0) |
| 139 buffer->DidConsume(bytes_written); |
| 140 } while (buffer->BytesRemaining() > 0 && bytes_written > 0); |
| 141 |
| 142 if (bytes_written < 0) { |
| 143 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) { |
| 144 // TODO(keybuk, youngki): Agree a common way to flag disconnected. |
| 145 error_message_ = "Disconnected"; |
| 146 return false; |
| 147 } else if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 148 error_message_ = safe_strerror(errno); |
| 149 return false; |
| 150 } |
| 151 } |
| 152 |
| 153 return true; |
| 154 } |
| 155 |
| 156 std::string BluetoothSocketExperimentalChromeOS::GetLastErrorMessage() const { |
| 157 return error_message_; |
| 158 } |
| 159 |
| 160 // static |
| 161 scoped_refptr<device::BluetoothSocket> |
| 162 BluetoothSocketExperimentalChromeOS::Create(dbus::FileDescriptor* fd) { |
| 163 DCHECK(fd->is_valid()); |
| 164 |
| 165 BluetoothSocketExperimentalChromeOS* bluetooth_socket = |
| 166 new BluetoothSocketExperimentalChromeOS(fd->TakeValue());; |
| 167 return scoped_refptr<BluetoothSocketExperimentalChromeOS>(bluetooth_socket); |
| 168 } |
| 169 |
| 170 } // namespace chromeos |
OLD | NEW |