2013-09-06 15:19:25 +04: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 "BluetoothProfileController.h"
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
|
|
|
|
#include "BluetoothA2dpManager.h"
|
|
|
|
#include "BluetoothHfpManager.h"
|
|
|
|
#include "BluetoothHidManager.h"
|
|
|
|
#include "BluetoothUtils.h"
|
2014-01-29 15:13:10 +04:00
|
|
|
|
2013-09-06 15:19:25 +04:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2014-01-29 15:13:10 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2013-09-06 15:19:25 +04:00
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2013-12-02 14:50:23 +04:00
|
|
|
#define BT_LOGR_PROFILE(mgr, msg, ...) \
|
|
|
|
do { \
|
|
|
|
nsCString name; \
|
|
|
|
mgr->GetName(name); \
|
|
|
|
BT_LOGR("[%s] " msg, name.get(), ##__VA_ARGS__); \
|
2013-09-14 02:51:00 +04:00
|
|
|
} while(0)
|
|
|
|
|
2014-01-29 15:13:10 +04:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2013-09-06 15:19:25 +04:00
|
|
|
BluetoothProfileController::BluetoothProfileController(
|
2013-10-28 08:06:38 +04:00
|
|
|
bool aConnect,
|
2013-09-06 15:19:25 +04:00
|
|
|
const nsAString& aDeviceAddress,
|
|
|
|
BluetoothReplyRunnable* aRunnable,
|
2013-10-28 08:06:38 +04:00
|
|
|
BluetoothProfileControllerCallback aCallback,
|
|
|
|
uint16_t aServiceUuid,
|
|
|
|
uint32_t aCod)
|
|
|
|
: mConnect(aConnect)
|
2013-09-11 11:21:48 +04:00
|
|
|
, mDeviceAddress(aDeviceAddress)
|
2013-09-06 15:19:25 +04:00
|
|
|
, mRunnable(aRunnable)
|
2013-10-28 08:06:38 +04:00
|
|
|
, mCallback(aCallback)
|
2014-01-29 15:13:10 +04:00
|
|
|
, mCurrentProfileFinished(false)
|
2013-09-11 11:21:48 +04:00
|
|
|
, mSuccess(false)
|
2013-10-28 08:06:38 +04:00
|
|
|
, mProfilesIndex(-1)
|
2013-09-06 15:19:25 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
|
|
|
|
MOZ_ASSERT(aRunnable);
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
|
2014-01-29 15:13:10 +04:00
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
MOZ_ASSERT(mTimer);
|
|
|
|
|
2013-09-06 15:19:25 +04:00
|
|
|
mProfiles.Clear();
|
2013-10-28 08:06:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothProfileController::~BluetoothProfileController()
|
|
|
|
{
|
|
|
|
mProfiles.Clear();
|
|
|
|
mRunnable = nullptr;
|
|
|
|
mCallback = nullptr;
|
2014-01-29 15:13:10 +04:00
|
|
|
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
void
|
2013-09-06 15:19:25 +04:00
|
|
|
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();
|
2013-10-28 08:06:38 +04:00
|
|
|
return;
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
AddProfile(profile);
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
void
|
2013-09-06 15:19:25 +04:00
|
|
|
BluetoothProfileController::AddProfile(BluetoothProfileManagerBase* aProfile,
|
|
|
|
bool aCheckConnected)
|
|
|
|
{
|
|
|
|
if (!aProfile) {
|
|
|
|
DispatchBluetoothReply(mRunnable, BluetoothValue(),
|
|
|
|
NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
|
|
|
|
mCallback();
|
2013-10-28 08:06:38 +04:00
|
|
|
return;
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCheckConnected && !aProfile->IsConnected()) {
|
2013-10-28 08:06:38 +04:00
|
|
|
BT_WARNING("The profile is not connected.");
|
|
|
|
return;
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mProfiles.AppendElement(aProfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-28 08:06:38 +04:00
|
|
|
BluetoothProfileController::SetupProfiles(bool aAssignServiceClass)
|
2013-09-06 15:19:25 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
/**
|
|
|
|
* When a service class is assigned, only its corresponding profile is put
|
|
|
|
* into array.
|
|
|
|
*/
|
|
|
|
if (aAssignServiceClass) {
|
|
|
|
AddProfileWithServiceClass(mTarget.service);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
// 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;
|
|
|
|
}
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
/**
|
|
|
|
* 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);
|
2013-12-05 06:05:39 +04:00
|
|
|
bool isRemoteControl = IS_REMOTE_CONTROL(mTarget.cod);
|
|
|
|
bool isKeyboard = IS_KEYBOARD(mTarget.cod);
|
|
|
|
bool isPointingDevice = IS_POINTING_DEVICE(mTarget.cod);
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2013-11-05 11:53:10 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(hasAudio || hasRendering || isPeripheral);
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2013-12-05 06:05:39 +04:00
|
|
|
// Audio bit should be set if remote device supports HFP/HSP.
|
2013-09-06 15:19:25 +04:00
|
|
|
if (hasAudio) {
|
|
|
|
AddProfile(BluetoothHfpManager::Get());
|
|
|
|
}
|
2013-12-05 06:05:39 +04:00
|
|
|
|
|
|
|
// 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)) {
|
2013-09-06 15:19:25 +04:00
|
|
|
AddProfile(BluetoothA2dpManager::Get());
|
|
|
|
}
|
2013-12-05 06:05:39 +04:00
|
|
|
|
|
|
|
// 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)) {
|
2013-09-06 15:19:25 +04:00
|
|
|
AddProfile(BluetoothHidManager::Get());
|
|
|
|
}
|
2013-10-28 08:06:38 +04:00
|
|
|
}
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CheckProfileStatusCallback, nsITimerCallback)
|
2014-01-29 15:13:10 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
void
|
2014-03-06 15:26:22 +04:00
|
|
|
BluetoothProfileController::StartSession()
|
2013-10-28 08:06:38 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mDeviceAddress.IsEmpty());
|
|
|
|
MOZ_ASSERT(mProfilesIndex == -1);
|
2014-01-29 15:13:10 +04:00
|
|
|
MOZ_ASSERT(mTimer);
|
2013-10-28 08:06:38 +04:00
|
|
|
|
2014-03-06 15:26:22 +04:00
|
|
|
if (mProfiles.Length() < 1) {
|
|
|
|
BT_LOGR("No queued profile.");
|
|
|
|
EndSession();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-28 08:06:38 +04:00
|
|
|
|
2014-01-29 15:13:10 +04:00
|
|
|
if (mTimer) {
|
2014-05-07 06:57:27 +04:00
|
|
|
mTimer->InitWithCallback(new CheckProfileStatusCallback(this),
|
|
|
|
CONNECTION_TIMEOUT_MS, nsITimer::TYPE_ONE_SHOT);
|
2014-01-29 15:13:10 +04:00
|
|
|
}
|
|
|
|
|
2014-03-06 15:26:22 +04:00
|
|
|
BT_LOGR("%s", mConnect ? "connecting" : "disconnecting");
|
|
|
|
|
|
|
|
Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothProfileController::EndSession()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRunnable && mCallback);
|
|
|
|
|
|
|
|
BT_LOGR("mSuccess %d", mSuccess);
|
|
|
|
|
|
|
|
// 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));
|
2013-10-28 08:06:38 +04:00
|
|
|
} else {
|
2014-03-06 15:26:22 +04:00
|
|
|
DispatchBluetoothReply(mRunnable, BluetoothValue(true),
|
|
|
|
NS_LITERAL_STRING(ERR_DISCONNECTION_FAILED));
|
2013-10-28 08:06:38 +04:00
|
|
|
}
|
2014-03-06 15:26:22 +04:00
|
|
|
|
|
|
|
mCallback();
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-28 08:06:38 +04:00
|
|
|
BluetoothProfileController::Next()
|
2013-09-06 15:19:25 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-28 08:06:38 +04:00
|
|
|
MOZ_ASSERT(!mDeviceAddress.IsEmpty());
|
2013-12-06 08:20:07 +04:00
|
|
|
MOZ_ASSERT(mProfilesIndex < (int)mProfiles.Length());
|
2014-01-29 15:13:10 +04:00
|
|
|
MOZ_ASSERT(mTimer);
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2014-01-29 15:13:10 +04:00
|
|
|
mCurrentProfileFinished = false;
|
2014-03-06 15:26:22 +04:00
|
|
|
|
|
|
|
if (++mProfilesIndex >= (int)mProfiles.Length()) {
|
|
|
|
EndSession();
|
2013-09-06 15:19:25 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-06 15:26:22 +04:00
|
|
|
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "");
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2014-03-06 15:26:22 +04:00
|
|
|
if (mConnect) {
|
|
|
|
mProfiles[mProfilesIndex]->Connect(mDeviceAddress, this);
|
2013-09-11 11:21:48 +04:00
|
|
|
} else {
|
2014-03-06 15:26:22 +04:00
|
|
|
mProfiles[mProfilesIndex]->Disconnect(this);
|
2013-09-11 11:21:48 +04:00
|
|
|
}
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-06 15:26:28 +04:00
|
|
|
BluetoothProfileController::NotifyCompletion(const nsAString& aErrorStr)
|
2013-09-06 15:19:25 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-01-29 15:13:10 +04:00
|
|
|
MOZ_ASSERT(mTimer);
|
2014-03-06 15:26:22 +04:00
|
|
|
MOZ_ASSERT(mProfiles.Length() > 0);
|
2014-01-29 15:13:10 +04:00
|
|
|
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "<%s>",
|
2014-03-06 15:26:22 +04:00
|
|
|
NS_ConvertUTF16toUTF8(aErrorStr).get());
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2014-01-29 15:13:10 +04:00
|
|
|
mCurrentProfileFinished = true;
|
2014-03-06 15:26:22 +04:00
|
|
|
|
2014-01-29 15:13:10 +04:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
2014-03-06 15:26:22 +04:00
|
|
|
mSuccess |= aErrorStr.IsEmpty();
|
2013-09-06 15:19:25 +04:00
|
|
|
|
2013-10-28 08:06:38 +04:00
|
|
|
Next();
|
2013-09-06 15:19:25 +04:00
|
|
|
}
|
2014-01-29 15:13:10 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothProfileController::GiveupAndContinue()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mCurrentProfileFinished);
|
|
|
|
MOZ_ASSERT(mProfilesIndex < (int)mProfiles.Length());
|
|
|
|
|
|
|
|
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], ERR_OPERATION_TIMEOUT);
|
|
|
|
mProfiles[mProfilesIndex]->Reset();
|
|
|
|
Next();
|
|
|
|
}
|
2014-03-06 15:26:22 +04:00
|
|
|
|