OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" | 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket_posix.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include <bluetooth/bluetooth.h> | 9 #include <bluetooth/bluetooth.h> |
10 #include <bluetooth/rfcomm.h> | 10 #include <bluetooth/rfcomm.h> |
11 #include <errno.h> | 11 #include <errno.h> |
12 #include <sys/socket.h> | 12 #include <sys/socket.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <string.h> | 14 #include <string.h> |
15 #include <unistd.h> | 15 #include <unistd.h> |
16 | 16 |
| 17 #include "base/logging.h" |
17 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" | 18 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" |
18 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" | 19 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" |
19 | 20 |
20 namespace chromeos { | 21 namespace chromeos { |
21 | 22 |
22 BluetoothSocket::BluetoothSocket(const std::string& address, int fd) | 23 BluetoothSocketPosix::BluetoothSocketPosix(const std::string& address, int fd) |
23 : address_(address), | 24 : address_(address), |
24 fd_(fd) { | 25 fd_(fd) { |
25 } | 26 } |
26 | 27 |
27 BluetoothSocket::~BluetoothSocket() { | 28 BluetoothSocketPosix::~BluetoothSocketPosix() { |
28 close(fd_); | 29 close(fd_); |
29 } | 30 } |
30 | 31 |
31 // static | 32 // static |
32 scoped_refptr<BluetoothSocket> BluetoothSocket::CreateBluetoothSocket( | 33 scoped_refptr<BluetoothSocket> BluetoothSocketPosix::CreateBluetoothSocket( |
33 const BluetoothServiceRecord& service_record) { | 34 const BluetoothServiceRecord& service_record) { |
34 BluetoothSocket* bluetooth_socket = NULL; | 35 BluetoothSocketPosix* bluetooth_socket = NULL; |
35 if (service_record.SupportsRfcomm()) { | 36 if (service_record.SupportsRfcomm()) { |
36 int socket_fd = socket( | 37 int socket_fd = socket( |
37 AF_BLUETOOTH, SOCK_STREAM | SOCK_NONBLOCK, BTPROTO_RFCOMM); | 38 AF_BLUETOOTH, SOCK_STREAM | SOCK_NONBLOCK, BTPROTO_RFCOMM); |
38 struct sockaddr_rc socket_address = { 0 }; | 39 struct sockaddr_rc socket_address = { 0 }; |
39 socket_address.rc_family = AF_BLUETOOTH; | 40 socket_address.rc_family = AF_BLUETOOTH; |
40 socket_address.rc_channel = service_record.rfcomm_channel(); | 41 socket_address.rc_channel = service_record.rfcomm_channel(); |
41 bluetooth_utils::str2ba(service_record.address(), | 42 bluetooth_utils::str2ba(service_record.address(), |
42 &socket_address.rc_bdaddr); | 43 &socket_address.rc_bdaddr); |
43 | 44 |
44 int status = connect(socket_fd, (struct sockaddr *)&socket_address, | 45 int status = connect(socket_fd, (struct sockaddr *)&socket_address, |
45 sizeof(socket_address)); | 46 sizeof(socket_address)); |
46 int errsv = errno; | 47 int errsv = errno; |
47 if (status == 0 || errno == EINPROGRESS) { | 48 if (status == 0 || errno == EINPROGRESS) { |
48 bluetooth_socket = new BluetoothSocket(service_record.address(), | 49 bluetooth_socket = new BluetoothSocketPosix(service_record.address(), |
49 socket_fd); | 50 socket_fd); |
50 } else { | 51 } else { |
51 LOG(ERROR) << "Failed to connect bluetooth socket " | 52 LOG(ERROR) << "Failed to connect bluetooth socket " |
52 << "(" << service_record.address() << "): " | 53 << "(" << service_record.address() << "): " |
53 << "(" << errsv << ") " << strerror(errsv); | 54 << "(" << errsv << ") " << strerror(errsv); |
54 close(socket_fd); | 55 close(socket_fd); |
55 } | 56 } |
56 } | 57 } |
57 // TODO(bryeung): add support for L2CAP sockets as well. | 58 // TODO(bryeung): add support for L2CAP sockets as well. |
58 | 59 |
59 return scoped_refptr<BluetoothSocket>(bluetooth_socket); | 60 return scoped_refptr<BluetoothSocketPosix>(bluetooth_socket); |
| 61 } |
| 62 |
| 63 int BluetoothSocketPosix::fd() const { |
| 64 return fd_; |
60 } | 65 } |
61 | 66 |
62 } // namespace chromeos | 67 } // namespace chromeos |
OLD | NEW |