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/. */
|
|
|
|
|
2012-09-29 02:39:05 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
#include "BluetoothHfpManager.h"
|
|
|
|
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
2012-09-29 02:39:05 -07:00
|
|
|
#include "BluetoothScoManager.h"
|
2012-09-26 06:34:55 -07:00
|
|
|
#include "BluetoothService.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-09-25 03:18:38 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.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"
|
2012-09-26 22:15:33 -07:00
|
|
|
#include "nsIRadioInterfaceLayer.h"
|
|
|
|
|
|
|
|
#include <unistd.h> /* usleep() */
|
2012-09-26 19:24:39 -07:00
|
|
|
|
2012-09-25 03:18:38 -07:00
|
|
|
#define MOZSETTINGS_CHANGED_ID "mozsettings-changed"
|
2012-11-18 23:10:38 -08:00
|
|
|
#define AUDIO_VOLUME_BT_SCO "audio.volume.bt_sco"
|
2012-09-25 03:18:38 -07:00
|
|
|
|
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-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,
|
|
|
|
SIGNAL,
|
|
|
|
SERVICE,
|
|
|
|
CALL,
|
|
|
|
CALLSETUP,
|
|
|
|
CALLHELD,
|
|
|
|
ROAM,
|
|
|
|
};
|
|
|
|
|
|
|
|
static CINDItem sCINDItems[] = {
|
|
|
|
{},
|
|
|
|
{"battchg", "0-5", 5},
|
|
|
|
{"signal", "0-5", 5},
|
|
|
|
{"service", "0,1", 1},
|
|
|
|
{"call", "0,1", CallState::NO_CALL},
|
|
|
|
{"callsetup", "0-3", CallSetupState::NO_CALLSETUP},
|
|
|
|
{"callheld", "0-2", CallHeldState::NO_CALLHELD},
|
|
|
|
{"roam", "0,1", 0}
|
|
|
|
};
|
|
|
|
|
2012-10-05 17:11:24 -07:00
|
|
|
class mozilla::dom::bluetooth::BluetoothHfpManagerObserver : public nsIObserver
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
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-10-05 17:11:24 -07:00
|
|
|
(NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) ||
|
|
|
|
NS_FAILED(obs->RemoveObserver(this, MOZSETTINGS_CHANGED_ID)))) {
|
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
|
|
|
}
|
2012-10-08 21:59:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BluetoothHfpManagerObserver()
|
|
|
|
{
|
|
|
|
Shutdown();
|
2012-10-05 17:11:24 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
class GetVolumeTask : public nsISettingsServiceCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Handle(const nsAString& aName, const jsval& aResult)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
hfp->SetVolume(aResult.toNumber());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
HandleError(const nsAString& aName)
|
|
|
|
{
|
|
|
|
NS_WARNING("Unable to get value for '" AUDIO_VOLUME_BT_SCO "'");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(GetVolumeTask, nsISettingsServiceCallback);
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
namespace {
|
|
|
|
StaticRefPtr<BluetoothHfpManager> gBluetoothHfpManager;
|
2012-10-08 21:59:33 -07:00
|
|
|
StaticRefPtr<BluetoothHfpManagerObserver> sHfpObserver;
|
2012-10-02 22:53:39 -07:00
|
|
|
bool gInShutdown = false;
|
|
|
|
static bool sStopSendingRingFlag = true;
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2012-10-25 18:25:15 -07:00
|
|
|
static int sRingInterval = 3000; //unit: ms
|
2012-10-02 22:53:39 -07:00
|
|
|
} // anonymous namespace
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2012-10-05 17:11:24 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothHfpManagerObserver, nsIObserver)
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "BluetoothHfpManager got unexpected topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-09-25 03:18:38 -07:00
|
|
|
|
2012-10-25 18:25:15 -07:00
|
|
|
class SendRingIndicatorTask : public Task
|
2012-09-26 22:15:33 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SendRingIndicatorTask()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_ENSURE_FALSE_VOID(sStopSendingRingFlag);
|
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-10-25 18:25:15 -07:00
|
|
|
gBluetoothHfpManager->SendLine("RING");
|
|
|
|
|
|
|
|
MessageLoop::current()->
|
|
|
|
PostDelayedTask(FROM_HERE,
|
|
|
|
new SendRingIndicatorTask(),
|
|
|
|
sRingInterval);
|
|
|
|
|
|
|
|
return;
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
|
|
|
};
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2012-09-29 02:39:05 -07:00
|
|
|
void
|
2012-10-22 03:34:15 -07:00
|
|
|
OpenScoSocket(const nsAString& aDeviceAddress)
|
2012-09-29 02:39:05 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
BluetoothScoManager* sco = BluetoothScoManager::Get();
|
|
|
|
if (!sco) {
|
|
|
|
NS_WARNING("BluetoothScoManager is not available!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
if (!sco->Connect(aDeviceAddress)) {
|
2012-09-29 02:39:05 -07:00
|
|
|
NS_WARNING("Failed to create a sco socket!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CloseScoSocket()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
BluetoothScoManager* sco = BluetoothScoManager::Get();
|
|
|
|
if (!sco) {
|
|
|
|
NS_WARNING("BluetoothScoManager is not available!");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-22 03:34:15 -07:00
|
|
|
sco->Disconnect();
|
2012-09-29 02:39:05 -07:00
|
|
|
}
|
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
BluetoothHfpManager::BluetoothHfpManager()
|
2012-10-25 03:50:41 -07:00
|
|
|
: mCurrentCallIndex(0)
|
|
|
|
, mReceiveVgsFlag(false)
|
2012-10-02 22:53:39 -07:00
|
|
|
{
|
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
|
|
|
|
|
|
|
mCurrentCallStateArray.AppendElement((int)nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED);
|
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-26 02:28:39 -07:00
|
|
|
mSocketStatus = GetConnectionStatus();
|
|
|
|
|
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
|
|
|
|
2012-09-27 05:24:06 -07:00
|
|
|
mListener = new BluetoothRilListener();
|
|
|
|
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
|
|
|
|
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
|
|
|
|
nsRefPtr<BluetoothHfpManager> manager = new BluetoothHfpManager();
|
|
|
|
NS_ENSURE_TRUE(manager, nullptr);
|
|
|
|
|
|
|
|
if (!manager->Init()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gBluetoothHfpManager = manager;
|
|
|
|
return gBluetoothHfpManager;
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::SetVolume(const int aVolume)
|
|
|
|
{
|
|
|
|
mCurrentVgs = aVolume;
|
|
|
|
}
|
|
|
|
|
2012-10-01 00:44:48 -07:00
|
|
|
void
|
2012-10-22 03:34:15 -07:00
|
|
|
BluetoothHfpManager::NotifySettings()
|
2012-10-01 00:44:48 -07:00
|
|
|
{
|
|
|
|
nsString type, name;
|
|
|
|
BluetoothValue v;
|
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
type.AssignLiteral("bluetooth-hfp-status-changed");
|
|
|
|
|
|
|
|
name.AssignLiteral("connected");
|
2012-11-18 23:10:38 -08:00
|
|
|
v = (GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTED)
|
|
|
|
? true : false ;
|
2012-10-01 00:44:48 -07:00
|
|
|
parameters.AppendElement(BluetoothNamedValue(name, v));
|
|
|
|
|
|
|
|
name.AssignLiteral("address");
|
2012-10-26 02:21:32 -07:00
|
|
|
v = mDevicePath;
|
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)
|
|
|
|
{
|
|
|
|
nsString type, name, command;
|
|
|
|
command = aCommand;
|
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
type.AssignLiteral("bluetooth-dialer-command");
|
|
|
|
|
|
|
|
BluetoothValue v(command);
|
|
|
|
parameters.AppendElement(BluetoothNamedValue(type, v));
|
|
|
|
|
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
|
|
|
NS_WARNING("Failed to broadcast system message to dialer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value val;
|
|
|
|
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
|
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTED) {
|
|
|
|
SendCommand("+VGS: ", mCurrentVgs);
|
2012-10-25 03:50:41 -07:00
|
|
|
}
|
2012-09-25 03:18:38 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
nsresult
|
|
|
|
BluetoothHfpManager::HandleShutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
gInShutdown = true;
|
2012-10-05 16:05:35 -07:00
|
|
|
CloseSocket();
|
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
|
|
|
|
BluetoothHfpManager::ReceiveSocketData(UnixSocketRawData* aMessage)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-26 19:24:39 -07:00
|
|
|
|
|
|
|
const char* msg = (const char*)aMessage->mData;
|
2012-10-30 20:53:36 -07:00
|
|
|
int currentCallState = mCurrentCallStateArray[mCurrentCallIndex];
|
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
|
|
|
|
if (!strncmp(msg, "AT+BRSF=", 8)) {
|
2012-10-22 03:34:15 -07:00
|
|
|
SendCommand("+BRSF: ", 23);
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CIND=?", 9)) {
|
2012-10-22 03:34:15 -07:00
|
|
|
// Asking for CIND range
|
|
|
|
SendCommand("+CIND: ", 0);
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
2012-10-09 04:19:29 -07:00
|
|
|
} else if (!strncmp(msg, "AT+CIND?", 8)) {
|
2012-10-22 03:34:15 -07:00
|
|
|
// Asking for CIND value
|
|
|
|
SendCommand("+CIND: ", 1);
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CMER=", 8)) {
|
2012-10-01 00:44:48 -07:00
|
|
|
// SLC establishment
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CHLD=?", 9)) {
|
2012-10-30 20:53:36 -07:00
|
|
|
SendLine("+CHLD: (1,2)");
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CHLD=", 8)) {
|
2012-10-30 20:53:36 -07:00
|
|
|
int length = strlen(msg) - 9;
|
|
|
|
nsAutoCString chldString(nsDependentCSubstring(msg+8, length));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
int chld = chldString.ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to extract volume value from bluetooth headset!");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(chld) {
|
|
|
|
case 1:
|
|
|
|
// Releases active calls and accepts the other (held or waiting) call
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHUP+ATA"));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// Places active calls on hold and accepts the other (held or waiting) call
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHLD+ATA"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Not handling chld value");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+VGS=", 7)) {
|
2012-11-18 23:10:38 -08:00
|
|
|
// Adjust volume by headset
|
2012-10-25 03:50:41 -07:00
|
|
|
mReceiveVgsFlag = true;
|
2012-09-26 19:24:39 -07:00
|
|
|
|
2012-10-25 03:50:41 -07:00
|
|
|
int length = strlen(msg) - 8;
|
|
|
|
nsAutoCString vgsString(nsDependentCSubstring(msg+7, length));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
int newVgs = vgsString.ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to extract volume value from bluetooth headset!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newVgs == mCurrentVgs) {
|
|
|
|
SendLine("OK");
|
|
|
|
return;
|
2012-09-26 19:24:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(newVgs >= 0 && newVgs <= 15, "Received invalid VGS value");
|
|
|
|
#endif
|
|
|
|
|
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());
|
2012-09-26 19:24:39 -07:00
|
|
|
|
2012-09-27 05:24:06 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+BLDN", 7)) {
|
2012-10-01 00:44:48 -07:00
|
|
|
NotifyDialer(NS_LITERAL_STRING("BLDN"));
|
2012-09-27 05:24:06 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "ATA", 3)) {
|
2012-10-01 00:44:48 -07:00
|
|
|
NotifyDialer(NS_LITERAL_STRING("ATA"));
|
2012-09-27 05:24:06 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CHUP", 7)) {
|
2012-10-01 00:44:48 -07:00
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHUP"));
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
2012-10-05 03:48:05 -07:00
|
|
|
} else if (!strncmp(msg, "AT+CKPD", 7)) {
|
|
|
|
// For Headset
|
2012-10-30 20:53:36 -07:00
|
|
|
switch (currentCallState) {
|
2012-10-05 03:48:05 -07:00
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("ATA"));
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_CONNECTED:
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DIALING:
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_ALERTING:
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("CHUP"));
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED:
|
|
|
|
NotifyDialer(NS_LITERAL_STRING("BLDN"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Not handling state changed");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SendLine("OK");
|
2012-09-26 19:24:39 -07:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling HFP message, reply ok: ");
|
|
|
|
warningMsg.Append(msg);
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
#endif
|
|
|
|
SendLine("OK");
|
|
|
|
}
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-10-22 03:34:15 -07:00
|
|
|
BluetoothHfpManager::Connect(const nsAString& aDevicePath,
|
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());
|
|
|
|
|
2012-10-02 22:53:39 -07:00
|
|
|
if (gInShutdown) {
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_WARNING("Connect called while in shutdown!");
|
2012-10-02 22:53:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-16 02:46:08 -08:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTED ||
|
|
|
|
GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTING) {
|
|
|
|
NS_WARNING("BluetoothHfpManager has connected/is connecting to a headset!");
|
2012-10-22 03:34:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:49:11 -07:00
|
|
|
CloseSocket();
|
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_ENSURE_TRUE(bs, false);
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2012-11-26 22:12:19 -08:00
|
|
|
nsString uuid;
|
2012-10-05 03:48:05 -07:00
|
|
|
if (aIsHandsfree) {
|
2012-11-26 22:12:19 -08:00
|
|
|
BluetoothUuidHelper::GetString(BluetoothServiceClass::HANDSFREE, uuid);
|
2012-10-05 03:48:05 -07:00
|
|
|
} else {
|
2012-11-26 22:12:19 -08:00
|
|
|
BluetoothUuidHelper::GetString(BluetoothServiceClass::HEADSET, uuid);
|
2012-10-05 03:48:05 -07:00
|
|
|
}
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2012-11-07 15:48:39 -08:00
|
|
|
mRunnable = aRunnable;
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
nsresult rv = bs->GetSocketViaService(aDevicePath,
|
2012-11-26 22:12:19 -08:00
|
|
|
uuid,
|
2012-09-26 06:34:55 -07:00
|
|
|
BluetoothSocketType::RFCOMM,
|
|
|
|
true,
|
2012-10-31 02:33:50 -07:00
|
|
|
true,
|
2012-09-26 06:34:55 -07:00
|
|
|
this,
|
2012-11-07 15:48:39 -08:00
|
|
|
mRunnable);
|
2012-09-26 06:34:55 -07:00
|
|
|
|
|
|
|
return NS_FAILED(rv) ? false : true;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
MOZ_ASSERT(false, "Listen called while in shutdown!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-27 23:59:20 -08:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_LISTENING) {
|
|
|
|
NS_WARNING("BluetoothHfpManager has been already listening");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:49:11 -07:00
|
|
|
CloseSocket();
|
|
|
|
|
2012-10-01 00:04:01 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_ENSURE_TRUE(bs, false);
|
2012-10-01 00:04:01 -07:00
|
|
|
|
2012-11-26 22:12:19 -08:00
|
|
|
nsresult rv =
|
|
|
|
bs->ListenSocketViaService(BluetoothReservedChannels::CHANNEL_HANDSFREE_AG,
|
|
|
|
BluetoothSocketType::RFCOMM,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
this);
|
2012-10-26 02:28:39 -07:00
|
|
|
|
|
|
|
mSocketStatus = GetConnectionStatus();
|
|
|
|
|
2012-10-01 00:04:01 -07:00
|
|
|
return NS_FAILED(rv) ? false : true;
|
|
|
|
}
|
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::Disconnect()
|
|
|
|
{
|
2012-10-22 03:34:15 -07:00
|
|
|
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_DISCONNECTED) {
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_WARNING("BluetoothHfpManager has been disconnected!");
|
2012-10-22 03:34:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
|
2012-09-26 19:24:39 -07:00
|
|
|
bool
|
|
|
|
BluetoothHfpManager::SendLine(const char* aMessage)
|
|
|
|
{
|
|
|
|
const char* kHfpCrlf = "\xd\xa";
|
|
|
|
nsAutoCString msg;
|
|
|
|
|
|
|
|
msg += kHfpCrlf;
|
|
|
|
msg += aMessage;
|
|
|
|
msg += kHfpCrlf;
|
|
|
|
|
|
|
|
return SendSocketData(msg);
|
|
|
|
}
|
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
bool
|
|
|
|
BluetoothHfpManager::SendCommand(const char* aCommand, const int aValue)
|
2012-10-09 04:19:29 -07:00
|
|
|
{
|
2012-10-22 03:34:15 -07:00
|
|
|
nsAutoCString message;
|
|
|
|
int value = aValue;
|
|
|
|
message += aCommand;
|
|
|
|
|
|
|
|
if (!strcmp(aCommand, "+CIEV: ")) {
|
|
|
|
message.AppendInt(aValue);
|
|
|
|
message += ",";
|
|
|
|
switch (aValue) {
|
|
|
|
case CINDType::CALL:
|
|
|
|
message.AppendInt(sCINDItems[CINDType::CALL].value);
|
|
|
|
break;
|
|
|
|
case CINDType::CALLSETUP:
|
|
|
|
message.AppendInt(sCINDItems[CINDType::CALLSETUP].value);
|
|
|
|
break;
|
|
|
|
case CINDType::CALLHELD:
|
|
|
|
message.AppendInt(sCINDItems[CINDType::CALLHELD].value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("unexpected CINDType for CIEV command");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(aCommand, "+CIND: ")) {
|
|
|
|
if (!aValue) {
|
|
|
|
for (uint8_t i = 1; i < ArrayLength(sCINDItems); i++) {
|
|
|
|
message += "(\"";
|
|
|
|
message += sCINDItems[i].name;
|
|
|
|
message += "\",(";
|
|
|
|
message += sCINDItems[i].range;
|
|
|
|
message += ")";
|
|
|
|
if (i == (ArrayLength(sCINDItems) - 1)) {
|
|
|
|
message +=")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
message += "),";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint8_t i = 1; i < ArrayLength(sCINDItems); i++) {
|
|
|
|
message.AppendInt(sCINDItems[i].value);
|
|
|
|
if (i == (ArrayLength(sCINDItems) - 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
message += ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message.AppendInt(value);
|
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
|
2012-10-22 03:34:15 -07:00
|
|
|
BluetoothHfpManager::SetupCIND(int aCallIndex, int aCallState, bool aInitial)
|
2012-09-26 22:15:22 -07:00
|
|
|
{
|
2012-09-26 22:15:33 -07:00
|
|
|
nsRefPtr<nsRunnable> sendRingTask;
|
2012-10-22 03:34:15 -07:00
|
|
|
nsString address;
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2012-10-30 20:53:36 -07:00
|
|
|
while (aCallIndex >= mCurrentCallStateArray.Length()) {
|
|
|
|
mCurrentCallStateArray.AppendElement((int)nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
int currentCallState = mCurrentCallStateArray[aCallIndex];
|
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
switch (aCallState) {
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
|
2012-10-22 03:34:15 -07:00
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::INCOMING;
|
|
|
|
if (!aInitial) {
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALLSETUP);
|
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
|
2012-10-30 20:53:36 -07:00
|
|
|
if (!mCurrentCallIndex) {
|
|
|
|
// Start sending RING indicator to HF
|
|
|
|
sStopSendingRingFlag = false;
|
|
|
|
MessageLoop::current()->PostTask(FROM_HERE,
|
|
|
|
new SendRingIndicatorTask());
|
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DIALING:
|
2012-10-22 03:34:15 -07:00
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::OUTGOING;
|
|
|
|
if (!aInitial) {
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALLSETUP);
|
|
|
|
|
|
|
|
GetSocketAddr(address);
|
|
|
|
OpenScoSocket(address);
|
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_ALERTING:
|
2012-10-22 03:34:15 -07:00
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::OUTGOING_ALERTING;
|
|
|
|
if (!aInitial) {
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALLSETUP);
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_CONNECTED:
|
2012-10-30 20:53:36 -07:00
|
|
|
mCurrentCallIndex = aCallIndex;
|
2012-10-22 03:34:15 -07:00
|
|
|
if (aInitial) {
|
|
|
|
sCINDItems[CINDType::CALL].value = CallState::IN_PROGRESS;
|
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::NO_CALLSETUP;
|
|
|
|
} else {
|
2012-10-30 20:53:36 -07:00
|
|
|
switch (currentCallState) {
|
2012-10-22 03:34:15 -07:00
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
|
|
|
|
// Incoming call, no break
|
|
|
|
sStopSendingRingFlag = true;
|
|
|
|
|
|
|
|
GetSocketAddr(address);
|
|
|
|
OpenScoSocket(address);
|
2012-10-30 20:53:36 -07:00
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED:
|
2012-10-22 03:34:15 -07:00
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_ALERTING:
|
|
|
|
// Outgoing call
|
|
|
|
sCINDItems[CINDType::CALL].value = CallState::IN_PROGRESS;
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALL);
|
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::NO_CALLSETUP;
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALLSETUP);
|
|
|
|
break;
|
|
|
|
default:
|
2012-09-26 22:15:33 -07:00
|
|
|
#ifdef DEBUG
|
2012-10-22 03:34:15 -07:00
|
|
|
NS_WARNING("Not handling state changed");
|
2012-09-26 22:15:33 -07:00
|
|
|
#endif
|
2012-10-22 03:34:15 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED:
|
2012-10-22 03:34:15 -07:00
|
|
|
if (!aInitial) {
|
2012-10-30 20:53:36 -07:00
|
|
|
switch (currentCallState) {
|
2012-10-22 03:34:15 -07:00
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
|
2012-11-25 18:35:55 -08:00
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_BUSY:
|
2012-10-22 03:34:15 -07:00
|
|
|
// Incoming call, no break
|
|
|
|
sStopSendingRingFlag = true;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DIALING:
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_ALERTING:
|
|
|
|
// Outgoing call
|
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::NO_CALLSETUP;
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALLSETUP);
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_CONNECTED:
|
|
|
|
sCINDItems[CINDType::CALL].value = CallState::NO_CALL;
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALL);
|
|
|
|
break;
|
2012-10-30 20:53:36 -07:00
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_HELD:
|
|
|
|
sCINDItems[CINDType::CALLHELD].value = NO_CALLHELD;
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALLHELD);
|
2012-10-22 03:34:15 -07:00
|
|
|
default:
|
2012-09-26 22:15:33 -07:00
|
|
|
#ifdef DEBUG
|
2012-10-22 03:34:15 -07:00
|
|
|
NS_WARNING("Not handling state changed");
|
2012-09-26 22:15:33 -07:00
|
|
|
#endif
|
2012-10-22 03:34:15 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-10-30 20:53:36 -07:00
|
|
|
|
|
|
|
if (aCallIndex == mCurrentCallIndex) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(mCurrentCallStateArray.Length() > aCallIndex,
|
|
|
|
"Call index out of bounds!");
|
|
|
|
#endif
|
|
|
|
mCurrentCallStateArray[aCallIndex] = aCallState;
|
|
|
|
|
|
|
|
// Find the first non-disconnected call (like connected, held),
|
|
|
|
// and update mCurrentCallIndex
|
|
|
|
int c;
|
|
|
|
for (c = 1; c < mCurrentCallStateArray.Length(); c++) {
|
|
|
|
if (mCurrentCallStateArray[c] != nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED) {
|
|
|
|
mCurrentCallIndex = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no call
|
|
|
|
if (c == mCurrentCallStateArray.Length()) {
|
|
|
|
mCurrentCallIndex = 0;
|
|
|
|
CloseScoSocket();
|
|
|
|
}
|
|
|
|
}
|
2012-09-26 22:15:33 -07:00
|
|
|
}
|
2012-10-30 20:53:36 -07:00
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_HELD:
|
|
|
|
sCINDItems[CINDType::CALLHELD].value = CallHeldState::ONHOLD_ACTIVE;
|
|
|
|
|
|
|
|
if (!aInitial) {
|
|
|
|
SendCommand("+CIEV: ", CINDType::CALLHELD);
|
|
|
|
}
|
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Not handling state changed");
|
2012-10-22 03:34:15 -07:00
|
|
|
sCINDItems[CINDType::CALL].value = CallState::NO_CALL;
|
|
|
|
sCINDItems[CINDType::CALLSETUP].value = CallSetupState::NO_CALLSETUP;
|
2012-10-30 20:53:36 -07:00
|
|
|
sCINDItems[CINDType::CALLHELD].value = CallHeldState::NO_CALLHELD;
|
2012-09-26 22:15:33 -07:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2012-09-26 22:15:22 -07:00
|
|
|
|
2012-10-30 20:53:36 -07:00
|
|
|
mCurrentCallStateArray[aCallIndex] = aCallState;
|
2012-09-26 22:15:22 -07:00
|
|
|
}
|
2012-10-10 22:49:11 -07:00
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
/*
|
|
|
|
* EnumerateCallState will be called for each call
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::EnumerateCallState(int aCallIndex, int aCallState,
|
|
|
|
const char* aNumber, bool aIsActive)
|
|
|
|
{
|
2012-10-30 20:53:36 -07:00
|
|
|
SetupCIND(aCallIndex, aCallState, true);
|
2012-10-22 03:34:15 -07:00
|
|
|
|
2012-10-30 20:53:36 -07:00
|
|
|
if (sCINDItems[CINDType::CALL].value == CallState::IN_PROGRESS ||
|
|
|
|
sCINDItems[CINDType::CALLSETUP].value == CallSetupState::OUTGOING ||
|
|
|
|
sCINDItems[CINDType::CALLSETUP].value == CallSetupState::OUTGOING_ALERTING) {
|
|
|
|
nsString address;
|
|
|
|
GetSocketAddr(address);
|
|
|
|
OpenScoSocket(address);
|
2012-10-22 03:34:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CallStateChanged will be called whenever call status is changed, and it
|
|
|
|
* also means we need to notify HS about the change. For more information,
|
|
|
|
* please refer to 4.13 ~ 4.15 in Bluetooth hands-free profile 1.6.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::CallStateChanged(int aCallIndex, int aCallState,
|
|
|
|
const char* aNumber, bool aIsActive)
|
|
|
|
{
|
|
|
|
if (GetConnectionStatus() != SocketConnectionStatus::SOCKET_CONNECTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupCIND(aCallIndex, aCallState, false);
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:49:11 -07:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnConnectSuccess()
|
|
|
|
{
|
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);
|
|
|
|
|
2012-11-07 15:48:39 -08:00
|
|
|
mRunnable.forget();
|
|
|
|
}
|
2012-11-18 23:10:38 -08:00
|
|
|
|
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
|
|
|
|
GetSocketAddr(mDevicePath);
|
2012-10-30 20:53:36 -07:00
|
|
|
mSocketStatus = GetConnectionStatus();
|
2012-10-26 02:21:32 -07:00
|
|
|
|
2012-10-30 20:53:36 -07:00
|
|
|
nsCOMPtr<nsIRILContentHelper> ril =
|
|
|
|
do_GetService("@mozilla.org/ril/content-helper;1");
|
2012-11-18 23:10:38 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(ril);
|
2012-10-30 20:53:36 -07:00
|
|
|
ril->EnumerateCalls(mListener->GetCallback());
|
2012-10-26 02:28:39 -07:00
|
|
|
|
2012-10-22 03:34:15 -07:00
|
|
|
NotifySettings();
|
2012-10-10 22:49:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnConnectError()
|
|
|
|
{
|
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;
|
2012-11-07 15:48:39 -08:00
|
|
|
nsString errorStr;
|
|
|
|
errorStr.AssignLiteral("Failed to connect with a bluetooth headset!");
|
2012-11-18 23:10:38 -08:00
|
|
|
DispatchBluetoothReply(mRunnable, v, errorStr);
|
|
|
|
|
2012-11-07 15:48:39 -08:00
|
|
|
mRunnable.forget();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
CloseSocket();
|
2012-10-26 02:28:39 -07:00
|
|
|
mSocketStatus = GetConnectionStatus();
|
2012-10-10 22:49:11 -07:00
|
|
|
Listen();
|
|
|
|
}
|
2012-10-12 11:38:14 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::OnDisconnect()
|
|
|
|
{
|
2012-11-18 23:10:38 -08:00
|
|
|
// When we close a connected socket, then restart listening again and
|
|
|
|
// notify Settings app.
|
2012-10-26 02:28:39 -07:00
|
|
|
if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
|
|
|
|
Listen();
|
|
|
|
NotifySettings();
|
2012-11-27 23:59:20 -08:00
|
|
|
} else if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTING) {
|
|
|
|
NS_WARNING("BluetoothHfpManager got unexpected socket status!");
|
2012-10-26 02:28:39 -07:00
|
|
|
}
|
|
|
|
|
2012-11-25 18:35:55 -08:00
|
|
|
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-12 11:38:14 -07:00
|
|
|
}
|