Commit 59630335 authored by Archie Pusaka's avatar Archie Pusaka Committed by Commit Bot

[CrOS Bluetooth] Wiring BT verbose logging toggle to D-Bus

When a user which primary account is Google corp account opts in for
BT verbose logging, a D-Bus command will be sent upon sign in to enable
verbose logging level 1.

Logging will be stopped upon sign out or device shutdown.

Bug: 1004572, 734152
Change-Id: I00b5d56d41d7df3669e1a7c73c86d4fb67226959
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1824721Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarMiao-chen Chou <mcchou@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Archie Pusaka <apusaka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720829}
parent 46531d75
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "chromeos/constants/chromeos_features.h" #include "chromeos/constants/chromeos_features.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "device/bluetooth/dbus/bluetooth_debug_manager_client.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"
#include "google_apis/gaia/gaia_auth_util.h" #include "google_apis/gaia/gaia_auth_util.h"
namespace chromeos { namespace chromeos {
...@@ -16,13 +18,24 @@ namespace bluetooth { ...@@ -16,13 +18,24 @@ namespace bluetooth {
namespace { namespace {
const char kVerboseLoggingEnablePrefName[] = "bluetooth.verboseLogging.enable"; const char kVerboseLoggingEnablePrefName[] = "bluetooth.verboseLogging.enable";
const uint8_t kVerboseDisabledLevel = 0;
const uint8_t kVerboseBasicLevel = 1;
const int kDbusRetryCount = 10;
constexpr base::TimeDelta kDbusRetryInterval = base::TimeDelta::FromSeconds(3);
} // namespace } // namespace
DebugLogsManager::DebugLogsManager(const std::string& primary_user_email, DebugLogsManager::DebugLogsManager(const std::string& primary_user_email,
PrefService* pref_service) PrefService* pref_service)
: primary_user_email_(primary_user_email), pref_service_(pref_service) {} : primary_user_email_(primary_user_email), pref_service_(pref_service) {
SetVerboseLogsEnable(GetDebugLogsState() ==
DebugLogsState::kSupportedAndEnabled);
}
DebugLogsManager::~DebugLogsManager() = default; DebugLogsManager::~DebugLogsManager() {
SetVerboseLogsEnable(false);
}
// static // static
void DebugLogsManager::RegisterPrefs(PrefRegistrySimple* registry) { void DebugLogsManager::RegisterPrefs(PrefRegistrySimple* registry) {
...@@ -51,7 +64,6 @@ void DebugLogsManager::ChangeDebugLogsState(bool should_debug_logs_be_enabled) { ...@@ -51,7 +64,6 @@ void DebugLogsManager::ChangeDebugLogsState(bool should_debug_logs_be_enabled) {
pref_service_->SetBoolean(kVerboseLoggingEnablePrefName, pref_service_->SetBoolean(kVerboseLoggingEnablePrefName,
should_debug_logs_be_enabled); should_debug_logs_be_enabled);
// TODO(crbug.com/734152): On login, enable logs based on this value
} }
bool DebugLogsManager::AreDebugLogsSupported() const { bool DebugLogsManager::AreDebugLogsSupported() const {
...@@ -63,6 +75,54 @@ bool DebugLogsManager::AreDebugLogsSupported() const { ...@@ -63,6 +75,54 @@ bool DebugLogsManager::AreDebugLogsSupported() const {
return gaia::IsGoogleInternalAccountEmail(primary_user_email_); return gaia::IsGoogleInternalAccountEmail(primary_user_email_);
} }
void DebugLogsManager::SetVerboseLogsEnable(bool enable) {
SendDBusVerboseLogsMessage(enable, 0 /* num_completed_attempts */);
}
void DebugLogsManager::SendDBusVerboseLogsMessage(bool enable,
int num_completed_attempts) {
uint8_t level = enable ? kVerboseBasicLevel : kVerboseDisabledLevel;
VLOG(1) << (enable ? "Enabling" : "Disabling") << " bluetooth verbose logs";
bluez::BluezDBusManager::Get()
->GetBluetoothDebugManagerClient()
->SetLogLevels(
level /* dispatcher */, level /* newblue */, level /* bluez */,
level /* kernel */,
base::BindOnce(&DebugLogsManager::OnVerboseLogsEnableSuccess,
weak_ptr_factory_.GetWeakPtr(), enable),
base::BindOnce(&DebugLogsManager::OnVerboseLogsEnableError,
weak_ptr_factory_.GetWeakPtr(), enable,
num_completed_attempts));
}
void DebugLogsManager::OnVerboseLogsEnableSuccess(bool enable) {
VLOG(1) << "Bluetooth verbose logs successfully "
<< (enable ? "enabled" : "disabled");
}
void DebugLogsManager::OnVerboseLogsEnableError(
const bool enable,
const int num_completed_attempts,
const std::string& error_name,
const std::string& error_message) {
bool should_retry = (num_completed_attempts < kDbusRetryCount);
LOG(ERROR) << "Setting bluetooth verbose logs failed: error: " << error_name
<< " - " << error_message << " "
<< (should_retry ? "Retrying." : "Giving up.");
if (!should_retry)
return;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&DebugLogsManager::SendDBusVerboseLogsMessage,
weak_ptr_factory_.GetWeakPtr(), enable,
num_completed_attempts + 1),
kDbusRetryInterval);
}
} // namespace bluetooth } // namespace bluetooth
} // namespace chromeos } // namespace chromeos
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_CHROMEOS_BLUETOOTH_DEBUG_LOGS_MANAGER_H_ #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_DEBUG_LOGS_MANAGER_H_
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/webui/bluetooth_internals/bluetooth_internals.mojom.h" #include "chrome/browser/ui/webui/bluetooth_internals/bluetooth_internals.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h" #include "mojo/public/cpp/bindings/receiver_set.h"
...@@ -50,11 +51,20 @@ class DebugLogsManager : public mojom::DebugLogsChangeHandler { ...@@ -50,11 +51,20 @@ class DebugLogsManager : public mojom::DebugLogsChangeHandler {
private: private:
bool AreDebugLogsSupported() const; bool AreDebugLogsSupported() const;
void SetVerboseLogsEnable(bool enable);
void SendDBusVerboseLogsMessage(bool enable, int num_completed_attempts);
void OnVerboseLogsEnableSuccess(bool enable);
void OnVerboseLogsEnableError(const bool enable,
const int num_completed_attempts,
const std::string& error_name,
const std::string& error_message);
const std::string primary_user_email_; const std::string primary_user_email_;
PrefService* pref_service_ = nullptr; PrefService* pref_service_ = nullptr;
mojo::ReceiverSet<mojom::DebugLogsChangeHandler> receivers_; mojo::ReceiverSet<mojom::DebugLogsChangeHandler> receivers_;
base::WeakPtrFactory<DebugLogsManager> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(DebugLogsManager); DISALLOW_COPY_AND_ASSIGN(DebugLogsManager);
}; };
......
...@@ -79,6 +79,10 @@ bool DebugLogsManagerFactory::ServiceIsCreatedWithBrowserContext() const { ...@@ -79,6 +79,10 @@ bool DebugLogsManagerFactory::ServiceIsCreatedWithBrowserContext() const {
return true; return true;
} }
bool DebugLogsManagerFactory::ServiceIsNULLWhileTesting() const {
return true;
}
} // namespace bluetooth } // namespace bluetooth
} // namespace chromeos } // namespace chromeos
...@@ -32,6 +32,7 @@ class DebugLogsManagerFactory : public BrowserContextKeyedServiceFactory { ...@@ -32,6 +32,7 @@ class DebugLogsManagerFactory : public BrowserContextKeyedServiceFactory {
KeyedService* BuildServiceInstanceFor( KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override; content::BrowserContext* context) const override;
bool ServiceIsCreatedWithBrowserContext() const override; bool ServiceIsCreatedWithBrowserContext() const override;
bool ServiceIsNULLWhileTesting() const override;
DISALLOW_COPY_AND_ASSIGN(DebugLogsManagerFactory); DISALLOW_COPY_AND_ASSIGN(DebugLogsManagerFactory);
}; };
......
...@@ -7,8 +7,11 @@ ...@@ -7,8 +7,11 @@
#include <memory> #include <memory>
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chromeos/constants/chromeos_features.h" #include "chromeos/constants/chromeos_features.h"
#include "components/prefs/testing_pref_service.h" #include "components/prefs/testing_pref_service.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"
#include "device/bluetooth/dbus/fake_bluetooth_debug_manager_client.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace chromeos { namespace chromeos {
...@@ -26,26 +29,47 @@ class DebugLogsManagerTest : public testing::Test { ...@@ -26,26 +29,47 @@ class DebugLogsManagerTest : public testing::Test {
public: public:
DebugLogsManagerTest() = default; DebugLogsManagerTest() = default;
void SetUp() override { DebugLogsManager::RegisterPrefs(prefs_.registry()); } void SetUp() override {
DebugLogsManager::RegisterPrefs(prefs_.registry());
void TearDown() override { debug_logs_manager_.reset(); } auto fake_bluetooth_debug_manager_client =
std::make_unique<bluez::FakeBluetoothDebugManagerClient>();
fake_bluetooth_debug_manager_client_ =
fake_bluetooth_debug_manager_client.get();
void InitDebugManager(const char* email, bool debug_flag_enabled) { std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter =
bluez::BluezDBusManager::GetSetterForTesting();
dbus_setter->SetBluetoothDebugManagerClient(
std::unique_ptr<bluez::BluetoothDebugManagerClient>(
std::move(fake_bluetooth_debug_manager_client)));
}
void TearDown() override {
debug_logs_manager_.reset();
bluez::BluezDBusManager::Shutdown();
}
void SetDebugFlagState(bool debug_flag_enabled) {
feature_list_.InitWithFeatureState( feature_list_.InitWithFeatureState(
chromeos::features::kShowBluetoothDebugLogToggle, debug_flag_enabled); chromeos::features::kShowBluetoothDebugLogToggle, debug_flag_enabled);
debug_logs_manager_ = std::make_unique<DebugLogsManager>(email, &prefs_);
} }
void DeleteAndRecreateDebugManager(const char* email) { void InstantiateDebugManager(const char* email) {
debug_logs_manager_.reset();
debug_logs_manager_ = std::make_unique<DebugLogsManager>(email, &prefs_); debug_logs_manager_ = std::make_unique<DebugLogsManager>(email, &prefs_);
} }
void DestroyDebugManager() { debug_logs_manager_.reset(); }
DebugLogsManager* debug_manager() const { return debug_logs_manager_.get(); } DebugLogsManager* debug_manager() const { return debug_logs_manager_.get(); }
bluez::FakeBluetoothDebugManagerClient* fake_bluetooth_debug_manager_client()
const {
return fake_bluetooth_debug_manager_client_;
}
private: private:
base::test::ScopedFeatureList feature_list_; base::test::ScopedFeatureList feature_list_;
bluez::FakeBluetoothDebugManagerClient* fake_bluetooth_debug_manager_client_;
std::unique_ptr<DebugLogsManager> debug_logs_manager_; std::unique_ptr<DebugLogsManager> debug_logs_manager_;
TestingPrefServiceSimple prefs_; TestingPrefServiceSimple prefs_;
...@@ -53,40 +77,102 @@ class DebugLogsManagerTest : public testing::Test { ...@@ -53,40 +77,102 @@ class DebugLogsManagerTest : public testing::Test {
}; };
TEST_F(DebugLogsManagerTest, FlagNotEnabled) { TEST_F(DebugLogsManagerTest, FlagNotEnabled) {
InitDebugManager(kTestGooglerEmail, false /* debug_flag_enabled */); SetDebugFlagState(false /* debug_flag_enabled */);
InstantiateDebugManager(kTestGooglerEmail);
EXPECT_EQ(debug_manager()->GetDebugLogsState(), EXPECT_EQ(debug_manager()->GetDebugLogsState(),
DebugLogsManager::DebugLogsState::kNotSupported); DebugLogsManager::DebugLogsState::kNotSupported);
} }
TEST_F(DebugLogsManagerTest, NonGoogler) { TEST_F(DebugLogsManagerTest, NonGoogler) {
InitDebugManager(kTestNonGooglerEmail, true /* debug_flag_enabled */); SetDebugFlagState(true /* debug_flag_enabled */);
InstantiateDebugManager(kTestNonGooglerEmail);
EXPECT_EQ(debug_manager()->GetDebugLogsState(), EXPECT_EQ(debug_manager()->GetDebugLogsState(),
DebugLogsManager::DebugLogsState::kNotSupported); DebugLogsManager::DebugLogsState::kNotSupported);
} }
TEST_F(DebugLogsManagerTest, ChangeDebugLogsState) { TEST_F(DebugLogsManagerTest, ChangeDebugLogsState) {
InitDebugManager(kTestGooglerEmail, true /* debug_flag_enabled */); SetDebugFlagState(true /* debug_flag_enabled */);
InstantiateDebugManager(kTestGooglerEmail);
EXPECT_EQ(debug_manager()->GetDebugLogsState(), EXPECT_EQ(debug_manager()->GetDebugLogsState(),
DebugLogsManager::DebugLogsState::kSupportedButDisabled); DebugLogsManager::DebugLogsState::kSupportedButDisabled);
debug_manager()->ChangeDebugLogsState(true); debug_manager()->ChangeDebugLogsState(
true /* should_debug_logs_be_enabled */);
EXPECT_EQ(debug_manager()->GetDebugLogsState(), EXPECT_EQ(debug_manager()->GetDebugLogsState(),
DebugLogsManager::DebugLogsState::kSupportedAndEnabled); DebugLogsManager::DebugLogsState::kSupportedAndEnabled);
// debug logs state should be saved despite DebugLogsManager is destroyed. // Despite DebugLogsManager is destroyed, the state of Debug logs is saved
DeleteAndRecreateDebugManager(kTestGooglerEmail); DestroyDebugManager();
InstantiateDebugManager(kTestGooglerEmail);
EXPECT_EQ(debug_manager()->GetDebugLogsState(), EXPECT_EQ(debug_manager()->GetDebugLogsState(),
DebugLogsManager::DebugLogsState::kSupportedAndEnabled); DebugLogsManager::DebugLogsState::kSupportedAndEnabled);
debug_manager()->ChangeDebugLogsState(false); debug_manager()->ChangeDebugLogsState(
false /* should_debug_logs_be_enabled */);
EXPECT_EQ(debug_manager()->GetDebugLogsState(), EXPECT_EQ(debug_manager()->GetDebugLogsState(),
DebugLogsManager::DebugLogsState::kSupportedButDisabled); DebugLogsManager::DebugLogsState::kSupportedButDisabled);
DeleteAndRecreateDebugManager(kTestGooglerEmail); DestroyDebugManager();
InstantiateDebugManager(kTestGooglerEmail);
EXPECT_EQ(debug_manager()->GetDebugLogsState(), EXPECT_EQ(debug_manager()->GetDebugLogsState(),
DebugLogsManager::DebugLogsState::kSupportedButDisabled); DebugLogsManager::DebugLogsState::kSupportedButDisabled);
} }
TEST_F(DebugLogsManagerTest, SendVerboseLogsRequestUponLoginAndLogout) {
SetDebugFlagState(true /* debug_flag_enabled */);
InstantiateDebugManager(kTestGooglerEmail);
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 0);
debug_manager()->ChangeDebugLogsState(
true /* should_debug_logs_be_enabled */);
// Dispatcher level is updated only on login/logout, so now it stays the same.
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 0);
// Deletion and Recreation of DebugManager simulates logout-login event
DestroyDebugManager();
InstantiateDebugManager(kTestGooglerEmail);
// After login, dispatcher level should change
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 1);
DestroyDebugManager();
// After logout, dispatcher level should reset to 0
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 0);
InstantiateDebugManager(kTestGooglerEmail);
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 1);
debug_manager()->ChangeDebugLogsState(
false /* should_debug_logs_be_enabled */);
// Dispatcher level is updated only on login/logout, so now it stays the same.
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 1);
DestroyDebugManager();
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 0);
InstantiateDebugManager(kTestGooglerEmail);
// dispatcher level should still be 0 because logging is disabled
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 0);
}
TEST_F(DebugLogsManagerTest, RetryUponSetVerboseLogsFailure) {
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
SetDebugFlagState(true /* debug_flag_enabled */);
InstantiateDebugManager(kTestGooglerEmail);
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 0);
debug_manager()->ChangeDebugLogsState(
true /* should_debug_logs_be_enabled */);
DestroyDebugManager();
fake_bluetooth_debug_manager_client()->MakeNextSetLogLevelsFail();
InstantiateDebugManager(kTestGooglerEmail);
task_environment.FastForwardUntilNoTasksRemain();
EXPECT_EQ(fake_bluetooth_debug_manager_client()->set_log_levels_fail_count(),
1);
// Message is re-sent upon failing, eventually dispatcher level should change.
EXPECT_EQ(fake_bluetooth_debug_manager_client()->dispatcher_level(), 1);
}
} // namespace bluetooth } // namespace bluetooth
} // namespace chromeos } // namespace chromeos
...@@ -324,6 +324,8 @@ component("bluetooth") { ...@@ -324,6 +324,8 @@ component("bluetooth") {
"dbus/bluetooth_agent_service_provider.h", "dbus/bluetooth_agent_service_provider.h",
"dbus/bluetooth_dbus_client_bundle.cc", "dbus/bluetooth_dbus_client_bundle.cc",
"dbus/bluetooth_dbus_client_bundle.h", "dbus/bluetooth_dbus_client_bundle.h",
"dbus/bluetooth_debug_manager_client.cc",
"dbus/bluetooth_debug_manager_client.h",
"dbus/bluetooth_device_client.cc", "dbus/bluetooth_device_client.cc",
"dbus/bluetooth_device_client.h", "dbus/bluetooth_device_client.h",
"dbus/bluetooth_gatt_application_service_provider.cc", "dbus/bluetooth_gatt_application_service_provider.cc",
...@@ -383,6 +385,8 @@ component("bluetooth") { ...@@ -383,6 +385,8 @@ component("bluetooth") {
"dbus/fake_bluetooth_agent_manager_client.h", "dbus/fake_bluetooth_agent_manager_client.h",
"dbus/fake_bluetooth_agent_service_provider.cc", "dbus/fake_bluetooth_agent_service_provider.cc",
"dbus/fake_bluetooth_agent_service_provider.h", "dbus/fake_bluetooth_agent_service_provider.h",
"dbus/fake_bluetooth_debug_manager_client.cc",
"dbus/fake_bluetooth_debug_manager_client.h",
"dbus/fake_bluetooth_device_client.cc", "dbus/fake_bluetooth_device_client.cc",
"dbus/fake_bluetooth_device_client.h", "dbus/fake_bluetooth_device_client.h",
"dbus/fake_bluetooth_gatt_application_service_provider.cc", "dbus/fake_bluetooth_gatt_application_service_provider.cc",
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "device/bluetooth/dbus/bluetooth_adapter_client.h" #include "device/bluetooth/dbus/bluetooth_adapter_client.h"
#include "device/bluetooth/dbus/bluetooth_agent_manager_client.h" #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h"
#include "device/bluetooth/dbus/bluetooth_debug_manager_client.h"
#include "device/bluetooth/dbus/bluetooth_device_client.h" #include "device/bluetooth/dbus/bluetooth_device_client.h"
#include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h" #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h"
#include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h" #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h"
...@@ -23,6 +24,7 @@ ...@@ -23,6 +24,7 @@
#include "device/bluetooth/dbus/bluetooth_profile_manager_client.h" #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_debug_manager_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_device_client.h" #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h" #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h" #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.h"
...@@ -44,6 +46,8 @@ BluetoothDBusClientBundle::BluetoothDBusClientBundle(bool use_fakes) ...@@ -44,6 +46,8 @@ BluetoothDBusClientBundle::BluetoothDBusClientBundle(bool use_fakes)
BluetoothLEAdvertisingManagerClient::Create()); BluetoothLEAdvertisingManagerClient::Create());
bluetooth_agent_manager_client_.reset( bluetooth_agent_manager_client_.reset(
BluetoothAgentManagerClient::Create()); BluetoothAgentManagerClient::Create());
bluetooth_debug_manager_client_.reset(
BluetoothDebugManagerClient::Create());
bluetooth_device_client_.reset(BluetoothDeviceClient::Create()); bluetooth_device_client_.reset(BluetoothDeviceClient::Create());
bluetooth_input_client_.reset(BluetoothInputClient::Create()); bluetooth_input_client_.reset(BluetoothInputClient::Create());
bluetooth_media_client_.reset(BluetoothMediaClient::Create()); bluetooth_media_client_.reset(BluetoothMediaClient::Create());
...@@ -65,6 +69,7 @@ BluetoothDBusClientBundle::BluetoothDBusClientBundle(bool use_fakes) ...@@ -65,6 +69,7 @@ BluetoothDBusClientBundle::BluetoothDBusClientBundle(bool use_fakes)
bluetooth_le_advertising_manager_client_.reset( bluetooth_le_advertising_manager_client_.reset(
new FakeBluetoothLEAdvertisingManagerClient); new FakeBluetoothLEAdvertisingManagerClient);
bluetooth_agent_manager_client_.reset(new FakeBluetoothAgentManagerClient); bluetooth_agent_manager_client_.reset(new FakeBluetoothAgentManagerClient);
bluetooth_debug_manager_client_.reset(new FakeBluetoothDebugManagerClient);
bluetooth_device_client_.reset(new FakeBluetoothDeviceClient); bluetooth_device_client_.reset(new FakeBluetoothDeviceClient);
bluetooth_input_client_.reset(new FakeBluetoothInputClient); bluetooth_input_client_.reset(new FakeBluetoothInputClient);
bluetooth_media_client_.reset(new FakeBluetoothMediaClient); bluetooth_media_client_.reset(new FakeBluetoothMediaClient);
......
...@@ -15,6 +15,7 @@ namespace bluez { ...@@ -15,6 +15,7 @@ namespace bluez {
class BluetoothAdapterClient; class BluetoothAdapterClient;
class BluetoothAgentManagerClient; class BluetoothAgentManagerClient;
class BluetoothDebugManagerClient;
class BluetoothDeviceClient; class BluetoothDeviceClient;
class BluetoothGattCharacteristicClient; class BluetoothGattCharacteristicClient;
class BluetoothGattDescriptorClient; class BluetoothGattDescriptorClient;
...@@ -50,6 +51,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDBusClientBundle { ...@@ -50,6 +51,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDBusClientBundle {
return bluetooth_agent_manager_client_.get(); return bluetooth_agent_manager_client_.get();
} }
BluetoothDebugManagerClient* bluetooth_debug_manager_client() {
return bluetooth_debug_manager_client_.get();
}
BluetoothDeviceClient* bluetooth_device_client() { BluetoothDeviceClient* bluetooth_device_client() {
return bluetooth_device_client_.get(); return bluetooth_device_client_.get();
} }
...@@ -103,6 +108,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDBusClientBundle { ...@@ -103,6 +108,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDBusClientBundle {
std::unique_ptr<BluetoothLEAdvertisingManagerClient> std::unique_ptr<BluetoothLEAdvertisingManagerClient>
bluetooth_le_advertising_manager_client_; bluetooth_le_advertising_manager_client_;
std::unique_ptr<BluetoothAgentManagerClient> bluetooth_agent_manager_client_; std::unique_ptr<BluetoothAgentManagerClient> bluetooth_agent_manager_client_;
std::unique_ptr<BluetoothDebugManagerClient> bluetooth_debug_manager_client_;
std::unique_ptr<BluetoothDeviceClient> bluetooth_device_client_; std::unique_ptr<BluetoothDeviceClient> bluetooth_device_client_;
std::unique_ptr<BluetoothGattCharacteristicClient> std::unique_ptr<BluetoothGattCharacteristicClient>
bluetooth_gatt_characteristic_client_; bluetooth_gatt_characteristic_client_;
......
// Copyright 2019 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.
#include "device/bluetooth/dbus/bluetooth_debug_manager_client.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/macros.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_manager.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace bluez {
// TODO(apusaka): move these consts to system_api/service_constants.h
namespace {
const char kBluetoothDebugObjectPath[] = "/org/chromium/Bluetooth";
const uint8_t kMinDispatcherLevel = 0;
const uint8_t kMinNewblueLevel = 0;
const uint8_t kMinBluezLevel = 0;
const uint8_t kMinKernelLevel = 0;
const uint8_t kMaxDispatcherLevel = 0xff;
const uint8_t kMaxNewblueLevel = 0xff;
const uint8_t kMaxBluezLevel = 2;
const uint8_t kMaxKernelLevel = 1;
} // namespace
const char BluetoothDebugManagerClient::kNoResponseError[] =
"org.chromium.Error.NoResponse";
const char BluetoothDebugManagerClient::kInvalidArgumentError[] =
"org.chromium.Error.InvalidArgument";
// The BluetoothDebugManagerClient implementation used in production.
class BluetoothDebugManagerClientImpl : public BluetoothDebugManagerClient,
public dbus::ObjectManager::Interface {
public:
BluetoothDebugManagerClientImpl() = default;
~BluetoothDebugManagerClientImpl() override = default;
// BluetoothDebugManagerClient override.
void SetLogLevels(const uint8_t dispatcher_level,
const uint8_t newblue_level,
const uint8_t bluez_level,
const uint8_t kernel_level,
base::OnceClosure callback,
ErrorCallback error_callback) override {
if (kMinDispatcherLevel > dispatcher_level ||
kMaxDispatcherLevel < dispatcher_level) {
std::move(error_callback)
.Run(kInvalidArgumentError, "dispatcher_level is out of range.");
return;
}
if (kMinNewblueLevel > newblue_level || kMaxNewblueLevel < newblue_level) {
std::move(error_callback)
.Run(kInvalidArgumentError, "newblue_level is out of range.");
return;
}
if (kMinBluezLevel > bluez_level || kMaxBluezLevel < bluez_level) {
std::move(error_callback)
.Run(kInvalidArgumentError, "bluez_level is out of range.");
return;
}
if (kMinKernelLevel > kernel_level || kMaxKernelLevel < kernel_level) {
std::move(error_callback)
.Run(kInvalidArgumentError, "kernel_level is out of range.");
return;
}
dbus::MethodCall method_call(bluetooth_debug::kBluetoothDebugInterface,
bluetooth_debug::kSetLevels);
dbus::MessageWriter writer(&method_call);
writer.AppendByte(dispatcher_level);
writer.AppendByte(newblue_level);
writer.AppendByte(bluez_level);
writer.AppendByte(kernel_level);
object_proxy_->CallMethodWithErrorCallback(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&BluetoothDebugManagerClientImpl::OnSuccess,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
base::BindOnce(&BluetoothDebugManagerClientImpl::OnError,
weak_ptr_factory_.GetWeakPtr(),
std::move(error_callback)));
}
protected:
void Init(dbus::Bus* bus,
const std::string& bluetooth_service_name) override {
DCHECK(bus);
object_proxy_ = bus->GetObjectProxy(
bluetooth_service_name, dbus::ObjectPath(kBluetoothDebugObjectPath));
object_manager_ = bus->GetObjectManager(
bluetooth_service_name,
dbus::ObjectPath(
bluetooth_object_manager::kBluetoothObjectManagerServicePath));
object_manager_->RegisterInterface(
bluetooth_debug::kBluetoothDebugInterface, this);
}
private:
// dbus::ObjectManager::Interface override.
dbus::PropertySet* CreateProperties(
dbus::ObjectProxy* object_proxy,
const dbus::ObjectPath& object_path,
const std::string& interface_name) override {
return new dbus::PropertySet(object_proxy, interface_name,
base::DoNothing());
}
// Called when a response for successful method call is received.
void OnSuccess(base::OnceClosure callback, dbus::Response* response) {
DCHECK(response);
std::move(callback).Run();
}
// Called when a response for a failed method call is received.
void OnError(ErrorCallback error_callback, dbus::ErrorResponse* response) {
// Error response has optional error message argument.
std::string error_name;
std::string error_message;
if (response) {
dbus::MessageReader reader(response);
error_name = response->GetErrorName();
reader.PopString(&error_message);
} else {
error_name = kNoResponseError;
}
std::move(error_callback).Run(error_name, error_message);
}
dbus::ObjectProxy* object_proxy_;
dbus::ObjectManager* object_manager_;
base::WeakPtrFactory<BluetoothDebugManagerClientImpl> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(BluetoothDebugManagerClientImpl);
};
BluetoothDebugManagerClient::BluetoothDebugManagerClient() = default;
BluetoothDebugManagerClient::~BluetoothDebugManagerClient() = default;
BluetoothDebugManagerClient* BluetoothDebugManagerClient::Create() {
return new BluetoothDebugManagerClientImpl();
}
} // namespace bluez
// Copyright 2019 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_DBUS_BLUETOOTH_DEBUG_MANAGER_CLIENT_H_
#define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_DEBUG_MANAGER_CLIENT_H_
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "base/values.h"
#include "dbus/object_path.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/dbus/bluez_dbus_client.h"
namespace bluez {
// BluetoothDebugManagerClient is used to communicate with the debug manager
// object of the Bluetooth daemon.
class DEVICE_BLUETOOTH_EXPORT BluetoothDebugManagerClient
: public BluezDBusClient {
public:
~BluetoothDebugManagerClient() override;
// The ErrorCallback is used by debug manager methods to indicate failure.
// It receives two arguments: the name of the error in |error_name| and
// an optional message in |error_message|.
typedef base::OnceCallback<void(const std::string& error_name,
const std::string& error_message)>
ErrorCallback;
// Invoke D-Bus API to set the levels of logging verbosity for each of
// the bluetooth daemons and kernel.
virtual void SetLogLevels(const uint8_t dispatcher_level,
const uint8_t newblue_level,
const uint8_t bluez_level,
const uint8_t kernel_level,
base::OnceClosure callback,
ErrorCallback error_callback) = 0;
// Creates the instance.
static BluetoothDebugManagerClient* Create();
// Constants used to indicate exceptional error conditions. These are
// returned as the |error_name| in ErrorCallback.
static const char kNoResponseError[];
static const char kInvalidArgumentError[];
protected:
BluetoothDebugManagerClient();
private:
DISALLOW_COPY_AND_ASSIGN(BluetoothDebugManagerClient);
};
} // namespace bluez
#endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_DEBUG_MANAGER_CLIENT_H_
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "device/base/features.h" #include "device/base/features.h"
#include "device/bluetooth/dbus/bluetooth_adapter_client.h" #include "device/bluetooth/dbus/bluetooth_adapter_client.h"
#include "device/bluetooth/dbus/bluetooth_agent_manager_client.h" #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h"
#include "device/bluetooth/dbus/bluetooth_debug_manager_client.h"
#include "device/bluetooth/dbus/bluetooth_device_client.h" #include "device/bluetooth/dbus/bluetooth_device_client.h"
#include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h" #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h"
#include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h" #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_client.h"
...@@ -109,6 +110,12 @@ bluez::BluezDBusManager::GetBluetoothAgentManagerClient() { ...@@ -109,6 +110,12 @@ bluez::BluezDBusManager::GetBluetoothAgentManagerClient() {
return client_bundle_->bluetooth_agent_manager_client(); return client_bundle_->bluetooth_agent_manager_client();
} }
BluetoothDebugManagerClient*
bluez::BluezDBusManager::GetBluetoothDebugManagerClient() {
DCHECK(object_manager_support_known_);
return client_bundle_->bluetooth_debug_manager_client();
}
BluetoothDeviceClient* bluez::BluezDBusManager::GetBluetoothDeviceClient() { BluetoothDeviceClient* bluez::BluezDBusManager::GetBluetoothDeviceClient() {
DCHECK(object_manager_support_known_); DCHECK(object_manager_support_known_);
return client_bundle_->bluetooth_device_client(); return client_bundle_->bluetooth_device_client();
...@@ -204,6 +211,8 @@ void BluezDBusManager::InitializeClients() { ...@@ -204,6 +211,8 @@ void BluezDBusManager::InitializeClients() {
bluetooth_service_name); bluetooth_service_name);
client_bundle_->bluetooth_agent_manager_client()->Init( client_bundle_->bluetooth_agent_manager_client()->Init(
GetSystemBus(), bluetooth_service_name); GetSystemBus(), bluetooth_service_name);
client_bundle_->bluetooth_debug_manager_client()->Init(
GetSystemBus(), bluetooth_service_name);
client_bundle_->bluetooth_device_client()->Init(GetSystemBus(), client_bundle_->bluetooth_device_client()->Init(GetSystemBus(),
bluetooth_service_name); bluetooth_service_name);
client_bundle_->bluetooth_gatt_characteristic_client()->Init( client_bundle_->bluetooth_gatt_characteristic_client()->Init(
...@@ -354,6 +363,12 @@ void BluezDBusManagerSetter::SetBluetoothAgentManagerClient( ...@@ -354,6 +363,12 @@ void BluezDBusManagerSetter::SetBluetoothAgentManagerClient(
->client_bundle_->bluetooth_agent_manager_client_ = std::move(client); ->client_bundle_->bluetooth_agent_manager_client_ = std::move(client);
} }
void BluezDBusManagerSetter::SetBluetoothDebugManagerClient(
std::unique_ptr<BluetoothDebugManagerClient> client) {
bluez::BluezDBusManager::Get()
->client_bundle_->bluetooth_debug_manager_client_ = std::move(client);
}
void BluezDBusManagerSetter::SetBluetoothDeviceClient( void BluezDBusManagerSetter::SetBluetoothDeviceClient(
std::unique_ptr<BluetoothDeviceClient> client) { std::unique_ptr<BluetoothDeviceClient> client) {
bluez::BluezDBusManager::Get()->client_bundle_->bluetooth_device_client_ = bluez::BluezDBusManager::Get()->client_bundle_->bluetooth_device_client_ =
......
...@@ -26,6 +26,7 @@ namespace bluez { ...@@ -26,6 +26,7 @@ namespace bluez {
// Style Note: Clients are sorted by names. // Style Note: Clients are sorted by names.
class BluetoothAdapterClient; class BluetoothAdapterClient;
class BluetoothAgentManagerClient; class BluetoothAgentManagerClient;
class BluetoothDebugManagerClient;
class BluetoothDeviceClient; class BluetoothDeviceClient;
class BluetoothGattCharacteristicClient; class BluetoothGattCharacteristicClient;
class BluetoothGattDescriptorClient; class BluetoothGattDescriptorClient;
...@@ -118,6 +119,7 @@ class DEVICE_BLUETOOTH_EXPORT BluezDBusManager { ...@@ -118,6 +119,7 @@ class DEVICE_BLUETOOTH_EXPORT BluezDBusManager {
BluetoothAdapterClient* GetBluetoothAdapterClient(); BluetoothAdapterClient* GetBluetoothAdapterClient();
BluetoothLEAdvertisingManagerClient* GetBluetoothLEAdvertisingManagerClient(); BluetoothLEAdvertisingManagerClient* GetBluetoothLEAdvertisingManagerClient();
BluetoothAgentManagerClient* GetBluetoothAgentManagerClient(); BluetoothAgentManagerClient* GetBluetoothAgentManagerClient();
BluetoothDebugManagerClient* GetBluetoothDebugManagerClient();
BluetoothDeviceClient* GetBluetoothDeviceClient(); BluetoothDeviceClient* GetBluetoothDeviceClient();
BluetoothGattCharacteristicClient* GetBluetoothGattCharacteristicClient(); BluetoothGattCharacteristicClient* GetBluetoothGattCharacteristicClient();
BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient(); BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient();
...@@ -188,6 +190,8 @@ class DEVICE_BLUETOOTH_EXPORT BluezDBusManagerSetter { ...@@ -188,6 +190,8 @@ class DEVICE_BLUETOOTH_EXPORT BluezDBusManagerSetter {
std::unique_ptr<BluetoothLEAdvertisingManagerClient> client); std::unique_ptr<BluetoothLEAdvertisingManagerClient> client);
void SetBluetoothAgentManagerClient( void SetBluetoothAgentManagerClient(
std::unique_ptr<BluetoothAgentManagerClient> client); std::unique_ptr<BluetoothAgentManagerClient> client);
void SetBluetoothDebugManagerClient(
std::unique_ptr<BluetoothDebugManagerClient> client);
void SetBluetoothDeviceClient(std::unique_ptr<BluetoothDeviceClient> client); void SetBluetoothDeviceClient(std::unique_ptr<BluetoothDeviceClient> client);
void SetBluetoothGattCharacteristicClient( void SetBluetoothGattCharacteristicClient(
std::unique_ptr<BluetoothGattCharacteristicClient> client); std::unique_ptr<BluetoothGattCharacteristicClient> client);
......
// Copyright (c) 2019 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.
#include "device/bluetooth/dbus/fake_bluetooth_debug_manager_client.h"
namespace bluez {
FakeBluetoothDebugManagerClient::FakeBluetoothDebugManagerClient() = default;
FakeBluetoothDebugManagerClient::~FakeBluetoothDebugManagerClient() = default;
void FakeBluetoothDebugManagerClient::Init(
dbus::Bus* bus,
const std::string& bluetooth_service_name) {}
void FakeBluetoothDebugManagerClient::SetLogLevels(
const uint8_t dispatcher_level,
const uint8_t newblue_level,
const uint8_t bluez_level,
const uint8_t kernel_level,
base::OnceClosure callback,
ErrorCallback error_callback) {
if (should_next_set_log_levels_fail_) {
should_next_set_log_levels_fail_ = false;
set_log_levels_fail_count_++;
std::move(error_callback).Run(kNoResponseError, "");
return;
}
dispatcher_level_ = dispatcher_level;
std::move(callback).Run();
}
void FakeBluetoothDebugManagerClient::MakeNextSetLogLevelsFail() {
should_next_set_log_levels_fail_ = true;
}
} // namespace bluez
// Copyright (c) 2019 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_DBUS_FAKE_BLUETOOTH_DEBUG_MANAGER_CLIENT_H_
#define DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_DEBUG_MANAGER_CLIENT_H_
#include "base/bind.h"
#include "base/callback.h"
#include "dbus/object_path.h"
#include "dbus/property.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/dbus/bluetooth_debug_manager_client.h"
namespace bluez {
// FakeBluetoothDebugManagerClient simulates the behavior of the Bluetooth
// Daemon's debug manager object and is used both in test cases in place of a
// mock and on the Linux desktop.
class DEVICE_BLUETOOTH_EXPORT FakeBluetoothDebugManagerClient
: public BluetoothDebugManagerClient {
public:
FakeBluetoothDebugManagerClient();
~FakeBluetoothDebugManagerClient() override;
// BluetoothDebugManagerClient overrides
void Init(dbus::Bus* bus, const std::string& bluetooth_service_name) override;
void SetLogLevels(const uint8_t dispatcher_level,
const uint8_t newblue_level,
const uint8_t bluez_level,
const uint8_t kernel_level,
base::OnceClosure callback,
ErrorCallback error_callback) override;
// Make the next call to SetLogLevels() to fail only once.
void MakeNextSetLogLevelsFail();
int set_log_levels_fail_count() const { return set_log_levels_fail_count_; }
int dispatcher_level() const { return dispatcher_level_; }
private:
// When set, next call to SetLogLevels() will fail.
bool should_next_set_log_levels_fail_ = false;
// Counter to track how many times SetLogLevels() fails.
int set_log_levels_fail_count_ = 0;
// The latest dispatcher_level assigned.
int dispatcher_level_ = 0;
};
} // namespace bluez
#endif // DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_DEBUG_MANAGER_CLIENT_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment