OSDN Git Service

am 2378ca29: (-s ours) am 26c3dd41: Fail LE secure pairing for secure only peripherals
[android-x86/system-bt.git] / service / daemon.cpp
1 //
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 <memory>
20
21 #include <base/logging.h>
22
23 #include "service/adapter.h"
24 #include "service/hal/bluetooth_interface.h"
25 #include "service/ipc/ipc_manager.h"
26 #include "service/settings.h"
27
28 namespace bluetooth {
29
30 namespace {
31
32 // The global Daemon instance.
33 Daemon* g_daemon = nullptr;
34
35 class DaemonImpl : public Daemon {
36  public:
37   DaemonImpl() : initialized_(false) {
38   }
39
40   ~DaemonImpl() override {
41     if (!initialized_)
42       return;
43
44     CleanUpBluetoothStack();
45   }
46
47   void StartMainLoop() override {
48     message_loop_->Run();
49   }
50
51   Settings* GetSettings() const override {
52     return settings_.get();
53   }
54
55   base::MessageLoop* GetMessageLoop() const override {
56     return message_loop_.get();
57   }
58
59  private:
60   void CleanUpBluetoothStack() {
61     // The Adapter object needs to be cleaned up before the HAL interfaces.
62     ipc_manager_.reset();
63     adapter_.reset();
64     hal::BluetoothInterface::CleanUp();
65   }
66
67   bool SetUpIPC() {
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";
73         return false;
74       }
75     } else if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, nullptr)) {
76       LOG(ERROR) << "Failed to set up Binder IPCManager";
77       return false;
78     }
79
80     return true;
81   }
82
83   bool Init() override {
84     CHECK(!initialized_);
85     message_loop_.reset(new base::MessageLoop());
86
87     settings_.reset(new Settings());
88     if (!settings_->Init()) {
89       LOG(ERROR) << "Failed to set up Settings";
90       return false;
91     }
92
93     if (!hal::BluetoothInterface::Initialize()) {
94       LOG(ERROR) << "Failed to set up BluetoothInterface";
95       return false;
96     }
97
98     adapter_.reset(new Adapter());
99     ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
100
101     if (!SetUpIPC()) {
102       CleanUpBluetoothStack();
103       return false;
104     }
105
106     initialized_ = true;
107     LOG(INFO) << "Daemon initialized";
108
109     return true;
110   }
111
112   bool 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_;
117
118   DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
119 };
120
121 }  // namespace
122
123 // static
124 bool Daemon::Initialize() {
125   CHECK(!g_daemon);
126
127   g_daemon = new DaemonImpl();
128   if (g_daemon->Init())
129     return true;
130
131   LOG(ERROR) << "Failed to initialize the Daemon object";
132
133   delete g_daemon;
134   g_daemon = nullptr;
135
136   return false;
137 }
138
139 // static
140 void Daemon::ShutDown() {
141   CHECK(g_daemon);
142   delete g_daemon;
143   g_daemon = nullptr;
144 }
145
146 // static
147 void Daemon::InitializeForTesting(Daemon* test_daemon) {
148   CHECK(test_daemon);
149   CHECK(!g_daemon);
150
151   g_daemon = test_daemon;
152 }
153
154 // static
155 Daemon* Daemon::Get() {
156   CHECK(g_daemon);
157   return g_daemon;
158 }
159
160 }  // namespace bluetooth