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.
5 #include "device/bluetooth/bluetooth_socket_chromeos.h"
9 #include <bluetooth/bluetooth.h>
10 #include <bluetooth/rfcomm.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
17 #include "base/logging.h"
18 #include "device/bluetooth/bluetooth_service_record.h"
19 #include "device/bluetooth/bluetooth_utils.h"
21 using device::BluetoothServiceRecord
;
22 using device::BluetoothSocket
;
26 BluetoothSocketChromeOs::BluetoothSocketChromeOs(
27 const std::string
& address
, int fd
)
32 BluetoothSocketChromeOs::~BluetoothSocketChromeOs() {
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 device::bluetooth_utils::str2ba(service_record
.address(),
47 &socket_address
.rc_bdaddr
);
49 int status
= connect(socket_fd
, (struct sockaddr
*)&socket_address
,
50 sizeof(socket_address
));
52 if (status
== 0 || errno
== EINPROGRESS
) {
53 bluetooth_socket
= new BluetoothSocketChromeOs(service_record
.address(),
56 LOG(ERROR
) << "Failed to connect bluetooth socket "
57 << "(" << service_record
.address() << "): "
58 << "(" << errsv
<< ") " << strerror(errsv
);
62 // TODO(bryeung): add support for L2CAP sockets as well.
64 return scoped_refptr
<BluetoothSocketChromeOs
>(bluetooth_socket
);
67 int BluetoothSocketChromeOs::fd() const {
71 } // namespace chromeos