2 // Copyright 2015 Google, Inc.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "service/daemon.h"
21 #include <base/logging.h>
22 #include <base/run_loop.h>
24 #include "service/adapter.h"
25 #include "service/hal/bluetooth_av_interface.h"
26 #include "service/hal/bluetooth_avrcp_interface.h"
27 #include "service/hal/bluetooth_gatt_interface.h"
28 #include "service/hal/bluetooth_interface.h"
29 #include "service/ipc/ipc_manager.h"
30 #include "service/settings.h"
31 #include "service/switches.h"
37 // The global Daemon instance.
38 Daemon* g_daemon = nullptr;
40 class DaemonImpl : public Daemon, public ipc::IPCManager::Delegate {
42 DaemonImpl() : initialized_(false) {}
44 ~DaemonImpl() override {
45 if (!initialized_) return;
47 CleanUpBluetoothStack();
50 void StartMainLoop() override { base::RunLoop().Run(); }
52 Settings* GetSettings() const override { return settings_.get(); }
54 base::MessageLoop* GetMessageLoop() const override {
55 return message_loop_.get();
59 // ipc::IPCManager::Delegate implementation:
60 void OnIPCHandlerStarted(ipc::IPCManager::Type /* type */) override {
61 if (!settings_->EnableOnStart()) return;
65 void OnIPCHandlerStopped(ipc::IPCManager::Type /* type */) override {
69 bool StartUpBluetoothInterfaces() {
70 if (!hal::BluetoothInterface::Initialize()) goto failed;
72 if (!hal::BluetoothGattInterface::Initialize()) goto failed;
74 if (!hal::BluetoothAvInterface::Initialize()) goto failed;
76 if (!hal::BluetoothAvrcpInterface::Initialize()) goto failed;
81 ShutDownBluetoothInterfaces();
85 void ShutDownBluetoothInterfaces() {
86 if (hal::BluetoothGattInterface::IsInitialized())
87 hal::BluetoothGattInterface::CleanUp();
88 if (hal::BluetoothInterface::IsInitialized())
89 hal::BluetoothInterface::CleanUp();
90 if (hal::BluetoothAvInterface::IsInitialized())
91 hal::BluetoothAvInterface::CleanUp();
92 if (hal::BluetoothAvrcpInterface::IsInitialized())
93 hal::BluetoothAvrcpInterface::CleanUp();
96 void CleanUpBluetoothStack() {
97 // The Adapter object needs to be cleaned up before the HAL interfaces.
100 ShutDownBluetoothInterfaces();
104 // If an IPC socket path was given, initialize IPC with it. Otherwise
105 // initialize Binder IPC.
106 if (settings_->UseSocketIPC()) {
107 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, this)) {
108 LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
114 #if !defined(OS_GENERIC)
115 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, this)) {
116 LOG(ERROR) << "Failed to set up Binder IPCManager";
120 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_DBUS, this)) {
121 LOG(ERROR) << "Failed to set up DBus IPCManager";
129 bool Init() override {
130 CHECK(!initialized_);
131 message_loop_.reset(new base::MessageLoop());
133 settings_.reset(new Settings());
134 if (!settings_->Init()) {
135 LOG(ERROR) << "Failed to set up Settings";
139 if (!StartUpBluetoothInterfaces()) {
140 LOG(ERROR) << "Failed to set up HAL Bluetooth interfaces";
144 adapter_ = Adapter::Create();
145 ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
148 CleanUpBluetoothStack();
153 LOG(INFO) << "Daemon initialized";
159 std::unique_ptr<base::MessageLoop> message_loop_;
160 std::unique_ptr<Settings> settings_;
161 std::unique_ptr<Adapter> adapter_;
162 std::unique_ptr<ipc::IPCManager> ipc_manager_;
164 DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
170 bool Daemon::Initialize() {
173 g_daemon = new DaemonImpl();
174 if (g_daemon->Init()) return true;
176 LOG(ERROR) << "Failed to initialize the Daemon object";
185 void Daemon::ShutDown() {
192 void Daemon::InitializeForTesting(Daemon* test_daemon) {
196 g_daemon = test_daemon;
200 Daemon* Daemon::Get() {
205 } // namespace bluetooth