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) {
41 ~DaemonImpl() override {
45 CleanUpBluetoothStack();
48 void StartMainLoop() override {
52 Settings* GetSettings() const override {
53 return settings_.get();
56 base::MessageLoop* GetMessageLoop() const override {
57 return message_loop_.get();
61 bool StartUpBluetoothInterfaces() {
62 if (!hal::BluetoothInterface::Initialize())
65 if (!hal::BluetoothGattInterface::Initialize())
71 ShutDownBluetoothInterfaces();
75 void ShutDownBluetoothInterfaces() {
76 if (hal::BluetoothGattInterface::IsInitialized())
77 hal::BluetoothGattInterface::CleanUp();
78 if (hal::BluetoothInterface::IsInitialized())
79 hal::BluetoothInterface::CleanUp();
82 void CleanUpBluetoothStack() {
83 // The Adapter object needs to be cleaned up before the HAL interfaces.
86 ShutDownBluetoothInterfaces();
90 // If an IPC socket path was given, initialize IPC with it. Otherwise
91 // initialize Binder IPC.
92 if (settings_->UseSocketIPC()) {
93 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, nullptr)) {
94 LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
97 } else if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, nullptr)) {
98 LOG(ERROR) << "Failed to set up Binder IPCManager";
105 bool Init() override {
106 CHECK(!initialized_);
107 message_loop_.reset(new base::MessageLoop());
109 settings_.reset(new Settings());
110 if (!settings_->Init()) {
111 LOG(ERROR) << "Failed to set up Settings";
115 if (!StartUpBluetoothInterfaces()) {
116 LOG(ERROR) << "Failed to set up HAL Bluetooth interfaces";
120 adapter_ = Adapter::Create();
121 ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
124 CleanUpBluetoothStack();
129 LOG(INFO) << "Daemon initialized";
135 std::unique_ptr<base::MessageLoop> message_loop_;
136 std::unique_ptr<Settings> settings_;
137 std::unique_ptr<Adapter> adapter_;
138 std::unique_ptr<ipc::IPCManager> ipc_manager_;
140 DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
146 bool Daemon::Initialize() {
149 g_daemon = new DaemonImpl();
150 if (g_daemon->Init())
153 LOG(ERROR) << "Failed to initialize the Daemon object";
162 void Daemon::ShutDown() {
169 void Daemon::InitializeForTesting(Daemon* test_daemon) {
173 g_daemon = test_daemon;
177 Daemon* Daemon::Get() {
182 } // namespace bluetooth