OSDN Git Service

Avoid more annoying crashing
[android-x86/system-bt.git] / service / daemon.cpp
index 7e754f7..0439c36 100644 (file)
@@ -21,6 +21,7 @@
 #include <base/logging.h>
 
 #include "service/adapter.h"
+#include "service/hal/bluetooth_gatt_interface.h"
 #include "service/hal/bluetooth_interface.h"
 #include "service/ipc/ipc_manager.h"
 #include "service/settings.h"
@@ -57,18 +58,39 @@ class DaemonImpl : public Daemon {
   }
 
  private:
+  bool StartUpBluetoothInterfaces() {
+    if (!hal::BluetoothInterface::Initialize())
+      goto failed;
+
+    if (!hal::BluetoothGattInterface::Initialize())
+      goto failed;
+
+    return true;
+
+  failed:
+    ShutDownBluetoothInterfaces();
+    return false;
+  }
+
+  void ShutDownBluetoothInterfaces() {
+    if (hal::BluetoothGattInterface::IsInitialized())
+      hal::BluetoothGattInterface::CleanUp();
+    if (hal::BluetoothInterface::IsInitialized())
+      hal::BluetoothInterface::CleanUp();
+  }
+
   void CleanUpBluetoothStack() {
     // The Adapter object needs to be cleaned up before the HAL interfaces.
     ipc_manager_.reset();
     adapter_.reset();
-    hal::BluetoothInterface::CleanUp();
+    ShutDownBluetoothInterfaces();
   }
 
   bool SetUpIPC() {
     // If an IPC socket path was given, initialize IPC with it. Otherwise
     // initialize Binder IPC.
     if (settings_->UseSocketIPC()) {
-      if (!ipc_manager_->Start(ipc::IPCManager::TYPE_UNIX, nullptr)) {
+      if (!ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, nullptr)) {
         LOG(ERROR) << "Failed to set up UNIX domain-socket IPCManager";
         return false;
       }
@@ -90,12 +112,12 @@ class DaemonImpl : public Daemon {
       return false;
     }
 
-    if (!hal::BluetoothInterface::Initialize()) {
-      LOG(ERROR) << "Failed to set up BluetoothInterface";
+    if (!StartUpBluetoothInterfaces()) {
+      LOG(ERROR) << "Failed to set up HAL Bluetooth interfaces";
       return false;
     }
 
-    adapter_.reset(new Adapter());
+    adapter_ = Adapter::Create();
     ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
 
     if (!SetUpIPC()) {