| 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 | |
| 23 | #include "service/core_stack.h" |
| 24 | #include "service/ipc/ipc_manager.h" |
| 25 | #include "service/settings.h" |
| 26 | |
| 27 | namespace bluetooth { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | // The global Daemon instance. |
| 32 | Daemon* g_daemon = nullptr; |
| 33 | |
| Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 34 | class 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 Uguray | b2286f3 | 2015-08-05 21:19:02 -0700 | [diff] [blame^] | 69 | // 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 Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 78 | 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 Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 92 | } // namespace |
| 93 | |
| 94 | // static |
| 95 | bool Daemon::Initialize() { |
| 96 | CHECK(!g_daemon); |
| 97 | |
| Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 98 | g_daemon = new DaemonImpl(); |
| Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 99 | 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 |
| 111 | void Daemon::ShutDown() { |
| 112 | CHECK(g_daemon); |
| Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 113 | delete g_daemon; |
| Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 114 | g_daemon = nullptr; |
| 115 | } |
| 116 | |
| 117 | // static |
| 118 | void Daemon::InitializeForTesting(Daemon* test_daemon) { |
| 119 | CHECK(test_daemon); |
| 120 | CHECK(!g_daemon); |
| 121 | |
| 122 | g_daemon = test_daemon; |
| Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // static |
| 126 | Daemon* Daemon::Get() { |
| 127 | CHECK(g_daemon); |
| 128 | return g_daemon; |
| 129 | } |
| 130 | |
| Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 131 | } // namespace bluetooth |