blob: a88fc5f38172020bc575dc8c958df3e5a1444fd0 [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
19#include <base/logging.h>
20
21#include "service/core_stack.h"
22#include "service/ipc/ipc_manager.h"
23#include "service/settings.h"
24
25namespace bluetooth {
26
27namespace {
28
29// The global Daemon instance.
30Daemon* g_daemon = nullptr;
31
32} // namespace
33
34// static
35bool Daemon::Initialize() {
36 CHECK(!g_daemon);
37
38 g_daemon = new Daemon();
39 if (g_daemon->Init())
40 return true;
41
42 LOG(ERROR) << "Failed to initialize the Daemon object";
43
44 delete g_daemon;
45 g_daemon = nullptr;
46
47 return false;
48}
49
50// static
51void Daemon::ShutDown() {
52 CHECK(g_daemon);
53 CHECK(g_daemon->initialized_);
54
55 delete g_daemon;
56 g_daemon = NULL;
57}
58
59// static
60Daemon* Daemon::Get() {
61 CHECK(g_daemon);
62 return g_daemon;
63}
64
65Daemon::Daemon() : initialized_(false) {
66}
67
68Daemon::~Daemon() {
69}
70
71void Daemon::StartMainLoop() {
72 CHECK(initialized_);
73 message_loop_->Run();
74}
75
76bool Daemon::Init() {
77 CHECK(!initialized_);
78
79 message_loop_.reset(new base::MessageLoop());
80
81 settings_.reset(new Settings());
82 if (!settings_->Init()) {
83 LOG(ERROR) << "Failed to set up Settings";
84 return false;
85 }
86
87 core_stack_.reset(new CoreStack());
88 if (!core_stack_->Initialize()) {
89 LOG(ERROR) << "Failed to set up CoreStack";
90 return false;
91 }
92
93 ipc_manager_.reset(new ipc::IPCManager(core_stack_.get()));
94
95 // If an IPC socket path was given, initialize the UNIX domain socket based
96 // IPC layer.
97 if (!settings_->ipc_socket_path().empty() &&
98 !ipc_manager_->Start(ipc::IPCManager::TYPE_UNIX)) {
99 LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
100 return false;
101 }
102
103 initialized_ = true;
104
105 return true;
106}
107
108} // namespace bluetooth