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_interface.h"
25 #include "service/ipc/ipc_manager.h"
26 #include "service/settings.h"
32 // The global Daemon instance.
33 Daemon* g_daemon = nullptr;
35 class DaemonImpl : public Daemon {
37 DaemonImpl() : initialized_(false) {
40 ~DaemonImpl() override {
44 CleanUpBluetoothStack();
47 void StartMainLoop() override {
51 Settings* GetSettings() const override {
52 return settings_.get();
55 base::MessageLoop* GetMessageLoop() const override {
56 return message_loop_.get();
60 void CleanUpBluetoothStack() {
61 // The Adapter object needs to be cleaned up before the HAL interfaces.
64 hal::BluetoothInterface::CleanUp();
68 // If an IPC socket path was given, initialize IPC with it. Otherwise
69 // initialize Binder IPC.
70 if (settings_->UseSocketIPC()) {
71 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_UNIX, nullptr)) {
72 LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
75 } else if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, nullptr)) {
76 LOG(ERROR) << "Failed to set up Binder IPCManager";
83 bool Init() override {
85 message_loop_.reset(new base::MessageLoop());
87 settings_.reset(new Settings());
88 if (!settings_->Init()) {
89 LOG(ERROR) << "Failed to set up Settings";
93 if (!hal::BluetoothInterface::Initialize()) {
94 LOG(ERROR) << "Failed to set up BluetoothInterface";
98 adapter_.reset(new Adapter());
99 ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
102 CleanUpBluetoothStack();
107 LOG(INFO) << "Daemon initialized";
113 std::unique_ptr<base::MessageLoop> message_loop_;
114 std::unique_ptr<Settings> settings_;
115 std::unique_ptr<Adapter> adapter_;
116 std::unique_ptr<ipc::IPCManager> ipc_manager_;
118 DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
124 bool Daemon::Initialize() {
127 g_daemon = new DaemonImpl();
128 if (g_daemon->Init())
131 LOG(ERROR) << "Failed to initialize the Daemon object";
140 void Daemon::ShutDown() {
147 void Daemon::InitializeForTesting(Daemon* test_daemon) {
151 g_daemon = test_daemon;
155 Daemon* Daemon::Get() {
160 } // namespace bluetooth