OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "device/bluetooth/bluez/bluetooth_device_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/optional.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "dbus/bus.h" | 17 #include "dbus/bus.h" |
17 #include "device/bluetooth/bluetooth_socket.h" | 18 #include "device/bluetooth/bluetooth_socket.h" |
18 #include "device/bluetooth/bluetooth_socket_thread.h" | 19 #include "device/bluetooth/bluetooth_socket_thread.h" |
19 #include "device/bluetooth/bluetooth_uuid.h" | 20 #include "device/bluetooth/bluetooth_uuid.h" |
20 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 21 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
21 #include "device/bluetooth/bluez/bluetooth_gatt_connection_bluez.h" | 22 #include "device/bluetooth/bluez/bluetooth_gatt_connection_bluez.h" |
22 #include "device/bluetooth/bluez/bluetooth_pairing_bluez.h" | 23 #include "device/bluetooth/bluez/bluetooth_pairing_bluez.h" |
23 #include "device/bluetooth/bluez/bluetooth_remote_gatt_service_bluez.h" | 24 #include "device/bluetooth/bluez/bluetooth_remote_gatt_service_bluez.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) { | 131 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) { |
131 error_code = BluetoothDevice::ERROR_AUTH_CANCELED; | 132 error_code = BluetoothDevice::ERROR_AUTH_CANCELED; |
132 } else if (error_name == bluetooth_device::kErrorAuthenticationRejected) { | 133 } else if (error_name == bluetooth_device::kErrorAuthenticationRejected) { |
133 error_code = BluetoothDevice::ERROR_AUTH_REJECTED; | 134 error_code = BluetoothDevice::ERROR_AUTH_REJECTED; |
134 } else if (error_name == bluetooth_device::kErrorAuthenticationTimeout) { | 135 } else if (error_name == bluetooth_device::kErrorAuthenticationTimeout) { |
135 error_code = BluetoothDevice::ERROR_AUTH_TIMEOUT; | 136 error_code = BluetoothDevice::ERROR_AUTH_TIMEOUT; |
136 } | 137 } |
137 return error_code; | 138 return error_code; |
138 } | 139 } |
139 | 140 |
| 141 base::Optional<int8_t> EnsureValidPower(int16_t power) { |
| 142 return ((power < INT8_MIN) || (power > INT8_MAX)) ? base::Optional<int8_t>() |
| 143 : power; |
| 144 } |
| 145 |
140 } // namespace | 146 } // namespace |
141 | 147 |
142 namespace bluez { | 148 namespace bluez { |
143 | 149 |
144 BluetoothDeviceBlueZ::BluetoothDeviceBlueZ( | 150 BluetoothDeviceBlueZ::BluetoothDeviceBlueZ( |
145 BluetoothAdapterBlueZ* adapter, | 151 BluetoothAdapterBlueZ* adapter, |
146 const dbus::ObjectPath& object_path, | 152 const dbus::ObjectPath& object_path, |
147 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 153 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
148 scoped_refptr<device::BluetoothSocketThread> socket_thread) | 154 scoped_refptr<device::BluetoothSocketThread> socket_thread) |
149 : BluetoothDevice(adapter), | 155 : BluetoothDevice(adapter), |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 const std::vector<std::string>& dbus_uuids = properties->uuids.value(); | 315 const std::vector<std::string>& dbus_uuids = properties->uuids.value(); |
310 for (std::vector<std::string>::const_iterator iter = dbus_uuids.begin(); | 316 for (std::vector<std::string>::const_iterator iter = dbus_uuids.begin(); |
311 iter != dbus_uuids.end(); ++iter) { | 317 iter != dbus_uuids.end(); ++iter) { |
312 device::BluetoothUUID uuid(*iter); | 318 device::BluetoothUUID uuid(*iter); |
313 DCHECK(uuid.IsValid()); | 319 DCHECK(uuid.IsValid()); |
314 uuids.push_back(uuid); | 320 uuids.push_back(uuid); |
315 } | 321 } |
316 return uuids; | 322 return uuids; |
317 } | 323 } |
318 | 324 |
319 int16_t BluetoothDeviceBlueZ::GetInquiryRSSI() const { | 325 base::Optional<int8_t> BluetoothDeviceBlueZ::GetInquiryRSSI() const { |
320 bluez::BluetoothDeviceClient::Properties* properties = | 326 bluez::BluetoothDeviceClient::Properties* properties = |
321 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 327 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
322 object_path_); | 328 object_path_); |
323 DCHECK(properties); | 329 DCHECK(properties); |
324 | 330 |
325 if (!properties->rssi.is_valid()) | 331 if (!properties->rssi.is_valid()) |
326 return kUnknownPower; | 332 return base::Optional<int8_t>(); |
327 | 333 |
328 return properties->rssi.value(); | 334 return EnsureValidPower(properties->rssi.value()); |
329 } | 335 } |
330 | 336 |
331 int16_t BluetoothDeviceBlueZ::GetInquiryTxPower() const { | 337 base::Optional<int8_t> BluetoothDeviceBlueZ::GetInquiryTxPower() const { |
332 bluez::BluetoothDeviceClient::Properties* properties = | 338 bluez::BluetoothDeviceClient::Properties* properties = |
333 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 339 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
334 object_path_); | 340 object_path_); |
335 DCHECK(properties); | 341 DCHECK(properties); |
336 | 342 |
337 if (!properties->tx_power.is_valid()) | 343 if (!properties->tx_power.is_valid()) |
338 return kUnknownPower; | 344 return base::Optional<int8_t>(); |
339 | 345 |
340 return properties->tx_power.value(); | 346 return EnsureValidPower(properties->tx_power.value()); |
341 } | 347 } |
342 | 348 |
343 bool BluetoothDeviceBlueZ::ExpectingPinCode() const { | 349 bool BluetoothDeviceBlueZ::ExpectingPinCode() const { |
344 return pairing_.get() && pairing_->ExpectingPinCode(); | 350 return pairing_.get() && pairing_->ExpectingPinCode(); |
345 } | 351 } |
346 | 352 |
347 bool BluetoothDeviceBlueZ::ExpectingPasskey() const { | 353 bool BluetoothDeviceBlueZ::ExpectingPasskey() const { |
348 return pairing_.get() && pairing_->ExpectingPasskey(); | 354 return pairing_.get() && pairing_->ExpectingPasskey(); |
349 } | 355 } |
350 | 356 |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 << "' from device: " << GetAddress(); | 593 << "' from device: " << GetAddress(); |
588 | 594 |
589 DCHECK(service->object_path() == object_path); | 595 DCHECK(service->object_path() == object_path); |
590 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 596 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
591 gatt_services_.take_and_erase(iter->first); | 597 gatt_services_.take_and_erase(iter->first); |
592 | 598 |
593 DCHECK(adapter_); | 599 DCHECK(adapter_); |
594 adapter()->NotifyGattServiceRemoved(service); | 600 adapter()->NotifyGattServiceRemoved(service); |
595 } | 601 } |
596 | 602 |
597 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, | 603 void BluetoothDeviceBlueZ::OnGetConnInfo( |
598 int16_t rssi, | 604 const ConnectionInfoCallback& callback, |
599 int16_t transmit_power, | 605 const base::Optional<int16_t>& rssi, |
600 int16_t max_transmit_power) { | 606 const base::Optional<int16_t>& transmit_power, |
601 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); | 607 const base::Optional<int16_t>& max_transmit_power) { |
| 608 if (!rssi) { |
| 609 DCHECK(!transmit_power); |
| 610 DCHECK(!max_transmit_power); |
| 611 callback.Run(ConnectionInfo()); |
| 612 } |
| 613 |
| 614 callback.Run(ConnectionInfo(EnsureValidPower(rssi.value()), |
| 615 EnsureValidPower(transmit_power.value()), |
| 616 EnsureValidPower(max_transmit_power.value()))); |
602 } | 617 } |
603 | 618 |
604 void BluetoothDeviceBlueZ::OnGetConnInfoError( | 619 void BluetoothDeviceBlueZ::OnGetConnInfoError( |
605 const ConnectionInfoCallback& callback, | 620 const ConnectionInfoCallback& callback, |
606 const std::string& error_name, | 621 const std::string& error_name, |
607 const std::string& error_message) { | 622 const std::string& error_message) { |
608 LOG(WARNING) << object_path_.value() | 623 LOG(WARNING) << object_path_.value() |
609 << ": Failed to get connection info: " << error_name << ": " | 624 << ": Failed to get connection info: " << error_name << ": " |
610 << error_message; | 625 << error_message; |
611 callback.Run(ConnectionInfo()); | 626 callback.Run(ConnectionInfo()); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, | 791 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, |
777 const std::string& error_name, | 792 const std::string& error_name, |
778 const std::string& error_message) { | 793 const std::string& error_message) { |
779 LOG(WARNING) << object_path_.value() | 794 LOG(WARNING) << object_path_.value() |
780 << ": Failed to remove device: " << error_name << ": " | 795 << ": Failed to remove device: " << error_name << ": " |
781 << error_message; | 796 << error_message; |
782 error_callback.Run(); | 797 error_callback.Run(); |
783 } | 798 } |
784 | 799 |
785 } // namespace bluez | 800 } // namespace bluez |
OLD | NEW |