Commit d2213ee5 authored by Gina Yeh's avatar Gina Yeh
Browse files

Bug 794903 - Final version: BluetoothScoManager implementation, r=qdot

parent 9a548686
Loading
Loading
Loading
Loading
+37 −2
Original line number Diff line number Diff line
@@ -5,9 +5,11 @@
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "base/basictypes.h" 

#include "BluetoothHfpManager.h"

#include "BluetoothReplyRunnable.h"
#include "BluetoothScoManager.h"
#include "BluetoothService.h"
#include "BluetoothServiceUuid.h"

@@ -57,6 +59,37 @@ public:
  }
};

void
OpenScoSocket(const nsAString& aDevicePath)
{
  MOZ_ASSERT(NS_IsMainThread());

  BluetoothScoManager* sco = BluetoothScoManager::Get();
  if (!sco) {
    NS_WARNING("BluetoothScoManager is not available!");
    return;
  }

  if (!sco->Connect(aDevicePath)) {
    NS_WARNING("Failed to create a sco socket!");
  }
}

void
CloseScoSocket()
{
  MOZ_ASSERT(NS_IsMainThread());

  BluetoothScoManager* sco = BluetoothScoManager::Get();
  if (!sco) {
    NS_WARNING("BluetoothScoManager is not available!");
    return;
  }

  if (sco->GetConnected())
    sco->Disconnect();
}

BluetoothHfpManager::BluetoothHfpManager()
  : mCurrentVgs(-1)
  , mCurrentCallIndex(0)
@@ -355,6 +388,7 @@ BluetoothHfpManager::Connect(const nsAString& aDeviceObjectPath,
    NS_WARNING("BluetoothService not available!");
    return false;
  }
  mDevicePath = aDeviceObjectPath;

  nsString serviceUuidStr =
    NS_ConvertUTF8toUTF16(mozilla::dom::bluetooth::BluetoothServiceUuidStr::Handsfree);
@@ -447,7 +481,7 @@ BluetoothHfpManager::CallStateChanged(int aCallIndex, int aCallState,
#endif
          break;
      }

      OpenScoSocket(mDevicePath);
      break;
    case nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED:
      switch (mCurrentCallState) {
@@ -469,6 +503,7 @@ BluetoothHfpManager::CallStateChanged(int aCallIndex, int aCallState,
#endif
          break;
      }
      CloseScoSocket();
      break;

    default:
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ private:
  int mCurrentCallIndex;
  int mCurrentCallState;
  nsAutoPtr<BluetoothRilListener> mListener;
  nsString mDevicePath;
};

END_BLUETOOTH_NAMESPACE
+113 −0
Original line number Diff line number Diff line
/* -*- 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 "BluetoothScoManager.h"

#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothServiceUuid.h"

#include "mozilla/Services.h"
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
#include "nsContentUtils.h"
#include "nsIDOMDOMRequest.h"
#include "nsIObserverService.h"
#include "nsISystemMessagesInternal.h"
#include "nsVariant.h"

USING_BLUETOOTH_NAMESPACE
using namespace mozilla::ipc;

static nsRefPtr<BluetoothScoManager> sInstance;
static nsCOMPtr<nsIThread> sScoCommandThread;

BluetoothScoManager::BluetoothScoManager()
{
  if (!sScoCommandThread) {
    if (NS_FAILED(NS_NewThread(getter_AddRefs(sScoCommandThread)))) {
      NS_ERROR("Failed to new thread for sScoCommandThread");
    }
  }
  mConnected = false;
}

BluetoothScoManager::~BluetoothScoManager()
{
  // Shut down the command thread if it still exists.
  if (sScoCommandThread) {
    nsCOMPtr<nsIThread> thread;
    sScoCommandThread.swap(thread);
    if (NS_FAILED(thread->Shutdown())) {
      NS_WARNING("Failed to shut down the bluetooth hfpmanager command thread!");
    }
  }
}

//static
BluetoothScoManager*
BluetoothScoManager::Get()
{
  if (sInstance == nullptr) {
    sInstance = new BluetoothScoManager();
  }

  // TODO: destroy pointer sInstance on shutdown
  return sInstance;
}

// Virtual function of class SocketConsumer
void
BluetoothScoManager::ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage)
{
  // SCO socket do nothing here
  MOZ_NOT_REACHED("This should never be called!");
}

bool
BluetoothScoManager::Connect(const nsAString& aDeviceObjectPath)
{
  MOZ_ASSERT(NS_IsMainThread());

  if (mConnected) {
    NS_WARNING("Sco socket has been ready");
    return true;
  }

  BluetoothService* bs = BluetoothService::Get();
  if (!bs) {
    NS_WARNING("BluetoothService not available!");
    return false;
  }

  nsresult rv = bs->GetScoSocket(aDeviceObjectPath,
                                 true,
                                 false,
                                 this);

  return NS_FAILED(rv) ? false : true;
}

void
BluetoothScoManager::Disconnect()
{
  CloseSocket();
  mConnected = false;
}

// FIXME: detect connection in UnixSocketConsumer
bool
BluetoothScoManager::GetConnected()
{
  return mConnected;
}

void
BluetoothScoManager::SetConnected(bool aConnected)
{
  mConnected = aConnected;
}
+38 −0
Original line number Diff line number Diff line
/* -*- 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_bluetoothscomanager_h__
#define mozilla_dom_bluetooth_bluetoothscomanager_h__

#include "BluetoothCommon.h"
#include "mozilla/ipc/UnixSocket.h"

BEGIN_BLUETOOTH_NAMESPACE

class BluetoothReplyRunnable;

class BluetoothScoManager : public mozilla::ipc::UnixSocketConsumer
{
public:
  ~BluetoothScoManager();

  static BluetoothScoManager* Get();
  void ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage);

  bool Connect(const nsAString& aDeviceObjectPath);
  void Disconnect();
  void SetConnected(bool aConnected);
  bool GetConnected();

private:
  BluetoothScoManager();
  void CreateScoSocket(const nsAString& aDevicePath);
  bool mConnected;
};

END_BLUETOOTH_NAMESPACE

#endif
+6 −0
Original line number Diff line number Diff line
@@ -229,6 +229,12 @@ public:
                       const nsAString& aObjectPath,
                       BluetoothReplyRunnable* aRunnable) = 0;

  virtual nsresult
  GetScoSocket(const nsAString& aObjectPath,
               bool aAuth,
               bool aEncrypt,
               mozilla::ipc::UnixSocketConsumer* aConsumer) = 0;

  virtual nsresult
  GetSocketViaService(const nsAString& aObjectPath,
                      const nsAString& aService,
Loading