Bug 1181827 - [Aries-L] Store remote device address and name mapping for pairing, r=shuang

This commit is contained in:
Ben Tian 2015-07-22 10:23:45 +08:00
parent 7b987c171d
commit ec7a3fd8bc

View File

@ -115,11 +115,8 @@ static bool sIsFirstTimeToggleOffBt(false);
#ifndef MOZ_B2G_BT_API_V1 #ifndef MOZ_B2G_BT_API_V1
static bool sAdapterEnabled(false); static bool sAdapterEnabled(false);
// Use a static hash table to keep the name of remote device during the pairing // Static hash table to map device name from address
// procedure. In this manner, BT service and adapter can get the name of paired static nsDataHashtable<nsStringHashKey, nsString> sDeviceNameMap;
// device name when bond state changed.
// The hash Key is BD address, the Value is remote BD name.
static nsDataHashtable<nsStringHashKey, nsString> sPairingNameTable;
static nsTArray<nsRefPtr<BluetoothReplyRunnable> > sChangeAdapterStateRunnableArray; static nsTArray<nsRefPtr<BluetoothReplyRunnable> > sChangeAdapterStateRunnableArray;
static nsTArray<nsRefPtr<BluetoothReplyRunnable> > sChangeDiscoveryRunnableArray; static nsTArray<nsRefPtr<BluetoothReplyRunnable> > sChangeDiscoveryRunnableArray;
@ -1896,7 +1893,7 @@ BluetoothServiceBluedroid::AdapterStateChangedNotification(bool aState)
sFetchUuidsRunnableArray.Clear(); sFetchUuidsRunnableArray.Clear();
sBondingRunnableArray.Clear(); sBondingRunnableArray.Clear();
sUnbondingRunnableArray.Clear(); sUnbondingRunnableArray.Clear();
sPairingNameTable.Clear(); sDeviceNameMap.Clear();
// Bluetooth scan mode is SCAN_MODE_CONNECTABLE by default, i.e., it should // Bluetooth scan mode is SCAN_MODE_CONNECTABLE by default, i.e., it should
// be connectable and non-discoverable. // be connectable and non-discoverable.
@ -2178,7 +2175,8 @@ BluetoothServiceBluedroid::RemoteDevicePropertiesNotification(
InfallibleTArray<BluetoothNamedValue> propertiesArray; InfallibleTArray<BluetoothNamedValue> propertiesArray;
BT_APPEND_NAMED_VALUE(propertiesArray, "Address", nsString(aBdAddr)); nsString bdAddr(aBdAddr);
BT_APPEND_NAMED_VALUE(propertiesArray, "Address", bdAddr);
for (int i = 0; i < aNumProperties; ++i) { for (int i = 0; i < aNumProperties; ++i) {
@ -2187,6 +2185,9 @@ BluetoothServiceBluedroid::RemoteDevicePropertiesNotification(
if (p.mType == PROPERTY_BDNAME) { if (p.mType == PROPERTY_BDNAME) {
BT_APPEND_NAMED_VALUE(propertiesArray, "Name", p.mString); BT_APPEND_NAMED_VALUE(propertiesArray, "Name", p.mString);
// Update <address, name> mapping
sDeviceNameMap.Remove(bdAddr);
sDeviceNameMap.Put(bdAddr, p.mString);
} else if (p.mType == PROPERTY_CLASS_OF_DEVICE) { } else if (p.mType == PROPERTY_CLASS_OF_DEVICE) {
uint32_t cod = p.mUint32; uint32_t cod = p.mUint32;
BT_APPEND_NAMED_VALUE(propertiesArray, "Cod", cod); BT_APPEND_NAMED_VALUE(propertiesArray, "Cod", cod);
@ -2382,19 +2383,19 @@ BluetoothServiceBluedroid::DeviceFoundNotification(
#ifndef MOZ_B2G_BT_API_V1 #ifndef MOZ_B2G_BT_API_V1
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
BluetoothValue propertyValue;
InfallibleTArray<BluetoothNamedValue> propertiesArray; InfallibleTArray<BluetoothNamedValue> propertiesArray;
nsString bdAddr, bdName;
for (int i = 0; i < aNumProperties; i++) { for (int i = 0; i < aNumProperties; i++) {
const BluetoothProperty& p = aProperties[i]; const BluetoothProperty& p = aProperties[i];
if (p.mType == PROPERTY_BDADDR) { if (p.mType == PROPERTY_BDADDR) {
BT_APPEND_NAMED_VALUE(propertiesArray, "Address", p.mString); BT_APPEND_NAMED_VALUE(propertiesArray, "Address", p.mString);
bdAddr = p.mString;
} else if (p.mType == PROPERTY_BDNAME) { } else if (p.mType == PROPERTY_BDNAME) {
BT_APPEND_NAMED_VALUE(propertiesArray, "Name", p.mString); BT_APPEND_NAMED_VALUE(propertiesArray, "Name", p.mString);
bdName = p.mString;
} else if (p.mType == PROPERTY_CLASS_OF_DEVICE) { } else if (p.mType == PROPERTY_CLASS_OF_DEVICE) {
BT_APPEND_NAMED_VALUE(propertiesArray, "Cod", p.mUint32); BT_APPEND_NAMED_VALUE(propertiesArray, "Cod", p.mUint32);
@ -2423,6 +2424,10 @@ BluetoothServiceBluedroid::DeviceFoundNotification(
} }
} }
// Update <address, name> mapping
sDeviceNameMap.Remove(bdAddr);
sDeviceNameMap.Put(bdAddr, bdName);
DistributeSignal(NS_LITERAL_STRING("DeviceFound"), DistributeSignal(NS_LITERAL_STRING("DeviceFound"),
NS_LITERAL_STRING(KEY_ADAPTER), NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray)); BluetoothValue(propertiesArray));
@ -2515,14 +2520,23 @@ BluetoothServiceBluedroid::PinRequestNotification(const nsAString& aRemoteBdAddr
InfallibleTArray<BluetoothNamedValue> propertiesArray; InfallibleTArray<BluetoothNamedValue> propertiesArray;
BT_APPEND_NAMED_VALUE(propertiesArray, "address", nsString(aRemoteBdAddr)); // If |aBdName| is empty, get device name from |sDeviceNameMap|;
BT_APPEND_NAMED_VALUE(propertiesArray, "name", nsString(aBdName)); // Otherwise update <address, name> mapping with |aBdName|
nsString bdAddr(aRemoteBdAddr);
nsString bdName(aBdName);
if (bdName.IsEmpty()) {
sDeviceNameMap.Get(bdAddr, &bdName);
} else {
sDeviceNameMap.Remove(bdAddr);
sDeviceNameMap.Put(bdAddr, bdName);
}
BT_APPEND_NAMED_VALUE(propertiesArray, "address", bdAddr);
BT_APPEND_NAMED_VALUE(propertiesArray, "name", bdName);
BT_APPEND_NAMED_VALUE(propertiesArray, "passkey", EmptyString()); BT_APPEND_NAMED_VALUE(propertiesArray, "passkey", EmptyString());
BT_APPEND_NAMED_VALUE(propertiesArray, "type", BT_APPEND_NAMED_VALUE(propertiesArray, "type",
NS_LITERAL_STRING(PAIRING_REQ_TYPE_ENTERPINCODE)); NS_LITERAL_STRING(PAIRING_REQ_TYPE_ENTERPINCODE));
sPairingNameTable.Put(nsString(aRemoteBdAddr), nsString(aBdName));
DistributeSignal(NS_LITERAL_STRING("PairingRequest"), DistributeSignal(NS_LITERAL_STRING("PairingRequest"),
NS_LITERAL_STRING(KEY_PAIRING_LISTENER), NS_LITERAL_STRING(KEY_PAIRING_LISTENER),
BluetoothValue(propertiesArray)); BluetoothValue(propertiesArray));
@ -2551,8 +2565,17 @@ BluetoothServiceBluedroid::SspRequestNotification(
#ifndef MOZ_B2G_BT_API_V1 #ifndef MOZ_B2G_BT_API_V1
InfallibleTArray<BluetoothNamedValue> propertiesArray; InfallibleTArray<BluetoothNamedValue> propertiesArray;
nsAutoString passkey;
nsAutoString pairingType; // If |aBdName| is empty, get device name from |sDeviceNameMap|;
// Otherwise update <address, name> mapping with |aBdName|
nsString bdAddr(aRemoteBdAddr);
nsString bdName(aBdName);
if (bdName.IsEmpty()) {
sDeviceNameMap.Get(bdAddr, &bdName);
} else {
sDeviceNameMap.Remove(bdAddr);
sDeviceNameMap.Put(bdAddr, bdName);
}
/** /**
* Assign pairing request type and passkey based on the pairing variant. * Assign pairing request type and passkey based on the pairing variant.
@ -2562,6 +2585,8 @@ BluetoothServiceBluedroid::SspRequestNotification(
* PAIRING_REQ_TYPE_DISPLAYPASSKEY * PAIRING_REQ_TYPE_DISPLAYPASSKEY
* 2) empty string: PAIRING_REQ_TYPE_CONSENT * 2) empty string: PAIRING_REQ_TYPE_CONSENT
*/ */
nsAutoString passkey;
nsAutoString pairingType;
switch (aPairingVariant) { switch (aPairingVariant) {
case SSP_VARIANT_PASSKEY_CONFIRMATION: case SSP_VARIANT_PASSKEY_CONFIRMATION:
pairingType.AssignLiteral(PAIRING_REQ_TYPE_CONFIRMATION); pairingType.AssignLiteral(PAIRING_REQ_TYPE_CONFIRMATION);
@ -2579,13 +2604,11 @@ BluetoothServiceBluedroid::SspRequestNotification(
return; return;
} }
BT_APPEND_NAMED_VALUE(propertiesArray, "address", nsString(aRemoteBdAddr)); BT_APPEND_NAMED_VALUE(propertiesArray, "address", bdAddr);
BT_APPEND_NAMED_VALUE(propertiesArray, "name", nsString(aBdName)); BT_APPEND_NAMED_VALUE(propertiesArray, "name", bdName);
BT_APPEND_NAMED_VALUE(propertiesArray, "passkey", passkey); BT_APPEND_NAMED_VALUE(propertiesArray, "passkey", passkey);
BT_APPEND_NAMED_VALUE(propertiesArray, "type", pairingType); BT_APPEND_NAMED_VALUE(propertiesArray, "type", pairingType);
sPairingNameTable.Put(nsString(aRemoteBdAddr), nsString(aBdName));
DistributeSignal(NS_LITERAL_STRING("PairingRequest"), DistributeSignal(NS_LITERAL_STRING("PairingRequest"),
NS_LITERAL_STRING(KEY_PAIRING_LISTENER), NS_LITERAL_STRING(KEY_PAIRING_LISTENER),
BluetoothValue(propertiesArray)); BluetoothValue(propertiesArray));
@ -2642,16 +2665,13 @@ BluetoothServiceBluedroid::BondStateChangedNotification(
return; return;
} }
// Retrieve and remove pairing device name from hash table // Query pairing device name from hash table
nsString deviceName; nsString remoteBdAddr(aRemoteBdAddr);
bool nameExists = sPairingNameTable.Get(aRemoteBdAddr, &deviceName); nsString remotebdName;
if (nameExists) { sDeviceNameMap.Get(remoteBdAddr, &remotebdName);
sPairingNameTable.Remove(aRemoteBdAddr);
}
// Update bonded address array and append pairing device name // Update bonded address array and append pairing device name
InfallibleTArray<BluetoothNamedValue> propertiesArray; InfallibleTArray<BluetoothNamedValue> propertiesArray;
nsString remoteBdAddr = nsString(aRemoteBdAddr);
if (!bonded) { if (!bonded) {
sAdapterBondedAddressArray.RemoveElement(remoteBdAddr); sAdapterBondedAddressArray.RemoveElement(remoteBdAddr);
} else { } else {
@ -2659,21 +2679,17 @@ BluetoothServiceBluedroid::BondStateChangedNotification(
sAdapterBondedAddressArray.AppendElement(remoteBdAddr); sAdapterBondedAddressArray.AppendElement(remoteBdAddr);
} }
// We don't assert |!deviceName.IsEmpty()| here since empty string is // We don't assert |!remotebdName.IsEmpty()| since empty string is also
// also a valid name. According to Bluetooth Core Spec. v3.0 - Sec. 6.22, // valid, according to Bluetooth Core Spec. v3.0 - Sec. 6.22:
// "a valid Bluetooth name is a UTF-8 encoding string which is up to 248 // "a valid Bluetooth name is a UTF-8 encoding string which is up to 248
// bytes in length." // bytes in length."
// Furthermore, we don't assert |nameExists| here since it's expected to be BT_APPEND_NAMED_VALUE(propertiesArray, "Name", remotebdName);
// 'false' if remote device is using "SSP just works without user
// interaction" or "legacy pairing with auto-pairing".
BT_APPEND_NAMED_VALUE(propertiesArray, "Name", deviceName);
} }
// Notify device of attribute changed // Notify device of attribute changed
BT_APPEND_NAMED_VALUE(propertiesArray, "Paired", bonded); BT_APPEND_NAMED_VALUE(propertiesArray, "Paired", bonded);
DistributeSignal(NS_LITERAL_STRING("PropertyChanged"), DistributeSignal(NS_LITERAL_STRING("PropertyChanged"),
aRemoteBdAddr, remoteBdAddr,
BluetoothValue(propertiesArray)); BluetoothValue(propertiesArray));
// Notify adapter of device paired/unpaired // Notify adapter of device paired/unpaired