Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2324463004: arc: bluetooth: Implement set discoverable state (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <bluetooth/bluetooth.h> 7 #include <bluetooth/bluetooth.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <sys/socket.h> 10 #include <sys/socket.h>
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 bool IsGattOffsetValid(int offset) { 177 bool IsGattOffsetValid(int offset) {
178 return 0 <= offset && offset < kMaxGattAttributeLength; 178 return 0 <= offset && offset < kMaxGattAttributeLength;
179 } 179 }
180 180
181 // This is needed because Android only support UUID 16 bits in advertising data. 181 // This is needed because Android only support UUID 16 bits in advertising data.
182 uint16_t GetUUID16(const BluetoothUUID& uuid) { 182 uint16_t GetUUID16(const BluetoothUUID& uuid) {
183 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy 183 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy
184 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); 184 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16);
185 } 185 }
186 186
187 mojo::Array<arc::mojom::BluetoothPropertyPtr> GetDiscoveryTimeoutProperty(
188 uint32_t timeout) {
189 arc::mojom::BluetoothPropertyPtr property =
190 arc::mojom::BluetoothProperty::New();
191 property->set_discovery_timeout(timeout);
192 mojo::Array<arc::mojom::BluetoothPropertyPtr> properties;
193 properties.push_back(std::move(property));
194 return properties;
195 }
196
187 } // namespace 197 } // namespace
188 198
189 namespace arc { 199 namespace arc {
190 200
191 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) 201 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service)
192 : ArcService(bridge_service), binding_(this), weak_factory_(this) { 202 : ArcService(bridge_service), binding_(this), weak_factory_(this) {
193 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { 203 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
194 VLOG(1) << "Registering bluetooth adapter."; 204 VLOG(1) << "Registering bluetooth adapter.";
195 BluetoothAdapterFactory::GetAdapter(base::Bind( 205 BluetoothAdapterFactory::GetAdapter(base::Bind(
196 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); 206 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr()));
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 if (!HasBluetoothInstance()) 621 if (!HasBluetoothInstance())
612 return; 622 return;
613 623
614 mojo::Array<mojom::BluetoothPropertyPtr> properties = 624 mojo::Array<mojom::BluetoothPropertyPtr> properties =
615 GetAdapterProperties(type); 625 GetAdapterProperties(type);
616 626
617 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( 627 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
618 mojom::BluetoothStatus::SUCCESS, std::move(properties)); 628 mojom::BluetoothStatus::SUCCESS, std::move(properties));
619 } 629 }
620 630
631 void ArcBluetoothBridge::OnSetDiscoverable(bool discoverable,
632 bool success,
633 uint32_t timeout) {
634 DCHECK(CalledOnValidThread());
635
636 if (success && discoverable && timeout > 0) {
637 discoverable_off_timer_.Start(
638 FROM_HERE, base::TimeDelta::FromSeconds(timeout),
639 base::Bind(&ArcBluetoothBridge::SetDiscoverable,
640 weak_factory_.GetWeakPtr(), false, 0));
641 }
642
643 if (!HasBluetoothInstance())
644 return;
645
646 if (success) {
647 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
648 mojom::BluetoothStatus::SUCCESS, GetDiscoveryTimeoutProperty(timeout));
649 } else {
650 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
651 mojom::BluetoothStatus::FAIL,
652 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0));
653 }
654 }
655
656 // Set discoverable state to on / off.
657 // In case of turning on, start timer to turn it back off in |timeout| seconds.
658 void ArcBluetoothBridge::SetDiscoverable(bool discoverable, uint32_t timeout) {
659 DCHECK(bluetooth_adapter_);
660 DCHECK(CalledOnValidThread());
661 DCHECK(!discoverable || timeout == 0);
662
663 bool currently_discoverable = bluetooth_adapter_->IsDiscoverable();
664
665 if (!discoverable && !currently_discoverable)
666 return;
667
668 if (discoverable && currently_discoverable) {
669 if (base::TimeDelta::FromSeconds(timeout) >
670 discoverable_off_timer_.GetCurrentDelay()) {
671 // Restart discoverable_off_timer_ if new timeout is greater
672 OnSetDiscoverable(true, true, timeout);
673 } else if (HasBluetoothInstance()) {
674 // Just send message to Android if new timeout is lower.
675 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
676 mojom::BluetoothStatus::SUCCESS,
677 GetDiscoveryTimeoutProperty(timeout));
678 }
679 return;
680 }
681
682 bluetooth_adapter_->SetDiscoverable(
683 discoverable,
684 base::Bind(&ArcBluetoothBridge::OnSetDiscoverable,
685 weak_factory_.GetWeakPtr(), discoverable, true, timeout),
686 base::Bind(&ArcBluetoothBridge::OnSetDiscoverable,
687 weak_factory_.GetWeakPtr(), discoverable, false, timeout));
688 }
689
621 void ArcBluetoothBridge::SetAdapterProperty( 690 void ArcBluetoothBridge::SetAdapterProperty(
622 mojom::BluetoothPropertyPtr property) { 691 mojom::BluetoothPropertyPtr property) {
623 DCHECK(bluetooth_adapter_); 692 DCHECK(bluetooth_adapter_);
624 if (!HasBluetoothInstance()) 693 if (!HasBluetoothInstance())
625 return; 694 return;
626 695
627 // TODO(smbarber): Implement SetAdapterProperty 696 if (property->is_discovery_timeout()) {
628 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( 697 uint32_t discovery_timeout = property->get_discovery_timeout();
629 mojom::BluetoothStatus::FAIL, 698 if (discovery_timeout > 0) {
630 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); 699 SetDiscoverable(true, discovery_timeout);
700 } else {
701 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
702 mojom::BluetoothStatus::PARM_INVALID,
703 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0));
704 }
705 } else {
706 // TODO(puthik) Implement other case.
707 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
708 mojom::BluetoothStatus::UNSUPPORTED,
709 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0));
710 }
631 } 711 }
632 712
633 void ArcBluetoothBridge::GetRemoteDeviceProperty( 713 void ArcBluetoothBridge::GetRemoteDeviceProperty(
634 mojom::BluetoothAddressPtr remote_addr, 714 mojom::BluetoothAddressPtr remote_addr,
635 mojom::BluetoothPropertyType type) { 715 mojom::BluetoothPropertyType type) {
636 DCHECK(bluetooth_adapter_); 716 DCHECK(bluetooth_adapter_);
637 if (!HasBluetoothInstance()) 717 if (!HasBluetoothInstance())
638 return; 718 return;
639 719
640 std::string addr_str = remote_addr->To<std::string>(); 720 std::string addr_str = remote_addr->To<std::string>();
(...skipping 1111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1752 LOG(WARNING) << "Bluetooth instance is too old (version " << version 1832 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1753 << ") need version " << version_need; 1833 << ") need version " << version_need;
1754 return false; 1834 return false;
1755 } 1835 }
1756 1836
1757 bool ArcBluetoothBridge::CalledOnValidThread() { 1837 bool ArcBluetoothBridge::CalledOnValidThread() {
1758 return thread_checker_.CalledOnValidThread(); 1838 return thread_checker_.CalledOnValidThread();
1759 } 1839 }
1760 1840
1761 } // namespace arc 1841 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698