| 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_chromeos.h" | 5 #include "device/bluetooth/bluetooth_socket_chromeos.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 "base/logging.h" |
| 18 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" | 18 #include "device/bluetooth/bluetooth_service_record.h" |
| 19 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" | 19 #include "device/bluetooth/bluetooth_utils.h" |
| 20 |
| 21 using device::BluetoothServiceRecord; |
| 22 using device::BluetoothSocket; |
| 20 | 23 |
| 21 namespace chromeos { | 24 namespace chromeos { |
| 22 | 25 |
| 23 BluetoothSocketChromeOs::BluetoothSocketChromeOs( | 26 BluetoothSocketChromeOs::BluetoothSocketChromeOs( |
| 24 const std::string& address, int fd) | 27 const std::string& address, int fd) |
| 25 : address_(address), | 28 : address_(address), |
| 26 fd_(fd) { | 29 fd_(fd) { |
| 27 } | 30 } |
| 28 | 31 |
| 29 BluetoothSocketChromeOs::~BluetoothSocketChromeOs() { | 32 BluetoothSocketChromeOs::~BluetoothSocketChromeOs() { |
| 30 close(fd_); | 33 close(fd_); |
| 31 } | 34 } |
| 32 | 35 |
| 33 // static | 36 // static |
| 34 scoped_refptr<BluetoothSocket> BluetoothSocketChromeOs::CreateBluetoothSocket( | 37 scoped_refptr<BluetoothSocket> BluetoothSocketChromeOs::CreateBluetoothSocket( |
| 35 const BluetoothServiceRecord& service_record) { | 38 const BluetoothServiceRecord& service_record) { |
| 36 BluetoothSocketChromeOs* bluetooth_socket = NULL; | 39 BluetoothSocketChromeOs* bluetooth_socket = NULL; |
| 37 if (service_record.SupportsRfcomm()) { | 40 if (service_record.SupportsRfcomm()) { |
| 38 int socket_fd = socket( | 41 int socket_fd = socket( |
| 39 AF_BLUETOOTH, SOCK_STREAM | SOCK_NONBLOCK, BTPROTO_RFCOMM); | 42 AF_BLUETOOTH, SOCK_STREAM | SOCK_NONBLOCK, BTPROTO_RFCOMM); |
| 40 struct sockaddr_rc socket_address = { 0 }; | 43 struct sockaddr_rc socket_address = { 0 }; |
| 41 socket_address.rc_family = AF_BLUETOOTH; | 44 socket_address.rc_family = AF_BLUETOOTH; |
| 42 socket_address.rc_channel = service_record.rfcomm_channel(); | 45 socket_address.rc_channel = service_record.rfcomm_channel(); |
| 43 bluetooth_utils::str2ba(service_record.address(), | 46 device::bluetooth_utils::str2ba(service_record.address(), |
| 44 &socket_address.rc_bdaddr); | 47 &socket_address.rc_bdaddr); |
| 45 | 48 |
| 46 int status = connect(socket_fd, (struct sockaddr *)&socket_address, | 49 int status = connect(socket_fd, (struct sockaddr *)&socket_address, |
| 47 sizeof(socket_address)); | 50 sizeof(socket_address)); |
| 48 int errsv = errno; | 51 int errsv = errno; |
| 49 if (status == 0 || errno == EINPROGRESS) { | 52 if (status == 0 || errno == EINPROGRESS) { |
| 50 bluetooth_socket = new BluetoothSocketChromeOs(service_record.address(), | 53 bluetooth_socket = new BluetoothSocketChromeOs(service_record.address(), |
| 51 socket_fd); | 54 socket_fd); |
| 52 } else { | 55 } else { |
| 53 LOG(ERROR) << "Failed to connect bluetooth socket " | 56 LOG(ERROR) << "Failed to connect bluetooth socket " |
| 54 << "(" << service_record.address() << "): " | 57 << "(" << service_record.address() << "): " |
| 55 << "(" << errsv << ") " << strerror(errsv); | 58 << "(" << errsv << ") " << strerror(errsv); |
| 56 close(socket_fd); | 59 close(socket_fd); |
| 57 } | 60 } |
| 58 } | 61 } |
| 59 // TODO(bryeung): add support for L2CAP sockets as well. | 62 // TODO(bryeung): add support for L2CAP sockets as well. |
| 60 | 63 |
| 61 return scoped_refptr<BluetoothSocketChromeOs>(bluetooth_socket); | 64 return scoped_refptr<BluetoothSocketChromeOs>(bluetooth_socket); |
| 62 } | 65 } |
| 63 | 66 |
| 64 int BluetoothSocketChromeOs::fd() const { | 67 int BluetoothSocketChromeOs::fd() const { |
| 65 return fd_; | 68 return fd_; |
| 66 } | 69 } |
| 67 | 70 |
| 68 } // namespace chromeos | 71 } // namespace chromeos |
| OLD | NEW |