Bug 853221 - Patch 1: Distribute connection/pair status to BluetoothAdapter, r=echou

This commit is contained in:
Gina Yeh 2013-08-02 18:32:57 +08:00
Родитель f002405956
Коммит de4f45b303
9 изменённых файлов: 118 добавлений и 31 удалений

Просмотреть файл

@ -204,7 +204,8 @@ BluetoothA2dpManager::HandleSinkPropertyChanged(const BluetoothSignal& aSignal)
// Indicates if a stream is setup to a A2DP sink on the remote device.
MOZ_ASSERT(value.type() == BluetoothValue::Tbool);
mA2dpConnected = value.get_bool();
NotifyStatusChanged();
NotifyConnectionStatusChanged();
DispatchConnectionStatusChanged();
} else if (name.EqualsLiteral("Playing")) {
// Indicates if a stream is active to a A2DP sink on the remote device.
MOZ_ASSERT(value.type() == BluetoothValue::Tbool);
@ -254,7 +255,16 @@ BluetoothA2dpManager::HandleSinkStateChanged(SinkState aState)
}
void
BluetoothA2dpManager::NotifyStatusChanged()
BluetoothA2dpManager::DispatchConnectionStatusChanged()
{
MOZ_ASSERT(NS_IsMainThread());
DispatchStatusChangedEvent(
NS_LITERAL_STRING(A2DP_STATUS_CHANGED_ID), mDeviceAddress, mA2dpConnected);
}
void
BluetoothA2dpManager::NotifyConnectionStatusChanged()
{
MOZ_ASSERT(NS_IsMainThread());

Просмотреть файл

@ -72,12 +72,14 @@ private:
void HandleSinkStateChanged(SinkState aState);
void HandleShutdown();
void NotifyStatusChanged();
void DispatchConnectionStatusChanged();
void NotifyConnectionStatusChanged();
nsString mDeviceAddress;
// A2DP data member
bool mA2dpConnected;
bool mPlaying;
nsString mDeviceAddress;
SinkState mSinkState;
// AVRCP data member

Просмотреть файл

@ -52,12 +52,26 @@ extern bool gBluetoothDebugFlag;
#define KEY_ADAPTER "/B2G/bluetooth/adapter"
/**
* When connection status of Bluetooth profiles change, we'll notify observers
* of following topics.
* When the connection status of a Bluetooth profile is changed, we'll notify
* observers which register the following topics.
*/
#define BLUETOOTH_HFP_STATUS_CHANGED_ID "bluetooth-hfp-status-changed"
#define BLUETOOTH_SCO_STATUS_CHANGED_ID "bluetooth-sco-status-changed"
#define BLUETOOTH_A2DP_STATUS_CHANGED_ID "bluetooth-a2dp-status-changed"
#define BLUETOOTH_A2DP_STATUS_CHANGED_ID "bluetooth-a2dp-status-changed"
#define BLUETOOTH_HFP_STATUS_CHANGED_ID "bluetooth-hfp-status-changed"
#define BLUETOOTH_SCO_STATUS_CHANGED_ID "bluetooth-sco-status-changed"
/**
* When the connection status of a Bluetooth profile is changed, we'll
* distribute one of the following events.
*/
#define A2DP_STATUS_CHANGED_ID "a2dpstatuschanged"
#define HFP_STATUS_CHANGED_ID "hfpstatuschanged"
#define SCO_STATUS_CHANGED_ID "scostatuschanged"
/**
* When the pair status of a Bluetooth device is changed, we'll distribute an
* event.
*/
#define PAIRED_STATUS_CHANGED_ID "pairedstatuschanged"
// Bluetooth address format: xx:xx:xx:xx:xx:xx (or xx_xx_xx_xx_xx_xx)
#define BLUETOOTH_ADDRESS_LENGTH 17

Просмотреть файл

@ -454,21 +454,37 @@ BluetoothHfpManager::Get()
}
void
BluetoothHfpManager::NotifyStatusChanged(const char* aType)
BluetoothHfpManager::DispatchConnectionStatusChanged(const nsAString& aType)
{
// Broadcast system message to Gaia
MOZ_ASSERT(NS_IsMainThread());
bool status = false;
if (aType.EqualsLiteral(HFP_STATUS_CHANGED_ID)) {
status = IsConnected();
} else if (aType.EqualsLiteral(SCO_STATUS_CHANGED_ID)) {
status = IsScoConnected();
} else {
BT_WARNING("Wrong type for DispatchConnectionStatusChanged");
return;
}
DispatchStatusChangedEvent(aType, mDeviceAddress, status);
}
void
BluetoothHfpManager::NotifyConnectionStatusChanged(const nsAString& aType)
{
MOZ_ASSERT(NS_IsMainThread());
BluetoothValue v;
InfallibleTArray<BluetoothNamedValue> parameters;
nsAutoString type, name;
type = NS_ConvertUTF8toUTF16(aType);
name.AssignLiteral("connected");
if (type.EqualsLiteral(BLUETOOTH_HFP_STATUS_CHANGED_ID)) {
nsString name = NS_LITERAL_STRING("connected");
if (aType.EqualsLiteral(BLUETOOTH_HFP_STATUS_CHANGED_ID)) {
v = IsConnected();
} else if (type.EqualsLiteral(BLUETOOTH_SCO_STATUS_CHANGED_ID)) {
} else if (aType.EqualsLiteral(BLUETOOTH_SCO_STATUS_CHANGED_ID)) {
v = IsScoConnected();
} else {
NS_WARNING("Wrong type for NotifyStatusChanged");
BT_WARNING("Wrong type for NotifyConnectionStatusChanged");
return;
}
parameters.AppendElement(BluetoothNamedValue(name, v));
@ -477,7 +493,7 @@ BluetoothHfpManager::NotifyStatusChanged(const char* aType)
v = mDeviceAddress;
parameters.AppendElement(BluetoothNamedValue(name, v));
if (!BroadcastSystemMessage(type, parameters)) {
if (!BroadcastSystemMessage(aType, parameters)) {
NS_WARNING("Failed to broadcast system message to settings");
}
@ -486,8 +502,9 @@ BluetoothHfpManager::NotifyStatusChanged(const char* aType)
do_GetService("@mozilla.org/observer-service;1");
NS_ENSURE_TRUE_VOID(obs);
if (NS_FAILED(obs->NotifyObservers(this, aType, mDeviceAddress.get()))) {
NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!");
if (NS_FAILED(obs->NotifyObservers(this, NS_ConvertUTF16toUTF8(aType).get(),
mDeviceAddress.get()))) {
NS_WARNING("Failed to notify observsers!");
}
}
@ -1482,7 +1499,8 @@ BluetoothHfpManager::OnConnectSuccess(BluetoothSocket* aSocket)
// Cache device path for NotifySettings() since we can't get socket address
// when a headset disconnect with us
mSocket->GetAddress(mDeviceAddress);
NotifyStatusChanged(BLUETOOTH_HFP_STATUS_CHANGED_ID);
NotifyConnectionStatusChanged(NS_LITERAL_STRING(BLUETOOTH_HFP_STATUS_CHANGED_ID));
DispatchConnectionStatusChanged(NS_LITERAL_STRING(HFP_STATUS_CHANGED_ID));
ListenSco();
@ -1537,7 +1555,8 @@ BluetoothHfpManager::OnDisconnect(BluetoothSocket* aSocket)
DisconnectSco();
Listen();
NotifyStatusChanged(BLUETOOTH_HFP_STATUS_CHANGED_ID);
NotifyConnectionStatusChanged(NS_LITERAL_STRING(BLUETOOTH_HFP_STATUS_CHANGED_ID));
DispatchConnectionStatusChanged(NS_LITERAL_STRING(HFP_STATUS_CHANGED_ID));
Reset();
}
@ -1617,7 +1636,8 @@ BluetoothHfpManager::OnScoConnectSuccess()
mScoRunnable = nullptr;
}
NotifyStatusChanged(BLUETOOTH_SCO_STATUS_CHANGED_ID);
NotifyConnectionStatusChanged(NS_LITERAL_STRING(BLUETOOTH_SCO_STATUS_CHANGED_ID));
DispatchConnectionStatusChanged(NS_LITERAL_STRING(SCO_STATUS_CHANGED_ID));
mScoSocketStatus = mScoSocket->GetConnectionStatus();
}
@ -1640,7 +1660,8 @@ BluetoothHfpManager::OnScoDisconnect()
{
if (mScoSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
ListenSco();
NotifyStatusChanged(BLUETOOTH_SCO_STATUS_CHANGED_ID);
NotifyConnectionStatusChanged(NS_LITERAL_STRING(BLUETOOTH_SCO_STATUS_CHANGED_ID));
DispatchConnectionStatusChanged(NS_LITERAL_STRING(SCO_STATUS_CHANGED_ID));
}
}

Просмотреть файл

@ -117,8 +117,9 @@ private:
uint32_t FindFirstCall(uint16_t aState);
uint32_t GetNumberOfCalls(uint16_t aState);
void DispatchConnectionStatusChanged(const nsAString& aType);
void NotifyDialer(const nsAString& aCommand);
void NotifyStatusChanged(const char* aType);
void NotifyConnectionStatusChanged(const nsAString& aType);
bool SendCommand(const char* aCommand, uint32_t aValue = 0);
bool SendLine(const char* aMessage);

Просмотреть файл

@ -788,9 +788,9 @@ BluetoothService::Notify(const BluetoothSignal& aData)
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 0,
"Cancel: Wrong length of parameters");
type.AssignLiteral("bluetooth-cancel");
} else if (aData.name().EqualsLiteral("PairedStatusChanged")) {
} else if (aData.name().EqualsLiteral(PAIRED_STATUS_CHANGED_ID)) {
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 1,
"PairedStatusChagned: Wrong length of parameters");
"pairedstatuschanged: Wrong length of parameters");
type.AssignLiteral("bluetooth-pairedstatuschanged");
} else {
nsCString warningMsg;

Просмотреть файл

@ -7,6 +7,7 @@
#include "base/basictypes.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothUtils.h"
#include "jsapi.h"
#include "mozilla/Scoped.h"
@ -173,4 +174,24 @@ ParseAtCommand(const nsACString& aAtCommand, const int aStart,
aRetValues.AppendElement(tmp);
}
void
DispatchStatusChangedEvent(const nsAString& aType,
const nsAString& aAddress,
bool aStatus)
{
MOZ_ASSERT(NS_IsMainThread());
InfallibleTArray<BluetoothNamedValue> data;
data.AppendElement(
BluetoothNamedValue(NS_LITERAL_STRING("address"), nsString(aAddress)));
data.AppendElement(
BluetoothNamedValue(NS_LITERAL_STRING("status"), aStatus));
BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->DistributeSignal(signal);
}
END_BLUETOOTH_NAMESPACE

Просмотреть файл

@ -43,6 +43,11 @@ void
ParseAtCommand(const nsACString& aAtCommand, const int aStart,
nsTArray<nsCString>& aRetValues);
void
DispatchStatusChangedEvent(const nsAString& aType,
const nsAString& aDeviceAddress,
bool aStatus);
END_BLUETOOTH_NAMESPACE
#endif

Просмотреть файл

@ -467,6 +467,8 @@ GetIntCallback(DBusMessage* aMsg, void* aBluetoothReplyRunnable)
static void
CheckForError(DBusMessage* aMsg, void *aParam, const nsAString& aError)
{
NS_ENSURE_TRUE_VOID(aMsg);
BluetoothValue v;
nsAutoString replyError;
UnpackVoidMessage(aMsg, nullptr, v, replyError);
@ -1484,16 +1486,27 @@ EventFilter(DBusConnection* aConn, DBusMessage* aMsg, void* aData)
sDeviceProperties,
ArrayLength(sDeviceProperties));
// Fire another task for sending system message of
// "bluetooth-pairedstatuschanged"
BluetoothNamedValue& property = v.get_ArrayOfBluetoothNamedValue()[0];
if (property.name().EqualsLiteral("Paired")) {
// Original approach: Broadcast system message of
// "bluetooth-pairedstatuschanged" from BluetoothService.
BluetoothValue newValue(v);
ToLowerCase(newValue.get_ArrayOfBluetoothNamedValue()[0].name());
BluetoothSignal signal(NS_LITERAL_STRING("PairedStatusChanged"),
BluetoothSignal signal(NS_LITERAL_STRING(PAIRED_STATUS_CHANGED_ID),
NS_LITERAL_STRING(KEY_LOCAL_AGENT),
newValue);
NS_DispatchToMainThread(new DistributeBluetoothSignalTask(signal));
// New approach: Dispatch event from BluetoothAdapter
bool status = property.value();
InfallibleTArray<BluetoothNamedValue> parameters;
parameters.AppendElement(
BluetoothNamedValue(NS_LITERAL_STRING("address"), signalPath));
parameters.AppendElement(
BluetoothNamedValue(NS_LITERAL_STRING("status"), status));
signal.path() = NS_LITERAL_STRING(KEY_ADAPTER);
signal.value() = parameters;
NS_DispatchToMainThread(new DistributeBluetoothSignalTask(signal));
}
} else if (dbus_message_is_signal(aMsg, DBUS_MANAGER_IFACE, "AdapterAdded")) {
const char* str;