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