Bug 1110049: Added Blueooth A2DP interface for daemon backend (under bluetooth2/), r=btian
authorThomas Zimmermann <tdz@users.sourceforge.net>
Mon, 26 Jan 2015 18:39:09 +0100
changeset 225656 b67e506b64691ee7b001e50c354c0b99a9396482
parent 225655 7379867abc827d831c4b07efcec07848fe0abeef
child 225657 52a2b9ae8e44a941914a34c3866ab3060a356883
push id13458
push usertdz@users.sourceforge.net
push dateMon, 26 Jan 2015 17:41:15 +0000
treeherderb2g-inbound@52a2b9ae8e44 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbtian
bugs1110049
milestone38.0a1
Bug 1110049: Added Blueooth A2DP interface for daemon backend (under bluetooth2/), r=btian
dom/bluetooth2/bluedroid/BluetoothDaemonA2dpInterface.cpp
dom/bluetooth2/bluedroid/BluetoothDaemonA2dpInterface.h
--- a/dom/bluetooth2/bluedroid/BluetoothDaemonA2dpInterface.cpp
+++ b/dom/bluetooth2/bluedroid/BluetoothDaemonA2dpInterface.cpp
@@ -1,15 +1,16 @@
 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
 /* vim: set ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "BluetoothDaemonA2dpInterface.h"
+#include "BluetoothDaemonSetupInterface.h"
 #include "mozilla/unused.h"
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 //
 // A2DP module
 //
 
@@ -277,9 +278,152 @@ BluetoothDaemonA2dpModule::HandleNtf(
   if (NS_WARN_IF(!(index < MOZ_ARRAY_LENGTH(HandleNtf))) ||
       NS_WARN_IF(!HandleNtf[index])) {
     return;
   }
 
   (this->*(HandleNtf[index]))(aHeader, aPDU);
 }
 
+//
+// A2DP interface
+//
+
+BluetoothDaemonA2dpInterface::BluetoothDaemonA2dpInterface(
+  BluetoothDaemonA2dpModule* aModule)
+  : mModule(aModule)
+{ }
+
+BluetoothDaemonA2dpInterface::~BluetoothDaemonA2dpInterface()
+{ }
+
+class BluetoothDaemonA2dpInterface::InitResultHandler MOZ_FINAL
+  : public BluetoothSetupResultHandler
+{
+public:
+  InitResultHandler(BluetoothA2dpResultHandler* aRes)
+    : mRes(aRes)
+  {
+    MOZ_ASSERT(mRes);
+  }
+
+  void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    mRes->OnError(aStatus);
+  }
+
+  void RegisterModule() MOZ_OVERRIDE
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    mRes->Init();
+  }
+
+private:
+  nsRefPtr<BluetoothA2dpResultHandler> mRes;
+};
+
+void
+BluetoothDaemonA2dpInterface::Init(
+  BluetoothA2dpNotificationHandler* aNotificationHandler,
+  BluetoothA2dpResultHandler* aRes)
+{
+  // Set notification handler _before_ registering the module. It could
+  // happen that we receive notifications, before the result handler runs.
+  mModule->SetNotificationHandler(aNotificationHandler);
+
+  InitResultHandler* res;
+
+  if (aRes) {
+    res = new InitResultHandler(aRes);
+  } else {
+    // We don't need a result handler if the caller is not interested.
+    res = nullptr;
+  }
+
+  nsresult rv = mModule->RegisterModule(BluetoothDaemonA2dpModule::SERVICE_ID,
+                                        0x00, res);
+  if (NS_FAILED(rv) && aRes) {
+    DispatchError(aRes, STATUS_FAIL);
+  }
+}
+
+class BluetoothDaemonA2dpInterface::CleanupResultHandler MOZ_FINAL
+  : public BluetoothSetupResultHandler
+{
+public:
+  CleanupResultHandler(BluetoothDaemonA2dpModule* aModule,
+                       BluetoothA2dpResultHandler* aRes)
+    : mModule(aModule)
+    , mRes(aRes)
+  {
+    MOZ_ASSERT(mModule);
+  }
+
+  void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    if (mRes) {
+      mRes->OnError(aStatus);
+    }
+  }
+
+  void UnregisterModule() MOZ_OVERRIDE
+  {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    // Clear notification handler _after_ module has been
+    // unregistered. While unregistering the module, we might
+    // still receive notifications.
+    mModule->SetNotificationHandler(nullptr);
+
+    if (mRes) {
+      mRes->Cleanup();
+    }
+  }
+
+private:
+  BluetoothDaemonA2dpModule* mModule;
+  nsRefPtr<BluetoothA2dpResultHandler> mRes;
+};
+
+void
+BluetoothDaemonA2dpInterface::Cleanup(
+  BluetoothA2dpResultHandler* aRes)
+{
+  mModule->UnregisterModule(BluetoothDaemonA2dpModule::SERVICE_ID,
+                            new CleanupResultHandler(mModule, aRes));
+}
+
+/* Connect / Disconnect */
+
+void
+BluetoothDaemonA2dpInterface::Connect(
+  const nsAString& aBdAddr, BluetoothA2dpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->ConnectCmd(aBdAddr, aRes);
+}
+
+void
+BluetoothDaemonA2dpInterface::Disconnect(
+  const nsAString& aBdAddr, BluetoothA2dpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->DisconnectCmd(aBdAddr, aRes);
+}
+
+void
+BluetoothDaemonA2dpInterface::DispatchError(
+  BluetoothA2dpResultHandler* aRes, BluetoothStatus aStatus)
+{
+  BluetoothResultRunnable1<BluetoothA2dpResultHandler, void,
+                           BluetoothStatus, BluetoothStatus>::Dispatch(
+    aRes, &BluetoothA2dpResultHandler::OnError,
+    ConstantInitOp1<BluetoothStatus>(aStatus));
+}
+
 END_BLUETOOTH_NAMESPACE
--- a/dom/bluetooth2/bluedroid/BluetoothDaemonA2dpInterface.h
+++ b/dom/bluetooth2/bluedroid/BluetoothDaemonA2dpInterface.h
@@ -113,11 +113,40 @@ protected:
 
   void HandleNtf(const BluetoothDaemonPDUHeader& aHeader,
                  BluetoothDaemonPDU& aPDU,
                  void* aUserData);
 
   static BluetoothA2dpNotificationHandler* sNotificationHandler;
 };
 
+class BluetoothDaemonA2dpInterface MOZ_FINAL
+  : public BluetoothA2dpInterface
+{
+  class CleanupResultHandler;
+  class InitResultHandler;
+
+public:
+  BluetoothDaemonA2dpInterface(BluetoothDaemonA2dpModule* aModule);
+  ~BluetoothDaemonA2dpInterface();
+
+  void Init(
+    BluetoothA2dpNotificationHandler* aNotificationHandler,
+    BluetoothA2dpResultHandler* aRes);
+  void Cleanup(BluetoothA2dpResultHandler* aRes);
+
+  /* Connect / Disconnect */
+
+  void Connect(const nsAString& aBdAddr,
+               BluetoothA2dpResultHandler* aRes);
+  void Disconnect(const nsAString& aBdAddr,
+                  BluetoothA2dpResultHandler* aRes);
+
+private:
+  void DispatchError(BluetoothA2dpResultHandler* aRes,
+                     BluetoothStatus aStatus);
+
+  BluetoothDaemonA2dpModule* mModule;
+};
+
 END_BLUETOOTH_NAMESPACE
 
 #endif