blob: 3d0e8473ecb4f8f9a363633e3dbf1e108b15c0c2 [file] [log] [blame]
Arman Ugurayfe65fb72015-07-24 19:14:42 -07001//
2// Copyright (C) 2015 Google, Inc.
3//
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:
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
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.
15//
16
17#include "service/daemon.h"
18
Arman Ugurayf8881fe2015-08-04 12:56:56 -070019#include <memory>
20
Arman Ugurayfe65fb72015-07-24 19:14:42 -070021#include <base/logging.h>
22
23#include "service/core_stack.h"
24#include "service/ipc/ipc_manager.h"
25#include "service/settings.h"
26
27namespace bluetooth {
28
29namespace {
30
31// The global Daemon instance.
32Daemon* g_daemon = nullptr;
33
Arman Ugurayf8881fe2015-08-04 12:56:56 -070034class DaemonImpl : public Daemon {
35 public:
36 DaemonImpl() = default;
37 ~DaemonImpl() override = default;
38
39 void StartMainLoop() override {
40 message_loop_->Run();
41 }
42
43 Settings* GetSettings() const override {
44 return settings_.get();
45 }
46
47 base::MessageLoop* GetMessageLoop() const override {
48 return message_loop_.get();
49 }
50
51 private:
52 bool Init() override {
53 message_loop_.reset(new base::MessageLoop());
54
55 settings_.reset(new Settings());
56 if (!settings_->Init()) {
57 LOG(ERROR) << "Failed to set up Settings";
58 return false;
59 }
60
61 core_stack_ = CoreStack::Create();
62 if (!core_stack_->Initialize()) {
63 LOG(ERROR) << "Failed to set up CoreStack";
64 return false;
65 }
66
67 ipc_manager_.reset(new ipc::IPCManager(core_stack_.get()));
68
Arman Ugurayb2286f32015-08-05 21:19:02 -070069 // If an IPC socket path was given, initialize IPC with it. Otherwise
70 // initialize Binder IPC.
71 if (settings_->UseSocketIPC()) {
72 if (!ipc_manager_->Start(ipc::IPCManager::TYPE_UNIX, nullptr)) {
73 LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
74 return false;
75 }
76 } else if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, nullptr)) {
77 LOG(ERROR) << "Failed to set up Binder IPCManager";
Arman Ugurayf8881fe2015-08-04 12:56:56 -070078 return false;
79 }
80
81 return true;
82 }
83
84 std::unique_ptr<base::MessageLoop> message_loop_;
85 std::unique_ptr<Settings> settings_;
86 std::unique_ptr<CoreStack> core_stack_;
87 std::unique_ptr<ipc::IPCManager> ipc_manager_;
88
89 DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
90};
91
Arman Ugurayfe65fb72015-07-24 19:14:42 -070092} // namespace
93
94// static
95bool Daemon::Initialize() {
96 CHECK(!g_daemon);
97
Arman Ugurayf8881fe2015-08-04 12:56:56 -070098 g_daemon = new DaemonImpl();
Arman Ugurayfe65fb72015-07-24 19:14:42 -070099 if (g_daemon->Init())
100 return true;
101
102 LOG(ERROR) << "Failed to initialize the Daemon object";
103
104 delete g_daemon;
105 g_daemon = nullptr;
106
107 return false;
108}
109
110// static
111void Daemon::ShutDown() {
112 CHECK(g_daemon);
Arman Ugurayfe65fb72015-07-24 19:14:42 -0700113 delete g_daemon;
Arman Ugurayf8881fe2015-08-04 12:56:56 -0700114 g_daemon = nullptr;
115}
116
117// static
118void Daemon::InitializeForTesting(Daemon* test_daemon) {
119 CHECK(test_daemon);
120 CHECK(!g_daemon);
121
122 g_daemon = test_daemon;
Arman Ugurayfe65fb72015-07-24 19:14:42 -0700123}
124
125// static
126Daemon* Daemon::Get() {
127 CHECK(g_daemon);
128 return g_daemon;
129}
130
Arman Ugurayfe65fb72015-07-24 19:14:42 -0700131} // namespace bluetooth