2012-09-26 06:34:55 -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/. */
|
|
|
|
|
2013-01-08 01:21:00 -08:00
|
|
|
#include "base/basictypes.h"
|
2012-09-29 02:39:05 -07:00
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
#include "BluetoothHfpManager.h"
|
|
|
|
|
2013-06-08 08:26:01 -07:00
|
|
|
#include "BluetoothA2dpManager.h"
|
2012-09-26 06:34:55 -07:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "BluetoothService.h"
|
2013-04-04 17:54:22 -07:00
|
|
|
#include "BluetoothSocket.h"
|
2012-10-03 02:06:40 -07:00
|
|
|
#include "BluetoothUtils.h"
|
2012-11-26 22:12:19 -08:00
|
|
|
#include "BluetoothUuid.h"
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2012-12-17 01:44:32 -08:00
|
|
|
#include "MobileConnection.h"
|
2012-09-25 03:18:38 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2013-01-21 21:53:28 -08:00
|
|
|
#include "mozilla/Hal.h"
|
2012-09-26 19:24:39 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-10-02 22:53:39 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-09-25 03:18:38 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-10-02 19:55:28 -07:00
|
|
|
#include "nsIAudioManager.h"
|
2012-09-26 19:24:39 -07:00
|
|
|
#include "nsIObserverService.h"
|
2012-11-18 23:10:38 -08:00
|
|
|
#include "nsISettingsService.h"
|
2013-03-06 01:53:23 -08:00
|
|
|
#include "nsITelephonyProvider.h"
|
2012-12-19 23:28:51 -08:00
|
|
|
#include "nsRadioInterfaceLayer.h"
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
#define AUDIO_VOLUME_BT_SCO "audio.volume.bt_sco"
|
2012-12-17 01:44:32 -08:00
|
|
|
#define MOZSETTINGS_CHANGED_ID "mozsettings-changed"
|
|
|
|
#define MOBILE_CONNECTION_ICCINFO_CHANGED "mobile-connection-iccinfo-changed"
|
2012-12-19 23:28:51 -08:00
|
|
|
#define MOBILE_CONNECTION_VOICE_CHANGED "mobile-connection-voice-changed"
|
2012-12-17 01:44:32 -08:00
|
|
|
|
2013-06-05 01:53:22 -07:00
|
|
|
/**
|
|
|
|
* BRSF bitmask of AG supported features. See 4.34.1 "Bluetooth Defined AT
|
|
|
|
* Capabilities" in Bluetooth hands-free profile 1.6
|
|
|
|
*/
|
|
|
|
#define BRSF_BIT_THREE_WAY_CALLING 1
|
|
|
|
#define BSRF_BIT_EC_NR_FUNCTION (1 << 1)
|
|
|
|
#define BRSF_BIT_VOICE_RECOGNITION (1 << 2)
|
|
|
|
#define BRSF_BIT_IN_BAND_RING_TONE (1 << 3)
|
|
|
|
#define BRSF_BIT_ATTACH_NUM_TO_VOICE_TAG (1 << 4)
|
|
|
|
#define BRSF_BIT_ABILITY_TO_REJECT_CALL (1 << 5)
|
|
|
|
#define BRSF_BIT_ENHANCED_CALL_STATUS (1 << 6)
|
|
|
|
#define BRSF_BIT_ENHANCED_CALL_CONTROL (1 << 7)
|
|
|
|
#define BRSF_BIT_EXTENDED_ERR_RESULT_CODES (1 << 8)
|
|
|
|
#define BRSF_BIT_CODEC_NEGOTIATION (1 << 9)
|
|
|
|
|
2012-12-17 07:24:35 -08:00
|
|
|
/**
|
|
|
|
* These constants are used in result code such as +CLIP and +CCWA. The value
|
|
|
|
* of these constants is the same as TOA_INTERNATIONAL/TOA_UNKNOWN defined in
|
|
|
|
* ril_consts.js
|
|
|
|
*/
|
|
|
|
#define TOA_UNKNOWN 0x81
|
|
|
|
#define TOA_INTERNATIONAL 0x91
|
|
|
|
|
2013-04-04 17:54:22 -07:00
|
|
|
#define CR_LF "\xd\xa";
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
using namespace mozilla;
|
2012-09-26 06:34:55 -07:00
|
|
|
using namespace mozilla::ipc;
|
2012-10-02 22:53:39 -07:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2012-12-19 23:28:51 -08:00
|
|
|
namespace {
|
2013-06-08 08:26:01 -07:00
|
|
|
StaticRefPtr<BluetoothHfpManager> gBluetoothHfpManager;
|
2012-12-19 23:28:51 -08:00
|
|
|
StaticRefPtr<BluetoothHfpManagerObserver> sHfpObserver;
|
|
|
|
bool gInShutdown = false;
|
2013-05-09 07:10:28 -07:00
|
|
|
static const char kHfpCrlf[] = "\xd\xa";
|
|
|
|
|
|
|
|
// Sending ringtone related
|
2012-12-19 23:28:51 -08:00
|
|
|
static bool sStopSendingRingFlag = true;
|
|
|
|
static int sRingInterval = 3000; //unit: ms
|
2013-05-09 07:10:28 -07:00
|
|
|
|
|
|
|
// Wait for 2 seconds for Dialer processing event 'BLDN'. '2' seconds is a
|
|
|
|
// magic number. The mechanism should be revised once we can get call history.
|
2013-05-31 05:43:18 -07:00
|
|
|
static int sWaitingForDialingInterval = 2000; //unit: ms
|
2012-12-19 23:28:51 -08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
/* CallState for sCINDItems[CINDType::CALL].value
|
|
|
|
* - NO_CALL: there are no calls in progress
|
|
|
|
* - IN_PROGRESS: at least one call is in progress
|
|
|
|
*/
|
|
|
|
enum CallState {
|
|
|
|
NO_CALL,
|
|
|
|
IN_PROGRESS
|
|
|
|
};
|
|
|
|
|
|
|
|
/* CallSetupState for sCINDItems[CINDType::CALLSETUP].value
|
|
|
|
* - NO_CALLSETUP: not currently in call set up
|
|
|
|
* - INCOMING: an incoming call process ongoing
|
|
|
|
* - OUTGOING: an outgoing call set up is ongoing
|
|
|
|
* - OUTGOING_ALERTING: remote party being alerted in an outgoing call
|
|
|
|
*/
|
|
|
|
enum CallSetupState {
|
|
|
|
NO_CALLSETUP,
|
|
|
|
INCOMING,
|
|
|
|
OUTGOING,
|
|
|
|
OUTGOING_ALERTING
|
|
|
|
};
|
|
|
|
|
|
|
|
/* CallHeldState for sCINDItems[CINDType::CALLHELD].value
|
|
|
|
* - NO_CALLHELD: no calls held
|
|
|
|
* - ONHOLD_ACTIVE: both an active and a held call
|
|
|
|
* - ONHOLD_NOACTIVE: call on hold, no active call
|
|
|
|
*/
|
|
|
|
enum CallHeldState {
|
|
|
|
NO_CALLHELD,
|
|
|
|
ONHOLD_ACTIVE,
|
|
|
|
ONHOLD_NOACTIVE
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char* name;
|
|
|
|
const char* range;
|
|
|
|
int value;
|
|
|
|
} CINDItem;
|
|
|
|
|
|
|
|
enum CINDType {
|
|
|
|
BATTCHG = 1,
|
|
|
|
CALL,
|
|
|
|
CALLHELD,
|
2012-12-19 23:28:51 -08:00
|
|
|
CALLSETUP,
|
|
|
|
SERVICE,
|
|
|
|
SIGNAL,
|
|
|
|
ROAM
|
2012-10-22 03:34:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static CINDItem sCINDItems[] = {
|
|
|
|
{},
|
|
|
|
{"battchg", "0-5", 5},
|
|
|
|
{"call", "0,1", CallState::NO_CALL},
|
|
|
|
{"callheld", "0-2", CallHeldState::NO_CALLHELD},
|
2012-12-19 23:28:51 -08:00
|
|
|
{"callsetup", "0-3", CallSetupState::NO_CALLSETUP},
|
|
|
|
{"service", "0,1", 0},
|
|
|
|
{"signal", "0-5", 0},
|
2012-10-22 03:34:15 -07:00
|
|
|
{"roam", "0,1", 0}
|
|
|
|
};
|
|
|
|
|
2013-01-15 22:13:58 -08:00
|
|
|
class mozilla::dom::bluetooth::Call {
|
|
|
|
public:
|
2013-03-06 01:53:23 -08:00
|
|
|
Call(uint16_t aState = nsITelephonyProvider::CALL_STATE_DISCONNECTED,
|
2013-01-15 22:13:58 -08:00
|
|
|
bool aDirection = false,
|
2013-05-10 04:01:10 -07:00
|
|
|
const nsAString& aNumber = EmptyString(),
|
2013-01-15 22:13:58 -08:00
|
|
|
int aType = TOA_UNKNOWN)
|
|
|
|
: mState(aState), mDirection(aDirection), mNumber(aNumber), mType(aType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t mState;
|
|
|
|
bool mDirection; // true: incoming call; false: outgoing call
|
|
|
|
nsString mNumber;
|
|
|
|
int mType;
|
|
|
|
};
|
|
|
|
|
2012-10-05 17:11:24 -07:00
|
|
|
class mozilla::dom::bluetooth::BluetoothHfpManagerObserver : public nsIObserver
|
2013-01-21 21:53:28 -08:00
|
|
|
, public BatteryObserver
|
2012-10-05 17:11:24 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
BluetoothHfpManagerObserver()
|
2012-10-08 21:59:33 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Init()
|
2012-10-05 17:11:24 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
MOZ_ASSERT(obs);
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, MOZSETTINGS_CHANGED_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add settings change observer!");
|
2012-10-08 21:59:33 -07:00
|
|
|
return false;
|
2012-10-05 17:11:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add shutdown observer!");
|
2012-10-08 21:59:33 -07:00
|
|
|
return false;
|
2012-10-05 17:11:24 -07:00
|
|
|
}
|
2012-10-08 21:59:33 -07:00
|
|
|
|
2012-12-17 01:44:32 -08:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, MOBILE_CONNECTION_ICCINFO_CHANGED, false))) {
|
|
|
|
NS_WARNING("Failed to add mobile connection iccinfo change observer!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-19 23:28:51 -08:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, MOBILE_CONNECTION_VOICE_CHANGED, false))) {
|
|
|
|
NS_WARNING("Failed to add mobile connection voice change observer!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-21 21:53:28 -08:00
|
|
|
hal::RegisterBatteryObserver(this);
|
|
|
|
|
2012-10-08 21:59:33 -07:00
|
|
|
return true;
|
2012-10-05 17:11:24 -07:00
|
|
|
}
|
|
|
|
|
2012-10-08 21:59:33 -07:00
|
|
|
bool Shutdown()
|
2012-10-05 17:11:24 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2012-10-08 21:59:33 -07:00
|
|
|
if (!obs ||
|
2012-12-17 01:44:32 -08:00
|
|
|
NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) ||
|
|
|
|
NS_FAILED(obs->RemoveObserver(this, MOZSETTINGS_CHANGED_ID)) ||
|
2012-12-19 23:28:51 -08:00
|
|
|
NS_FAILED(obs->RemoveObserver(this, MOBILE_CONNECTION_ICCINFO_CHANGED)) ||
|
|
|
|
NS_FAILED(obs->RemoveObserver(this, MOBILE_CONNECTION_VOICE_CHANGED))) {
|
2012-10-08 21:59:33 -07:00
|
|
|
NS_WARNING("Can't unregister observers, or already unregistered!");
|
|
|
|
return false;
|
2012-10-05 17:11:24 -07:00
|
|
|
}
|
2013-01-21 21:53:28 -08:00
|
|
|
|
|
|
|
hal::UnregisterBatteryObserver(this);
|
|
|
|
|
2012-10-08 21:59:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BluetoothHfpManagerObserver()
|
|
|
|
{
|
|
|
|
Shutdown();
|
2012-10-05 17:11:24 -07:00
|
|
|
}
|
2013-01-21 21:53:28 -08:00
|
|
|
|
|
|
|
void Notify(const hal::BatteryInformation& aBatteryInfo)
|
|
|
|
{
|
|
|
|
// Range of battery level: [0, 1], double
|
|
|
|
// Range of CIND::BATTCHG: [0, 5], int
|
|
|
|
int level = ceil(aBatteryInfo.level() * 5.0);
|
|
|
|
if (level != sCINDItems[CINDType::BATTCHG].value) {
|
|
|
|
sCINDItems[CINDType::BATTCHG].value = level;
|
|
|
|
gBluetoothHfpManager->SendCommand("+CIEV: ", CINDType::BATTCHG);
|
|
|
|
}
|
|
|
|
}
|
2012-10-05 17:11:24 -07:00
|
|
|
};
|
|
|
|
|
2013-01-15 22:13:41 -08:00
|
|
|
class BluetoothHfpManager::GetVolumeTask : public nsISettingsServiceCallback
|
2012-11-18 23:10:38 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2013-04-02 16:06:14 -07:00
|
|
|
Handle(const nsAString& aName, const JS::Value& aResult)
|
2012-11-18 23:10:38 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContext();
|
|
|
|
NS_ENSURE_TRUE(cx, NS_OK);
|
|
|
|
|
|
|
|
if (!aResult.isNumber()) {
|
|
|
|
NS_WARNING("'" AUDIO_VOLUME_BT_SCO "' is not a number!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
|
2013-01-15 22:13:41 -08:00
|
|
|
hfp->mCurrentVgs = aResult.toNumber();
|
2012-11-18 23:10:38 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
HandleError(const nsAString& aName)
|
|
|
|
{
|
|
|
|
NS_WARNING("Unable to get value for '" AUDIO_VOLUME_BT_SCO "'");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-15 22:13:41 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothHfpManager::GetVolumeTask,
|
|
|
|
nsISettingsServiceCallback);
|
2012-11-18 23:10:38 -08:00
|
|
|
|
2012-10-05 17:11:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothHfpManagerObserver::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gBluetoothHfpManager);
|
2012-10-22 03:34:15 -07:00
|
|
|
|
2012-10-05 17:11:24 -07:00
|
|
|
if (!strcmp(aTopic, MOZSETTINGS_CHANGED_ID)) {
|
|
|
|
return gBluetoothHfpManager->HandleVolumeChanged(nsDependentString(aData));
|
|
|
|
} else if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
return gBluetoothHfpManager->HandleShutdown();
|
2012-12-17 01:44:32 -08:00
|
|
|
} else if (!strcmp(aTopic, MOBILE_CONNECTION_ICCINFO_CHANGED)) {
|
|
|
|
return gBluetoothHfpManager->HandleIccInfoChanged();
|
2012-12-19 23:28:51 -08:00
|
|
|
} else if (!strcmp(aTopic, MOBILE_CONNECTION_VOICE_CHANGED)) {
|
|
|
|
return gBluetoothHfpManager->HandleVoiceConnectionChanged();
|
2012-10-05 17:11:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "BluetoothHfpManager got unexpected topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-09-25 03:18:38 -07:00
|
|
|
|
2012-12-19 23:28:51 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothHfpManagerObserver, nsIObserver)
|
|
|
|
|
2013-05-09 07:10:28 -07:00
|
|
|
class BluetoothHfpManager::RespondToBLDNTask : public Task
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
void Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gBluetoothHfpManager);
|
|
|
|
|
2013-05-31 05:43:18 -07:00
|
|
|
if (!gBluetoothHfpManager->mDialingRequestProcessed) {
|
|
|
|
gBluetoothHfpManager->mDialingRequestProcessed = true;
|
2013-05-09 07:10:28 -07:00
|
|
|
gBluetoothHfpManager->SendLine("ERROR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-04 17:54:22 -07:00
|
|
|
class BluetoothHfpManager::SendRingIndicatorTask : public Task
|
2012-09-26 22:15:33 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-01-15 22:13:58 -08:00
|
|
|
SendRingIndicatorTask(const nsAString& aNumber, int aType)
|
2012-12-17 07:24:35 -08:00
|
|
|
: mNumber(aNumber)
|
|
|
|
, mType(aType)
|
2012-09-26 22:15:33 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2012-10-25 18:25:15 -07:00
|
|
|
void Run() MOZ_OVERRIDE
|
2012-09-26 22:15:33 -07:00
|
|
|
{
|
2012-10-25 18:25:15 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2013-03-01 20:04:59 -08:00
|
|
|
// Stop sending RING indicator
|
|
|
|
if (sStopSendingRingFlag) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2012-10-25 18:25:15 -07:00
|
|
|
if (!gBluetoothHfpManager) {
|
|
|
|
NS_WARNING("BluetoothHfpManager no longer exists, cannot send ring!");
|
|
|
|
return;
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
2012-12-17 07:24:35 -08:00
|
|
|
|
2013-06-03 00:04:10 -07:00
|
|
|
nsAutoCString ringMsg("RING");
|
2013-04-04 17:54:22 -07:00
|
|
|
gBluetoothHfpManager->SendLine(ringMsg.get());
|
2012-10-25 18:25:15 -07:00
|
|
|
|
2012-12-17 07:24:35 -08:00
|
|
|
if (!mNumber.IsEmpty()) {
|
2013-06-03 00:04:10 -07:00
|
|
|
nsAutoCString clipMsg("+CLIP: \"");
|
2013-04-04 17:54:22 -07:00
|
|
|
clipMsg.Append(NS_ConvertUTF16toUTF8(mNumber).get());
|
|
|
|
clipMsg.AppendLiteral("\",");
|
2013-01-15 22:13:41 -08:00
|
|
|
clipMsg.AppendInt(mType);
|
2013-04-04 17:54:22 -07:00
|
|
|
gBluetoothHfpManager->SendLine(clipMsg.get());
|
2012-12-17 07:24:35 -08:00
|
|
|
}
|
|
|
|
|
2012-10-25 18:25:15 -07:00
|
|
|
MessageLoop::current()->
|
|
|
|
PostDelayedTask(FROM_HERE,
|
2013-01-15 22:13:58 -08:00
|
|
|
new SendRingIndicatorTask(mNumber, mType),
|
2012-10-25 18:25:15 -07:00
|
|
|
sRingInterval);
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
2012-12-17 07:24:35 -08:00
|
|
|
|
|
|
|
private:
|
2013-01-15 22:13:58 -08:00
|
|
|
nsString mNumber;
|
2012-12-17 07:24:35 -08:00
|
|
|
int mType;
|
2012-09-26 22:15:33 -07:00
|
|
|
};
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2013-01-15 22:13:41 -08:00
|
|
|
static bool
|
2013-01-08 01:21:00 -08:00
|
|
|
IsValidDtmf(const char aChar) {
|
|
|
|
// Valid DTMF: [*#0-9ABCD]
|
|
|
|
if (aChar == '*' || aChar == '#') {
|
|
|
|
return true;
|
|
|
|
} else if (aChar >= '0' && aChar <= '9') {
|
|
|
|
return true;
|
|
|
|
} else if (aChar >= 'A' && aChar <= 'D') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
BluetoothHfpManager::BluetoothHfpManager()
|
2012-10-02 22:53:39 -07:00
|
|
|
{
|
2013-01-15 22:13:58 -08:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::ResetCallArray()
|
|
|
|
{
|
|
|
|
mCurrentCallArray.Clear();
|
|
|
|
// Append a call object at the beginning of mCurrentCallArray since call
|
|
|
|
// index from RIL starts at 1.
|
|
|
|
Call call;
|
|
|
|
mCurrentCallArray.AppendElement(call);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::Reset()
|
|
|
|
{
|
|
|
|
sStopSendingRingFlag = true;
|
2012-10-22 03:34:15 -07:00
|
|
|
sCINDItems[CINDType::CALL].value = CallState::NO_CALL;
|
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::NO_CALLSETUP;
|
|
|
|
sCINDItems[CINDType::CALLHELD].value = CallHeldState::NO_CALLHELD;
|
2012-10-30 20:53:36 -07:00
|
|
|
|
2013-01-15 22:17:41 -08:00
|
|
|
mCCWA = false;
|
2013-01-15 22:13:58 -08:00
|
|
|
mCLIP = false;
|
|
|
|
mCMEE = false;
|
|
|
|
mCMER = false;
|
|
|
|
mReceiveVgsFlag = false;
|
2013-05-31 05:43:18 -07:00
|
|
|
mDialingRequestProcessed = true;
|
2013-01-15 22:13:58 -08:00
|
|
|
|
2013-06-05 01:53:22 -07:00
|
|
|
// We disable BSIR by default as it requires OEM implement BT SCO + SPEAKER
|
|
|
|
// output audio path in audio driver. OEM can enable BSIR by setting
|
|
|
|
// mBSIR=true here.
|
|
|
|
//
|
|
|
|
// Please see Bug 878728 for more information.
|
|
|
|
mBSIR = false;
|
|
|
|
|
2013-01-15 22:13:58 -08:00
|
|
|
ResetCallArray();
|
2012-10-02 22:53:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothHfpManager::Init()
|
2012-09-26 06:34:55 -07:00
|
|
|
{
|
2012-11-18 23:10:38 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-10-05 17:11:24 -07:00
|
|
|
sHfpObserver = new BluetoothHfpManagerObserver();
|
2012-10-08 21:59:33 -07:00
|
|
|
if (!sHfpObserver->Init()) {
|
|
|
|
NS_WARNING("Cannot set up Hfp Observers!");
|
|
|
|
}
|
2012-10-02 22:53:39 -07:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
mListener = new BluetoothTelephonyListener();
|
2012-09-27 05:24:06 -07:00
|
|
|
if (!mListener->StartListening()) {
|
|
|
|
NS_WARNING("Failed to start listening RIL");
|
2012-10-02 22:53:39 -07:00
|
|
|
return false;
|
2012-09-27 05:24:06 -07:00
|
|
|
}
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
nsCOMPtr<nsISettingsService> settings =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
NS_ENSURE_TRUE(settings, false);
|
2012-10-25 03:50:41 -07:00
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
nsCOMPtr<nsISettingsServiceLock> settingsLock;
|
|
|
|
nsresult rv = settings->CreateLock(getter_AddRefs(settingsLock));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
nsRefPtr<GetVolumeTask> callback = new GetVolumeTask();
|
|
|
|
rv = settingsLock->Get(AUDIO_VOLUME_BT_SCO, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2012-10-25 03:50:41 -07:00
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
Listen();
|
|
|
|
|
2013-05-10 04:01:10 -07:00
|
|
|
mScoSocket = new BluetoothSocket(this,
|
|
|
|
BluetoothSocketType::SCO,
|
|
|
|
true,
|
|
|
|
false);
|
|
|
|
mScoSocketStatus = mScoSocket->GetConnectionStatus();
|
|
|
|
ListenSco();
|
2012-10-02 22:53:39 -07:00
|
|
|
return true;
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothHfpManager::~BluetoothHfpManager()
|
2012-10-02 22:53:39 -07:00
|
|
|
{
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::Cleanup()
|
2012-09-26 06:34:55 -07:00
|
|
|
{
|
2012-09-27 05:24:06 -07:00
|
|
|
if (!mListener->StopListening()) {
|
|
|
|
NS_WARNING("Failed to stop listening RIL");
|
|
|
|
}
|
|
|
|
mListener = nullptr;
|
|
|
|
|
2012-10-08 21:59:33 -07:00
|
|
|
sHfpObserver->Shutdown();
|
2012-10-05 17:11:24 -07:00
|
|
|
sHfpObserver = nullptr;
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
BluetoothHfpManager*
|
|
|
|
BluetoothHfpManager::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
// If we already exist, exit early
|
|
|
|
if (gBluetoothHfpManager) {
|
|
|
|
return gBluetoothHfpManager;
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
// If we're in shutdown, don't create a new instance
|
|
|
|
if (gInShutdown) {
|
|
|
|
NS_WARNING("BluetoothHfpManager can't be created during shutdown");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new instance, register, return
|
2013-04-04 17:54:22 -07:00
|
|
|
BluetoothHfpManager* manager = new BluetoothHfpManager();
|
|
|
|
NS_ENSURE_TRUE(manager->Init(), nullptr);
|
2012-10-02 22:53:39 -07:00
|
|
|
|
|
|
|
gBluetoothHfpManager = manager;
|
|
|
|
return gBluetoothHfpManager;
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
2012-10-01 00:44:48 -07:00
|
|
|
void
|
2013-05-10 23:06:16 -07:00
|
|
|
BluetoothHfpManager::NotifyStatusChanged(const nsAString& aType)
|
2012-10-01 00:44:48 -07:00
|
|
|
{
|
|
|
|
nsString type, name;
|
|
|
|
BluetoothValue v;
|
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
2013-05-10 23:06:16 -07:00
|
|
|
type = aType;
|
2012-10-01 00:44:48 -07:00
|
|
|
|
|
|
|
name.AssignLiteral("connected");
|
2013-05-10 23:06:16 -07:00
|
|
|
if (type.EqualsLiteral("bluetooth-hfp-status-changed")) {
|
|
|
|
v = IsConnected();
|
|
|
|
} else if (type.EqualsLiteral("bluetooth-sco-status-changed")) {
|
|
|
|
v = IsScoConnected();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Wrong type for NotifyStatusChanged");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-01 00:44:48 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("address");
|
2013-05-10 04:01:10 -07:00
|
|
|
v = mDeviceAddress;
|
2012-10-01 00:44:48 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_WARNING("Failed to broadcast system message to settings");
|
2012-10-01 00:44:48 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::NotifyDialer(const nsAString& aCommand)
|
|
|
|
{
|
2012-12-17 01:51:38 -08:00
|
|
|
nsString type, name;
|
|
|
|
BluetoothValue v;
|
2012-10-01 00:44:48 -07:00
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
type.AssignLiteral("bluetooth-dialer-command");
|
|
|
|
|
2012-12-17 01:51:38 -08:00
|
|
|
name.AssignLiteral("command");
|
|
|
|
v = nsString(aCommand);
|
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
2012-10-01 00:44:48 -07:00
|
|
|
|
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
|
|
|
NS_WARNING("Failed to broadcast system message to dialer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 04:01:10 -07:00
|
|
|
void
|
2013-06-08 08:26:01 -07:00
|
|
|
BluetoothHfpManager::NotifyAudioManager(bool aStatus)
|
2013-05-10 04:01:10 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
NS_ENSURE_TRUE_VOID(obs);
|
|
|
|
|
2013-06-08 08:26:01 -07:00
|
|
|
nsAutoString data;
|
|
|
|
data.AppendInt(aStatus);
|
|
|
|
|
|
|
|
if (NS_FAILED(obs->NotifyObservers(this,
|
2013-05-10 04:01:10 -07:00
|
|
|
BLUETOOTH_SCO_STATUS_CHANGED,
|
2013-06-08 08:26:01 -07:00
|
|
|
data.BeginReading()))) {
|
2013-05-10 04:01:10 -07:00
|
|
|
NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-25 03:18:38 -07:00
|
|
|
nsresult
|
|
|
|
BluetoothHfpManager::HandleVolumeChanged(const nsAString& aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// The string that we're interested in will be a JSON string that looks like:
|
2012-11-18 23:10:38 -08:00
|
|
|
// {"key":"volumeup", "value":10}
|
|
|
|
// {"key":"volumedown", "value":2}
|
2012-09-25 03:18:38 -07:00
|
|
|
|
|
|
|
JSContext* cx = nsContentUtils::GetSafeJSContext();
|
|
|
|
if (!cx) {
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_WARNING("Failed to get JSContext");
|
2012-09-25 03:18:38 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-29 01:29:39 -07:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2012-09-25 03:18:38 -07:00
|
|
|
if (!JS_ParseJSON(cx, aData.BeginReading(), aData.Length(), &val)) {
|
|
|
|
return JS_ReportPendingException(cx) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!val.isObject()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject& obj(val.toObject());
|
|
|
|
|
|
|
|
JS::Value key;
|
|
|
|
if (!JS_GetProperty(cx, &obj, "key", &key)) {
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key.isString()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool match;
|
2012-11-18 23:10:38 -08:00
|
|
|
if (!JS_StringEqualsAscii(cx, key.toString(), AUDIO_VOLUME_BT_SCO, &match)) {
|
2012-09-25 03:18:38 -07:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value value;
|
|
|
|
if (!JS_GetProperty(cx, &obj, "value", &value)) {
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value.isNumber()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
mCurrentVgs = value.toNumber();
|
2012-10-25 03:50:41 -07:00
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
// Adjust volume by headset and we don't have to send volume back to headset
|
2012-10-25 03:50:41 -07:00
|
|
|
if (mReceiveVgsFlag) {
|
|
|
|
mReceiveVgsFlag = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
// Only send volume back when there's a connected headset
|
2013-04-19 02:08:00 -07:00
|
|
|
if (IsConnected()) {
|
2012-11-18 23:10:38 -08:00
|
|
|
SendCommand("+VGS: ", mCurrentVgs);
|
2012-10-25 03:50:41 -07:00
|
|
|
}
|
2012-09-25 03:18:38 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-19 23:28:51 -08:00
|
|
|
nsresult
|
|
|
|
BluetoothHfpManager::HandleVoiceConnectionChanged()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMobileConnectionProvider> connection =
|
|
|
|
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(connection, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsIDOMMozMobileConnectionInfo* voiceInfo;
|
|
|
|
connection->GetVoiceConnectionInfo(&voiceInfo);
|
|
|
|
NS_ENSURE_TRUE(voiceInfo, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
bool roaming;
|
|
|
|
voiceInfo->GetRoaming(&roaming);
|
|
|
|
if (roaming != sCINDItems[CINDType::ROAM].value) {
|
|
|
|
sCINDItems[CINDType::ROAM].value = roaming;
|
|
|
|
SendCommand("+CIEV: ", CINDType::ROAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool service = false;
|
|
|
|
nsString regState;
|
|
|
|
voiceInfo->GetState(regState);
|
|
|
|
if (regState.EqualsLiteral("registered")) {
|
|
|
|
service = true;
|
|
|
|
}
|
|
|
|
if (service != sCINDItems[CINDType::SERVICE].value) {
|
|
|
|
sCINDItems[CINDType::SERVICE].value = service;
|
|
|
|
SendCommand("+CIEV: ", CINDType::SERVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t signal;
|
|
|
|
JS::Value value;
|
|
|
|
voiceInfo->GetRelSignalStrength(&value);
|
|
|
|
if (!value.isNumber()) {
|
|
|
|
NS_WARNING("Failed to get relSignalStrength in BluetoothHfpManager");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
signal = ceil(value.toNumber() / 20.0);
|
|
|
|
|
|
|
|
if (signal != sCINDItems[CINDType::SIGNAL].value) {
|
|
|
|
sCINDItems[CINDType::SIGNAL].value = signal;
|
|
|
|
SendCommand("+CIEV: ", CINDType::SIGNAL);
|
|
|
|
}
|
|
|
|
|
2013-01-22 17:22:08 -08:00
|
|
|
/**
|
|
|
|
* Possible return values for mode are:
|
|
|
|
* - null (unknown): set mNetworkSelectionMode to 0 (auto)
|
|
|
|
* - automatic: set mNetworkSelectionMode to 0 (auto)
|
|
|
|
* - manual: set mNetworkSelectionMode to 1 (manual)
|
|
|
|
*/
|
|
|
|
nsString mode;
|
|
|
|
connection->GetNetworkSelectionMode(mode);
|
|
|
|
if (mode.EqualsLiteral("manual")) {
|
|
|
|
mNetworkSelectionMode = 1;
|
|
|
|
} else {
|
|
|
|
mNetworkSelectionMode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMMozMobileNetworkInfo* network;
|
|
|
|
voiceInfo->GetNetwork(&network);
|
|
|
|
NS_ENSURE_TRUE(network, NS_ERROR_FAILURE);
|
|
|
|
network->GetLongName(mOperatorName);
|
|
|
|
|
2013-05-15 02:12:48 -07:00
|
|
|
// According to GSM 07.07, "<format> indicates if the format is alphanumeric
|
|
|
|
// or numeric; long alphanumeric format can be upto 16 characters long and
|
|
|
|
// short format up to 8 characters (refer GSM MoU SE.13 [9])..."
|
|
|
|
// However, we found that the operator name may sometimes be longer than 16
|
|
|
|
// characters. After discussion, we decided to fix this here but not in RIL
|
|
|
|
// or modem.
|
|
|
|
//
|
|
|
|
// Please see Bug 871366 for more information.
|
|
|
|
if (mOperatorName.Length() > 16) {
|
|
|
|
NS_WARNING("The operator name was longer than 16 characters. We cut it.");
|
|
|
|
mOperatorName.Left(mOperatorName, 16);
|
|
|
|
}
|
|
|
|
|
2012-12-19 23:28:51 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-17 01:44:32 -08:00
|
|
|
nsresult
|
|
|
|
BluetoothHfpManager::HandleIccInfoChanged()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMobileConnectionProvider> connection =
|
|
|
|
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(connection, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsIDOMMozMobileICCInfo* iccInfo;
|
|
|
|
connection->GetIccInfo(&iccInfo);
|
|
|
|
NS_ENSURE_TRUE(iccInfo, NS_ERROR_FAILURE);
|
2013-01-22 17:22:08 -08:00
|
|
|
iccInfo->GetMsisdn(mMsisdn);
|
2012-12-17 01:44:32 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
nsresult
|
|
|
|
BluetoothHfpManager::HandleShutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
gInShutdown = true;
|
2013-04-04 17:54:22 -07:00
|
|
|
Disconnect();
|
2013-05-10 04:01:10 -07:00
|
|
|
DisconnectSco();
|
2012-10-02 22:53:39 -07:00
|
|
|
gBluetoothHfpManager = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
// Virtual function of class SocketConsumer
|
|
|
|
void
|
2013-04-04 17:55:55 -07:00
|
|
|
BluetoothHfpManager::ReceiveSocketData(BluetoothSocket* aSocket,
|
|
|
|
nsAutoPtr<UnixSocketRawData>& aMessage)
|
2012-09-26 06:34:55 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-04-19 02:08:06 -07:00
|
|
|
MOZ_ASSERT(aSocket);
|
2012-09-26 19:24:39 -07:00
|
|
|
|
2013-01-29 20:58:40 -08:00
|
|
|
nsAutoCString msg((const char*)aMessage->mData.get(), aMessage->mSize);
|
2012-12-17 07:24:35 -08:00
|
|
|
msg.StripWhitespace();
|
|
|
|
|
|
|
|
nsTArray<nsCString> atCommandValues;
|
|
|
|
|
2012-09-26 19:24:39 -07:00
|
|
|
// For more information, please refer to 4.34.1 "Bluetooth Defined AT
|
|
|
|
// Capabilities" in Bluetooth hands-free profile 1.6
|
2012-12-17 07:24:35 -08:00
|
|
|
if (msg.Find("AT+BRSF=") != -1) {
|
2013-06-05 01:53:22 -07:00
|
|
|
uint32_t brsf = BRSF_BIT_THREE_WAY_CALLING |
|
|
|
|
BRSF_BIT_ABILITY_TO_REJECT_CALL |
|
|
|
|
BRSF_BIT_ENHANCED_CALL_STATUS;
|
|
|
|
|
|
|
|
if (mBSIR) {
|
|
|
|
brsf |= BRSF_BIT_IN_BAND_RING_TONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendCommand("+BRSF: ", brsf);
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CIND=?") != -1) {
|
2012-10-22 03:34:15 -07:00
|
|
|
// Asking for CIND range
|
|
|
|
SendCommand("+CIND: ", 0);
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CIND?") != -1) {
|
2012-10-22 03:34:15 -07:00
|
|
|
// Asking for CIND value
|
|
|
|
SendCommand("+CIND: ", 1);
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CMER=") != -1) {
|
|
|
|
/**
|
|
|
|
* SLC establishment is done when AT+CMER has been received.
|
|
|
|
* Do nothing but respond with "OK".
|
|
|
|
*/
|
2013-01-08 22:05:34 -08:00
|
|
|
ParseAtCommand(msg, 8, atCommandValues);
|
|
|
|
|
2013-01-11 01:45:28 -08:00
|
|
|
if (atCommandValues.Length() < 4) {
|
2013-01-08 22:05:34 -08:00
|
|
|
NS_WARNING("Could't get the value of command [AT+CMER=]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!atCommandValues[0].EqualsLiteral("3") ||
|
|
|
|
!atCommandValues[1].EqualsLiteral("0") ||
|
|
|
|
!atCommandValues[2].EqualsLiteral("0")) {
|
|
|
|
NS_WARNING("Wrong value of CMER");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
2013-01-10 22:00:04 -08:00
|
|
|
mCMER = atCommandValues[3].EqualsLiteral("1");
|
|
|
|
} else if (msg.Find("AT+CMEE=") != -1) {
|
|
|
|
ParseAtCommand(msg, 8, atCommandValues);
|
|
|
|
|
|
|
|
if (atCommandValues.IsEmpty()) {
|
|
|
|
NS_WARNING("Could't get the value of command [AT+CMEE=]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AT+CMEE = 0: +CME ERROR shall not be used
|
|
|
|
// AT+CMEE = 1: use numeric <err>
|
|
|
|
// AT+CMEE = 2: use verbose <err>
|
|
|
|
mCMEE = !atCommandValues[0].EqualsLiteral("0");
|
2013-01-22 17:22:08 -08:00
|
|
|
} else if (msg.Find("AT+COPS=") != -1) {
|
|
|
|
ParseAtCommand(msg, 8, atCommandValues);
|
|
|
|
|
|
|
|
if (atCommandValues.Length() != 2) {
|
|
|
|
NS_WARNING("Could't get the value of command [AT+COPS=]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handsfree only support AT+COPS=3,0
|
|
|
|
if (!atCommandValues[0].EqualsLiteral("3") ||
|
|
|
|
!atCommandValues[1].EqualsLiteral("0")) {
|
|
|
|
if (mCMEE) {
|
|
|
|
SendCommand("+CME ERROR: ", BluetoothCmeError::OPERATION_NOT_SUPPORTED);
|
|
|
|
} else {
|
|
|
|
SendLine("ERROR");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (msg.Find("AT+COPS?") != -1) {
|
|
|
|
nsAutoCString message("+COPS: ");
|
|
|
|
message.AppendInt(mNetworkSelectionMode);
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(",0,\"");
|
|
|
|
message.Append(NS_ConvertUTF16toUTF8(mOperatorName));
|
|
|
|
message.AppendLiteral("\"");
|
2013-01-22 17:22:08 -08:00
|
|
|
SendLine(message.get());
|
2013-01-08 01:21:00 -08:00
|
|
|
} else if (msg.Find("AT+VTS=") != -1) {
|
|
|
|
ParseAtCommand(msg, 7, atCommandValues);
|
2013-01-22 17:22:08 -08:00
|
|
|
|
2013-01-08 01:21:00 -08:00
|
|
|
if (atCommandValues.Length() != 1) {
|
|
|
|
NS_WARNING("Couldn't get the value of command [AT+VTS=]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsValidDtmf(atCommandValues[0].get()[0])) {
|
|
|
|
nsAutoCString message("VTS=");
|
|
|
|
message += atCommandValues[0].get()[0];
|
|
|
|
NotifyDialer(NS_ConvertUTF8toUTF16(message));
|
|
|
|
}
|
2013-01-07 00:58:59 -08:00
|
|
|
} else if (msg.Find("AT+VGM=") != -1) {
|
|
|
|
ParseAtCommand(msg, 7, atCommandValues);
|
|
|
|
|
|
|
|
if (atCommandValues.IsEmpty()) {
|
|
|
|
NS_WARNING("Couldn't get the value of command [AT+VGM]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
int vgm = atCommandValues[0].ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to extract microphone volume from bluetooth headset!");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(vgm >= 0 && vgm <= 15, "Received invalid VGM value");
|
|
|
|
mCurrentVgm = vgm;
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CHLD=?") != -1) {
|
2012-10-30 20:53:36 -07:00
|
|
|
SendLine("+CHLD: (1,2)");
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CHLD=") != -1) {
|
|
|
|
ParseAtCommand(msg, 8, atCommandValues);
|
|
|
|
|
|
|
|
if (atCommandValues.IsEmpty()) {
|
2013-01-18 02:53:00 -08:00
|
|
|
NS_WARNING("Could't get the value of command [AT+CHLD=]");
|
2012-12-17 07:24:35 -08:00
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
2012-10-30 20:53:36 -07:00
|
|
|
|
2013-01-18 02:53:00 -08:00
|
|
|
/**
|
|
|
|
* The following two cases are supported:
|
|
|
|
* AT+CHLD=1 - Releases active calls and accepts the other (held or
|
|
|
|
* waiting) call
|
|
|
|
* AT+CHLD=2 - Places active calls on hold and accepts the other (held
|
|
|
|
* or waiting) call
|
|
|
|
*
|
|
|
|
* The following cases are NOT supported yet:
|
|
|
|
* AT+CHLD=0, AT+CHLD=1<idx>, AT+CHLD=2<idx>, AT+CHLD=3, AT+CHLD=4
|
|
|
|
* Please see 4.33.2 in Bluetooth hands-free profile 1.6 for more
|
|
|
|
* information.
|
|
|
|
*/
|
2013-04-19 05:40:51 -07:00
|
|
|
char chld = atCommandValues[0][0];
|
|
|
|
bool valid = true;
|
2013-01-18 02:53:00 -08:00
|
|
|
if (atCommandValues[0].Length() > 1) {
|
2013-04-19 05:40:51 -07:00
|
|
|
NS_WARNING("No index should be included in command [AT+CHLD]");
|
|
|
|
valid = false;
|
|
|
|
} else if (chld == '0' || chld == '3' || chld == '4') {
|
|
|
|
NS_WARNING("The value of command [AT+CHLD] is not supported");
|
|
|
|
valid = false;
|
|
|
|
} else if (chld == '1') {
|
2013-01-18 02:53:00 -08:00
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHUP+ATA"));
|
|
|
|
} else if (chld == '2') {
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHLD+ATA"));
|
|
|
|
} else {
|
2013-04-19 05:40:51 -07:00
|
|
|
NS_WARNING("Wrong value of command [AT+CHLD]");
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
SendLine("ERROR");
|
|
|
|
return;
|
2012-10-30 20:53:36 -07:00
|
|
|
}
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+VGS=") != -1) {
|
2012-11-18 23:10:38 -08:00
|
|
|
// Adjust volume by headset
|
2012-10-25 03:50:41 -07:00
|
|
|
mReceiveVgsFlag = true;
|
2012-12-17 07:24:35 -08:00
|
|
|
ParseAtCommand(msg, 7, atCommandValues);
|
2012-09-26 19:24:39 -07:00
|
|
|
|
2012-12-17 07:24:35 -08:00
|
|
|
if (atCommandValues.IsEmpty()) {
|
|
|
|
NS_WARNING("Could't get the value of command [AT+VGS=]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
2012-10-25 03:50:41 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
2012-12-17 07:24:35 -08:00
|
|
|
int newVgs = atCommandValues[0].ToInteger(&rv);
|
2012-10-25 03:50:41 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to extract volume value from bluetooth headset!");
|
2012-12-17 07:24:35 -08:00
|
|
|
goto respond_with_ok;
|
2012-10-25 03:50:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newVgs == mCurrentVgs) {
|
2012-12-17 07:24:35 -08:00
|
|
|
goto respond_with_ok;
|
2012-09-26 19:24:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(newVgs >= 0 && newVgs <= 15, "Received invalid VGS value");
|
|
|
|
|
2012-10-25 03:50:41 -07:00
|
|
|
nsString data;
|
2012-09-26 19:24:39 -07:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
2012-11-18 23:10:38 -08:00
|
|
|
data.AppendInt(newVgs);
|
2012-10-25 03:50:41 -07:00
|
|
|
os->NotifyObservers(nullptr, "bluetooth-volume-change", data.get());
|
2013-05-09 07:10:28 -07:00
|
|
|
} else if ((msg.Find("AT+BLDN") != -1) || (msg.Find("ATD>") != -1)) {
|
|
|
|
// Dialer app of FFOS v1 does not have plan to support Memory Dailing.
|
2013-05-31 05:43:18 -07:00
|
|
|
// However, in order to pass Bluetooth HFP certification, we still have to
|
|
|
|
// make a call when we receive AT command 'ATD>n'.
|
|
|
|
mDialingRequestProcessed = false;
|
|
|
|
|
|
|
|
if (msg.Find("AT+BLDN") != -1) {
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("BLDN"));
|
|
|
|
} else {
|
|
|
|
NotifyDialer(NS_ConvertUTF8toUTF16(msg));
|
|
|
|
}
|
2013-05-09 07:10:28 -07:00
|
|
|
|
|
|
|
MessageLoop::current()->
|
|
|
|
PostDelayedTask(FROM_HERE, new RespondToBLDNTask(),
|
2013-05-31 05:43:18 -07:00
|
|
|
sWaitingForDialingInterval);
|
2013-05-09 07:10:28 -07:00
|
|
|
|
|
|
|
// Don't send response 'OK' here because we'll respond later in either
|
|
|
|
// RespondToBLDNTask or HandleCallStateChanged()
|
|
|
|
return;
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("ATA") != -1) {
|
2012-10-01 00:44:48 -07:00
|
|
|
NotifyDialer(NS_LITERAL_STRING("ATA"));
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CHUP") != -1) {
|
2012-10-01 00:44:48 -07:00
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHUP"));
|
2013-01-18 02:53:00 -08:00
|
|
|
} else if (msg.Find("AT+CLCC") != -1) {
|
|
|
|
SendCommand("+CLCC: ");
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("ATD") != -1) {
|
2012-12-17 01:51:38 -08:00
|
|
|
nsAutoCString message(msg), newMsg;
|
|
|
|
int end = message.FindChar(';');
|
|
|
|
if (end < 0) {
|
|
|
|
NS_WARNING("Could't get the value of command [ATD]");
|
2012-12-17 07:24:35 -08:00
|
|
|
goto respond_with_ok;
|
2012-12-17 01:51:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
newMsg += nsDependentCSubstring(message, 0, end);
|
|
|
|
NotifyDialer(NS_ConvertUTF8toUTF16(newMsg));
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CLIP=") != -1) {
|
|
|
|
ParseAtCommand(msg, 8, atCommandValues);
|
|
|
|
|
|
|
|
if (atCommandValues.IsEmpty()) {
|
|
|
|
NS_WARNING("Could't get the value of command [AT+CLIP=]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:17:41 -08:00
|
|
|
mCLIP = atCommandValues[0].EqualsLiteral("1");
|
|
|
|
} else if (msg.Find("AT+CCWA=") != -1) {
|
|
|
|
ParseAtCommand(msg, 8, atCommandValues);
|
|
|
|
|
|
|
|
if (atCommandValues.IsEmpty()) {
|
|
|
|
NS_WARNING("Could't get the value of command [AT+CCWA=]");
|
|
|
|
goto respond_with_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCCWA = atCommandValues[0].EqualsLiteral("1");
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CKPD") != -1) {
|
2013-04-19 02:08:06 -07:00
|
|
|
if (!sStopSendingRingFlag) {
|
|
|
|
// Bluetooth HSP spec 4.2.2
|
|
|
|
// There is an incoming call, notify Dialer to pick up the phone call
|
|
|
|
// and SCO will be established after we get the CallStateChanged event
|
|
|
|
// indicating the call is answered successfully.
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("ATA"));
|
|
|
|
} else {
|
2013-05-10 04:03:25 -07:00
|
|
|
if (!IsScoConnected()) {
|
2013-04-19 02:08:06 -07:00
|
|
|
// Bluetooth HSP spec 4.3
|
|
|
|
// If there's no SCO, set up a SCO link.
|
2013-05-10 04:03:25 -07:00
|
|
|
ConnectSco();
|
2013-04-19 02:08:06 -07:00
|
|
|
} else if (!mFirstCKPD) {
|
|
|
|
// Bluetooth HSP spec 4.5
|
|
|
|
// There are two ways to release SCO: sending CHUP to dialer or closing
|
|
|
|
// SCO socket directly. We notify dialer only if there is at least one
|
|
|
|
// active call.
|
|
|
|
if (mCurrentCallArray.Length() > 1) {
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHUP"));
|
|
|
|
} else {
|
2013-05-10 04:03:25 -07:00
|
|
|
DisconnectSco();
|
2013-04-19 02:08:06 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Three conditions have to be matched to come in here:
|
|
|
|
// (1) Not sending RING indicator
|
|
|
|
// (2) A SCO link exists
|
|
|
|
// (3) This is the very first AT+CKPD=200 of this session
|
|
|
|
// It is the case of Figure 4.3, Bluetooth HSP spec. Do nothing.
|
|
|
|
NS_WARNING("AT+CKPD=200: Do nothing");
|
|
|
|
}
|
2012-10-05 03:48:05 -07:00
|
|
|
}
|
2013-04-19 02:08:06 -07:00
|
|
|
|
|
|
|
mFirstCKPD = false;
|
2012-12-17 07:24:35 -08:00
|
|
|
} else if (msg.Find("AT+CNUM") != -1) {
|
2012-12-17 01:44:32 -08:00
|
|
|
if (!mMsisdn.IsEmpty()) {
|
|
|
|
nsAutoCString message("+CNUM: ,\"");
|
2013-04-04 17:54:22 -07:00
|
|
|
message.Append(NS_ConvertUTF16toUTF8(mMsisdn).get());
|
|
|
|
message.AppendLiteral("\",");
|
2012-12-17 01:44:32 -08:00
|
|
|
message.AppendInt(TOA_UNKNOWN);
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(",,4");
|
2012-12-17 01:44:32 -08:00
|
|
|
SendLine(message.get());
|
|
|
|
}
|
2012-09-26 19:24:39 -07:00
|
|
|
} else {
|
|
|
|
nsCString warningMsg;
|
2013-03-11 22:46:43 -07:00
|
|
|
warningMsg.Append(NS_LITERAL_CSTRING("Unsupported AT command: "));
|
2012-09-26 19:24:39 -07:00
|
|
|
warningMsg.Append(msg);
|
2013-03-11 22:46:43 -07:00
|
|
|
warningMsg.Append(NS_LITERAL_CSTRING(", reply with ERROR"));
|
2012-09-26 19:24:39 -07:00
|
|
|
NS_WARNING(warningMsg.get());
|
2013-03-11 22:46:43 -07:00
|
|
|
|
|
|
|
SendLine("ERROR");
|
|
|
|
return;
|
2012-09-26 19:24:39 -07:00
|
|
|
}
|
2012-12-17 07:24:35 -08:00
|
|
|
|
|
|
|
respond_with_ok:
|
|
|
|
// We always respond to remote device with "OK" in general cases.
|
|
|
|
SendLine("OK");
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
2013-05-09 23:39:12 -07:00
|
|
|
void
|
2013-05-22 04:08:21 -07:00
|
|
|
BluetoothHfpManager::Connect(const nsAString& aDeviceAddress,
|
2012-10-05 03:48:05 -07:00
|
|
|
const bool aIsHandsfree,
|
2012-09-26 06:34:55 -07:00
|
|
|
BluetoothReplyRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-06-11 03:24:19 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs || gInShutdown) {
|
|
|
|
DispatchBluetoothReply(aRunnable, BluetoothValue(),
|
|
|
|
NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSocket) {
|
|
|
|
DispatchBluetoothReply(aRunnable, BluetoothValue(),
|
|
|
|
NS_LITERAL_STRING(ERR_REACHED_CONNECTION_LIMIT));
|
|
|
|
return;
|
|
|
|
}
|
2013-05-09 23:39:12 -07:00
|
|
|
|
2013-05-22 04:08:21 -07:00
|
|
|
mNeedsUpdatingSdpRecords = true;
|
|
|
|
mIsHandsfree = aIsHandsfree;
|
|
|
|
|
2013-05-09 23:39:12 -07:00
|
|
|
nsString uuid;
|
|
|
|
if (aIsHandsfree) {
|
|
|
|
BluetoothUuidHelper::GetString(BluetoothServiceClass::HANDSFREE, uuid);
|
|
|
|
} else {
|
|
|
|
BluetoothUuidHelper::GetString(BluetoothServiceClass::HEADSET, uuid);
|
2012-10-02 22:53:39 -07:00
|
|
|
}
|
|
|
|
|
2013-05-22 04:08:21 -07:00
|
|
|
if (NS_FAILED(bs->GetServiceChannel(aDeviceAddress, uuid, this))) {
|
|
|
|
DispatchBluetoothReply(aRunnable, BluetoothValue(),
|
|
|
|
NS_LITERAL_STRING(ERR_SERVICE_CHANNEL_NOT_FOUND));
|
2013-05-09 23:39:12 -07:00
|
|
|
return;
|
2012-10-22 03:34:15 -07:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
// Stop listening because currently we only support one connection at a time.
|
|
|
|
if (mHandsfreeSocket) {
|
|
|
|
mHandsfreeSocket->Disconnect();
|
|
|
|
mHandsfreeSocket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHeadsetSocket) {
|
|
|
|
mHeadsetSocket->Disconnect();
|
|
|
|
mHeadsetSocket = nullptr;
|
|
|
|
}
|
2012-10-10 22:49:11 -07:00
|
|
|
|
2013-05-13 03:08:40 -07:00
|
|
|
MOZ_ASSERT(!mRunnable);
|
|
|
|
|
2012-11-07 15:48:39 -08:00
|
|
|
mRunnable = aRunnable;
|
2013-04-19 02:08:00 -07:00
|
|
|
mSocket =
|
|
|
|
new BluetoothSocket(this, BluetoothSocketType::RFCOMM, true, true);
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
2012-10-01 00:04:01 -07:00
|
|
|
bool
|
|
|
|
BluetoothHfpManager::Listen()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
if (gInShutdown) {
|
2013-05-10 04:01:10 -07:00
|
|
|
NS_WARNING("Listen called while in shutdown!");
|
2012-10-02 22:53:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
if (mSocket) {
|
|
|
|
NS_WARNING("mSocket exists. Failed to listen.");
|
|
|
|
return false;
|
2012-11-27 23:59:20 -08:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
if (!mHandsfreeSocket) {
|
|
|
|
mHandsfreeSocket =
|
|
|
|
new BluetoothSocket(this, BluetoothSocketType::RFCOMM, true, true);
|
2012-10-10 22:49:11 -07:00
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
if (!mHandsfreeSocket->Listen(
|
|
|
|
BluetoothReservedChannels::CHANNEL_HANDSFREE_AG)) {
|
|
|
|
NS_WARNING("[HFP] Can't listen on RFCOMM socket!");
|
|
|
|
mHandsfreeSocket = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHeadsetSocket) {
|
|
|
|
mHeadsetSocket =
|
|
|
|
new BluetoothSocket(this, BluetoothSocketType::RFCOMM, true, true);
|
|
|
|
|
|
|
|
if (!mHeadsetSocket->Listen(
|
|
|
|
BluetoothReservedChannels::CHANNEL_HEADSET_AG)) {
|
|
|
|
NS_WARNING("[HSP] Can't listen on RFCOMM socket!");
|
|
|
|
mHandsfreeSocket->Disconnect();
|
|
|
|
mHandsfreeSocket = nullptr;
|
|
|
|
mHeadsetSocket = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-04 17:54:22 -07:00
|
|
|
}
|
2013-04-04 17:45:06 -07:00
|
|
|
|
2013-04-04 17:54:22 -07:00
|
|
|
return true;
|
2012-10-01 00:04:01 -07:00
|
|
|
}
|
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::Disconnect()
|
|
|
|
{
|
2013-04-19 02:08:00 -07:00
|
|
|
if (mSocket) {
|
|
|
|
mSocket->Disconnect();
|
|
|
|
mSocket = nullptr;
|
|
|
|
}
|
2013-06-08 08:26:01 -07:00
|
|
|
|
|
|
|
BluetoothA2dpManager* a2dp = BluetoothA2dpManager::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(a2dp);
|
|
|
|
a2dp->Disconnect();
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
2012-09-26 19:24:39 -07:00
|
|
|
bool
|
|
|
|
BluetoothHfpManager::SendLine(const char* aMessage)
|
|
|
|
{
|
2013-04-19 02:08:00 -07:00
|
|
|
MOZ_ASSERT(mSocket);
|
|
|
|
|
2012-09-26 19:24:39 -07:00
|
|
|
nsAutoCString msg;
|
|
|
|
|
2013-04-04 17:54:22 -07:00
|
|
|
msg.AppendLiteral(kHfpCrlf);
|
|
|
|
msg.Append(aMessage);
|
|
|
|
msg.AppendLiteral(kHfpCrlf);
|
2012-09-26 19:24:39 -07:00
|
|
|
|
2013-04-04 17:54:22 -07:00
|
|
|
return mSocket->SendSocketData(msg);
|
2012-09-26 19:24:39 -07:00
|
|
|
}
|
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
bool
|
2013-06-05 01:53:22 -07:00
|
|
|
BluetoothHfpManager::SendCommand(const char* aCommand, uint32_t aValue)
|
2012-10-09 04:19:29 -07:00
|
|
|
{
|
2013-04-19 02:08:00 -07:00
|
|
|
if (!IsConnected()) {
|
|
|
|
NS_WARNING("Trying to SendCommand() without a SLC");
|
2012-12-19 23:28:51 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
nsAutoCString message;
|
|
|
|
message += aCommand;
|
|
|
|
|
|
|
|
if (!strcmp(aCommand, "+CIEV: ")) {
|
2013-01-08 22:05:34 -08:00
|
|
|
if (!mCMER) {
|
|
|
|
// Indicator status update is disabled
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-19 23:28:51 -08:00
|
|
|
if ((aValue < 1) || (aValue > ArrayLength(sCINDItems) - 1)) {
|
|
|
|
NS_WARNING("unexpected CINDType for CIEV command");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
message.AppendInt(aValue);
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(",");
|
2012-12-19 23:28:51 -08:00
|
|
|
message.AppendInt(sCINDItems[aValue].value);
|
2012-10-22 03:34:15 -07:00
|
|
|
} else if (!strcmp(aCommand, "+CIND: ")) {
|
|
|
|
if (!aValue) {
|
2013-01-15 22:13:58 -08:00
|
|
|
// Query for range
|
2012-10-22 03:34:15 -07:00
|
|
|
for (uint8_t i = 1; i < ArrayLength(sCINDItems); i++) {
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral("(\"");
|
|
|
|
message.Append(sCINDItems[i].name);
|
|
|
|
message.AppendLiteral("\",(");
|
|
|
|
message.Append(sCINDItems[i].range);
|
|
|
|
message.AppendLiteral(")");
|
2012-10-22 03:34:15 -07:00
|
|
|
if (i == (ArrayLength(sCINDItems) - 1)) {
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(")");
|
2012-10-22 03:34:15 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral("),");
|
2012-10-22 03:34:15 -07:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-15 22:13:58 -08:00
|
|
|
// Query for value
|
2012-10-22 03:34:15 -07:00
|
|
|
for (uint8_t i = 1; i < ArrayLength(sCINDItems); i++) {
|
|
|
|
message.AppendInt(sCINDItems[i].value);
|
|
|
|
if (i == (ArrayLength(sCINDItems) - 1)) {
|
|
|
|
break;
|
|
|
|
}
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(",");
|
2012-10-22 03:34:15 -07:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 22:13:58 -08:00
|
|
|
} else if (!strcmp(aCommand, "+CLCC: ")) {
|
|
|
|
bool rv = true;
|
|
|
|
uint32_t callNumbers = mCurrentCallArray.Length();
|
|
|
|
for (uint32_t i = 1; i < callNumbers; i++) {
|
|
|
|
Call& call = mCurrentCallArray[i];
|
2013-03-06 01:53:23 -08:00
|
|
|
if (call.mState == nsITelephonyProvider::CALL_STATE_DISCONNECTED) {
|
2013-01-15 22:13:58 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.AssignLiteral("+CLCC: ");
|
|
|
|
message.AppendInt(i);
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(",");
|
2013-01-15 22:13:58 -08:00
|
|
|
message.AppendInt(call.mDirection);
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(",");
|
2013-01-15 22:13:58 -08:00
|
|
|
|
|
|
|
switch (call.mState) {
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_CONNECTED:
|
2013-01-15 22:13:58 -08:00
|
|
|
message.AppendInt(0);
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_HELD:
|
2013-01-15 22:13:58 -08:00
|
|
|
message.AppendInt(1);
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_DIALING:
|
2013-01-15 22:13:58 -08:00
|
|
|
message.AppendInt(2);
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_ALERTING:
|
2013-01-15 22:13:58 -08:00
|
|
|
message.AppendInt(3);
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_INCOMING:
|
2013-05-27 23:58:51 -07:00
|
|
|
if (!FindFirstCall(nsITelephonyProvider::CALL_STATE_CONNECTED)) {
|
|
|
|
message.AppendInt(4);
|
|
|
|
} else {
|
|
|
|
message.AppendInt(5);
|
|
|
|
}
|
2013-01-15 22:13:58 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Not handling call status for CLCC");
|
|
|
|
break;
|
|
|
|
}
|
2013-04-04 17:54:22 -07:00
|
|
|
message.AppendLiteral(",0,0,\"");
|
|
|
|
message.Append(NS_ConvertUTF16toUTF8(call.mNumber));
|
|
|
|
message.AppendLiteral("\",");
|
2013-01-15 22:13:58 -08:00
|
|
|
message.AppendInt(call.mType);
|
|
|
|
|
|
|
|
rv &= SendLine(message.get());
|
|
|
|
}
|
|
|
|
return rv;
|
2012-10-22 03:34:15 -07:00
|
|
|
} else {
|
2013-06-05 01:53:22 -07:00
|
|
|
message.AppendInt(aValue);
|
2012-10-09 04:19:29 -07:00
|
|
|
}
|
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
return SendLine(message.get());
|
2012-10-09 04:19:29 -07:00
|
|
|
}
|
|
|
|
|
2012-09-26 22:15:22 -07:00
|
|
|
void
|
2013-01-28 03:28:51 -08:00
|
|
|
BluetoothHfpManager::UpdateCIND(uint8_t aType, uint8_t aValue, bool aSend)
|
2012-09-26 22:15:22 -07:00
|
|
|
{
|
2013-01-28 03:28:51 -08:00
|
|
|
if (sCINDItems[aType].value != aValue) {
|
|
|
|
sCINDItems[aType].value = aValue;
|
|
|
|
// Indicator status update is enabled
|
|
|
|
if (aSend && mCMER) {
|
|
|
|
SendCommand("+CIEV: ", aType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 23:58:51 -07:00
|
|
|
uint32_t
|
|
|
|
BluetoothHfpManager::FindFirstCall(uint16_t aState)
|
|
|
|
{
|
|
|
|
uint32_t callLength = mCurrentCallArray.Length();
|
|
|
|
|
|
|
|
for (uint32_t i = 1; i < callLength; ++i) {
|
|
|
|
if (mCurrentCallArray[i].mState == aState) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
BluetoothHfpManager::GetNumberOfCalls(uint16_t aState)
|
|
|
|
{
|
|
|
|
uint32_t num = 0;
|
|
|
|
uint32_t callLength = mCurrentCallArray.Length();
|
|
|
|
|
|
|
|
for (uint32_t i = 1; i < callLength; ++i) {
|
|
|
|
if (mCurrentCallArray[i].mState == aState) {
|
|
|
|
++num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:28:51 -08:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::HandleCallStateChanged(uint32_t aCallIndex,
|
|
|
|
uint16_t aCallState,
|
|
|
|
const nsAString& aNumber,
|
2013-05-10 02:47:16 -07:00
|
|
|
const bool aIsOutgoing,
|
2013-01-28 03:28:51 -08:00
|
|
|
bool aSend)
|
|
|
|
{
|
2013-04-19 02:08:00 -07:00
|
|
|
if (!IsConnected()) {
|
|
|
|
// Normal case. No need to print out warnings.
|
2013-01-28 03:28:51 -08:00
|
|
|
return;
|
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2013-01-15 22:13:58 -08:00
|
|
|
while (aCallIndex >= mCurrentCallArray.Length()) {
|
|
|
|
Call call;
|
|
|
|
mCurrentCallArray.AppendElement(call);
|
|
|
|
}
|
|
|
|
|
2013-03-01 20:04:59 -08:00
|
|
|
uint16_t prevCallState = mCurrentCallArray[aCallIndex].mState;
|
|
|
|
mCurrentCallArray[aCallIndex].mState = aCallState;
|
2013-05-10 02:47:16 -07:00
|
|
|
mCurrentCallArray[aCallIndex].mDirection = !aIsOutgoing;
|
2013-03-01 20:04:59 -08:00
|
|
|
|
2013-01-15 22:13:58 -08:00
|
|
|
// Same logic as implementation in ril_worker.js
|
|
|
|
if (aNumber.Length() && aNumber[0] == '+') {
|
|
|
|
mCurrentCallArray[aCallIndex].mType = TOA_INTERNATIONAL;
|
2012-10-30 20:53:36 -07:00
|
|
|
}
|
2013-01-15 22:13:58 -08:00
|
|
|
mCurrentCallArray[aCallIndex].mNumber = aNumber;
|
2012-10-30 20:53:36 -07:00
|
|
|
|
2013-01-28 03:28:51 -08:00
|
|
|
nsRefPtr<nsRunnable> sendRingTask;
|
|
|
|
nsString address;
|
2012-10-30 20:53:36 -07:00
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
switch (aCallState) {
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_HELD:
|
2013-05-31 06:49:42 -07:00
|
|
|
if (!FindFirstCall(nsITelephonyProvider::CALL_STATE_CONNECTED)) {
|
|
|
|
sCINDItems[CINDType::CALLHELD].value = CallHeldState::ONHOLD_NOACTIVE;
|
|
|
|
} else {
|
|
|
|
sCINDItems[CINDType::CALLHELD].value = CallHeldState::ONHOLD_ACTIVE;
|
|
|
|
}
|
2013-01-28 03:28:51 -08:00
|
|
|
SendCommand("+CIEV: ", CINDType::CALLHELD);
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_INCOMING:
|
2013-05-27 23:58:51 -07:00
|
|
|
if (FindFirstCall(nsITelephonyProvider::CALL_STATE_CONNECTED)) {
|
2013-01-15 22:17:41 -08:00
|
|
|
if (mCCWA) {
|
|
|
|
nsAutoCString ccwaMsg("+CCWA: \"");
|
2013-04-04 17:54:22 -07:00
|
|
|
ccwaMsg.Append(NS_ConvertUTF16toUTF8(aNumber));
|
|
|
|
ccwaMsg.AppendLiteral("\",");
|
2013-01-15 22:17:41 -08:00
|
|
|
ccwaMsg.AppendInt(mCurrentCallArray[aCallIndex].mType);
|
|
|
|
SendLine(ccwaMsg.get());
|
|
|
|
}
|
2013-01-28 03:28:51 -08:00
|
|
|
UpdateCIND(CINDType::CALLSETUP, CallSetupState::INCOMING, aSend);
|
2013-01-15 22:17:41 -08:00
|
|
|
} else {
|
2012-10-30 20:53:36 -07:00
|
|
|
// Start sending RING indicator to HF
|
|
|
|
sStopSendingRingFlag = false;
|
2013-01-28 03:28:51 -08:00
|
|
|
UpdateCIND(CINDType::CALLSETUP, CallSetupState::INCOMING, aSend);
|
2013-01-15 22:17:41 -08:00
|
|
|
|
2013-06-05 01:53:22 -07:00
|
|
|
if (mBSIR) {
|
|
|
|
// Setup audio connection for in-band ring tone
|
|
|
|
ConnectSco();
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:13:58 -08:00
|
|
|
nsAutoString number(aNumber);
|
2012-12-17 07:24:35 -08:00
|
|
|
if (!mCLIP) {
|
2013-01-15 22:13:58 -08:00
|
|
|
number.AssignLiteral("");
|
2012-12-17 07:24:35 -08:00
|
|
|
}
|
2013-01-15 22:13:58 -08:00
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
MessageLoop::current()->PostDelayedTask(
|
|
|
|
FROM_HERE,
|
|
|
|
new SendRingIndicatorTask(number,
|
|
|
|
mCurrentCallArray[aCallIndex].mType),
|
2013-01-15 22:13:58 -08:00
|
|
|
sRingInterval);
|
2012-10-30 20:53:36 -07:00
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_DIALING:
|
2013-05-31 05:43:18 -07:00
|
|
|
if (!mDialingRequestProcessed) {
|
2013-05-09 07:10:28 -07:00
|
|
|
SendLine("OK");
|
2013-05-31 05:43:18 -07:00
|
|
|
mDialingRequestProcessed = true;
|
2013-05-09 07:10:28 -07:00
|
|
|
}
|
|
|
|
|
2013-01-28 03:28:51 -08:00
|
|
|
UpdateCIND(CINDType::CALLSETUP, CallSetupState::OUTGOING, aSend);
|
2013-05-10 04:03:25 -07:00
|
|
|
ConnectSco();
|
2012-09-26 22:15:33 -07:00
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_ALERTING:
|
2013-01-28 03:28:51 -08:00
|
|
|
UpdateCIND(CINDType::CALLSETUP, CallSetupState::OUTGOING_ALERTING, aSend);
|
|
|
|
|
|
|
|
// If there's an ongoing call when the headset is just connected, we have
|
|
|
|
// to open a sco socket here.
|
2013-05-10 04:03:25 -07:00
|
|
|
ConnectSco();
|
2012-09-26 22:15:33 -07:00
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_CONNECTED:
|
2013-01-28 03:28:51 -08:00
|
|
|
switch (prevCallState) {
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_INCOMING:
|
|
|
|
case nsITelephonyProvider::CALL_STATE_DISCONNECTED:
|
2013-01-28 03:28:51 -08:00
|
|
|
// Incoming call, no break
|
|
|
|
sStopSendingRingFlag = true;
|
2013-05-10 04:03:25 -07:00
|
|
|
ConnectSco();
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_ALERTING:
|
2013-01-28 03:28:51 -08:00
|
|
|
// Outgoing call
|
|
|
|
UpdateCIND(CINDType::CALL, CallState::IN_PROGRESS, aSend);
|
|
|
|
UpdateCIND(CINDType::CALLSETUP, CallSetupState::NO_CALLSETUP, aSend);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Not handling state changed");
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
2013-05-31 06:49:42 -07:00
|
|
|
|
|
|
|
// = Handle callheld separately =
|
|
|
|
// Besides checking if there is still held calls, another thing we
|
|
|
|
// need to consider is the state change when receiving AT+CHLD=2.
|
|
|
|
// Assume that there is one active call(c1) and one call on hold(c2).
|
|
|
|
// We got AT+CHLD=2, which swaps active/held position. The first
|
|
|
|
// action would be c2 -> ACTIVE, then c1 -> HELD. When we get the
|
|
|
|
// CallStateChanged event of c2 becoming ACTIVE, we enter here.
|
|
|
|
// However we can't send callheld=0 at this time because we should
|
|
|
|
// see c2 -> ACTIVE + c1 -> HELD as one operation. That's the reason
|
|
|
|
// why I added the GetNumberOfCalls() condition check.
|
|
|
|
if (GetNumberOfCalls(nsITelephonyProvider::CALL_STATE_CONNECTED) == 1) {
|
|
|
|
if (FindFirstCall(nsITelephonyProvider::CALL_STATE_HELD)) {
|
|
|
|
UpdateCIND(CINDType::CALLHELD, CallHeldState::ONHOLD_ACTIVE, aSend);
|
|
|
|
} else if (prevCallState == nsITelephonyProvider::CALL_STATE_HELD) {
|
|
|
|
UpdateCIND(CINDType::CALLHELD, CallHeldState::NO_CALLHELD, aSend);
|
|
|
|
}
|
|
|
|
}
|
2012-10-30 20:53:36 -07:00
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_DISCONNECTED:
|
2013-01-28 03:28:51 -08:00
|
|
|
switch (prevCallState) {
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_INCOMING:
|
|
|
|
case nsITelephonyProvider::CALL_STATE_BUSY:
|
2013-01-28 03:28:51 -08:00
|
|
|
// Incoming call, no break
|
|
|
|
sStopSendingRingFlag = true;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_DIALING:
|
|
|
|
case nsITelephonyProvider::CALL_STATE_ALERTING:
|
2013-01-28 03:28:51 -08:00
|
|
|
// Outgoing call
|
|
|
|
UpdateCIND(CINDType::CALLSETUP, CallSetupState::NO_CALLSETUP, aSend);
|
|
|
|
break;
|
2013-03-06 01:53:23 -08:00
|
|
|
case nsITelephonyProvider::CALL_STATE_CONNECTED:
|
2013-01-28 03:28:51 -08:00
|
|
|
// No call is ongoing
|
2013-04-19 02:08:00 -07:00
|
|
|
if (sCINDItems[CINDType::CALLHELD].value ==
|
|
|
|
CallHeldState::NO_CALLHELD) {
|
2013-01-28 03:28:51 -08:00
|
|
|
UpdateCIND(CINDType::CALL, CallState::NO_CALL, aSend);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Not handling state changed");
|
2012-10-30 20:53:36 -07:00
|
|
|
}
|
2013-01-08 22:05:34 -08:00
|
|
|
|
2013-05-31 06:49:42 -07:00
|
|
|
// Handle held calls separately
|
|
|
|
if (!FindFirstCall(nsITelephonyProvider::CALL_STATE_HELD)) {
|
|
|
|
UpdateCIND(CINDType::CALLHELD, CallHeldState::NO_CALLHELD, aSend);
|
|
|
|
} else if (!FindFirstCall(nsITelephonyProvider::CALL_STATE_CONNECTED)) {
|
|
|
|
UpdateCIND(CINDType::CALLHELD, CallHeldState::ONHOLD_NOACTIVE, aSend);
|
|
|
|
} else {
|
|
|
|
UpdateCIND(CINDType::CALLHELD, CallHeldState::ONHOLD_ACTIVE, aSend);
|
|
|
|
}
|
|
|
|
|
2013-05-27 23:58:51 -07:00
|
|
|
// -1 is necessary because call 0 is an invalid (padding) call object.
|
|
|
|
if (mCurrentCallArray.Length() - 1 ==
|
|
|
|
GetNumberOfCalls(nsITelephonyProvider::CALL_STATE_DISCONNECTED)) {
|
2013-01-28 03:28:51 -08:00
|
|
|
// There is no call, close Sco and clear mCurrentCallArray
|
2013-05-27 23:58:51 -07:00
|
|
|
DisconnectSco();
|
|
|
|
ResetCallArray();
|
2013-01-28 03:28:51 -08:00
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Not handling state changed");
|
2013-05-07 23:16:51 -07:00
|
|
|
break;
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
2012-09-26 22:15:22 -07:00
|
|
|
}
|
2012-10-10 22:49:11 -07:00
|
|
|
|
|
|
|
void
|
2013-04-04 17:55:55 -07:00
|
|
|
BluetoothHfpManager::OnConnectSuccess(BluetoothSocket* aSocket)
|
2012-10-10 22:49:11 -07:00
|
|
|
{
|
2013-04-19 02:08:00 -07:00
|
|
|
MOZ_ASSERT(aSocket);
|
|
|
|
|
2013-05-10 04:01:10 -07:00
|
|
|
// Success to create a SCO socket
|
|
|
|
if (aSocket == mScoSocket) {
|
|
|
|
OnScoConnectSuccess();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
/**
|
|
|
|
* If the created connection is an inbound connection, close another server
|
|
|
|
* socket because currently only one SLC is allowed. After that, we need to
|
|
|
|
* make sure that both server socket would be nulled out. As for outbound
|
|
|
|
* connections, we do nothing since sockets have been already handled in
|
|
|
|
* function Connect().
|
|
|
|
*/
|
|
|
|
if (aSocket == mHandsfreeSocket) {
|
|
|
|
MOZ_ASSERT(!mSocket);
|
|
|
|
mHandsfreeSocket.swap(mSocket);
|
|
|
|
|
|
|
|
mHeadsetSocket->Disconnect();
|
|
|
|
mHeadsetSocket = nullptr;
|
|
|
|
} else if (aSocket == mHeadsetSocket) {
|
|
|
|
MOZ_ASSERT(!mSocket);
|
|
|
|
mHeadsetSocket.swap(mSocket);
|
|
|
|
|
|
|
|
mHandsfreeSocket->Disconnect();
|
|
|
|
mHandsfreeSocket = nullptr;
|
|
|
|
}
|
2013-04-04 17:55:55 -07:00
|
|
|
|
2013-03-06 01:53:23 -08:00
|
|
|
nsCOMPtr<nsITelephonyProvider> provider =
|
2013-01-28 03:28:51 -08:00
|
|
|
do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
|
2013-03-06 01:53:23 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(provider);
|
|
|
|
provider->EnumerateCalls(mListener->GetListener());
|
2013-01-28 03:28:51 -08:00
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
// For active connection request, we need to reply the DOMRequest
|
2012-11-07 15:48:39 -08:00
|
|
|
if (mRunnable) {
|
2012-11-18 23:10:38 -08:00
|
|
|
BluetoothValue v = true;
|
|
|
|
nsString errorStr;
|
|
|
|
DispatchBluetoothReply(mRunnable, v, errorStr);
|
|
|
|
|
2013-05-10 04:01:10 -07:00
|
|
|
mRunnable = nullptr;
|
2012-11-07 15:48:39 -08:00
|
|
|
}
|
2012-11-18 23:10:38 -08:00
|
|
|
|
2013-04-19 02:08:06 -07:00
|
|
|
mFirstCKPD = true;
|
|
|
|
|
2012-10-26 02:21:32 -07:00
|
|
|
// Cache device path for NotifySettings() since we can't get socket address
|
|
|
|
// when a headset disconnect with us
|
2013-05-10 04:01:10 -07:00
|
|
|
mSocket->GetAddress(mDeviceAddress);
|
2013-05-10 23:06:16 -07:00
|
|
|
NotifyStatusChanged(NS_LITERAL_STRING("bluetooth-hfp-status-changed"));
|
2013-05-10 04:01:10 -07:00
|
|
|
|
|
|
|
ListenSco();
|
2013-06-08 08:26:01 -07:00
|
|
|
|
|
|
|
BluetoothA2dpManager* a2dp = BluetoothA2dpManager::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(a2dp);
|
|
|
|
a2dp->Connect(mDeviceAddress);
|
2012-10-10 22:49:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-04 17:55:55 -07:00
|
|
|
BluetoothHfpManager::OnConnectError(BluetoothSocket* aSocket)
|
2012-10-10 22:49:11 -07:00
|
|
|
{
|
2013-05-10 04:01:10 -07:00
|
|
|
// Failed to create a SCO socket
|
|
|
|
if (aSocket == mScoSocket) {
|
|
|
|
OnScoConnectError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
// For active connection request, we need to reply the DOMRequest
|
2012-11-07 15:48:39 -08:00
|
|
|
if (mRunnable) {
|
2013-05-10 04:01:10 -07:00
|
|
|
NS_NAMED_LITERAL_STRING(replyError,
|
|
|
|
"Failed to connect with a bluetooth headset!");
|
|
|
|
DispatchBluetoothReply(mRunnable, BluetoothValue(), replyError);
|
|
|
|
|
|
|
|
mRunnable = nullptr;
|
2012-11-07 15:48:39 -08:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
mSocket = nullptr;
|
|
|
|
mHandsfreeSocket = nullptr;
|
|
|
|
mHeadsetSocket = nullptr;
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
// If connecting for some reason didn't work, restart listening
|
2012-10-10 22:49:11 -07:00
|
|
|
Listen();
|
|
|
|
}
|
2012-10-12 11:38:14 -07:00
|
|
|
|
|
|
|
void
|
2013-04-04 17:55:55 -07:00
|
|
|
BluetoothHfpManager::OnDisconnect(BluetoothSocket* aSocket)
|
2012-10-12 11:38:14 -07:00
|
|
|
{
|
2013-04-19 02:08:00 -07:00
|
|
|
MOZ_ASSERT(aSocket);
|
|
|
|
|
2013-05-10 04:01:10 -07:00
|
|
|
if (aSocket == mScoSocket) {
|
|
|
|
// SCO socket is closed
|
|
|
|
OnScoDisconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
if (aSocket != mSocket) {
|
|
|
|
// Do nothing when a listening server socket is closed.
|
|
|
|
return;
|
2012-10-26 02:28:39 -07:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:00 -07:00
|
|
|
mSocket = nullptr;
|
2013-05-10 04:01:10 -07:00
|
|
|
DisconnectSco();
|
2013-04-19 02:08:00 -07:00
|
|
|
|
|
|
|
Listen();
|
2013-05-10 23:06:16 -07:00
|
|
|
NotifyStatusChanged(NS_LITERAL_STRING("bluetooth-hfp-status-changed"));
|
2013-01-15 22:13:58 -08:00
|
|
|
Reset();
|
2012-10-12 11:38:14 -07:00
|
|
|
}
|
2013-04-04 17:54:22 -07:00
|
|
|
|
2013-05-22 04:08:21 -07:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
|
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
|
|
|
|
|
|
nsString uuid;
|
|
|
|
if (mIsHandsfree) {
|
|
|
|
BluetoothUuidHelper::GetString(BluetoothServiceClass::HANDSFREE, uuid);
|
|
|
|
} else {
|
|
|
|
BluetoothUuidHelper::GetString(BluetoothServiceClass::HEADSET, uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we have updated SDP records of the target device, we should
|
|
|
|
// try to get the channel of target service again.
|
|
|
|
if (NS_FAILED(bs->GetServiceChannel(aDeviceAddress, uuid, this))) {
|
|
|
|
DispatchBluetoothReply(mRunnable, BluetoothValue(),
|
|
|
|
NS_LITERAL_STRING(ERR_SERVICE_CHANNEL_NOT_FOUND));
|
|
|
|
mRunnable = nullptr;
|
|
|
|
mSocket = nullptr;
|
|
|
|
Listen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:39:12 -07:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnGetServiceChannel(const nsAString& aDeviceAddress,
|
|
|
|
const nsAString& aServiceUuid,
|
|
|
|
int aChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-05-22 04:08:21 -07:00
|
|
|
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
|
2013-05-09 23:39:12 -07:00
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
|
2013-05-22 04:08:21 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
|
|
|
2013-05-09 23:39:12 -07:00
|
|
|
BluetoothValue v;
|
|
|
|
|
|
|
|
if (aChannel < 0) {
|
2013-05-22 04:08:21 -07:00
|
|
|
if (mNeedsUpdatingSdpRecords) {
|
|
|
|
mNeedsUpdatingSdpRecords = false;
|
|
|
|
bs->UpdateSdpRecords(aDeviceAddress, this);
|
|
|
|
} else {
|
|
|
|
DispatchBluetoothReply(mRunnable, v,
|
|
|
|
NS_LITERAL_STRING(ERR_SERVICE_CHANNEL_NOT_FOUND));
|
|
|
|
mRunnable = nullptr;
|
|
|
|
mSocket = nullptr;
|
|
|
|
Listen();
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:39:12 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSocket->Connect(NS_ConvertUTF16toUTF8(aDeviceAddress), aChannel)) {
|
|
|
|
DispatchBluetoothReply(mRunnable, v,
|
|
|
|
NS_LITERAL_STRING("SocketConnectionError"));
|
2013-05-22 04:08:21 -07:00
|
|
|
mRunnable = nullptr;
|
2013-05-09 23:39:12 -07:00
|
|
|
mSocket = nullptr;
|
|
|
|
Listen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-10 04:01:10 -07:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnScoConnectSuccess()
|
|
|
|
{
|
|
|
|
// For active connection request, we need to reply the DOMRequest
|
|
|
|
if (mScoRunnable) {
|
|
|
|
DispatchBluetoothReply(mScoRunnable,
|
|
|
|
BluetoothValue(true), EmptyString());
|
|
|
|
mScoRunnable = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-06-08 08:26:01 -07:00
|
|
|
NotifyAudioManager(true);
|
2013-05-10 23:06:16 -07:00
|
|
|
NotifyStatusChanged(NS_LITERAL_STRING("bluetooth-sco-status-changed"));
|
2013-05-10 04:01:10 -07:00
|
|
|
|
|
|
|
mScoSocketStatus = mScoSocket->GetConnectionStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnScoConnectError()
|
|
|
|
{
|
|
|
|
if (mScoRunnable) {
|
|
|
|
NS_NAMED_LITERAL_STRING(replyError, "Failed to create SCO socket!");
|
|
|
|
DispatchBluetoothReply(mScoRunnable, BluetoothValue(), replyError);
|
|
|
|
|
|
|
|
mScoRunnable = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ListenSco();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnScoDisconnect()
|
|
|
|
{
|
|
|
|
if (mScoSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
|
|
|
|
ListenSco();
|
2013-06-08 08:26:01 -07:00
|
|
|
NotifyAudioManager(false);
|
2013-05-10 23:06:16 -07:00
|
|
|
NotifyStatusChanged(NS_LITERAL_STRING("bluetooth-sco-status-changed"));
|
2013-05-10 04:01:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 17:54:22 -07:00
|
|
|
bool
|
|
|
|
BluetoothHfpManager::IsConnected()
|
|
|
|
{
|
2013-04-19 02:08:00 -07:00
|
|
|
if (mSocket) {
|
|
|
|
return mSocket->GetConnectionStatus() ==
|
|
|
|
SocketConnectionStatus::SOCKET_CONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-04-04 17:54:22 -07:00
|
|
|
}
|
2013-04-26 03:48:21 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::GetAddress(nsAString& aDeviceAddress)
|
|
|
|
{
|
|
|
|
return mSocket->GetAddress(aDeviceAddress);
|
|
|
|
}
|
2013-05-10 04:01:10 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothHfpManager::ConnectSco(BluetoothReplyRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (gInShutdown) {
|
|
|
|
NS_WARNING("ConnecteSco called while in shutdown!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsConnected()) {
|
|
|
|
NS_WARNING("BluetoothHfpManager is not connected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketConnectionStatus status = mScoSocket->GetConnectionStatus();
|
|
|
|
if (status == SocketConnectionStatus::SOCKET_CONNECTED ||
|
|
|
|
status == SocketConnectionStatus::SOCKET_CONNECTING ||
|
|
|
|
(mScoRunnable && (mScoRunnable != aRunnable))) {
|
|
|
|
NS_WARNING("SCO connection exists or is being established");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mScoSocket->Disconnect();
|
|
|
|
|
|
|
|
mScoRunnable = aRunnable;
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE(bs, false);
|
|
|
|
nsresult rv = bs->GetScoSocket(mDeviceAddress, true, false, mScoSocket);
|
|
|
|
|
2013-05-10 23:06:16 -07:00
|
|
|
mScoSocketStatus = mSocket->GetConnectionStatus();
|
2013-05-10 04:01:10 -07:00
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothHfpManager::DisconnectSco()
|
|
|
|
{
|
|
|
|
if (!mScoSocket) {
|
|
|
|
NS_WARNING("BluetoothHfpManager is not connected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mScoSocket->Disconnect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothHfpManager::ListenSco()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (gInShutdown) {
|
|
|
|
NS_WARNING("ListenSco called while in shutdown!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mScoSocket->GetConnectionStatus() ==
|
|
|
|
SocketConnectionStatus::SOCKET_LISTENING) {
|
|
|
|
NS_WARNING("SCO socket has been already listening");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mScoSocket->Disconnect();
|
|
|
|
|
|
|
|
if (!mScoSocket->Listen(-1)) {
|
|
|
|
NS_WARNING("Can't listen on SCO socket!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mScoSocketStatus = mScoSocket->GetConnectionStatus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothHfpManager::IsScoConnected()
|
|
|
|
{
|
|
|
|
if (mScoSocket) {
|
|
|
|
return mScoSocket->GetConnectionStatus() ==
|
|
|
|
SocketConnectionStatus::SOCKET_CONNECTED;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-08 08:26:01 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(BluetoothHfpManager)
|
|
|
|
|