Bug 1002391 - Get the latest SDP records of the target device, r=shuang

Use BlueZ API 'CreateDevice' to update the SDP records of those
unknown devices. On the other hand, still use 'DiscoverServices'
to update SDP records of paired devices.
This commit is contained in:
Eric Chou 2014-06-06 18:14:22 +08:00
Родитель 2879fc7c4b
Коммит b5110e88a0
1 изменённых файлов: 67 добавлений и 20 удалений

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

@ -3436,14 +3436,13 @@ BluetoothDBusService::ToggleCalls(BluetoothReplyRunnable* aRunnable)
class OnUpdateSdpRecordsRunnable : public nsRunnable
{
public:
OnUpdateSdpRecordsRunnable(const nsAString& aObjectPath,
OnUpdateSdpRecordsRunnable(const nsAString& aDeviceAddress,
BluetoothProfileManagerBase* aManager)
: mManager(aManager)
: mDeviceAddress(aDeviceAddress)
, mManager(aManager)
{
MOZ_ASSERT(!aObjectPath.IsEmpty());
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
MOZ_ASSERT(aManager);
mDeviceAddress = GetAddressFromObjectPath(aObjectPath);
}
nsresult
@ -3456,6 +3455,12 @@ public:
return NS_OK;
}
void
GetDeviceAddress(nsAString& aRetDeviceAddress)
{
aRetDeviceAddress = mDeviceAddress;
}
private:
nsString mDeviceAddress;
BluetoothProfileManagerBase* mManager;
@ -3641,31 +3646,74 @@ public:
MOZ_ASSERT(sDBusConnection);
MOZ_ASSERT(!sAdapterPath.IsEmpty());
const nsString objectPath =
GetObjectPathFromAddress(sAdapterPath, mDeviceAddress);
// We first guess that the device doesn't exist at all. So we use BlueZ
// API "CreateDevice" to create an object path for the BluetoothDevice
// object. "CreateDevice" will connect to the remote device and retrieve
// SDP records of the target.
NS_ConvertUTF16toUTF8 address(mDeviceAddress);
const char* cAddress = address.get();
// I choose to use raw pointer here because this is going to be passed as an
// argument into SendWithReply() at once.
OnUpdateSdpRecordsRunnable* callbackRunnable =
new OnUpdateSdpRecordsRunnable(objectPath, mBluetoothProfileManager);
new OnUpdateSdpRecordsRunnable(mDeviceAddress, mBluetoothProfileManager);
sDBusConnection->SendWithReply(DiscoverServicesCallback,
(void*)callbackRunnable, -1,
BLUEZ_DBUS_BASE_IFC,
NS_ConvertUTF16toUTF8(objectPath).get(),
DBUS_DEVICE_IFACE,
"DiscoverServices",
DBUS_TYPE_STRING, &EmptyCString(),
DBUS_TYPE_INVALID);
sDBusConnection->SendWithReply(
CreateDeviceCallback, callbackRunnable, -1,
BLUEZ_DBUS_BASE_IFC,
NS_ConvertUTF16toUTF8(sAdapterPath).get(),
DBUS_ADAPTER_IFACE,
"CreateDevice",
DBUS_TYPE_STRING, &cAddress,
DBUS_TYPE_INVALID);
}
protected:
static void CreateDeviceCallback(DBusMessage* aMsg, void* aData)
{
MOZ_ASSERT(!NS_IsMainThread()); // I/O thread
nsAutoString errorString;
OnUpdateSdpRecordsRunnable* r =
static_cast<OnUpdateSdpRecordsRunnable*>(aData);
if (IsDBusMessageError(aMsg, nullptr, errorString)) {
// If the device already exists it comes here. If we want to refresh its
// SDP records then we have to do "DiscoverServices"
BT_LOGR("%s", NS_ConvertUTF16toUTF8(errorString).get());
nsString deviceAddress;
r->GetDeviceAddress(deviceAddress);
const nsString objectPath =
GetObjectPathFromAddress(sAdapterPath, deviceAddress);
sDBusConnection->SendWithReply(DiscoverServicesCallback,
aData, -1,
BLUEZ_DBUS_BASE_IFC,
NS_ConvertUTF16toUTF8(objectPath).get(),
DBUS_DEVICE_IFACE,
"DiscoverServices",
DBUS_TYPE_STRING, &EmptyCString(),
DBUS_TYPE_INVALID);
return;
}
NS_DispatchToMainThread(r);
}
static void DiscoverServicesCallback(DBusMessage* aMsg, void* aData)
{
MOZ_ASSERT(!NS_IsMainThread()); // I/O thread
nsRefPtr<OnUpdateSdpRecordsRunnable> r(
static_cast<OnUpdateSdpRecordsRunnable*>(aData));
nsAutoString errorStr;
if (IsDBusMessageError(aMsg, nullptr, errorStr)) {
BT_LOGR("%s", NS_ConvertUTF16toUTF8(errorStr).get());
}
OnUpdateSdpRecordsRunnable* r =
static_cast<OnUpdateSdpRecordsRunnable*>(aData);
NS_DispatchToMainThread(r);
}
@ -3680,8 +3728,7 @@ BluetoothDBusService::UpdateSdpRecords(const nsAString& aDeviceAddress,
{
MOZ_ASSERT(NS_IsMainThread());
Task* task = new UpdateSdpRecordsTask(aDeviceAddress, aManager);
DispatchToDBusThread(task);
DispatchToDBusThread(new UpdateSdpRecordsTask(aDeviceAddress, aManager));
return true;
}