OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/proximity_auth/bluetooth_util.h" | 5 #include "components/proximity_auth/bluetooth_util.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 struct SeekDeviceResult { | 62 struct SeekDeviceResult { |
63 // Whether the connection to the device succeeded. | 63 // Whether the connection to the device succeeded. |
64 bool success; | 64 bool success; |
65 | 65 |
66 // If the connection failed, an error message describing the failure. | 66 // If the connection failed, an error message describing the failure. |
67 std::string error_message; | 67 std::string error_message; |
68 }; | 68 }; |
69 | 69 |
70 // Writes |address| into the |result|. Return true on success, false if the | 70 // Writes |address| into the |result|. Return true on success, false if the |
71 // |address| is not a valid Bluetooth address. | 71 // |address| is not a valid Bluetooth address. |
72 bool BluetoothAddressToBdaddr(const std::string& address, | 72 bool BluetoothAddressToBdaddr(const std::string& address, bdaddr_t* result) { |
73 bdaddr_t* result) { | |
74 std::string canonical_address = BluetoothDevice::CanonicalizeAddress(address); | 73 std::string canonical_address = BluetoothDevice::CanonicalizeAddress(address); |
75 if (canonical_address.empty()) | 74 if (canonical_address.empty()) |
76 return false; | 75 return false; |
77 | 76 |
78 std::vector<std::string> octets; | 77 std::vector<std::string> octets; |
79 base::SplitString(canonical_address, ':', &octets); | 78 base::SplitString(canonical_address, ':', &octets); |
80 DCHECK_EQ(octets.size(), 6U); | 79 DCHECK_EQ(octets.size(), 6U); |
81 | 80 |
82 // BlueZ expects the octets in the reverse order. | 81 // BlueZ expects the octets in the reverse order. |
83 std::reverse(octets.begin(), octets.end()); | 82 std::reverse(octets.begin(), octets.end()); |
(...skipping 27 matching lines...) Expand all Loading... |
111 addr.l2_family = AF_BLUETOOTH; | 110 addr.l2_family = AF_BLUETOOTH; |
112 addr.l2_psm = base::ByteSwapToLE16(SDP_PSM); | 111 addr.l2_psm = base::ByteSwapToLE16(SDP_PSM); |
113 if (!BluetoothAddressToBdaddr(device_address, &addr.l2_bdaddr)) { | 112 if (!BluetoothAddressToBdaddr(device_address, &addr.l2_bdaddr)) { |
114 seek_result.error_message = kInvalidDeviceAddress; | 113 seek_result.error_message = kInvalidDeviceAddress; |
115 return seek_result; | 114 return seek_result; |
116 } | 115 } |
117 | 116 |
118 net::SocketDescriptor socket_descriptor = | 117 net::SocketDescriptor socket_descriptor = |
119 socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); | 118 socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); |
120 int result = connect(socket_descriptor, | 119 int result = connect(socket_descriptor, |
121 reinterpret_cast<struct sockaddr *>(&addr), | 120 reinterpret_cast<struct sockaddr*>(&addr), |
122 sizeof(addr)); | 121 sizeof(addr)); |
123 if (result == 0) { | 122 if (result == 0) { |
124 seek_result.success = true; | 123 seek_result.success = true; |
125 task_runner->PostDelayedTask( | 124 task_runner->PostDelayedTask( |
126 FROM_HERE, | 125 FROM_HERE, |
127 base::Bind(&CloseSocket, socket_descriptor), | 126 base::Bind(&CloseSocket, socket_descriptor), |
128 base::TimeDelta::FromSeconds(kCloseSDPConnectionDelaySec)); | 127 base::TimeDelta::FromSeconds(kCloseSDPConnectionDelaySec)); |
129 } else { | 128 } else { |
130 // TODO(isherman): Pass a better message based on the errno? | 129 // TODO(isherman): Pass a better message based on the errno? |
131 seek_result.error_message = kUnableToConnectToDevice; | 130 seek_result.error_message = kUnableToConnectToDevice; |
(...skipping 12 matching lines...) Expand all Loading... |
144 | 143 |
145 } // namespace | 144 } // namespace |
146 | 145 |
147 void SeekDeviceByAddress(const std::string& device_address, | 146 void SeekDeviceByAddress(const std::string& device_address, |
148 const base::Closure& callback, | 147 const base::Closure& callback, |
149 const ErrorCallback& error_callback, | 148 const ErrorCallback& error_callback, |
150 base::TaskRunner* task_runner) { | 149 base::TaskRunner* task_runner) { |
151 base::PostTaskAndReplyWithResult( | 150 base::PostTaskAndReplyWithResult( |
152 task_runner, | 151 task_runner, |
153 FROM_HERE, | 152 FROM_HERE, |
154 base::Bind(&SeekDeviceByAddressImpl, device_address, | 153 base::Bind(&SeekDeviceByAddressImpl, |
| 154 device_address, |
155 make_scoped_refptr(task_runner)), | 155 make_scoped_refptr(task_runner)), |
156 base::Bind(&OnSeekDeviceResult, callback, error_callback)); | 156 base::Bind(&OnSeekDeviceResult, callback, error_callback)); |
157 } | 157 } |
158 | 158 |
159 void ConnectToServiceInsecurely( | 159 void ConnectToServiceInsecurely( |
160 device::BluetoothDevice* device, | 160 device::BluetoothDevice* device, |
161 const device::BluetoothUUID& uuid, | 161 const device::BluetoothUUID& uuid, |
162 const BluetoothDevice::ConnectToServiceCallback& callback, | 162 const BluetoothDevice::ConnectToServiceCallback& callback, |
163 const BluetoothDevice::ConnectToServiceErrorCallback& error_callback) { | 163 const BluetoothDevice::ConnectToServiceErrorCallback& error_callback) { |
164 static_cast<chromeos::BluetoothDeviceChromeOS*>(device) | 164 static_cast<chromeos::BluetoothDeviceChromeOS*>(device) |
165 ->ConnectToServiceInsecurely(uuid, callback, error_callback); | 165 ->ConnectToServiceInsecurely(uuid, callback, error_callback); |
166 } | 166 } |
167 | 167 |
168 } // namespace bluetooth_util | 168 } // namespace bluetooth_util |
169 } // namespace proximity_auth | 169 } // namespace proximity_auth |
OLD | NEW |