Skip to content

Commit

Permalink
device/bluetooth: split out transport enum
Browse files Browse the repository at this point in the history
The transport enum can be reused to describe supported transport
types for remote devices and adapters. As such, move it out in
preparation for adding this functionality to transports and
adapters.

BUG=b:29268565
TEST=compile and run unit tests

Review-Url: https://codereview.chromium.org/2063353002
Cr-Commit-Position: refs/heads/master@{#400798}
  • Loading branch information
ejcaruso authored and Commit bot committed Jun 20, 2016
1 parent bf4dd97 commit ee39a56
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 167 deletions.
5 changes: 3 additions & 2 deletions components/arc/bluetooth/arc_bluetooth_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "components/arc/arc_bridge_service.h"
#include "components/arc/bluetooth/bluetooth_type_converters.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_gatt_connection.h"
#include "device/bluetooth/bluetooth_gatt_notify_session.h"
Expand Down Expand Up @@ -518,8 +519,8 @@ void ArcBluetoothBridge::StartLEScan() {
return;
}
bluetooth_adapter_->StartDiscoverySessionWithFilter(
base::WrapUnique(new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)),
base::WrapUnique(
new BluetoothDiscoveryFilter(device::BLUETOOTH_TRANSPORT_LE)),
base::Bind(&ArcBluetoothBridge::OnDiscoveryStarted,
weak_factory_.GetWeakPtr()),
base::Bind(&ArcBluetoothBridge::OnDiscoveryError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h"
#include "components/proximity_auth/logging/logging.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/bluetooth_uuid.h"
Expand Down Expand Up @@ -227,8 +228,8 @@ void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() {
}

// Discover only low energy (LE) devices with strong enough signal.
std::unique_ptr<BluetoothDiscoveryFilter> filter(new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
std::unique_ptr<BluetoothDiscoveryFilter> filter(
new BluetoothDiscoveryFilter(device::BLUETOOTH_TRANSPORT_LE));
filter->SetRSSI(kMinDiscoveryRSSI);

adapter_->StartDiscoverySessionWithFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_discovery_session.h"

using device::BluetoothUUID;
Expand Down Expand Up @@ -132,7 +133,7 @@ std::unique_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter(
}
}
auto discovery_filter = base::MakeUnique<device::BluetoothDiscoveryFilter>(
device::BluetoothDiscoveryFilter::TRANSPORT_DUAL);
device::BLUETOOTH_TRANSPORT_DUAL);
for (const BluetoothUUID& service : services) {
discovery_filter->AddUUID(service);
}
Expand Down
4 changes: 2 additions & 2 deletions device/bluetooth/bluetooth_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/stl_util.h"
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/bluetooth_discovery_session_outcome.h"
Expand Down Expand Up @@ -352,8 +353,7 @@ BluetoothAdapter::GetMergedDiscoveryFilterHelper(
if (first_merge) {
first_merge = false;
if (curr_filter) {
result.reset(new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL));
result.reset(new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_DUAL));
result->CopyFrom(*curr_filter);
}
continue;
Expand Down
15 changes: 7 additions & 8 deletions device/bluetooth/bluetooth_adapter_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "device/bluetooth/bluetooth_classic_device_mac.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/bluetooth_discovery_session_outcome.h"
#include "device/bluetooth/bluetooth_low_energy_central_manager_delegate.h"
Expand Down Expand Up @@ -320,20 +321,19 @@
}

// Default to dual discovery if |discovery_filter| is NULL.
BluetoothDiscoveryFilter::TransportMask transport =
BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL;
BluetoothTransport transport = BLUETOOTH_TRANSPORT_DUAL;
if (discovery_filter)
transport = discovery_filter->GetTransport();

if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC) {
if (transport & BLUETOOTH_TRANSPORT_CLASSIC) {
if (!classic_discovery_manager_->StopDiscovery()) {
DVLOG(1) << "Failed to stop classic discovery";
// TODO: Provide a more precise error here.
error_callback.Run(UMABluetoothDiscoverySessionOutcome::UNKNOWN);
return;
}
}
if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) {
if (transport & BLUETOOTH_TRANSPORT_LE) {
if (IsLowEnergyAvailable())
low_energy_discovery_manager_->StopDiscovery();
}
Expand All @@ -355,12 +355,11 @@
BluetoothDiscoveryFilter* discovery_filter) {
// Default to dual discovery if |discovery_filter| is NULL. IOBluetooth seems
// allow starting low energy and classic discovery at once.
BluetoothDiscoveryFilter::TransportMask transport =
BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL;
BluetoothTransport transport = BLUETOOTH_TRANSPORT_DUAL;
if (discovery_filter)
transport = discovery_filter->GetTransport();

if ((transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC) &&
if ((transport & BLUETOOTH_TRANSPORT_CLASSIC) &&
!classic_discovery_manager_->IsDiscovering()) {
// TODO(krstnmnlsn): If a classic discovery session is already running then
// we should update its filter. crbug.com/498056
Expand All @@ -369,7 +368,7 @@
return false;
}
}
if (transport & BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) {
if (transport & BLUETOOTH_TRANSPORT_LE) {
// Begin a low energy discovery session or update it if one is already
// running.
if (IsLowEnergyAvailable())
Expand Down
13 changes: 5 additions & 8 deletions device/bluetooth/bluetooth_adapter_mac_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/test/test_simple_task_runner.h"
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/bluetooth_discovery_session_outcome.h"
#include "device/bluetooth/bluetooth_low_energy_device_mac.h"
Expand Down Expand Up @@ -165,8 +166,7 @@ void DiscoveryErrorCallback(UMABluetoothDiscoverySessionOutcome) {
EXPECT_EQ(0, NumDiscoverySessions());

std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(
new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE));
AddDiscoverySession(discovery_filter.get());
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
Expand All @@ -183,8 +183,7 @@ new BluetoothDiscoveryFilter(
if (!SetMockCentralManager(CBCentralManagerStatePoweredOn))
return;
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(
new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE));
AddDiscoverySession(discovery_filter.get());
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
Expand All @@ -207,8 +206,7 @@ new BluetoothDiscoveryFilter(
EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]);

std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(
new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE));
AddDiscoverySession(discovery_filter.get());
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
Expand All @@ -233,8 +231,7 @@ new BluetoothDiscoveryFilter(
EXPECT_EQ(0, NumDiscoverySessions());

std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(
new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE));
RemoveDiscoverySession(discovery_filter.get());
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
Expand Down
62 changes: 28 additions & 34 deletions device/bluetooth/bluetooth_adapter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/bluetooth_gatt_service.h"
Expand Down Expand Up @@ -266,13 +267,13 @@ TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRssi) {
uint16_t resulting_pathloss;
std::unique_ptr<BluetoothDiscoveryFilter> resulting_filter;

BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
BluetoothDiscoveryFilter* df =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
df->SetRSSI(-30);
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(df);

BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
BluetoothDiscoveryFilter* df2 =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
df2->SetRSSI(-65);
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);

Expand Down Expand Up @@ -306,8 +307,8 @@ TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRssi) {
resulting_filter->GetRSSI(&resulting_rssi);
EXPECT_EQ(-30, resulting_rssi);

BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
BluetoothDiscoveryFilter* df3 =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
df3->SetPathloss(60);
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);

Expand All @@ -325,42 +326,37 @@ TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterTransport) {
scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
std::unique_ptr<BluetoothDiscoveryFilter> resulting_filter;

BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
BluetoothDiscoveryFilter* df =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_CLASSIC);
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(df);

BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
BluetoothDiscoveryFilter* df2 =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);

adapter->InjectFilteredSession(std::move(discovery_filter));

// Just one filter, make sure transport was properly rewritten
resulting_filter = adapter->GetMergedDiscoveryFilter();
EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
resulting_filter->GetTransport());
EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, resulting_filter->GetTransport());

adapter->InjectFilteredSession(std::move(discovery_filter2));

// Two filters, should have OR of both transport's
resulting_filter = adapter->GetMergedDiscoveryFilter();
EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
resulting_filter->GetTransport());
EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, resulting_filter->GetTransport());

// When 1st filter is masked, 2nd filter transport should be returned.
resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE,
resulting_filter->GetTransport());
EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, resulting_filter->GetTransport());

// When 2nd filter is masked, 1st filter transport should be returned.
resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
resulting_filter->GetTransport());
EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, resulting_filter->GetTransport());

BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
df3->CopyFrom(BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL));
BluetoothDiscoveryFilter* df3 =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
df3->CopyFrom(BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_DUAL));
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);

// Merging empty filter in should result in empty filter
Expand All @@ -376,24 +372,24 @@ TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterAllFields) {
int16_t resulting_rssi;
std::set<device::BluetoothUUID> resulting_uuids;

BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
BluetoothDiscoveryFilter* df =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
df->SetRSSI(-60);
df->AddUUID(device::BluetoothUUID("1000"));
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(df);

BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
BluetoothDiscoveryFilter* df2 =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
df2->SetRSSI(-85);
df2->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
df2->SetTransport(BLUETOOTH_TRANSPORT_LE);
df2->AddUUID(device::BluetoothUUID("1020"));
df2->AddUUID(device::BluetoothUUID("1001"));
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);

BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
BluetoothDiscoveryFilter* df3 =
new BluetoothDiscoveryFilter(BLUETOOTH_TRANSPORT_LE);
df3->SetRSSI(-65);
df3->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
df3->SetTransport(BLUETOOTH_TRANSPORT_CLASSIC);
df3->AddUUID(device::BluetoothUUID("1020"));
df3->AddUUID(device::BluetoothUUID("1003"));
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
Expand All @@ -408,8 +404,7 @@ TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterAllFields) {
resulting_filter->GetRSSI(&resulting_rssi);
resulting_filter->GetUUIDs(resulting_uuids);
EXPECT_TRUE(resulting_filter->GetTransport());
EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
resulting_filter->GetTransport());
EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, resulting_filter->GetTransport());
EXPECT_EQ(-85, resulting_rssi);
EXPECT_EQ(4UL, resulting_uuids.size());
EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1000")) !=
Expand All @@ -422,8 +417,7 @@ TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterAllFields) {
resulting_uuids.end());

resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
resulting_filter->GetTransport());
EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, resulting_filter->GetTransport());

adapter->CleanupSessions();
}
Expand Down
29 changes: 29 additions & 0 deletions device/bluetooth/bluetooth_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_TYPES_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_TYPES_H_

#include "device/bluetooth/bluetooth_export.h"

// This file is for enums and small types common to several
// parts of bluetooth.

namespace device {

// Devices and adapters can support a number of transports,
// and bluetooth hosts can scan for devices based on the
// transports they support.
enum BluetoothTransport : uint8_t {
BLUETOOTH_TRANSPORT_INVALID = 0x00,
// Valid transports are given as a bitset.
BLUETOOTH_TRANSPORT_CLASSIC = 0x01,
BLUETOOTH_TRANSPORT_LE = 0x02,
BLUETOOTH_TRANSPORT_DUAL =
(BLUETOOTH_TRANSPORT_CLASSIC | BLUETOOTH_TRANSPORT_LE)
};

} // namespace device

#endif // DEVICE_BLUETOOTH_BLUETOOTH_TYPES_H_
Loading

0 comments on commit ee39a56

Please sign in to comment.