| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/arc/bluetooth/arc_bluetooth_bridge.h" | 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <iomanip> | 10 #include <iomanip> |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 bool IsGattOffsetValid(int offset) { | 168 bool IsGattOffsetValid(int offset) { |
| 169 return 0 <= offset && offset < kMaxGattAttributeLength; | 169 return 0 <= offset && offset < kMaxGattAttributeLength; |
| 170 } | 170 } |
| 171 | 171 |
| 172 // This is needed because Android only support UUID 16 bits in advertising data. | 172 // This is needed because Android only support UUID 16 bits in advertising data. |
| 173 uint16_t GetUUID16(const BluetoothUUID& uuid) { | 173 uint16_t GetUUID16(const BluetoothUUID& uuid) { |
| 174 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy | 174 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy |
| 175 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); | 175 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); |
| 176 } | 176 } |
| 177 | 177 |
| 178 mojo::Array<arc::mojom::BluetoothPropertyPtr> GetDiscoveryTimeoutProperty( |
| 179 uint32_t timeout) { |
| 180 arc::mojom::BluetoothPropertyPtr property = |
| 181 arc::mojom::BluetoothProperty::New(); |
| 182 property->set_discovery_timeout(timeout); |
| 183 mojo::Array<arc::mojom::BluetoothPropertyPtr> properties; |
| 184 properties.push_back(std::move(property)); |
| 185 return properties; |
| 186 } |
| 187 |
| 178 } // namespace | 188 } // namespace |
| 179 | 189 |
| 180 namespace arc { | 190 namespace arc { |
| 181 | 191 |
| 182 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) | 192 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) |
| 183 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | 193 : ArcService(bridge_service), binding_(this), weak_factory_(this) { |
| 184 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 194 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 185 VLOG(1) << "registering bluetooth adapter"; | 195 VLOG(1) << "registering bluetooth adapter"; |
| 186 BluetoothAdapterFactory::GetAdapter(base::Bind( | 196 BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 187 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 197 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 if (!HasBluetoothInstance()) | 593 if (!HasBluetoothInstance()) |
| 584 return; | 594 return; |
| 585 | 595 |
| 586 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 596 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 587 GetAdapterProperties(type); | 597 GetAdapterProperties(type); |
| 588 | 598 |
| 589 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( | 599 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
| 590 mojom::BluetoothStatus::SUCCESS, std::move(properties)); | 600 mojom::BluetoothStatus::SUCCESS, std::move(properties)); |
| 591 } | 601 } |
| 592 | 602 |
| 603 void ArcBluetoothBridge::OnSetDiscoverable(bool discoverable, |
| 604 bool success, |
| 605 uint32_t timeout) { |
| 606 DCHECK(CalledOnValidThread()); |
| 607 |
| 608 if (success && discoverable && timeout > 0) { |
| 609 discoverable_off_timer_.Start( |
| 610 FROM_HERE, base::TimeDelta::FromSeconds(timeout), |
| 611 base::Bind(&ArcBluetoothBridge::SetDiscoverable, |
| 612 weak_factory_.GetWeakPtr(), false, 0)); |
| 613 } |
| 614 |
| 615 if (!HasBluetoothInstance()) |
| 616 return; |
| 617 |
| 618 if (success) { |
| 619 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
| 620 mojom::BluetoothStatus::SUCCESS, GetDiscoveryTimeoutProperty(timeout)); |
| 621 } else { |
| 622 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
| 623 mojom::BluetoothStatus::FAIL, |
| 624 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0)); |
| 625 } |
| 626 } |
| 627 |
| 628 // Set discoverable state to on / off. |
| 629 // In case of turning on, start timer to turn it back off in |timeout| seconds. |
| 630 void ArcBluetoothBridge::SetDiscoverable(bool discoverable, uint32_t timeout) { |
| 631 DCHECK(bluetooth_adapter_); |
| 632 DCHECK(CalledOnValidThread()); |
| 633 DCHECK(!discoverable || timeout == 0); |
| 634 |
| 635 bool currently_discoverable = bluetooth_adapter_->IsDiscoverable(); |
| 636 |
| 637 if (!discoverable && !currently_discoverable) |
| 638 return; |
| 639 |
| 640 if (discoverable && currently_discoverable) { |
| 641 if (base::TimeDelta::FromSeconds(timeout) > |
| 642 discoverable_off_timer_.GetCurrentDelay()) { |
| 643 // Restart discoverable_off_timer_ if new timeout is greater |
| 644 OnSetDiscoverable(true, true, timeout); |
| 645 } else if (HasBluetoothInstance()) { |
| 646 // Just send message to Android if new timeout is lower. |
| 647 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
| 648 mojom::BluetoothStatus::SUCCESS, |
| 649 GetDiscoveryTimeoutProperty(timeout)); |
| 650 } |
| 651 return; |
| 652 } |
| 653 |
| 654 bluetooth_adapter_->SetDiscoverable( |
| 655 discoverable, |
| 656 base::Bind(&ArcBluetoothBridge::OnSetDiscoverable, |
| 657 weak_factory_.GetWeakPtr(), discoverable, true, timeout), |
| 658 base::Bind(&ArcBluetoothBridge::OnSetDiscoverable, |
| 659 weak_factory_.GetWeakPtr(), discoverable, false, timeout)); |
| 660 } |
| 661 |
| 593 void ArcBluetoothBridge::SetAdapterProperty( | 662 void ArcBluetoothBridge::SetAdapterProperty( |
| 594 mojom::BluetoothPropertyPtr property) { | 663 mojom::BluetoothPropertyPtr property) { |
| 595 DCHECK(bluetooth_adapter_); | 664 DCHECK(bluetooth_adapter_); |
| 596 if (!HasBluetoothInstance()) | 665 if (!HasBluetoothInstance()) |
| 597 return; | 666 return; |
| 598 | 667 |
| 599 // TODO(smbarber): Implement SetAdapterProperty | 668 if (property->is_discovery_timeout()) { |
| 600 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( | 669 uint32_t discovery_timeout = property->get_discovery_timeout(); |
| 601 mojom::BluetoothStatus::FAIL, | 670 if (discovery_timeout > 0) { |
| 602 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); | 671 SetDiscoverable(true, discovery_timeout); |
| 672 } else { |
| 673 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
| 674 mojom::BluetoothStatus::PARM_INVALID, |
| 675 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0)); |
| 676 } |
| 677 } else { |
| 678 // TODO(puthik) Implement other case. |
| 679 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
| 680 mojom::BluetoothStatus::UNSUPPORTED, |
| 681 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0)); |
| 682 } |
| 603 } | 683 } |
| 604 | 684 |
| 605 void ArcBluetoothBridge::GetRemoteDeviceProperty( | 685 void ArcBluetoothBridge::GetRemoteDeviceProperty( |
| 606 mojom::BluetoothAddressPtr remote_addr, | 686 mojom::BluetoothAddressPtr remote_addr, |
| 607 mojom::BluetoothPropertyType type) { | 687 mojom::BluetoothPropertyType type) { |
| 608 DCHECK(bluetooth_adapter_); | 688 DCHECK(bluetooth_adapter_); |
| 609 if (!HasBluetoothInstance()) | 689 if (!HasBluetoothInstance()) |
| 610 return; | 690 return; |
| 611 | 691 |
| 612 std::string addr_str = remote_addr->To<std::string>(); | 692 std::string addr_str = remote_addr->To<std::string>(); |
| (...skipping 1066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1679 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1759 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
| 1680 << ") need version " << version_need; | 1760 << ") need version " << version_need; |
| 1681 return false; | 1761 return false; |
| 1682 } | 1762 } |
| 1683 | 1763 |
| 1684 bool ArcBluetoothBridge::CalledOnValidThread() { | 1764 bool ArcBluetoothBridge::CalledOnValidThread() { |
| 1685 return thread_checker_.CalledOnValidThread(); | 1765 return thread_checker_.CalledOnValidThread(); |
| 1686 } | 1766 } |
| 1687 | 1767 |
| 1688 } // namespace arc | 1768 } // namespace arc |
| OLD | NEW |