Bug 1095488: Added Bluetooth AVRCP interface, r=shuang
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 07 Jan 2015 11:32:05 +0100
changeset 239174 e48fd6fe791f8a1d8a6997164cafea772d0c309a
parent 239173 68e1922b83f0ac9bb064d4f87677d4ef6371b5fa
child 239175 5968614a47687f73ee97919fa19498c2fe692dc2
push id7472
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 20:36:27 +0000
treeherdermozilla-aurora@300ca104f8fb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshuang
bugs1095488
milestone37.0a1
Bug 1095488: Added Bluetooth AVRCP interface, r=shuang
dom/bluetooth/bluedroid/BluetoothDaemonAvrcpInterface.cpp
dom/bluetooth/bluedroid/BluetoothDaemonAvrcpInterface.h
--- a/dom/bluetooth/bluedroid/BluetoothDaemonAvrcpInterface.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonAvrcpInterface.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 "BluetoothDaemonAvrcpInterface.h"
+#include "BluetoothDaemonSetupInterface.h"
 #include "mozilla/unused.h"
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 //
 // AVRCP module
 //
 
@@ -820,9 +821,236 @@ BluetoothDaemonAvrcpModule::HandleNtf(
   if (NS_WARN_IF(!(index < MOZ_ARRAY_LENGTH(HandleNtf))) ||
       NS_WARN_IF(!HandleNtf[index])) {
     return;
   }
 
   (this->*(HandleNtf[index]))(aHeader, aPDU);
 }
 
+//
+// AVRCP interface
+//
+
+BluetoothDaemonAvrcpInterface::BluetoothDaemonAvrcpInterface(
+  BluetoothDaemonAvrcpModule* aModule)
+  : mModule(aModule)
+{ }
+
+BluetoothDaemonAvrcpInterface::~BluetoothDaemonAvrcpInterface()
+{ }
+
+class BluetoothDaemonAvrcpInterface::InitResultHandler MOZ_FINAL
+  : public BluetoothSetupResultHandler
+{
+public:
+  InitResultHandler(BluetoothAvrcpResultHandler* 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<BluetoothAvrcpResultHandler> mRes;
+};
+
+void
+BluetoothDaemonAvrcpInterface::Init(
+  BluetoothAvrcpNotificationHandler* aNotificationHandler,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  // 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(
+    BluetoothDaemonAvrcpModule::SERVICE_ID, 0x00, res);
+
+  if (NS_FAILED(rv) && aRes) {
+    DispatchError(aRes, STATUS_FAIL);
+  }
+}
+
+class BluetoothDaemonAvrcpInterface::CleanupResultHandler MOZ_FINAL
+  : public BluetoothSetupResultHandler
+{
+public:
+  CleanupResultHandler(BluetoothDaemonAvrcpModule* aModule,
+                       BluetoothAvrcpResultHandler* 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:
+  BluetoothDaemonAvrcpModule* mModule;
+  nsRefPtr<BluetoothAvrcpResultHandler> mRes;
+};
+
+void
+BluetoothDaemonAvrcpInterface::Cleanup(
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->UnregisterModule(BluetoothDaemonAvrcpModule::SERVICE_ID,
+                            new CleanupResultHandler(mModule, aRes));
+}
+
+void
+BluetoothDaemonAvrcpInterface::GetPlayStatusRsp(
+  ControlPlayStatus aPlayStatus, uint32_t aSongLen, uint32_t aSongPos,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->GetPlayStatusRspCmd(aPlayStatus, aSongLen, aSongPos, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::ListPlayerAppAttrRsp(
+  int aNumAttr, const BluetoothAvrcpPlayerAttribute* aPAttrs,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->ListPlayerAppAttrRspCmd(aNumAttr, aPAttrs, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::ListPlayerAppValueRsp(
+  int aNumVal, uint8_t* aPVals, BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->ListPlayerAppValueRspCmd(aNumVal, aPVals, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::GetPlayerAppValueRsp(
+  uint8_t aNumAttrs, const uint8_t* aIds, const uint8_t* aValues,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->GetPlayerAppValueRspCmd(aNumAttrs, aIds, aValues, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::GetPlayerAppAttrTextRsp(
+  int aNumAttr, const uint8_t* aIds, const char** aTexts,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->GetPlayerAppAttrTextRspCmd(aNumAttr, aIds, aTexts, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::GetPlayerAppValueTextRsp(
+  int aNumVal, const uint8_t* aIds, const char** aTexts,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->GetPlayerAppValueTextRspCmd(aNumVal, aIds, aTexts, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::GetElementAttrRsp(
+  uint8_t aNumAttr, const BluetoothAvrcpElementAttribute* aAttr,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->GetElementAttrRspCmd(aNumAttr, aAttr, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::SetPlayerAppValueRsp(
+  BluetoothAvrcpStatus aRspStatus, BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->SetPlayerAppValueRspCmd(aRspStatus, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::RegisterNotificationRsp(
+  BluetoothAvrcpEvent aEvent,
+  BluetoothAvrcpNotification aType,
+  const BluetoothAvrcpNotificationParam& aParam,
+  BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->RegisterNotificationRspCmd(aEvent, aType, aParam, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::SetVolume(
+  uint8_t aVolume, BluetoothAvrcpResultHandler* aRes)
+{
+  MOZ_ASSERT(mModule);
+
+  mModule->SetVolumeCmd(aVolume, aRes);
+}
+
+void
+BluetoothDaemonAvrcpInterface::DispatchError(
+  BluetoothAvrcpResultHandler* aRes, BluetoothStatus aStatus)
+{
+  BluetoothResultRunnable1<BluetoothAvrcpResultHandler, void,
+                           BluetoothStatus, BluetoothStatus>::Dispatch(
+    aRes, &BluetoothAvrcpResultHandler::OnError,
+    ConstantInitOp1<BluetoothStatus>(aStatus));
+}
+
 END_BLUETOOTH_NAMESPACE
--- a/dom/bluetooth/bluedroid/BluetoothDaemonAvrcpInterface.h
+++ b/dom/bluetooth/bluedroid/BluetoothDaemonAvrcpInterface.h
@@ -282,11 +282,71 @@ protected:
 
   void HandleNtf(const BluetoothDaemonPDUHeader& aHeader,
                  BluetoothDaemonPDU& aPDU,
                  void* aUserData);
 
   static BluetoothAvrcpNotificationHandler* sNotificationHandler;
 };
 
+class BluetoothDaemonAvrcpInterface MOZ_FINAL
+  : public BluetoothAvrcpInterface
+{
+  class CleanupResultHandler;
+  class InitResultHandler;
+
+public:
+  BluetoothDaemonAvrcpInterface(BluetoothDaemonAvrcpModule* aModule);
+  ~BluetoothDaemonAvrcpInterface();
+
+  void Init(BluetoothAvrcpNotificationHandler* aNotificationHandler,
+            BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void Cleanup(BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void GetPlayStatusRsp(ControlPlayStatus aPlayStatus,
+                        uint32_t aSongLen, uint32_t aSongPos,
+                        BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void ListPlayerAppAttrRsp(int aNumAttr,
+                            const BluetoothAvrcpPlayerAttribute* aPAttrs,
+                            BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void ListPlayerAppValueRsp(int aNumVal, uint8_t* aPVals,
+                             BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void GetPlayerAppValueRsp(uint8_t aNumAttrs, const uint8_t* aIds,
+                            const uint8_t* aValues,
+                            BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void GetPlayerAppAttrTextRsp(int aNumAttr, const uint8_t* aIds,
+                               const char** aTexts,
+                               BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void GetPlayerAppValueTextRsp(int aNumVal, const uint8_t* aIds,
+                                const char** aTexts,
+                                BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void GetElementAttrRsp(uint8_t aNumAttr,
+                         const BluetoothAvrcpElementAttribute* aAttr,
+                         BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void SetPlayerAppValueRsp(BluetoothAvrcpStatus aRspStatus,
+                            BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void RegisterNotificationRsp(BluetoothAvrcpEvent aEvent,
+                               BluetoothAvrcpNotification aType,
+                               const BluetoothAvrcpNotificationParam& aParam,
+                               BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+  void SetVolume(uint8_t aVolume,
+                 BluetoothAvrcpResultHandler* aRes) MOZ_OVERRIDE;
+
+private:
+  void DispatchError(BluetoothAvrcpResultHandler* aRes,
+                     BluetoothStatus aStatus);
+
+  BluetoothDaemonAvrcpModule* mModule;
+};
+
 END_BLUETOOTH_NAMESPACE
 
 #endif