Create a new-installs-only uniformity trial.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_socket_chromeos.cc
blob7e75a9dce29a34556b28409a9603120ca9b7b3e7
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"
7 #include <vector>
9 #include <bluetooth/bluetooth.h>
10 #include <bluetooth/rfcomm.h>
11 #include <errno.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
14 #include <string.h>
15 #include <unistd.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;
24 namespace chromeos {
26 BluetoothSocketChromeOs::BluetoothSocketChromeOs(
27 const std::string& address, int fd)
28 : address_(address),
29 fd_(fd) {
32 BluetoothSocketChromeOs::~BluetoothSocketChromeOs() {
33 close(fd_);
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 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));
51 int errsv = errno;
52 if (status == 0 || errno == EINPROGRESS) {
53 bluetooth_socket = new BluetoothSocketChromeOs(service_record.address(),
54 socket_fd);
55 } else {
56 LOG(ERROR) << "Failed to connect bluetooth socket "
57 << "(" << service_record.address() << "): "
58 << "(" << errsv << ") " << strerror(errsv);
59 close(socket_fd);
62 // TODO(bryeung): add support for L2CAP sockets as well.
64 return scoped_refptr<BluetoothSocketChromeOs>(bluetooth_socket);
67 int BluetoothSocketChromeOs::fd() const {
68 return fd_;
71 } // namespace chromeos