gecko/dom/bluetooth2/BluetoothProfileController.cpp
Jocelyn Liu 4af9c98d9d Bug 1009347 - Patch 1/4: Make copy of dom/bluetooth for API refinement, f=btian, r=echou
--HG--
rename : dom/bluetooth/BluetoothAdapter.cpp => dom/bluetooth2/BluetoothAdapter.cpp
rename : dom/bluetooth/BluetoothAdapter.h => dom/bluetooth2/BluetoothAdapter.h
rename : dom/bluetooth/BluetoothCommon.h => dom/bluetooth2/BluetoothCommon.h
rename : dom/bluetooth/BluetoothDevice.cpp => dom/bluetooth2/BluetoothDevice.cpp
rename : dom/bluetooth/BluetoothDevice.h => dom/bluetooth2/BluetoothDevice.h
rename : dom/bluetooth/BluetoothHfpManagerBase.h => dom/bluetooth2/BluetoothHfpManagerBase.h
rename : dom/bluetooth/BluetoothHidManager.cpp => dom/bluetooth2/BluetoothHidManager.cpp
rename : dom/bluetooth/BluetoothHidManager.h => dom/bluetooth2/BluetoothHidManager.h
rename : dom/bluetooth/BluetoothManager.cpp => dom/bluetooth2/BluetoothManager.cpp
rename : dom/bluetooth/BluetoothManager.h => dom/bluetooth2/BluetoothManager.h
rename : dom/bluetooth/BluetoothProfileController.cpp => dom/bluetooth2/BluetoothProfileController.cpp
rename : dom/bluetooth/BluetoothProfileController.h => dom/bluetooth2/BluetoothProfileController.h
rename : dom/bluetooth/BluetoothProfileManagerBase.h => dom/bluetooth2/BluetoothProfileManagerBase.h
rename : dom/bluetooth/BluetoothPropertyContainer.cpp => dom/bluetooth2/BluetoothPropertyContainer.cpp
rename : dom/bluetooth/BluetoothPropertyContainer.h => dom/bluetooth2/BluetoothPropertyContainer.h
rename : dom/bluetooth/BluetoothReplyRunnable.cpp => dom/bluetooth2/BluetoothReplyRunnable.cpp
rename : dom/bluetooth/BluetoothReplyRunnable.h => dom/bluetooth2/BluetoothReplyRunnable.h
rename : dom/bluetooth/BluetoothRilListener.cpp => dom/bluetooth2/BluetoothRilListener.cpp
rename : dom/bluetooth/BluetoothRilListener.h => dom/bluetooth2/BluetoothRilListener.h
rename : dom/bluetooth/BluetoothService.cpp => dom/bluetooth2/BluetoothService.cpp
rename : dom/bluetooth/BluetoothService.h => dom/bluetooth2/BluetoothService.h
rename : dom/bluetooth/BluetoothSocketObserver.h => dom/bluetooth2/BluetoothSocketObserver.h
rename : dom/bluetooth/BluetoothUuid.cpp => dom/bluetooth2/BluetoothUuid.cpp
rename : dom/bluetooth/BluetoothUuid.h => dom/bluetooth2/BluetoothUuid.h
rename : dom/bluetooth/Makefile.in => dom/bluetooth2/Makefile.in
rename : dom/bluetooth/ObexBase.cpp => dom/bluetooth2/ObexBase.cpp
rename : dom/bluetooth/ObexBase.h => dom/bluetooth2/ObexBase.h
rename : dom/bluetooth/bluedroid/BluetoothA2dpManager.cpp => dom/bluetooth2/bluedroid/BluetoothA2dpManager.cpp
rename : dom/bluetooth/bluedroid/BluetoothA2dpManager.h => dom/bluetooth2/bluedroid/BluetoothA2dpManager.h
rename : dom/bluetooth/bluedroid/BluetoothOppManager.cpp => dom/bluetooth2/bluedroid/BluetoothOppManager.cpp
rename : dom/bluetooth/bluedroid/BluetoothOppManager.h => dom/bluetooth2/bluedroid/BluetoothOppManager.h
rename : dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp => dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp
rename : dom/bluetooth/bluedroid/BluetoothServiceBluedroid.h => dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.h
rename : dom/bluetooth/bluedroid/BluetoothSocket.cpp => dom/bluetooth2/bluedroid/BluetoothSocket.cpp
rename : dom/bluetooth/bluedroid/BluetoothSocket.h => dom/bluetooth2/bluedroid/BluetoothSocket.h
rename : dom/bluetooth/bluedroid/BluetoothUtils.cpp => dom/bluetooth2/bluedroid/BluetoothUtils.cpp
rename : dom/bluetooth/bluedroid/BluetoothUtils.h => dom/bluetooth2/bluedroid/BluetoothUtils.h
rename : dom/bluetooth/bluedroid/b2g_bdroid_buildcfg.h => dom/bluetooth2/bluedroid/b2g_bdroid_buildcfg.h
rename : dom/bluetooth/bluedroid/hfp-fallback/BluetoothHfpManager.cpp => dom/bluetooth2/bluedroid/hfp-fallback/BluetoothHfpManager.cpp
rename : dom/bluetooth/bluedroid/hfp-fallback/BluetoothHfpManager.h => dom/bluetooth2/bluedroid/hfp-fallback/BluetoothHfpManager.h
rename : dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.cpp => dom/bluetooth2/bluedroid/hfp/BluetoothHfpManager.cpp
rename : dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.h => dom/bluetooth2/bluedroid/hfp/BluetoothHfpManager.h
rename : dom/bluetooth/bluez/BluetoothA2dpManager.cpp => dom/bluetooth2/bluez/BluetoothA2dpManager.cpp
rename : dom/bluetooth/bluez/BluetoothA2dpManager.h => dom/bluetooth2/bluez/BluetoothA2dpManager.h
rename : dom/bluetooth/bluez/BluetoothDBusService.cpp => dom/bluetooth2/bluez/BluetoothDBusService.cpp
rename : dom/bluetooth/bluez/BluetoothDBusService.h => dom/bluetooth2/bluez/BluetoothDBusService.h
rename : dom/bluetooth/bluez/BluetoothHfpManager.cpp => dom/bluetooth2/bluez/BluetoothHfpManager.cpp
rename : dom/bluetooth/bluez/BluetoothHfpManager.h => dom/bluetooth2/bluez/BluetoothHfpManager.h
rename : dom/bluetooth/bluez/BluetoothOppManager.cpp => dom/bluetooth2/bluez/BluetoothOppManager.cpp
rename : dom/bluetooth/bluez/BluetoothOppManager.h => dom/bluetooth2/bluez/BluetoothOppManager.h
rename : dom/bluetooth/bluez/BluetoothSocket.cpp => dom/bluetooth2/bluez/BluetoothSocket.cpp
rename : dom/bluetooth/bluez/BluetoothSocket.h => dom/bluetooth2/bluez/BluetoothSocket.h
rename : dom/bluetooth/bluez/BluetoothUnixSocketConnector.cpp => dom/bluetooth2/bluez/BluetoothUnixSocketConnector.cpp
rename : dom/bluetooth/bluez/BluetoothUnixSocketConnector.h => dom/bluetooth2/bluez/BluetoothUnixSocketConnector.h
rename : dom/bluetooth/bluez/BluetoothUtils.cpp => dom/bluetooth2/bluez/BluetoothUtils.cpp
rename : dom/bluetooth/bluez/BluetoothUtils.h => dom/bluetooth2/bluez/BluetoothUtils.h
rename : dom/bluetooth/ipc/BluetoothChild.cpp => dom/bluetooth2/ipc/BluetoothChild.cpp
rename : dom/bluetooth/ipc/BluetoothChild.h => dom/bluetooth2/ipc/BluetoothChild.h
rename : dom/bluetooth/ipc/BluetoothMessageUtils.h => dom/bluetooth2/ipc/BluetoothMessageUtils.h
rename : dom/bluetooth/ipc/BluetoothParent.cpp => dom/bluetooth2/ipc/BluetoothParent.cpp
rename : dom/bluetooth/ipc/BluetoothParent.h => dom/bluetooth2/ipc/BluetoothParent.h
rename : dom/bluetooth/ipc/BluetoothServiceChildProcess.cpp => dom/bluetooth2/ipc/BluetoothServiceChildProcess.cpp
rename : dom/bluetooth/ipc/BluetoothServiceChildProcess.h => dom/bluetooth2/ipc/BluetoothServiceChildProcess.h
rename : dom/bluetooth/ipc/BluetoothTypes.ipdlh => dom/bluetooth2/ipc/BluetoothTypes.ipdlh
rename : dom/bluetooth/ipc/PBluetooth.ipdl => dom/bluetooth2/ipc/PBluetooth.ipdl
rename : dom/bluetooth/ipc/PBluetoothRequest.ipdl => dom/bluetooth2/ipc/PBluetoothRequest.ipdl
rename : dom/bluetooth/moz.build => dom/bluetooth2/moz.build
rename : dom/bluetooth/tests/marionette/head.js => dom/bluetooth2/tests/marionette/head.js
rename : dom/bluetooth/tests/marionette/manifest.ini => dom/bluetooth2/tests/marionette/manifest.ini
rename : dom/bluetooth/tests/marionette/test_dom_BluetoothAdapter_discovery.js => dom/bluetooth2/tests/marionette/test_dom_BluetoothAdapter_discovery.js
rename : dom/bluetooth/tests/marionette/test_dom_BluetoothAdapter_getters.js => dom/bluetooth2/tests/marionette/test_dom_BluetoothAdapter_getters.js
rename : dom/bluetooth/tests/marionette/test_dom_BluetoothAdapter_setters.js => dom/bluetooth2/tests/marionette/test_dom_BluetoothAdapter_setters.js
rename : dom/bluetooth/tests/marionette/test_dom_BluetoothManager_adapteradded.js => dom/bluetooth2/tests/marionette/test_dom_BluetoothManager_adapteradded.js
rename : dom/bluetooth/tests/marionette/test_dom_BluetoothManager_enabled.js => dom/bluetooth2/tests/marionette/test_dom_BluetoothManager_enabled.js
2014-05-21 14:11:31 +08:00

343 lines
8.7 KiB
C++

/* -*- 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 "BluetoothProfileController.h"
#include "BluetoothA2dpManager.h"
#include "BluetoothHfpManager.h"
#include "BluetoothHidManager.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothUtils.h"
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
#include "nsComponentManagerUtils.h"
USING_BLUETOOTH_NAMESPACE
#define BT_LOGR_PROFILE(mgr, msg, ...) \
do { \
nsCString name; \
mgr->GetName(name); \
BT_LOGR("[%s] " msg, name.get(), ##__VA_ARGS__); \
} while(0)
#define CONNECTION_TIMEOUT_MS 15000
class CheckProfileStatusCallback : public nsITimerCallback
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSITIMERCALLBACK
CheckProfileStatusCallback(BluetoothProfileController* aController)
: mController(aController)
{
MOZ_ASSERT(aController);
}
virtual ~CheckProfileStatusCallback()
{
mController = nullptr;
}
private:
nsRefPtr<BluetoothProfileController> mController;
};
BluetoothProfileController::BluetoothProfileController(
bool aConnect,
const nsAString& aDeviceAddress,
BluetoothReplyRunnable* aRunnable,
BluetoothProfileControllerCallback aCallback,
uint16_t aServiceUuid,
uint32_t aCod)
: mConnect(aConnect)
, mDeviceAddress(aDeviceAddress)
, mRunnable(aRunnable)
, mCallback(aCallback)
, mCurrentProfileFinished(false)
, mSuccess(false)
, mProfilesIndex(-1)
{
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
MOZ_ASSERT(aRunnable);
MOZ_ASSERT(aCallback);
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
MOZ_ASSERT(mTimer);
mProfiles.Clear();
/**
* If the service uuid is not specified, either connect multiple profiles
* based on Cod, or disconnect all connected profiles.
*/
if (!aServiceUuid) {
mTarget.cod = aCod;
SetupProfiles(false);
} else {
BluetoothServiceClass serviceClass =
BluetoothUuidHelper::GetBluetoothServiceClass(aServiceUuid);
mTarget.service = serviceClass;
SetupProfiles(true);
}
}
BluetoothProfileController::~BluetoothProfileController()
{
mProfiles.Clear();
mRunnable = nullptr;
mCallback = nullptr;
if (mTimer) {
mTimer->Cancel();
}
}
void
BluetoothProfileController::AddProfileWithServiceClass(
BluetoothServiceClass aClass)
{
BluetoothProfileManagerBase* profile;
switch (aClass) {
case BluetoothServiceClass::HANDSFREE:
case BluetoothServiceClass::HEADSET:
profile = BluetoothHfpManager::Get();
break;
case BluetoothServiceClass::A2DP:
profile = BluetoothA2dpManager::Get();
break;
case BluetoothServiceClass::HID:
profile = BluetoothHidManager::Get();
break;
default:
DispatchBluetoothReply(mRunnable, BluetoothValue(),
NS_LITERAL_STRING(ERR_UNKNOWN_PROFILE));
mCallback();
return;
}
AddProfile(profile);
}
void
BluetoothProfileController::AddProfile(BluetoothProfileManagerBase* aProfile,
bool aCheckConnected)
{
if (!aProfile) {
DispatchBluetoothReply(mRunnable, BluetoothValue(),
NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
mCallback();
return;
}
if (aCheckConnected && !aProfile->IsConnected()) {
BT_WARNING("The profile is not connected.");
return;
}
mProfiles.AppendElement(aProfile);
}
void
BluetoothProfileController::SetupProfiles(bool aAssignServiceClass)
{
MOZ_ASSERT(NS_IsMainThread());
/**
* When a service class is assigned, only its corresponding profile is put
* into array.
*/
if (aAssignServiceClass) {
AddProfileWithServiceClass(mTarget.service);
return;
}
// For a disconnect request, all connected profiles are put into array.
if (!mConnect) {
AddProfile(BluetoothHidManager::Get(), true);
AddProfile(BluetoothA2dpManager::Get(), true);
AddProfile(BluetoothHfpManager::Get(), true);
return;
}
/**
* For a connect request, put multiple profiles into array and connect to
* all of them sequencely.
*/
bool hasAudio = HAS_AUDIO(mTarget.cod);
bool hasRendering = HAS_RENDERING(mTarget.cod);
bool isPeripheral = IS_PERIPHERAL(mTarget.cod);
bool isRemoteControl = IS_REMOTE_CONTROL(mTarget.cod);
bool isKeyboard = IS_KEYBOARD(mTarget.cod);
bool isPointingDevice = IS_POINTING_DEVICE(mTarget.cod);
NS_ENSURE_TRUE_VOID(hasAudio || hasRendering || isPeripheral);
// Audio bit should be set if remote device supports HFP/HSP.
if (hasAudio) {
AddProfile(BluetoothHfpManager::Get());
}
// Rendering bit should be set if remote device supports A2DP.
// A device which supports AVRCP should claim that it's a peripheral and it's
// a remote control.
if (hasRendering || (isPeripheral && isRemoteControl)) {
AddProfile(BluetoothA2dpManager::Get());
}
// A device which supports HID should claim that it's a peripheral and it's
// either a keyboard, a pointing device, or both.
if (isPeripheral && (isKeyboard || isPointingDevice)) {
AddProfile(BluetoothHidManager::Get());
}
}
NS_IMPL_ISUPPORTS(CheckProfileStatusCallback, nsITimerCallback)
NS_IMETHODIMP
CheckProfileStatusCallback::Notify(nsITimer* aTimer)
{
MOZ_ASSERT(mController);
// Continue on the next profile since we haven't got the callback after
// timeout.
mController->GiveupAndContinue();
return NS_OK;
}
void
BluetoothProfileController::StartSession()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mDeviceAddress.IsEmpty());
MOZ_ASSERT(mProfilesIndex == -1);
MOZ_ASSERT(mTimer);
if (!IsBtServiceAvailable()) {
EndSession();
return;
}
if (mProfiles.Length() < 1) {
BT_LOGR("No queued profile.");
EndSession();
return;
}
if (mTimer) {
mTimer->InitWithCallback(new CheckProfileStatusCallback(this),
CONNECTION_TIMEOUT_MS, nsITimer::TYPE_ONE_SHOT);
}
BT_LOGR("%s", mConnect ? "connecting" : "disconnecting");
Next();
}
void
BluetoothProfileController::EndSession()
{
MOZ_ASSERT(mRunnable && mCallback);
BT_LOGR("mSuccess %d", mSuccess);
// Don't have to check profile status and retrigger session after connection
// timeout, since session is end.
if (mTimer) {
mTimer->Cancel();
}
// The action has completed, so the DOM request should be replied then invoke
// the callback.
if (mSuccess) {
DispatchBluetoothReply(mRunnable, BluetoothValue(true), EmptyString());
} else if (mConnect) {
DispatchBluetoothReply(mRunnable, BluetoothValue(true),
NS_LITERAL_STRING(ERR_CONNECTION_FAILED));
} else {
DispatchBluetoothReply(mRunnable, BluetoothValue(true),
NS_LITERAL_STRING(ERR_DISCONNECTION_FAILED));
}
mCallback();
}
void
BluetoothProfileController::Next()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mDeviceAddress.IsEmpty());
MOZ_ASSERT(mProfilesIndex < (int)mProfiles.Length());
MOZ_ASSERT(mTimer);
mCurrentProfileFinished = false;
if (!IsBtServiceAvailable()) {
EndSession();
return;
}
if (++mProfilesIndex >= (int)mProfiles.Length()) {
EndSession();
return;
}
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "");
if (mConnect) {
mProfiles[mProfilesIndex]->Connect(mDeviceAddress, this);
} else {
mProfiles[mProfilesIndex]->Disconnect(this);
}
}
bool
BluetoothProfileController::IsBtServiceAvailable() const
{
BluetoothService* bs = BluetoothService::Get();
return (bs && bs->IsEnabled() && !bs->IsToggling());
}
void
BluetoothProfileController::NotifyCompletion(const nsAString& aErrorStr)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mTimer);
MOZ_ASSERT(mProfiles.Length() > 0);
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "<%s>",
NS_ConvertUTF16toUTF8(aErrorStr).get());
mCurrentProfileFinished = true;
if (mTimer) {
mTimer->Cancel();
}
mSuccess |= aErrorStr.IsEmpty();
Next();
}
void
BluetoothProfileController::GiveupAndContinue()
{
MOZ_ASSERT(!mCurrentProfileFinished);
MOZ_ASSERT(mProfilesIndex < (int)mProfiles.Length());
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], ERR_OPERATION_TIMEOUT);
mProfiles[mProfilesIndex]->Reset();
if (IsBtServiceAvailable()) {
Next();
} else {
EndSession();
}
}