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_gatt_interface.h"
26 #include "service/hal/bluetooth_interface.h"
27 #include "service/ipc/ipc_manager.h"
28 #include "service/settings.h"
29 #include "service/switches.h"
35 // The global Daemon instance.
36 Daemon* g_daemon = nullptr;
38 class DaemonImpl : public Daemon, public ipc::IPCManager::Delegate {
40 DaemonImpl() : initialized_(false) {}
42 ~DaemonImpl() override {
43 if (!initialized_) return;
45 CleanUpBluetoothStack();
48 void StartMainLoop() override { base::RunLoop().Run(); }
50 Settings* GetSettings() const override { return settings_.get(); }
52 base::MessageLoop* GetMessageLoop() const override {
53 return message_loop_.get();
57 // ipc::IPCManager::Delegate implementation:
58 void OnIPCHandlerStarted(ipc::IPCManager::Type /* type */) override {
59 if (!settings_->EnableOnStart()) return;
60 adapter_->Enable(false /* start_restricted */);
63 void OnIPCHandlerStopped(ipc::IPCManager::Type /* type */) override {
67 bool StartUpBluetoothInterfaces() {
68 if (!hal::BluetoothInterface::Initialize()) goto failed;
70 if (!hal::BluetoothGattInterface::Initialize()) goto failed;
75 ShutDownBluetoothInterfaces();
79 void ShutDownBluetoothInterfaces() {
80 if (hal::BluetoothGattInterface::IsInitialized())
81 hal::BluetoothGattInterface::CleanUp();
82 if (hal::BluetoothInterface::IsInitialized())
83 hal::BluetoothInterface::CleanUp();
86 void CleanUpBluetoothStack() {
87 // The Adapter object needs to be cleaned up before the HAL interfaces.
90 ShutDownBluetoothInterfaces();
94 // If an IPC socket path was given, initialize IPC with it. Otherwise
95 // initialize Binder IPC.
96 if (settings_->UseSocketIPC()) {
97 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, this)) {
98 LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
104 #if !defined(OS_GENERIC)
105 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, this)) {
106 LOG(ERROR) << "Failed to set up Binder IPCManager";
110 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_DBUS, this)) {
111 LOG(ERROR) << "Failed to set up DBus IPCManager";
119 bool Init() override {
120 CHECK(!initialized_);
121 message_loop_.reset(new base::MessageLoop());
123 settings_.reset(new Settings());
124 if (!settings_->Init()) {
125 LOG(ERROR) << "Failed to set up Settings";
129 if (!StartUpBluetoothInterfaces()) {
130 LOG(ERROR) << "Failed to set up HAL Bluetooth interfaces";
134 adapter_ = Adapter::Create();
135 ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
138 CleanUpBluetoothStack();
143 LOG(INFO) << "Daemon initialized";
149 std::unique_ptr<base::MessageLoop> message_loop_;
150 std::unique_ptr<Settings> settings_;
151 std::unique_ptr<Adapter> adapter_;
152 std::unique_ptr<ipc::IPCManager> ipc_manager_;
154 DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
160 bool Daemon::Initialize() {
163 g_daemon = new DaemonImpl();
164 if (g_daemon->Init()) return true;
166 LOG(ERROR) << "Failed to initialize the Daemon object";
175 void Daemon::ShutDown() {
182 void Daemon::InitializeForTesting(Daemon* test_daemon) {
186 g_daemon = test_daemon;
190 Daemon* Daemon::Get() {
195 } // namespace bluetooth