2 // Copyright (C) 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>
23 #include "service/adapter.h"
24 #include "service/hal/bluetooth_gatt_interface.h"
25 #include "service/hal/bluetooth_interface.h"
26 #include "service/ipc/ipc_manager.h"
27 #include "service/settings.h"
33 // The global Daemon instance.
34 Daemon* g_daemon = nullptr;
36 class DaemonImpl : public Daemon {
38 DaemonImpl() : initialized_(false) {}
40 ~DaemonImpl() override {
41 if (!initialized_) return;
43 CleanUpBluetoothStack();
46 void StartMainLoop() override { message_loop_->Run(); }
48 Settings* GetSettings() const override { return settings_.get(); }
50 base::MessageLoop* GetMessageLoop() const override {
51 return message_loop_.get();
55 bool StartUpBluetoothInterfaces() {
56 if (!hal::BluetoothInterface::Initialize()) goto failed;
58 if (!hal::BluetoothGattInterface::Initialize()) goto failed;
63 ShutDownBluetoothInterfaces();
67 void ShutDownBluetoothInterfaces() {
68 if (hal::BluetoothGattInterface::IsInitialized())
69 hal::BluetoothGattInterface::CleanUp();
70 if (hal::BluetoothInterface::IsInitialized())
71 hal::BluetoothInterface::CleanUp();
74 void CleanUpBluetoothStack() {
75 // The Adapter object needs to be cleaned up before the HAL interfaces.
78 ShutDownBluetoothInterfaces();
82 // If an IPC socket path was given, initialize IPC with it. Otherwise
83 // initialize Binder IPC.
84 if (settings_->UseSocketIPC()) {
85 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, nullptr)) {
86 LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
89 } else if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, nullptr)) {
90 LOG(ERROR) << "Failed to set up Binder IPCManager";
97 bool Init() override {
99 message_loop_.reset(new base::MessageLoop());
101 settings_.reset(new Settings());
102 if (!settings_->Init()) {
103 LOG(ERROR) << "Failed to set up Settings";
107 if (!StartUpBluetoothInterfaces()) {
108 LOG(ERROR) << "Failed to set up HAL Bluetooth interfaces";
112 adapter_ = Adapter::Create();
113 ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
116 CleanUpBluetoothStack();
121 LOG(INFO) << "Daemon initialized";
127 std::unique_ptr<base::MessageLoop> message_loop_;
128 std::unique_ptr<Settings> settings_;
129 std::unique_ptr<Adapter> adapter_;
130 std::unique_ptr<ipc::IPCManager> ipc_manager_;
132 DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
138 bool Daemon::Initialize() {
141 g_daemon = new DaemonImpl();
142 if (g_daemon->Init()) return true;
144 LOG(ERROR) << "Failed to initialize the Daemon object";
153 void Daemon::ShutDown() {
160 void Daemon::InitializeForTesting(Daemon* test_daemon) {
164 g_daemon = test_daemon;
168 Daemon* Daemon::Get() {
173 } // namespace bluetooth