Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 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 Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
Hidehiko Abe | aeca7aa | 2017-12-13 18:57:10 +0900 | [diff] [blame] | 22 | #include <base/run_loop.h> |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 23 | |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 24 | #include "service/adapter.h" |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 25 | #include "service/hal/bluetooth_av_interface.h" |
| 26 | #include "service/hal/bluetooth_avrcp_interface.h" |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 27 | #include "service/hal/bluetooth_gatt_interface.h" |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 28 | #include "service/hal/bluetooth_interface.h" |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 29 | #include "service/ipc/ipc_manager.h" |
| 30 | #include "service/settings.h" |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 31 | #include "service/switches.h" |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 32 | |
| 33 | namespace bluetooth { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // The global Daemon instance. |
| 38 | Daemon* g_daemon = nullptr; |
| 39 | |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 40 | class DaemonImpl : public Daemon, public ipc::IPCManager::Delegate { |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 41 | public: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 42 | DaemonImpl() : initialized_(false) {} |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 43 | |
| 44 | ~DaemonImpl() override { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 45 | if (!initialized_) return; |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 46 | |
| 47 | CleanUpBluetoothStack(); |
| 48 | } |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 49 | |
Hidehiko Abe | aeca7aa | 2017-12-13 18:57:10 +0900 | [diff] [blame] | 50 | void StartMainLoop() override { base::RunLoop().Run(); } |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 51 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 52 | Settings* GetSettings() const override { return settings_.get(); } |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 53 | |
| 54 | base::MessageLoop* GetMessageLoop() const override { |
| 55 | return message_loop_.get(); |
| 56 | } |
| 57 | |
| 58 | private: |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 59 | // ipc::IPCManager::Delegate implementation: |
| 60 | void OnIPCHandlerStarted(ipc::IPCManager::Type /* type */) override { |
| 61 | if (!settings_->EnableOnStart()) return; |
Martin Brabham | 1a88ace | 2019-05-10 12:42:15 -0700 | [diff] [blame] | 62 | adapter_->Enable(); |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void OnIPCHandlerStopped(ipc::IPCManager::Type /* type */) override { |
| 66 | // Do nothing. |
| 67 | } |
| 68 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 69 | bool StartUpBluetoothInterfaces() { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 70 | if (!hal::BluetoothInterface::Initialize()) goto failed; |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 71 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 72 | if (!hal::BluetoothGattInterface::Initialize()) goto failed; |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 73 | |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 74 | if (!hal::BluetoothAvInterface::Initialize()) goto failed; |
| 75 | |
| 76 | if (!hal::BluetoothAvrcpInterface::Initialize()) goto failed; |
| 77 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 78 | return true; |
| 79 | |
| 80 | failed: |
| 81 | ShutDownBluetoothInterfaces(); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | void ShutDownBluetoothInterfaces() { |
| 86 | if (hal::BluetoothGattInterface::IsInitialized()) |
| 87 | hal::BluetoothGattInterface::CleanUp(); |
| 88 | if (hal::BluetoothInterface::IsInitialized()) |
| 89 | hal::BluetoothInterface::CleanUp(); |
Bailey Forrest | 62aa15f | 2017-01-30 17:38:58 -0800 | [diff] [blame] | 90 | if (hal::BluetoothAvInterface::IsInitialized()) |
| 91 | hal::BluetoothAvInterface::CleanUp(); |
| 92 | if (hal::BluetoothAvrcpInterface::IsInitialized()) |
| 93 | hal::BluetoothAvrcpInterface::CleanUp(); |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 96 | void CleanUpBluetoothStack() { |
| 97 | // The Adapter object needs to be cleaned up before the HAL interfaces. |
| 98 | ipc_manager_.reset(); |
| 99 | adapter_.reset(); |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 100 | ShutDownBluetoothInterfaces(); |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 101 | } |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 102 | |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 103 | bool SetUpIPC() { |
Arman Uguray | b2286f3 | 2015-08-05 21:19:02 -0700 | [diff] [blame] | 104 | // If an IPC socket path was given, initialize IPC with it. Otherwise |
| 105 | // initialize Binder IPC. |
| 106 | if (settings_->UseSocketIPC()) { |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 107 | if (!ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, this)) { |
Arman Uguray | b2286f3 | 2015-08-05 21:19:02 -0700 | [diff] [blame] | 108 | LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager"; |
| 109 | return false; |
| 110 | } |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
| 114 | #if !defined(OS_GENERIC) |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 115 | if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, this)) { |
Arman Uguray | b2286f3 | 2015-08-05 21:19:02 -0700 | [diff] [blame] | 116 | LOG(ERROR) << "Failed to set up Binder IPCManager"; |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 119 | #else |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 120 | if (!ipc_manager_->Start(ipc::IPCManager::TYPE_DBUS, this)) { |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 121 | LOG(ERROR) << "Failed to set up DBus IPCManager"; |
| 122 | return false; |
| 123 | } |
| 124 | #endif |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 129 | bool Init() override { |
| 130 | CHECK(!initialized_); |
| 131 | message_loop_.reset(new base::MessageLoop()); |
| 132 | |
| 133 | settings_.reset(new Settings()); |
| 134 | if (!settings_->Init()) { |
| 135 | LOG(ERROR) << "Failed to set up Settings"; |
| 136 | return false; |
| 137 | } |
| 138 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 139 | if (!StartUpBluetoothInterfaces()) { |
| 140 | LOG(ERROR) << "Failed to set up HAL Bluetooth interfaces"; |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
Arman Uguray | 0a0d393 | 2015-11-19 15:57:57 -0800 | [diff] [blame] | 144 | adapter_ = Adapter::Create(); |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 145 | ipc_manager_.reset(new ipc::IPCManager(adapter_.get())); |
| 146 | |
| 147 | if (!SetUpIPC()) { |
| 148 | CleanUpBluetoothStack(); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | initialized_ = true; |
| 153 | LOG(INFO) << "Daemon initialized"; |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | bool initialized_; |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 159 | std::unique_ptr<base::MessageLoop> message_loop_; |
| 160 | std::unique_ptr<Settings> settings_; |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 161 | std::unique_ptr<Adapter> adapter_; |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 162 | std::unique_ptr<ipc::IPCManager> ipc_manager_; |
| 163 | |
| 164 | DISALLOW_COPY_AND_ASSIGN(DaemonImpl); |
| 165 | }; |
| 166 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 167 | } // namespace |
| 168 | |
| 169 | // static |
| 170 | bool Daemon::Initialize() { |
| 171 | CHECK(!g_daemon); |
| 172 | |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 173 | g_daemon = new DaemonImpl(); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 174 | if (g_daemon->Init()) return true; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 175 | |
| 176 | LOG(ERROR) << "Failed to initialize the Daemon object"; |
| 177 | |
| 178 | delete g_daemon; |
| 179 | g_daemon = nullptr; |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // static |
| 185 | void Daemon::ShutDown() { |
| 186 | CHECK(g_daemon); |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 187 | delete g_daemon; |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 188 | g_daemon = nullptr; |
| 189 | } |
| 190 | |
| 191 | // static |
| 192 | void Daemon::InitializeForTesting(Daemon* test_daemon) { |
| 193 | CHECK(test_daemon); |
| 194 | CHECK(!g_daemon); |
| 195 | |
| 196 | g_daemon = test_daemon; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // static |
| 200 | Daemon* Daemon::Get() { |
| 201 | CHECK(g_daemon); |
| 202 | return g_daemon; |
| 203 | } |
| 204 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 205 | } // namespace bluetooth |