2012-09-29 09:39:05 +00:00
|
|
|
/* -*- 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"
|
2012-10-22 10:34:15 +00:00
|
|
|
#include "BluetoothUtils.h"
|
2012-09-29 09:39:05 +00:00
|
|
|
|
|
|
|
#include "mozilla/Services.h"
|
2012-10-03 03:27:36 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-09-29 09:39:05 +00:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-10-22 10:34:15 +00:00
|
|
|
#include "nsIAudioManager.h"
|
2012-09-29 09:39:05 +00:00
|
|
|
#include "nsIObserverService.h"
|
2012-10-22 10:34:15 +00:00
|
|
|
|
|
|
|
#define BLUETOOTH_SCO_STATUS_CHANGED "bluetooth-sco-status-changed"
|
2012-09-29 09:39:05 +00:00
|
|
|
|
2012-10-03 03:27:36 +00:00
|
|
|
using namespace mozilla;
|
2012-09-29 09:39:05 +00:00
|
|
|
using namespace mozilla::ipc;
|
2012-10-03 03:27:36 +00:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
2012-09-29 09:39:05 +00:00
|
|
|
|
2012-10-06 00:11:24 +00:00
|
|
|
class mozilla::dom::bluetooth::BluetoothScoManagerObserver : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
BluetoothScoManagerObserver()
|
2012-10-09 04:59:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Init()
|
2012-10-06 00:11:24 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
MOZ_ASSERT(obs);
|
|
|
|
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add shutdown observer!");
|
2012-10-09 04:59:33 +00:00
|
|
|
return false;
|
2012-10-06 00:11:24 +00:00
|
|
|
}
|
2012-10-09 04:59:33 +00:00
|
|
|
return true;
|
2012-10-06 00:11:24 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 04:59:33 +00:00
|
|
|
bool Shutdown()
|
2012-10-06 00:11:24 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2012-10-09 04:59:33 +00:00
|
|
|
if (!obs ||
|
2012-10-06 00:11:24 +00:00
|
|
|
(NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)))) {
|
|
|
|
NS_WARNING("Can't unregister observers!");
|
2012-10-09 04:59:33 +00:00
|
|
|
return false;
|
2012-10-06 00:11:24 +00:00
|
|
|
}
|
2012-10-09 04:59:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BluetoothScoManagerObserver()
|
|
|
|
{
|
|
|
|
Shutdown();
|
2012-10-06 00:11:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-22 10:34:15 +00:00
|
|
|
void
|
|
|
|
BluetoothScoManager::NotifyAudioManager(const nsAString& aAddress) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (!obs) {
|
|
|
|
NS_WARNING("Failed to get observser service!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aAddress.IsEmpty()) {
|
|
|
|
if (NS_FAILED(obs->NotifyObservers(nullptr, BLUETOOTH_SCO_STATUS_CHANGED, nullptr))) {
|
|
|
|
NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (NS_FAILED(obs->NotifyObservers(nullptr, BLUETOOTH_SCO_STATUS_CHANGED, aAddress.BeginReading()))) {
|
|
|
|
NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAudioManager> am = do_GetService("@mozilla.org/telephony/audiomanager;1");
|
|
|
|
if (!am) {
|
|
|
|
NS_WARNING("Failed to get AudioManager service!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
am->SetForceForUse(am->USE_COMMUNICATION, am->FORCE_BT_SCO);
|
|
|
|
}
|
|
|
|
|
2012-10-06 00:11:24 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothScoManagerObserver, nsIObserver)
|
|
|
|
|
2012-10-03 03:27:36 +00:00
|
|
|
namespace {
|
|
|
|
StaticRefPtr<BluetoothScoManager> gBluetoothScoManager;
|
2012-10-09 04:59:33 +00:00
|
|
|
StaticRefPtr<BluetoothScoManagerObserver> sScoObserver;
|
2012-10-03 03:27:36 +00:00
|
|
|
bool gInShutdown = false;
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-10-06 00:11:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothScoManagerObserver::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gBluetoothScoManager);
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
return gBluetoothScoManager->HandleShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "BluetoothScoManager got unexpected topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-09-29 09:39:05 +00:00
|
|
|
|
|
|
|
BluetoothScoManager::BluetoothScoManager()
|
|
|
|
{
|
2012-10-03 03:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothScoManager::Init()
|
|
|
|
{
|
2012-10-26 09:48:47 +00:00
|
|
|
mSocketStatus = GetConnectionStatus();
|
|
|
|
|
2012-10-06 00:11:24 +00:00
|
|
|
sScoObserver = new BluetoothScoManagerObserver();
|
2012-10-09 04:59:33 +00:00
|
|
|
if (sScoObserver->Init()) {
|
|
|
|
NS_WARNING("Cannot set up SCO observers!");
|
|
|
|
}
|
2012-10-03 03:27:36 +00:00
|
|
|
return true;
|
2012-09-29 09:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothScoManager::~BluetoothScoManager()
|
2012-10-03 03:27:36 +00:00
|
|
|
{
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothScoManager::Cleanup()
|
2012-09-29 09:39:05 +00:00
|
|
|
{
|
2012-10-09 04:59:33 +00:00
|
|
|
sScoObserver->Shutdown();
|
2012-10-06 00:11:24 +00:00
|
|
|
sScoObserver = nullptr;
|
2012-09-29 09:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
BluetoothScoManager*
|
|
|
|
BluetoothScoManager::Get()
|
|
|
|
{
|
2012-10-03 03:27:36 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// If we already exist, exit early
|
|
|
|
if (gBluetoothScoManager) {
|
|
|
|
return gBluetoothScoManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're in shutdown, don't create a new instance
|
|
|
|
if (gInShutdown) {
|
|
|
|
NS_WARNING("BluetoothScoManager can't be created during shutdown");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new instance, register, return
|
|
|
|
nsRefPtr<BluetoothScoManager> manager = new BluetoothScoManager();
|
|
|
|
NS_ENSURE_TRUE(manager, nullptr);
|
|
|
|
|
|
|
|
if (!manager->Init()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gBluetoothScoManager = manager;
|
|
|
|
return gBluetoothScoManager;
|
|
|
|
}
|
|
|
|
|
2012-09-29 09:39:05 +00:00
|
|
|
// 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!");
|
|
|
|
}
|
|
|
|
|
2012-10-03 03:27:36 +00:00
|
|
|
nsresult
|
|
|
|
BluetoothScoManager::HandleShutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
gInShutdown = true;
|
2012-10-05 23:05:35 +00:00
|
|
|
CloseSocket();
|
2012-10-03 03:27:36 +00:00
|
|
|
gBluetoothScoManager = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-29 09:39:05 +00:00
|
|
|
bool
|
2012-10-22 10:34:15 +00:00
|
|
|
BluetoothScoManager::Connect(const nsAString& aDeviceAddress)
|
2012-09-29 09:39:05 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-10-03 03:27:36 +00:00
|
|
|
if (gInShutdown) {
|
|
|
|
MOZ_ASSERT(false, "Connect called while in shutdown!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-22 10:34:15 +00:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTED) {
|
|
|
|
NS_WARNING("Sco socket has been connected");
|
|
|
|
return false;
|
2012-09-29 09:39:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 09:48:47 +00:00
|
|
|
CloseSocket();
|
|
|
|
|
2012-09-29 09:39:05 +00:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-22 10:34:15 +00:00
|
|
|
nsresult rv = bs->GetScoSocket(aDeviceAddress,
|
2012-09-29 09:39:05 +00:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
this);
|
|
|
|
|
|
|
|
return NS_FAILED(rv) ? false : true;
|
|
|
|
}
|
|
|
|
|
2012-10-26 09:48:47 +00:00
|
|
|
bool
|
|
|
|
BluetoothScoManager::Listen()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (gInShutdown) {
|
|
|
|
MOZ_ASSERT(false, "Listen called while in shutdown!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseSocket();
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = bs->ListenSocketViaService(-1,
|
|
|
|
BluetoothSocketType::SCO,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
this);
|
|
|
|
|
|
|
|
mSocketStatus = GetConnectionStatus();
|
|
|
|
|
|
|
|
return NS_FAILED(rv) ? false : true;
|
|
|
|
}
|
|
|
|
|
2012-09-29 09:39:05 +00:00
|
|
|
void
|
|
|
|
BluetoothScoManager::Disconnect()
|
|
|
|
{
|
2012-10-22 10:34:15 +00:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_DISCONNECTED) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-29 09:39:05 +00:00
|
|
|
|
2012-10-22 10:34:15 +00:00
|
|
|
CloseSocket();
|
2012-09-29 09:39:05 +00:00
|
|
|
}
|
2012-10-11 05:49:11 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothScoManager::OnConnectSuccess()
|
|
|
|
{
|
2012-10-22 10:34:15 +00:00
|
|
|
nsString address;
|
|
|
|
GetSocketAddr(address);
|
|
|
|
NotifyAudioManager(address);
|
2012-10-26 09:48:47 +00:00
|
|
|
|
|
|
|
mSocketStatus = GetConnectionStatus();
|
2012-10-11 05:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothScoManager::OnConnectError()
|
|
|
|
{
|
2012-10-22 10:34:15 +00:00
|
|
|
CloseSocket();
|
2012-10-26 09:48:47 +00:00
|
|
|
mSocketStatus = GetConnectionStatus();
|
|
|
|
Listen();
|
2012-10-11 05:49:11 +00:00
|
|
|
}
|
2012-10-12 18:38:14 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothScoManager::OnDisconnect()
|
|
|
|
{
|
2012-10-26 09:48:47 +00:00
|
|
|
if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
|
|
|
|
Listen();
|
|
|
|
|
|
|
|
nsString address = NS_LITERAL_STRING("");
|
|
|
|
NotifyAudioManager(address);
|
|
|
|
}
|
2012-10-12 18:38:14 +00:00
|
|
|
}
|