daemon.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Copyright 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. #include "service/daemon.h"
  17. #include <memory>
  18. #include <base/logging.h>
  19. #include <base/run_loop.h>
  20. #include "service/adapter.h"
  21. #include "service/hal/bluetooth_av_interface.h"
  22. #include "service/hal/bluetooth_avrcp_interface.h"
  23. #include "service/hal/bluetooth_gatt_interface.h"
  24. #include "service/hal/bluetooth_interface.h"
  25. #include "service/ipc/ipc_manager.h"
  26. #include "service/settings.h"
  27. #include "service/switches.h"
  28. namespace bluetooth {
  29. namespace {
  30. // The global Daemon instance.
  31. Daemon* g_daemon = nullptr;
  32. class DaemonImpl : public Daemon, public ipc::IPCManager::Delegate {
  33. public:
  34. DaemonImpl() : initialized_(false) {}
  35. ~DaemonImpl() override {
  36. if (!initialized_) return;
  37. CleanUpBluetoothStack();
  38. }
  39. void StartMainLoop() override { base::RunLoop().Run(); }
  40. Settings* GetSettings() const override { return settings_.get(); }
  41. base::MessageLoop* GetMessageLoop() const override {
  42. return message_loop_.get();
  43. }
  44. private:
  45. // ipc::IPCManager::Delegate implementation:
  46. void OnIPCHandlerStarted(ipc::IPCManager::Type /* type */) override {
  47. if (!settings_->EnableOnStart()) return;
  48. adapter_->Enable();
  49. }
  50. void OnIPCHandlerStopped(ipc::IPCManager::Type /* type */) override {
  51. // Do nothing.
  52. }
  53. bool StartUpBluetoothInterfaces() {
  54. if (!hal::BluetoothInterface::Initialize()) goto failed;
  55. if (!hal::BluetoothGattInterface::Initialize()) goto failed;
  56. if (!hal::BluetoothAvInterface::Initialize()) goto failed;
  57. if (!hal::BluetoothAvrcpInterface::Initialize()) goto failed;
  58. return true;
  59. failed:
  60. ShutDownBluetoothInterfaces();
  61. return false;
  62. }
  63. void ShutDownBluetoothInterfaces() {
  64. if (hal::BluetoothGattInterface::IsInitialized())
  65. hal::BluetoothGattInterface::CleanUp();
  66. if (hal::BluetoothInterface::IsInitialized())
  67. hal::BluetoothInterface::CleanUp();
  68. if (hal::BluetoothAvInterface::IsInitialized())
  69. hal::BluetoothAvInterface::CleanUp();
  70. if (hal::BluetoothAvrcpInterface::IsInitialized())
  71. hal::BluetoothAvrcpInterface::CleanUp();
  72. }
  73. void CleanUpBluetoothStack() {
  74. // The Adapter object needs to be cleaned up before the HAL interfaces.
  75. ipc_manager_.reset();
  76. adapter_.reset();
  77. ShutDownBluetoothInterfaces();
  78. }
  79. bool SetUpIPC() {
  80. // If an IPC socket path was given, initialize IPC with it. Otherwise
  81. // initialize Binder IPC.
  82. if (settings_->UseSocketIPC()) {
  83. if (!ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, this)) {
  84. LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
  85. return false;
  86. }
  87. return true;
  88. }
  89. #if !defined(OS_GENERIC)
  90. if (!ipc_manager_->Start(ipc::IPCManager::TYPE_BINDER, this)) {
  91. LOG(ERROR) << "Failed to set up Binder IPCManager";
  92. return false;
  93. }
  94. #else
  95. if (!ipc_manager_->Start(ipc::IPCManager::TYPE_DBUS, this)) {
  96. LOG(ERROR) << "Failed to set up DBus IPCManager";
  97. return false;
  98. }
  99. #endif
  100. return true;
  101. }
  102. bool Init() override {
  103. CHECK(!initialized_);
  104. message_loop_.reset(new base::MessageLoop());
  105. settings_.reset(new Settings());
  106. if (!settings_->Init()) {
  107. LOG(ERROR) << "Failed to set up Settings";
  108. return false;
  109. }
  110. if (!StartUpBluetoothInterfaces()) {
  111. LOG(ERROR) << "Failed to set up HAL Bluetooth interfaces";
  112. return false;
  113. }
  114. adapter_ = Adapter::Create();
  115. ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
  116. if (!SetUpIPC()) {
  117. CleanUpBluetoothStack();
  118. return false;
  119. }
  120. initialized_ = true;
  121. LOG(INFO) << "Daemon initialized";
  122. return true;
  123. }
  124. bool initialized_;
  125. std::unique_ptr<base::MessageLoop> message_loop_;
  126. std::unique_ptr<Settings> settings_;
  127. std::unique_ptr<Adapter> adapter_;
  128. std::unique_ptr<ipc::IPCManager> ipc_manager_;
  129. DISALLOW_COPY_AND_ASSIGN(DaemonImpl);
  130. };
  131. } // namespace
  132. // static
  133. bool Daemon::Initialize() {
  134. CHECK(!g_daemon);
  135. g_daemon = new DaemonImpl();
  136. if (g_daemon->Init()) return true;
  137. LOG(ERROR) << "Failed to initialize the Daemon object";
  138. delete g_daemon;
  139. g_daemon = nullptr;
  140. return false;
  141. }
  142. // static
  143. void Daemon::ShutDown() {
  144. CHECK(g_daemon);
  145. delete g_daemon;
  146. g_daemon = nullptr;
  147. }
  148. // static
  149. void Daemon::InitializeForTesting(Daemon* test_daemon) {
  150. CHECK(test_daemon);
  151. CHECK(!g_daemon);
  152. g_daemon = test_daemon;
  153. }
  154. // static
  155. Daemon* Daemon::Get() {
  156. CHECK(g_daemon);
  157. return g_daemon;
  158. }
  159. } // namespace bluetooth