OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_chromeos.h" | |
6 | |
7 #include <errno.h> | |
8 #include <string.h> | |
9 #include <unistd.h> | |
10 | |
11 #include <string> | |
12 | |
13 #include <bluetooth/bluetooth.h> | |
14 #include <bluetooth/rfcomm.h> | |
15 #include <sys/socket.h> | |
16 #include <sys/types.h> | |
17 | |
18 #include "base/logging.h" | |
19 #include "base/safe_strerror_posix.h" | |
20 #include "device/bluetooth/bluetooth_service_record_chromeos.h" | |
21 #include "device/bluetooth/bluetooth_utils.h" | |
22 #include "net/base/io_buffer.h" | |
23 | |
24 using device::BluetoothServiceRecord; | |
25 using device::BluetoothSocket; | |
26 | |
27 namespace chromeos { | |
28 | |
29 BluetoothSocketChromeOS::BluetoothSocketChromeOS(int fd) : fd_(fd) { | |
30 } | |
31 | |
32 BluetoothSocketChromeOS::~BluetoothSocketChromeOS() { | |
33 close(fd_); | |
34 } | |
35 | |
36 // static | |
37 scoped_refptr<BluetoothSocket> BluetoothSocketChromeOS::CreateBluetoothSocket( | |
38 const BluetoothServiceRecord& service_record) { | |
39 BluetoothSocketChromeOS* bluetooth_socket = NULL; | |
40 if (service_record.SupportsRfcomm()) { | |
41 int socket_fd = socket( | |
42 AF_BLUETOOTH, SOCK_STREAM | SOCK_NONBLOCK, BTPROTO_RFCOMM); | |
43 struct sockaddr_rc socket_address = { 0 }; | |
44 socket_address.rc_family = AF_BLUETOOTH; | |
45 socket_address.rc_channel = service_record.rfcomm_channel(); | |
46 const BluetoothServiceRecordChromeOS* service_record_chromeos = | |
47 static_cast<const BluetoothServiceRecordChromeOS*>(&service_record); | |
48 service_record_chromeos->GetBluetoothAddress(&socket_address.rc_bdaddr); | |
49 | |
50 int status = connect(socket_fd, (struct sockaddr *)&socket_address, | |
51 sizeof(socket_address)); | |
52 int errsv = errno; | |
53 if (status == 0 || errno == EINPROGRESS) { | |
54 bluetooth_socket = new BluetoothSocketChromeOS(socket_fd); | |
55 } else { | |
56 LOG(ERROR) << "Failed to connect bluetooth socket " | |
57 << "(" << service_record.address() << "): " | |
58 << "(" << errsv << ") " << strerror(errsv); | |
59 close(socket_fd); | |
60 } | |
61 } | |
62 // TODO(bryeung): add support for L2CAP sockets as well. | |
63 | |
64 return scoped_refptr<BluetoothSocketChromeOS>(bluetooth_socket); | |
65 } | |
66 | |
67 bool BluetoothSocketChromeOS::Receive(net::GrowableIOBuffer* buffer) { | |
68 buffer->SetCapacity(1024); | |
69 ssize_t bytes_read; | |
70 do { | |
71 if (buffer->RemainingCapacity() == 0) | |
72 buffer->SetCapacity(buffer->capacity() * 2); | |
73 bytes_read = read(fd_, buffer->data(), buffer->RemainingCapacity()); | |
74 if (bytes_read > 0) | |
75 buffer->set_offset(buffer->offset() + bytes_read); | |
76 } while (bytes_read > 0); | |
77 | |
78 if (bytes_read < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { | |
79 error_message_ = safe_strerror(errno); | |
80 return false; | |
81 } | |
82 return true; | |
83 } | |
84 | |
85 bool BluetoothSocketChromeOS::Send(net::DrainableIOBuffer* buffer) { | |
86 ssize_t bytes_written; | |
87 do { | |
88 bytes_written = write(fd_, buffer->data(), buffer->BytesRemaining()); | |
89 if (bytes_written > 0) | |
90 buffer->DidConsume(bytes_written); | |
91 } while (buffer->BytesRemaining() > 0 && bytes_written > 0); | |
92 | |
93 if (bytes_written < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { | |
94 error_message_ = safe_strerror(errno); | |
95 return false; | |
96 } | |
97 return true; | |
98 } | |
99 | |
100 std::string BluetoothSocketChromeOS::GetLastErrorMessage() const { | |
101 return error_message_; | |
102 } | |
103 | |
104 } // namespace chromeos | |
OLD | NEW |