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 // static | |
keybuk
2012/04/19 23:30:11
usually these go at the end of the file rather tha
bryeung
2012/04/20 00:17:56
Done.
| |
21 scoped_refptr<BluetoothSocket> BluetoothSocket::CreateBluetoothSocket( | |
22 const BluetoothServiceRecord& service_record) { | |
23 BluetoothSocket* bluetooth_socket = NULL; | |
24 if (service_record.SupportsRfcomm()) { | |
25 int socket_fd = socket( | |
26 AF_BLUETOOTH, SOCK_STREAM | SOCK_NONBLOCK, BTPROTO_RFCOMM); | |
27 struct sockaddr_rc socket_address = { 0 }; | |
28 socket_address.rc_family = AF_BLUETOOTH; | |
29 socket_address.rc_channel = service_record.rfcomm_channel(); | |
30 bdaddr_t bluetooth_address; | |
31 bluetooth_utils::str2ba(service_record.address(), &bluetooth_address); | |
32 | |
33 int status = connect(socket_fd, (struct sockaddr *)&socket_address, | |
34 sizeof(socket_address)); | |
35 if (status == 0 || errno == EINPROGRESS) | |
36 bluetooth_socket = new BluetoothSocket(service_record.address(), | |
37 socket_fd); | |
38 else | |
39 close(socket_fd); | |
40 } else { | |
41 // TODO(bryeung): add support for L2CAP sockets as well. | |
42 return NULL; | |
43 } | |
44 | |
45 return scoped_refptr<BluetoothSocket>(bluetooth_socket); | |
46 } | |
47 | |
48 BluetoothSocket::BluetoothSocket(const std::string& address, int fd) | |
49 : address_(address), | |
50 fd_(fd) { | |
51 } | |
52 | |
53 BluetoothSocket::~BluetoothSocket() { | |
54 close(fd_); | |
55 } | |
56 | |
57 } // namespace chromeos | |
OLD | NEW |