Bug 939672 - Patch 1/2: [bluedroid] Add fallback BluetoothHfpManager, r=gyeh
authorBen Tian <btian@mozilla.com>
Tue, 25 Feb 2014 11:37:37 +0800
changeset 170741 498aa10dff2f7e2914dbb3c67fc6981c77361104
parent 170740 51db94e70a28138fb358dfc1ef6ccc875b247fc9
child 170742 f1ab0605c3e0408f867799d546faac59af77f4ff
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersgyeh
bugs939672
milestone30.0a1
Bug 939672 - Patch 1/2: [bluedroid] Add fallback BluetoothHfpManager, r=gyeh
dom/bluetooth/bluedroid/BluetoothHfpManager.cpp
dom/bluetooth/bluedroid/BluetoothHfpManager.h
dom/bluetooth/bluedroid/hfp-fallback/BluetoothHfpManager.cpp
dom/bluetooth/bluedroid/hfp-fallback/BluetoothHfpManager.h
dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.cpp
dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.h
dom/bluetooth/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth/bluedroid/hfp-fallback/BluetoothHfpManager.cpp
@@ -0,0 +1,183 @@
+/* -*- 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 "base/basictypes.h"
+#include "BluetoothHfpManager.h"
+#include "BluetoothProfileController.h"
+#include "mozilla/Services.h"
+#include "mozilla/StaticPtr.h"
+#include "nsIObserverService.h"
+#include "nsThreadUtils.h"
+
+using namespace mozilla;
+USING_BLUETOOTH_NAMESPACE
+
+namespace {
+  StaticRefPtr<BluetoothHfpManager> sBluetoothHfpManager;
+  bool sInShutdown = false;
+} // anonymous namespace
+
+/**
+ * nsIObserver function
+ */
+NS_IMETHODIMP
+BluetoothHfpManager::Observe(nsISupports* aSubject,
+                             const char* aTopic,
+                             const char16_t* aData)
+{
+  if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
+    HandleShutdown();
+  } else {
+    MOZ_ASSERT(false, "BluetoothHfpManager got unexpected topic!");
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  return NS_OK;
+}
+
+/**
+ * BluetoothProfileManagerBase functions
+ */
+void
+BluetoothHfpManager::Connect(const nsAString& aDeviceAddress,
+                             BluetoothProfileController* aController)
+{
+  MOZ_ASSERT(aController);
+
+  aController->OnConnect(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
+}
+
+void
+BluetoothHfpManager::Disconnect(BluetoothProfileController* aController)
+{
+  MOZ_ASSERT(aController);
+
+  aController->OnDisconnect(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
+}
+
+bool
+BluetoothHfpManager::IsConnected()
+{
+  return false;
+}
+
+void
+BluetoothHfpManager::OnConnect(const nsAString& aErrorStr)
+{
+  MOZ_ASSERT(false);
+}
+
+void
+BluetoothHfpManager::OnDisconnect(const nsAString& aErrorStr)
+{
+  MOZ_ASSERT(false);
+}
+
+void
+BluetoothHfpManager::GetAddress(nsAString& aDeviceAddress)
+{
+  aDeviceAddress.AssignLiteral(BLUETOOTH_ADDRESS_NONE);
+}
+
+void
+BluetoothHfpManager::OnGetServiceChannel(const nsAString& aDeviceAddress,
+                                         const nsAString& aServiceUuid,
+                                         int aChannel)
+{
+  MOZ_ASSERT(false);
+}
+
+void
+BluetoothHfpManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress)
+{
+  MOZ_ASSERT(false);
+}
+
+/**
+ * BluetoothHfpManagerBase function
+ */
+bool
+BluetoothHfpManager::IsScoConnected()
+{
+  return false;
+}
+
+/**
+ * Non-inherited functions
+ */
+// static
+BluetoothHfpManager*
+BluetoothHfpManager::Get()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  // If sBluetoothHfpManager already exists, exit early
+  if (sBluetoothHfpManager) {
+    return sBluetoothHfpManager;
+  }
+
+  // If we're in shutdown, don't create a new instance
+  NS_ENSURE_FALSE(sInShutdown, nullptr);
+
+  // Create a new instance and return
+  BluetoothHfpManager* manager = new BluetoothHfpManager();
+  NS_ENSURE_TRUE(manager->Init(), nullptr);
+
+  sBluetoothHfpManager = manager;
+  return sBluetoothHfpManager;
+}
+
+bool
+BluetoothHfpManager::Init()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
+  NS_ENSURE_TRUE(obs, false);
+
+  if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
+    BT_WARNING("Failed to add observers!");
+    return false;
+  }
+
+  return true;
+}
+
+void
+BluetoothHfpManager::HandleShutdown()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  sInShutdown = true;
+  sBluetoothHfpManager = nullptr;
+}
+
+bool
+BluetoothHfpManager::ConnectSco()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  /**
+   * TODO:
+   *   Implement ConnectSco() for applications that want to create SCO link
+   *   without a HFP connection (e.g., VoIP).
+   */
+  return false;
+}
+
+bool
+BluetoothHfpManager::DisconnectSco()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  /**
+   * TODO:
+   *   Implement DisconnectSco() for applications that want to destroy SCO link
+   *   without a HFP connection (e.g., VoIP).
+   */
+  return false;
+}
+
+NS_IMPL_ISUPPORTS1(BluetoothHfpManager, nsIObserver)
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth/bluedroid/hfp-fallback/BluetoothHfpManager.h
@@ -0,0 +1,47 @@
+/* -*- 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/. */
+
+#ifndef mozilla_dom_bluetooth_bluetoothhfpmanager_h__
+#define mozilla_dom_bluetooth_bluetoothhfpmanager_h__
+
+#include "BluetoothHfpManagerBase.h"
+
+/**
+ * Fallback BluetoothHfpManager is built for non-phone devices (e.g., tablets).
+ * These devices has no radio interface and the build flag MOZ_B2G_RIL is
+ * disabled. To prevent build breaks of accessing radio interface, we implement
+ * fallback BluetoothHfpManager with empty functions to keep original
+ * BluetoothHfpManager away from numerous #ifdef/#endif statements.
+ */
+
+BEGIN_BLUETOOTH_NAMESPACE
+
+class BluetoothHfpManager : public BluetoothHfpManagerBase
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIOBSERVER
+  BT_DECL_HFP_MGR_BASE
+  virtual void GetName(nsACString& aName)
+  {
+    aName.AssignLiteral("Fallback HFP/HSP");
+  }
+
+  static BluetoothHfpManager* Get();
+  virtual ~BluetoothHfpManager() { }
+
+  bool ConnectSco();
+  bool DisconnectSco();
+
+private:
+  BluetoothHfpManager() { }
+  bool Init();
+  void HandleShutdown();
+};
+
+END_BLUETOOTH_NAMESPACE
+
+#endif
rename from dom/bluetooth/bluedroid/BluetoothHfpManager.cpp
rename to dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.cpp
rename from dom/bluetooth/bluedroid/BluetoothHfpManager.h
rename to dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.h
--- a/dom/bluetooth/moz.build
+++ b/dom/bluetooth/moz.build
@@ -39,25 +39,40 @@ if CONFIG['MOZ_B2G_BT']:
             ]
             LOCAL_INCLUDES += [
                 'bluez',
             ]
             DEFINES['MOZ_B2G_BT_BLUEZ'] = True
         elif CONFIG['MOZ_B2G_BT_BLUEDROID']:
             SOURCES += [
                 'bluedroid/BluetoothA2dpManager.cpp',
-                'bluedroid/BluetoothHfpManager.cpp',
                 'bluedroid/BluetoothOppManager.cpp',
                 'bluedroid/BluetoothServiceBluedroid.cpp',
                 'bluedroid/BluetoothSocket.cpp',
                 'bluedroid/BluetoothUtils.cpp',
             ]
             LOCAL_INCLUDES += [
                 'bluedroid',
             ]
+
+            if CONFIG['MOZ_B2G_RIL']:
+                SOURCES += [
+                    'bluedroid/hfp/BluetoothHfpManager.cpp',
+                ]
+                LOCAL_INCLUDES += [
+                    'bluedroid/hfp',
+                ]
+            else:
+                SOURCES += [
+                    'bluedroid/hfp-fallback/BluetoothHfpManager.cpp',
+                ]
+                LOCAL_INCLUDES += [
+                    'bluedroid/hfp-fallback',
+                ]
+
             DEFINES['MOZ_B2G_BT_BLUEDROID'] = True
     elif CONFIG['MOZ_ENABLE_DBUS']:
         SOURCES += [
             'bluez/BluetoothDBusService.cpp',
             'bluez/BluetoothHfpManager.cpp',
         ]
         LOCAL_INCLUDES += [
             'bluez',