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 <unistd.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/safe_strerror_posix.h" |
| 15 #include "dbus/file_descriptor.h" |
| 16 #include "device/bluetooth/bluetooth_socket.h" |
| 17 #include "net/base/io_buffer.h" |
| 18 |
| 19 namespace chromeos { |
| 20 |
| 21 BluetoothSocketExperimentalChromeOS::BluetoothSocketExperimentalChromeOS( |
| 22 int fd) |
| 23 : fd_(fd) { |
| 24 } |
| 25 |
| 26 BluetoothSocketExperimentalChromeOS::~BluetoothSocketExperimentalChromeOS() { |
| 27 close(fd_); |
| 28 } |
| 29 |
| 30 bool BluetoothSocketExperimentalChromeOS::Receive( |
| 31 net::GrowableIOBuffer *buffer) { |
| 32 buffer->SetCapacity(1024); |
| 33 ssize_t bytes_read; |
| 34 do { |
| 35 if (buffer->RemainingCapacity() == 0) |
| 36 buffer->SetCapacity(buffer->capacity() * 2); |
| 37 bytes_read = read(fd_, buffer, buffer->RemainingCapacity()); |
| 38 if (bytes_read > 0) |
| 39 buffer->set_offset(buffer->offset() + bytes_read); |
| 40 } while (bytes_read > 0); |
| 41 |
| 42 if (bytes_read < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { |
| 43 error_message_ = safe_strerror(errno); |
| 44 return false; |
| 45 } |
| 46 return true; |
| 47 } |
| 48 |
| 49 bool BluetoothSocketExperimentalChromeOS::Send( |
| 50 net::DrainableIOBuffer *buffer) { |
| 51 ssize_t bytes_written; |
| 52 do { |
| 53 bytes_written = write(fd_, buffer, buffer->BytesRemaining()); |
| 54 if (bytes_written > 0) |
| 55 buffer->DidConsume(bytes_written); |
| 56 } while (buffer->BytesRemaining() > 0 && bytes_written > 0); |
| 57 |
| 58 if (bytes_written < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { |
| 59 error_message_ = safe_strerror(errno); |
| 60 return false; |
| 61 } |
| 62 return true; |
| 63 } |
| 64 |
| 65 std::string BluetoothSocketExperimentalChromeOS::GetLastErrorMessage() const { |
| 66 return error_message_; |
| 67 } |
| 68 |
| 69 // static |
| 70 scoped_refptr<device::BluetoothSocket> |
| 71 BluetoothSocketExperimentalChromeOS::Create(dbus::FileDescriptor* fd) { |
| 72 BluetoothSocketExperimentalChromeOS* bluetooth_socket = |
| 73 new BluetoothSocketExperimentalChromeOS(fd->TakeValue());; |
| 74 return scoped_refptr<BluetoothSocketExperimentalChromeOS>(bluetooth_socket); |
| 75 } |
| 76 |
| 77 } // namespace chromeos |
OLD | NEW |