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

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

Issue 2297903002: arc: bluetooth: Implement set discoverable state (Closed)
Patch Set: Implement timer to turn discovery off 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),
203 binding_(this),
204 discoverable_off_timer_(true, false),
Luis Héctor Chávez 2016/09/01 19:28:15 it looks like you can use base::OneShotTimer inste
puthik_chromium 2016/09/01 20:42:24 Done.
205 weak_factory_(this) {
193 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { 206 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
194 VLOG(1) << "Registering bluetooth adapter."; 207 VLOG(1) << "Registering bluetooth adapter.";
195 BluetoothAdapterFactory::GetAdapter(base::Bind( 208 BluetoothAdapterFactory::GetAdapter(base::Bind(
196 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); 209 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr()));
197 } else { 210 } else {
198 VLOG(1) << "No bluetooth adapter available."; 211 VLOG(1) << "No bluetooth adapter available.";
199 } 212 }
200 arc_bridge_service()->bluetooth()->AddObserver(this); 213 arc_bridge_service()->bluetooth()->AddObserver(this);
201 } 214 }
202 215
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 if (!HasBluetoothInstance()) 624 if (!HasBluetoothInstance())
612 return; 625 return;
613 626
614 mojo::Array<mojom::BluetoothPropertyPtr> properties = 627 mojo::Array<mojom::BluetoothPropertyPtr> properties =
615 GetAdapterProperties(type); 628 GetAdapterProperties(type);
616 629
617 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( 630 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
618 mojom::BluetoothStatus::SUCCESS, std::move(properties)); 631 mojom::BluetoothStatus::SUCCESS, std::move(properties));
619 } 632 }
620 633
634 void ArcBluetoothBridge::OnSetDiscoverable(bool discoverable,
635 bool success,
636 uint32_t timeout) {
637 if (!success) {
Luis Héctor Chávez 2016/09/01 19:28:15 nit: DCHECK(CalledOnValidThread()); if (!HasBluet
puthik_chromium 2016/09/01 20:42:24 Done.
638 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
639 mojom::BluetoothStatus::FAIL,
640 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0));
641 return;
642 }
643
644 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
645 mojom::BluetoothStatus::SUCCESS, GetDiscoveryTimeoutProperty(timeout));
646
647 if (discoverable && timeout > 0) {
648 discoverable_off_timer_.Start(
649 FROM_HERE, base::TimeDelta::FromSeconds(timeout),
650 base::Bind(&ArcBluetoothBridge::SetDiscoverable,
651 weak_factory_.GetWeakPtr(), false, 0));
652 }
653 }
654
655 // Set discoverable state to on / off.
656 // In case of turning on, start timer to turn it back off in |timeout| seconds.
657 void ArcBluetoothBridge::SetDiscoverable(bool discoverable, uint32_t timeout) {
658 DCHECK(bluetooth_adapter_);
659 DCHECK(CalledOnValidThread());
660 DCHECK(!discoverable || timeout == 0);
661
662 bool current = bluetooth_adapter_->IsDiscoverable();
Luis Héctor Chávez 2016/09/01 19:28:15 nit: currently_discoverable.
puthik_chromium 2016/09/01 20:42:24 Done.
663
664 if (!discoverable && !current)
665 return;
666
667 if (discoverable && current) {
668 if (base::TimeDelta::FromSeconds(timeout) >
669 discoverable_off_timer_.GetCurrentDelay()) {
670 // Restart discoverable_off_timer_ if new timeout is greater
671 OnSetDiscoverable(true, true, timeout);
672 } else {
673 // Just send message to Chrome is new timeout is lower.
Luis Héctor Chávez 2016/09/01 19:28:15 nit: "Android if"? this also needs to be gated upo
puthik_chromium 2016/09/01 20:42:24 Done.
674 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
675 mojom::BluetoothStatus::SUCCESS,
676 GetDiscoveryTimeoutProperty(timeout));
677 }
678 return;
679 }
680
681 bluetooth_adapter_->SetDiscoverable(
682 discoverable,
683 base::Bind(&ArcBluetoothBridge::OnSetDiscoverable,
684 weak_factory_.GetWeakPtr(), discoverable, true, timeout),
685 base::Bind(&ArcBluetoothBridge::OnSetDiscoverable,
686 weak_factory_.GetWeakPtr(), discoverable, false, timeout));
687 }
688
621 void ArcBluetoothBridge::SetAdapterProperty( 689 void ArcBluetoothBridge::SetAdapterProperty(
622 mojom::BluetoothPropertyPtr property) { 690 mojom::BluetoothPropertyPtr property) {
623 DCHECK(bluetooth_adapter_); 691 DCHECK(bluetooth_adapter_);
624 if (!HasBluetoothInstance()) 692 if (!HasBluetoothInstance())
625 return; 693 return;
626 694
627 // TODO(smbarber): Implement SetAdapterProperty 695 if (property->is_discovery_timeout()) {
628 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( 696 uint32_t discovery_timeout = property->get_discovery_timeout();
629 mojom::BluetoothStatus::FAIL, 697 mojo::Array<mojom::BluetoothPropertyPtr> success_result;
630 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); 698 success_result.push_back(std::move(property));
699 if (discovery_timeout > 0) {
700 SetDiscoverable(true, discovery_timeout);
701 } else {
702 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
703 mojom::BluetoothStatus::PARM_INVALID,
704 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0));
705 }
706 } else {
707 // TODO(puthik) Implement other case.
708 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties(
709 mojom::BluetoothStatus::UNSUPPORTED,
710 mojo::Array<arc::mojom::BluetoothPropertyPtr>::New(0));
711 }
631 } 712 }
632 713
633 void ArcBluetoothBridge::GetRemoteDeviceProperty( 714 void ArcBluetoothBridge::GetRemoteDeviceProperty(
634 mojom::BluetoothAddressPtr remote_addr, 715 mojom::BluetoothAddressPtr remote_addr,
635 mojom::BluetoothPropertyType type) { 716 mojom::BluetoothPropertyType type) {
636 DCHECK(bluetooth_adapter_); 717 DCHECK(bluetooth_adapter_);
637 if (!HasBluetoothInstance()) 718 if (!HasBluetoothInstance())
638 return; 719 return;
639 720
640 std::string addr_str = remote_addr->To<std::string>(); 721 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 1833 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1753 << ") need version " << version_need; 1834 << ") need version " << version_need;
1754 return false; 1835 return false;
1755 } 1836 }
1756 1837
1757 bool ArcBluetoothBridge::CalledOnValidThread() { 1838 bool ArcBluetoothBridge::CalledOnValidThread() {
1758 return thread_checker_.CalledOnValidThread(); 1839 return thread_checker_.CalledOnValidThread();
1759 } 1840 }
1760 1841
1761 } // namespace arc 1842 } // 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