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 "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 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 |
| 15 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" |
| 16 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 BluetoothSocket::BluetoothSocket(const std::string& address, int fd) |
| 21 : address_(address), |
| 22 fd_(fd) { |
| 23 } |
| 24 |
| 25 BluetoothSocket::~BluetoothSocket() { |
| 26 close(fd_); |
| 27 } |
| 28 |
| 29 // static |
| 30 scoped_refptr<BluetoothSocket> BluetoothSocket::CreateBluetoothSocket( |
| 31 const BluetoothServiceRecord& service_record) { |
| 32 BluetoothSocket* bluetooth_socket = NULL; |
| 33 if (service_record.SupportsRfcomm()) { |
| 34 int socket_fd = socket( |
| 35 AF_BLUETOOTH, SOCK_STREAM | SOCK_NONBLOCK, BTPROTO_RFCOMM); |
| 36 struct sockaddr_rc socket_address = { 0 }; |
| 37 socket_address.rc_family = AF_BLUETOOTH; |
| 38 socket_address.rc_channel = service_record.rfcomm_channel(); |
| 39 bdaddr_t bluetooth_address; |
| 40 bluetooth_utils::str2ba(service_record.address(), &bluetooth_address); |
| 41 |
| 42 int status = connect(socket_fd, (struct sockaddr *)&socket_address, |
| 43 sizeof(socket_address)); |
| 44 if (status == 0 || errno == EINPROGRESS) |
| 45 bluetooth_socket = new BluetoothSocket(service_record.address(), |
| 46 socket_fd); |
| 47 else |
| 48 close(socket_fd); |
| 49 } else { |
| 50 // TODO(bryeung): add support for L2CAP sockets as well. |
| 51 return NULL; |
| 52 } |
| 53 |
| 54 return scoped_refptr<BluetoothSocket>(bluetooth_socket); |
| 55 } |
| 56 |
| 57 } // namespace chromeos |
OLD | NEW |