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-25 03:18:38 -07:00
|
|
|
#include "base/basictypes.h"
|
2012-09-26 06:34:55 -07:00
|
|
|
#include "BluetoothHfpManager.h"
|
|
|
|
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothServiceUuid.h"
|
|
|
|
|
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-09-25 03:18:38 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-09-26 19:24:39 -07:00
|
|
|
#include "nsIObserverService.h"
|
2012-09-26 22:15:33 -07:00
|
|
|
#include "nsIRadioInterfaceLayer.h"
|
2012-09-25 03:18:38 -07:00
|
|
|
#include "nsVariant.h"
|
2012-09-26 22:15:33 -07:00
|
|
|
|
|
|
|
#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"
|
|
|
|
#define AUDIO_VOLUME_MASTER "audio.volume.master"
|
|
|
|
|
2012-09-26 06:34:55 -07:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
static nsRefPtr<BluetoothHfpManager> sInstance = nullptr;
|
2012-09-26 22:15:33 -07:00
|
|
|
static nsCOMPtr<nsIThread> sHfpCommandThread;
|
|
|
|
static bool sStopSendingRingFlag = true;
|
|
|
|
|
|
|
|
static int kRingInterval = 3000000; //unit: us
|
|
|
|
|
2012-09-25 03:18:38 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothHfpManager, nsIObserver)
|
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
class SendRingIndicatorTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SendRingIndicatorTask()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
while (!sStopSendingRingFlag) {
|
|
|
|
sInstance->SendLine("RING");
|
|
|
|
|
|
|
|
usleep(kRingInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
2012-09-26 06:34:55 -07:00
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
BluetoothHfpManager::BluetoothHfpManager()
|
|
|
|
: mCurrentVgs(-1)
|
|
|
|
, mCurrentCallIndex(0)
|
|
|
|
, mCurrentCallState(nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED)
|
2012-09-26 06:34:55 -07:00
|
|
|
{
|
2012-09-25 03:18:38 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
|
|
|
|
if (obs && NS_FAILED(obs->AddObserver(sInstance, MOZSETTINGS_CHANGED_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add settings change observer!");
|
|
|
|
}
|
2012-09-26 06:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothHfpManager::~BluetoothHfpManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
BluetoothHfpManager*
|
|
|
|
BluetoothHfpManager::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (sInstance == nullptr) {
|
|
|
|
sInstance = new BluetoothHfpManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
// {"key":"volumeup", "value":1.0}
|
|
|
|
// {"key":"volumedown", "value":0.2}
|
|
|
|
|
|
|
|
JSContext* cx = nsContentUtils::GetSafeJSContext();
|
|
|
|
if (!cx) {
|
|
|
|
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;
|
|
|
|
if (!JS_StringEqualsAscii(cx, key.toString(), AUDIO_VOLUME_MASTER, &match)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AG volume range: [0.0, 1.0]
|
|
|
|
float volume = value.toNumber();
|
|
|
|
|
|
|
|
// HS volume range: [0, 15]
|
|
|
|
mCurrentVgs = ceil(volume * 15);
|
|
|
|
|
|
|
|
nsDiscriminatedUnion du;
|
|
|
|
du.mType = 0;
|
|
|
|
du.u.mInt8Value = mCurrentVgs;
|
|
|
|
|
|
|
|
nsCString vgs;
|
|
|
|
if (NS_FAILED(nsVariant::ConvertToACString(du, vgs))) {
|
|
|
|
NS_WARNING("Failed to convert volume to string");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString newVgs;
|
|
|
|
newVgs += "+VGS: ";
|
|
|
|
newVgs += vgs;
|
|
|
|
|
|
|
|
SendLine(newVgs.get());
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothHfpManager::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, MOZSETTINGS_CHANGED_ID)) {
|
|
|
|
return HandleVolumeChanged(nsDependentString(aData));
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "BluetoothHfpManager got unexpected topic!");
|
|
|
|
}
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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)) {
|
|
|
|
SendLine("+BRSF: 23");
|
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CIND=?", 9)) {
|
|
|
|
nsAutoCString cindRange;
|
|
|
|
|
|
|
|
cindRange += "+CIND: ";
|
|
|
|
cindRange += "(\"battchg\",(0-5)),";
|
|
|
|
cindRange += "(\"signal\",(0-5)),";
|
|
|
|
cindRange += "(\"service\",(0,1)),";
|
|
|
|
cindRange += "(\"call\",(0,1)),";
|
|
|
|
cindRange += "(\"callsetup\",(0-3)),";
|
|
|
|
cindRange += "(\"callheld\",(0-2)),";
|
|
|
|
cindRange += "(\"roam\",(0,1))";
|
|
|
|
|
|
|
|
SendLine(cindRange.get());
|
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CIND", 7)) {
|
|
|
|
// FIXME - Bug 794349
|
|
|
|
// This value reflects current status of telephony, roaming, battery ...,
|
|
|
|
// so obviously fixed value must be wrong if there is an ongoing call.
|
|
|
|
// Need a patch for this, but currently just using fixed value for basic
|
|
|
|
// SLC establishment.
|
|
|
|
SendLine("+CIND: 5,5,1,0,0,0,0");
|
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CMER=", 8)) {
|
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CHLD=?", 9)) {
|
|
|
|
SendLine("+CHLD: (0,1,2,3)");
|
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+CHLD=", 8)) {
|
|
|
|
SendLine("OK");
|
|
|
|
} else if (!strncmp(msg, "AT+VGS=", 7)) {
|
2012-09-26 19:24:39 -07:00
|
|
|
// VGS range: [0, 15]
|
|
|
|
int newVgs = msg[7] - '0';
|
|
|
|
|
|
|
|
if (strlen(msg) > 8) {
|
|
|
|
newVgs *= 10;
|
|
|
|
newVgs += (msg[8] - '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(newVgs >= 0 && newVgs <= 15, "Received invalid VGS value");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Currently, we send volume up/down commands to represent that
|
|
|
|
// volume has been changed by Bluetooth headset, and that will affect
|
|
|
|
// the main stream volume of our device. In the future, we may want to
|
|
|
|
// be able to set volume by stream.
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (newVgs > mCurrentVgs) {
|
|
|
|
os->NotifyObservers(nullptr, "bluetooth-volume-change", NS_LITERAL_STRING("up").get());
|
|
|
|
} else if (newVgs < mCurrentVgs) {
|
|
|
|
os->NotifyObservers(nullptr, "bluetooth-volume-change", NS_LITERAL_STRING("down").get());
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentVgs = newVgs;
|
|
|
|
|
2012-09-26 19:24:39 -07:00
|
|
|
SendLine("OK");
|
|
|
|
} 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
|
|
|
|
BluetoothHfpManager::Connect(const nsAString& aDeviceObjectPath,
|
|
|
|
BluetoothReplyRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString serviceUuidStr =
|
|
|
|
NS_ConvertUTF8toUTF16(mozilla::dom::bluetooth::BluetoothServiceUuidStr::Handsfree);
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothReplyRunnable> runnable = aRunnable;
|
|
|
|
|
|
|
|
nsresult rv = bs->GetSocketViaService(aDeviceObjectPath,
|
|
|
|
serviceUuidStr,
|
|
|
|
BluetoothSocketType::RFCOMM,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
this,
|
|
|
|
runnable);
|
|
|
|
|
|
|
|
runnable.forget();
|
|
|
|
return NS_FAILED(rv) ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothHfpManager::Disconnect()
|
|
|
|
{
|
|
|
|
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-09-26 22:15:33 -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.
|
|
|
|
*/
|
2012-09-26 22:15:22 -07:00
|
|
|
void
|
|
|
|
BluetoothHfpManager::CallStateChanged(int aCallIndex, int aCallState,
|
|
|
|
const char* aNumber, bool aIsActive)
|
|
|
|
{
|
2012-09-26 22:15:33 -07:00
|
|
|
nsRefPtr<nsRunnable> sendRingTask;
|
|
|
|
|
|
|
|
switch (aCallState) {
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
|
|
|
|
// Send "CallSetup = 1"
|
|
|
|
SendLine("+CIEV: 5,1");
|
|
|
|
|
|
|
|
// Start sending RING indicator to HF
|
|
|
|
sStopSendingRingFlag = false;
|
|
|
|
sendRingTask = new SendRingIndicatorTask();
|
|
|
|
sHfpCommandThread->Dispatch(sendRingTask, NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DIALING:
|
|
|
|
// Send "CallSetup = 2"
|
|
|
|
SendLine("+CIEV: 5,2");
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_ALERTING:
|
|
|
|
// Send "CallSetup = 3"
|
|
|
|
if (mCurrentCallIndex == nsIRadioInterfaceLayer::CALL_STATE_DIALING) {
|
|
|
|
SendLine("+CIEV: 5,3");
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Not handling state changed");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_CONNECTED:
|
|
|
|
switch (mCurrentCallState) {
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
|
|
|
|
sStopSendingRingFlag = true;
|
|
|
|
// Continue executing, no break
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DIALING:
|
|
|
|
// Send "Call = 1, CallSetup = 0"
|
|
|
|
SendLine("+CIEV: 4,1");
|
|
|
|
SendLine("+CIEV: 5,0");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Not handling state changed");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED:
|
|
|
|
switch (mCurrentCallState) {
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
|
|
|
|
sStopSendingRingFlag = true;
|
|
|
|
// Continue executing, no break
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_DIALING:
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_ALERTING:
|
|
|
|
// Send "CallSetup = 0"
|
|
|
|
SendLine("+CIEV: 5,0");
|
|
|
|
break;
|
|
|
|
case nsIRadioInterfaceLayer::CALL_STATE_CONNECTED:
|
|
|
|
// Send "Call = 0"
|
|
|
|
SendLine("+CIEV: 4,0");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Not handling state changed");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Not handling state changed");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2012-09-26 22:15:22 -07:00
|
|
|
|
2012-09-26 22:15:33 -07:00
|
|
|
mCurrentCallIndex = aCallIndex;
|
|
|
|
mCurrentCallState = aCallState;
|
2012-09-26 22:15:22 -07:00
|
|
|
}
|