2013-09-06 04:19:25 -07: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"
|
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2013-09-13 15:51:00 -07:00
|
|
|
#define BT_LOGR_PROFILE(mgr, args...) \
|
|
|
|
do { \
|
|
|
|
nsCString name; \
|
|
|
|
mgr->GetName(name); \
|
|
|
|
BT_LOGR("%s: [%s] %s", __FUNCTION__, name.get(), \
|
|
|
|
nsPrintfCString(args).get()); \
|
|
|
|
} while(0)
|
|
|
|
|
2013-09-06 04:19:25 -07:00
|
|
|
BluetoothProfileController::BluetoothProfileController(
|
2013-10-27 21:06:38 -07:00
|
|
|
bool aConnect,
|
2013-09-06 04:19:25 -07:00
|
|
|
const nsAString& aDeviceAddress,
|
|
|
|
BluetoothReplyRunnable* aRunnable,
|
2013-10-27 21:06:38 -07:00
|
|
|
BluetoothProfileControllerCallback aCallback,
|
|
|
|
uint16_t aServiceUuid,
|
|
|
|
uint32_t aCod)
|
|
|
|
: mConnect(aConnect)
|
2013-09-11 00:21:48 -07:00
|
|
|
, mDeviceAddress(aDeviceAddress)
|
2013-09-06 04:19:25 -07:00
|
|
|
, mRunnable(aRunnable)
|
2013-10-27 21:06:38 -07:00
|
|
|
, mCallback(aCallback)
|
2013-09-11 00:21:48 -07:00
|
|
|
, mSuccess(false)
|
2013-10-27 21:06:38 -07:00
|
|
|
, mProfilesIndex(-1)
|
2013-09-06 04:19:25 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
|
|
|
|
MOZ_ASSERT(aRunnable);
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
|
|
|
|
mProfiles.Clear();
|
2013-10-27 21:06:38 -07: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 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothProfileController::~BluetoothProfileController()
|
|
|
|
{
|
|
|
|
mProfiles.Clear();
|
|
|
|
mRunnable = nullptr;
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
void
|
2013-09-06 04:19:25 -07: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-27 21:06:38 -07:00
|
|
|
return;
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
AddProfile(profile);
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
void
|
2013-09-06 04:19:25 -07:00
|
|
|
BluetoothProfileController::AddProfile(BluetoothProfileManagerBase* aProfile,
|
|
|
|
bool aCheckConnected)
|
|
|
|
{
|
|
|
|
if (!aProfile) {
|
|
|
|
DispatchBluetoothReply(mRunnable, BluetoothValue(),
|
|
|
|
NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
|
|
|
|
mCallback();
|
2013-10-27 21:06:38 -07:00
|
|
|
return;
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCheckConnected && !aProfile->IsConnected()) {
|
2013-10-27 21:06:38 -07:00
|
|
|
BT_WARNING("The profile is not connected.");
|
|
|
|
return;
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mProfiles.AppendElement(aProfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-27 21:06:38 -07:00
|
|
|
BluetoothProfileController::SetupProfiles(bool aAssignServiceClass)
|
2013-09-06 04:19:25 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
/**
|
|
|
|
* When a service class is assigned, only its corresponding profile is put
|
|
|
|
* into array.
|
|
|
|
*/
|
|
|
|
if (aAssignServiceClass) {
|
|
|
|
AddProfileWithServiceClass(mTarget.service);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-06 04:19:25 -07:00
|
|
|
|
2013-10-27 21:06:38 -07: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 04:19:25 -07:00
|
|
|
|
2013-10-27 21:06:38 -07: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-09-06 04:19:25 -07:00
|
|
|
|
2013-11-04 23:53:10 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(hasAudio || hasRendering || isPeripheral);
|
2013-09-06 04:19:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to HFP/HSP first. Then, connect A2DP if Rendering bit is set.
|
|
|
|
*/
|
|
|
|
if (hasAudio) {
|
|
|
|
AddProfile(BluetoothHfpManager::Get());
|
|
|
|
}
|
|
|
|
if (hasRendering) {
|
|
|
|
AddProfile(BluetoothA2dpManager::Get());
|
|
|
|
}
|
|
|
|
if (isPeripheral) {
|
|
|
|
AddProfile(BluetoothHidManager::Get());
|
|
|
|
}
|
2013-10-27 21:06:38 -07:00
|
|
|
}
|
2013-09-06 04:19:25 -07:00
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
void
|
|
|
|
BluetoothProfileController::Start()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mDeviceAddress.IsEmpty());
|
|
|
|
MOZ_ASSERT(mProfilesIndex == -1);
|
|
|
|
|
|
|
|
++mProfilesIndex;
|
|
|
|
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "");
|
|
|
|
|
|
|
|
if (mConnect) {
|
|
|
|
mProfiles[mProfilesIndex]->Connect(mDeviceAddress, this);
|
|
|
|
} else {
|
|
|
|
mProfiles[mProfilesIndex]->Disconnect(this);
|
|
|
|
}
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-27 21:06:38 -07:00
|
|
|
BluetoothProfileController::Next()
|
2013-09-06 04:19:25 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-27 21:06:38 -07:00
|
|
|
MOZ_ASSERT(!mDeviceAddress.IsEmpty());
|
|
|
|
MOZ_ASSERT(mProfilesIndex < mProfiles.Length());
|
2013-09-06 04:19:25 -07:00
|
|
|
|
|
|
|
if (++mProfilesIndex < mProfiles.Length()) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "");
|
2013-09-06 04:19:25 -07:00
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
if (mConnect) {
|
|
|
|
mProfiles[mProfilesIndex]->Connect(mDeviceAddress, this);
|
|
|
|
} else {
|
|
|
|
mProfiles[mProfilesIndex]->Disconnect(this);
|
|
|
|
}
|
2013-09-06 04:19:25 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mRunnable && mCallback);
|
|
|
|
|
|
|
|
// The action has been completed, so the dom request is replied and then
|
|
|
|
// the callback is invoked
|
2013-09-11 00:21:48 -07:00
|
|
|
if (mSuccess) {
|
|
|
|
DispatchBluetoothReply(mRunnable, BluetoothValue(true), EmptyString());
|
|
|
|
} else {
|
|
|
|
DispatchBluetoothReply(mRunnable, BluetoothValue(),
|
|
|
|
NS_LITERAL_STRING(ERR_CONNECTION_FAILED));
|
|
|
|
}
|
2013-09-06 04:19:25 -07:00
|
|
|
mCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothProfileController::OnConnect(const nsAString& aErrorStr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "<%s>",
|
|
|
|
NS_ConvertUTF16toUTF8(aErrorStr).get());
|
2013-09-06 04:19:25 -07:00
|
|
|
|
|
|
|
if (!aErrorStr.IsEmpty()) {
|
|
|
|
BT_WARNING(NS_ConvertUTF16toUTF8(aErrorStr).get());
|
2013-09-11 00:21:48 -07:00
|
|
|
} else {
|
|
|
|
mSuccess = true;
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
Next();
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothProfileController::OnDisconnect(const nsAString& aErrorStr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGR_PROFILE(mProfiles[mProfilesIndex], "<%s>",
|
|
|
|
NS_ConvertUTF16toUTF8(aErrorStr).get());
|
2013-09-06 04:19:25 -07:00
|
|
|
|
|
|
|
if (!aErrorStr.IsEmpty()) {
|
|
|
|
BT_WARNING(NS_ConvertUTF16toUTF8(aErrorStr).get());
|
2013-09-11 00:21:48 -07:00
|
|
|
} else {
|
|
|
|
mSuccess = true;
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|
|
|
|
|
2013-10-27 21:06:38 -07:00
|
|
|
Next();
|
2013-09-06 04:19:25 -07:00
|
|
|
}
|