Bug 1033961: Asynchronous Bluedroid device-property methods (under bluetooth2/), r=btian

This commit is contained in:
Thomas Zimmermann 2014-07-09 09:38:35 +02:00
parent 2244cbf6c3
commit 06ae360365
3 changed files with 83 additions and 34 deletions

View File

@ -613,24 +613,45 @@ BluetoothInterface::SetAdapterProperty(const bt_property_t* aProperty,
/* Remote Device Properties */
int
BluetoothInterface::GetRemoteDeviceProperties(bt_bdaddr_t *aRemoteAddr)
void
BluetoothInterface::GetRemoteDeviceProperties(bt_bdaddr_t *aRemoteAddr,
BluetoothResultHandler* aRes)
{
return mInterface->get_remote_device_properties(aRemoteAddr);
int status = mInterface->get_remote_device_properties(aRemoteAddr);
if (aRes) {
DispatchBluetoothResult(aRes,
&BluetoothResultHandler::GetRemoteDeviceProperties,
status);
}
}
int
void
BluetoothInterface::GetRemoteDeviceProperty(bt_bdaddr_t* aRemoteAddr,
bt_property_type_t aType)
bt_property_type_t aType,
BluetoothResultHandler* aRes)
{
return mInterface->get_remote_device_property(aRemoteAddr, aType);
int status = mInterface->get_remote_device_property(aRemoteAddr, aType);
if (aRes) {
DispatchBluetoothResult(aRes,
&BluetoothResultHandler::GetRemoteDeviceProperty,
status);
}
}
int
void
BluetoothInterface::SetRemoteDeviceProperty(bt_bdaddr_t* aRemoteAddr,
const bt_property_t* aProperty)
const bt_property_t* aProperty,
BluetoothResultHandler* aRes)
{
return mInterface->set_remote_device_property(aRemoteAddr, aProperty);
int status = mInterface->set_remote_device_property(aRemoteAddr, aProperty);
if (aRes) {
DispatchBluetoothResult(aRes,
&BluetoothResultHandler::SetRemoteDeviceProperty,
status);
}
}
/* Remote Services */

View File

@ -247,11 +247,14 @@ public:
/* Remote Device Properties */
int GetRemoteDeviceProperties(bt_bdaddr_t *aRemoteAddr);
int GetRemoteDeviceProperty(bt_bdaddr_t* aRemoteAddr,
bt_property_type_t aType);
int SetRemoteDeviceProperty(bt_bdaddr_t* aRemoteAddr,
const bt_property_t* aProperty);
void GetRemoteDeviceProperties(bt_bdaddr_t *aRemoteAddr,
BluetoothResultHandler* aRes);
void GetRemoteDeviceProperty(bt_bdaddr_t* aRemoteAddr,
bt_property_type_t aType,
BluetoothResultHandler* aRes);
void SetRemoteDeviceProperty(bt_bdaddr_t* aRemoteAddr,
const bt_property_t* aProperty,
BluetoothResultHandler* aRes);
/* Remote Services */

View File

@ -57,6 +57,7 @@ static InfallibleTArray<nsString> sAdapterBondedAddressArray;
// Static variables below should only be used on *main thread*
static BluetoothInterface* sBtInterface;
static nsTArray<nsRefPtr<BluetoothProfileController> > sControllerArray;
static InfallibleTArray<BluetoothNamedValue> sRemoteDevicesPack;
static nsTArray<int> sRequestedDeviceCountArray;
static nsTArray<nsRefPtr<BluetoothReplyRunnable> > sChangeAdapterStateRunnableArray;
static nsTArray<nsRefPtr<BluetoothReplyRunnable> > sChangeDiscoveryRunnableArray;
@ -491,8 +492,6 @@ public:
return NS_OK;
}
static InfallibleTArray<BluetoothNamedValue> sRemoteDevicesPack;
// Use address as the index
sRemoteDevicesPack.AppendElement(
BluetoothNamedValue(mRemoteDeviceBdAddress, mProps));
@ -1079,6 +1078,39 @@ BluetoothServiceBluedroid::GetAdaptersInternal(
return NS_OK;
}
class GetRemoteDevicePropertiesResultHandler MOZ_FINAL
: public BluetoothResultHandler
{
public:
GetRemoteDevicePropertiesResultHandler(const nsAString& aDeviceAddress)
: mDeviceAddress(aDeviceAddress)
{ }
void OnError(int aStatus) MOZ_OVERRIDE
{
MOZ_ASSERT(NS_IsMainThread());
BT_WARNING("GetRemoteDeviceProperties(%s) failed: %d",
NS_ConvertUTF16toUTF8(mDeviceAddress).get(), aStatus);
/* dispatch result after final pending operation */
if (--sRequestedDeviceCountArray[0] == 0) {
if (!sGetDeviceRunnableArray.IsEmpty()) {
DispatchBluetoothReply(
sGetDeviceRunnableArray[0], sRemoteDevicesPack,
NS_LITERAL_STRING("GetRemoteDeviceProperties failed"));
sGetDeviceRunnableArray.RemoveElementAt(0);
}
sRequestedDeviceCountArray.RemoveElementAt(0);
sRemoteDevicesPack.Clear();
}
}
private:
nsString mDeviceAddress;
};
nsresult
BluetoothServiceBluedroid::GetConnectedDevicePropertiesInternal(
uint16_t aServiceUuid, BluetoothReplyRunnable* aRunnable)
@ -1110,22 +1142,18 @@ BluetoothServiceBluedroid::GetConnectedDevicePropertiesInternal(
return NS_OK;
}
sRequestedDeviceCountArray.AppendElement(requestedDeviceCount);
sGetDeviceRunnableArray.AppendElement(aRunnable);
for (int i = 0; i < requestedDeviceCount; i++) {
// Retrieve all properties of devices
bt_bdaddr_t addressType;
StringToBdAddressType(deviceAddresses[i], &addressType);
int ret = sBtInterface->GetRemoteDeviceProperties(&addressType);
if (ret != BT_STATUS_SUCCESS) {
DispatchBluetoothReply(aRunnable, BluetoothValue(true),
NS_LITERAL_STRING("GetConnectedDeviceFailed"));
return NS_OK;
}
sBtInterface->GetRemoteDeviceProperties(&addressType,
new GetRemoteDevicePropertiesResultHandler(deviceAddresses[i]));
}
sRequestedDeviceCountArray.AppendElement(requestedDeviceCount);
sGetDeviceRunnableArray.AppendElement(aRunnable);
return NS_OK;
}
@ -1144,20 +1172,17 @@ BluetoothServiceBluedroid::GetPairedDevicePropertiesInternal(
return NS_OK;
}
sRequestedDeviceCountArray.AppendElement(requestedDeviceCount);
sGetDeviceRunnableArray.AppendElement(aRunnable);
for (int i = 0; i < requestedDeviceCount; i++) {
// Retrieve all properties of devices
bt_bdaddr_t addressType;
StringToBdAddressType(aDeviceAddress[i], &addressType);
int ret = sBtInterface->GetRemoteDeviceProperties(&addressType);
if (ret != BT_STATUS_SUCCESS) {
DispatchBluetoothReply(aRunnable, BluetoothValue(true),
NS_LITERAL_STRING("GetPairedDeviceFailed"));
return NS_OK;
}
}
sRequestedDeviceCountArray.AppendElement(requestedDeviceCount);
sGetDeviceRunnableArray.AppendElement(aRunnable);
sBtInterface->GetRemoteDeviceProperties(&addressType,
new GetRemoteDevicePropertiesResultHandler(aDeviceAddress[i]));
}
return NS_OK;
}