2012-05-07 14:50:25 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-20 20:21:24 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-07 14:50:25 -07:00
|
|
|
/*
|
|
|
|
** Copyright 2006, The Android Open Source Project
|
|
|
|
**
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
**
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
**
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-06-02 11:23:16 -07:00
|
|
|
#include "DBusUtils.h"
|
2013-01-28 01:15:31 -08:00
|
|
|
#include "DBusThread.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-03-01 04:43:45 -08:00
|
|
|
#include "mozilla/Monitor.h"
|
2013-01-28 01:15:31 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2012-06-02 11:23:16 -07:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2012-05-07 14:50:25 -07:00
|
|
|
|
|
|
|
#undef LOG
|
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args);
|
|
|
|
#else
|
|
|
|
#define LOG(args...) printf(args);
|
|
|
|
#endif
|
|
|
|
|
2012-06-02 11:23:16 -07:00
|
|
|
#define BLUEZ_DBUS_BASE_PATH "/org/bluez"
|
|
|
|
#define BLUEZ_DBUS_BASE_IFC "org.bluez"
|
|
|
|
#define BLUEZ_ERROR_IFC "org.bluez.Error"
|
|
|
|
|
2012-05-07 14:50:25 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
void
|
|
|
|
log_and_free_dbus_error(DBusError* err, const char* function, DBusMessage* msg)
|
|
|
|
{
|
2012-07-17 20:41:54 -07:00
|
|
|
if (msg) {
|
2012-05-07 14:50:25 -07:00
|
|
|
LOG("%s: D-Bus error in %s: %s (%s)", function,
|
|
|
|
dbus_message_get_member((msg)), (err)->name, (err)->message);
|
|
|
|
} else {
|
|
|
|
LOG("%s: D-Bus error: %s (%s)", __FUNCTION__,
|
|
|
|
(err)->name, (err)->message);
|
|
|
|
}
|
|
|
|
dbus_error_free((err));
|
|
|
|
}
|
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
class DBusConnectionSendRunnableBase : public nsRunnable
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
DBusConnectionSendRunnableBase(DBusConnection* aConnection,
|
|
|
|
DBusMessage* aMessage)
|
|
|
|
: mConnection(aConnection),
|
|
|
|
mMessage(aMessage)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mConnection);
|
|
|
|
MOZ_ASSERT(mMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DBusConnectionSendRunnableBase()
|
|
|
|
{ }
|
2012-06-02 11:23:16 -07:00
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
DBusConnection* mConnection;
|
|
|
|
DBusMessageRefPtr mMessage;
|
|
|
|
};
|
|
|
|
|
2013-03-01 04:43:45 -08:00
|
|
|
class DBusConnectionSendSyncRunnable : public DBusConnectionSendRunnableBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool WaitForCompletion()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
MonitorAutoLock autoLock(mCompletedMonitor);
|
|
|
|
while (!mCompleted) {
|
|
|
|
mCompletedMonitor.Wait();
|
|
|
|
}
|
|
|
|
return mSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
DBusConnectionSendSyncRunnable(DBusConnection* aConnection,
|
|
|
|
DBusMessage* aMessage)
|
|
|
|
: DBusConnectionSendRunnableBase(aConnection, aMessage),
|
|
|
|
mCompletedMonitor("DBusConnectionSendSyncRunnable.mCompleted"),
|
|
|
|
mCompleted(false),
|
|
|
|
mSuccess(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual ~DBusConnectionSendSyncRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
// Call this function at the end of Run() to notify waiting
|
|
|
|
// threads.
|
|
|
|
void Completed(bool aSuccess)
|
|
|
|
{
|
|
|
|
MonitorAutoLock autoLock(mCompletedMonitor);
|
|
|
|
MOZ_ASSERT(!mCompleted);
|
|
|
|
mSuccess = aSuccess;
|
|
|
|
mCompleted = true;
|
|
|
|
mCompletedMonitor.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Monitor mCompletedMonitor;
|
|
|
|
bool mCompleted;
|
|
|
|
bool mSuccess;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sends a message and returns the message's serial number to the
|
|
|
|
// disaptching thread. Only run it in DBus thread.
|
|
|
|
//
|
|
|
|
class DBusConnectionSendRunnable : public DBusConnectionSendSyncRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DBusConnectionSendRunnable(DBusConnection* aConnection,
|
|
|
|
DBusMessage* aMessage,
|
|
|
|
dbus_uint32_t* aSerial)
|
|
|
|
: DBusConnectionSendSyncRunnable(aConnection, aMessage),
|
|
|
|
mSerial(aSerial)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
dbus_bool_t success = dbus_connection_send(mConnection, mMessage, mSerial);
|
|
|
|
Completed(success == TRUE);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(success == TRUE, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~DBusConnectionSendRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
dbus_uint32_t* mSerial;
|
|
|
|
};
|
|
|
|
|
|
|
|
dbus_bool_t
|
|
|
|
dbus_func_send(DBusConnection* aConnection, dbus_uint32_t* aSerial,
|
|
|
|
DBusMessage* aMessage)
|
|
|
|
{
|
|
|
|
nsRefPtr<DBusConnectionSendRunnable> t(
|
|
|
|
new DBusConnectionSendRunnable(aConnection, aMessage, aSerial));
|
|
|
|
MOZ_ASSERT(t);
|
|
|
|
|
|
|
|
nsresult rv = DispatchToDBusThread(t);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (aMessage) {
|
|
|
|
dbus_message_unref(aMessage);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSerial && !t->WaitForCompletion()) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dbus_bool_t
|
|
|
|
dbus_func_args_send_valist(DBusConnection* aConnection,
|
|
|
|
dbus_uint32_t* aSerial,
|
|
|
|
const char* aPath,
|
|
|
|
const char* aInterface,
|
|
|
|
const char* aFunction,
|
|
|
|
int aFirstArgType,
|
|
|
|
va_list aArgs)
|
|
|
|
{
|
|
|
|
// Compose the command...
|
|
|
|
DBusMessage* message = dbus_message_new_method_call(BLUEZ_DBUS_BASE_IFC,
|
|
|
|
aPath,
|
|
|
|
aInterface,
|
|
|
|
aFunction);
|
|
|
|
if (!message) {
|
|
|
|
LOG("Could not allocate D-Bus message object!");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... and append arguments.
|
|
|
|
if (!dbus_message_append_args_valist(message, aFirstArgType, aArgs)) {
|
|
|
|
LOG("Could not append argument to method call!");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbus_func_send(aConnection, aSerial, message);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (message) {
|
|
|
|
dbus_message_unref(message);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_bool_t
|
|
|
|
dbus_func_args_send(DBusConnection* aConnection,
|
|
|
|
dbus_uint32_t* aSerial,
|
|
|
|
const char* aPath,
|
|
|
|
const char* aInterface,
|
|
|
|
const char* aFunction,
|
|
|
|
int aFirstArgType, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, aFirstArgType);
|
|
|
|
|
|
|
|
dbus_bool_t success = dbus_func_args_send_valist(aConnection,
|
|
|
|
aSerial,
|
|
|
|
aPath,
|
|
|
|
aInterface,
|
|
|
|
aFunction,
|
|
|
|
aFirstArgType,
|
|
|
|
args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
//
|
|
|
|
// Sends a message and executes a callback function for the reply. Only
|
|
|
|
// run it in DBus thread.
|
|
|
|
//
|
|
|
|
class DBusConnectionSendWithReplyRunnable : public DBusConnectionSendRunnableBase
|
2013-01-28 01:15:31 -08:00
|
|
|
{
|
2013-03-01 03:49:57 -08:00
|
|
|
private:
|
|
|
|
class NotifyData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NotifyData(void (*aCallback)(DBusMessage*, void*), void* aData)
|
|
|
|
: mCallback(aCallback),
|
|
|
|
mData(aData)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void RunNotifyCallback(DBusMessage* aMessage)
|
|
|
|
{
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback(aMessage, mData);
|
|
|
|
}
|
|
|
|
}
|
2012-06-02 11:23:16 -07:00
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
private:
|
|
|
|
void (*mCallback)(DBusMessage*, void*);
|
|
|
|
void* mData;
|
|
|
|
};
|
2012-06-02 11:23:16 -07:00
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
// Callback function for DBus replies. Only run it in DBus thread.
|
|
|
|
//
|
|
|
|
static void Notify(DBusPendingCall* aCall, void* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
nsAutoPtr<NotifyData> data(static_cast<NotifyData*>(aData));
|
|
|
|
|
|
|
|
// The reply can be non-null if the timeout
|
|
|
|
// has been reached.
|
|
|
|
DBusMessage* reply = dbus_pending_call_steal_reply(aCall);
|
|
|
|
|
|
|
|
if (reply) {
|
|
|
|
data->RunNotifyCallback(reply);
|
|
|
|
dbus_message_unref(reply);
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
dbus_pending_call_cancel(aCall);
|
|
|
|
dbus_pending_call_unref(aCall);
|
|
|
|
}
|
2012-06-02 11:23:16 -07:00
|
|
|
|
2013-01-28 01:15:31 -08:00
|
|
|
public:
|
2013-03-01 03:49:57 -08:00
|
|
|
DBusConnectionSendWithReplyRunnable(DBusConnection* aConnection,
|
|
|
|
DBusMessage* aMessage,
|
|
|
|
int aTimeout,
|
|
|
|
void (*aCallback)(DBusMessage*, void*),
|
|
|
|
void* aData)
|
|
|
|
: DBusConnectionSendRunnableBase(aConnection, aMessage),
|
2013-01-28 01:15:31 -08:00
|
|
|
mCallback(aCallback),
|
2013-03-01 03:49:57 -08:00
|
|
|
mData(aData),
|
|
|
|
mTimeout(aTimeout)
|
2013-01-28 01:15:31 -08:00
|
|
|
{ }
|
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
NS_IMETHOD Run()
|
2013-01-28 01:15:31 -08:00
|
|
|
{
|
2013-03-01 03:49:57 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2013-01-28 01:15:31 -08:00
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
// Freed at end of Notify
|
|
|
|
nsAutoPtr<NotifyData> data(new NotifyData(mCallback, mData));
|
|
|
|
NS_ENSURE_TRUE(data, NS_ERROR_OUT_OF_MEMORY);
|
2013-01-28 01:15:31 -08:00
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
DBusPendingCall* call;
|
2013-01-28 01:15:31 -08:00
|
|
|
|
|
|
|
dbus_bool_t success = dbus_connection_send_with_reply(mConnection,
|
|
|
|
mMessage,
|
|
|
|
&call,
|
|
|
|
mTimeout);
|
|
|
|
NS_ENSURE_TRUE(success == TRUE, NS_ERROR_FAILURE);
|
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
success = dbus_pending_call_set_notify(call, Notify, data, nullptr);
|
2013-01-28 01:15:31 -08:00
|
|
|
NS_ENSURE_TRUE(success == TRUE, NS_ERROR_FAILURE);
|
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
data.forget();
|
2013-01-28 01:15:31 -08:00
|
|
|
dbus_message_unref(mMessage);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
2013-03-01 03:49:57 -08:00
|
|
|
~DBusConnectionSendWithReplyRunnable()
|
2013-01-28 01:15:31 -08:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void (*mCallback)(DBusMessage*, void*);
|
2013-03-01 03:49:57 -08:00
|
|
|
void* mData;
|
|
|
|
int mTimeout;
|
2013-01-28 01:15:31 -08:00
|
|
|
};
|
|
|
|
|
2013-03-01 03:49:57 -08:00
|
|
|
dbus_bool_t dbus_func_send_async(DBusConnection* conn,
|
|
|
|
DBusMessage* msg,
|
2012-07-17 20:41:54 -07:00
|
|
|
int timeout_ms,
|
|
|
|
void (*user_cb)(DBusMessage*,
|
|
|
|
void*),
|
2013-03-01 03:49:57 -08:00
|
|
|
void* user)
|
2013-01-28 01:15:31 -08:00
|
|
|
{
|
2013-03-01 03:49:57 -08:00
|
|
|
nsRefPtr<nsIRunnable> t(new DBusConnectionSendWithReplyRunnable(conn, msg,
|
|
|
|
timeout_ms,
|
|
|
|
user_cb,
|
|
|
|
user));
|
|
|
|
MOZ_ASSERT(t);
|
2013-01-10 07:55:43 -08:00
|
|
|
|
2013-01-28 01:15:31 -08:00
|
|
|
nsresult rv = DispatchToDBusThread(t);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (msg) {
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
}
|
|
|
|
return FALSE;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
2013-01-28 01:15:31 -08:00
|
|
|
return TRUE;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
2012-06-02 11:23:16 -07:00
|
|
|
static dbus_bool_t dbus_func_args_async_valist(DBusConnection *conn,
|
|
|
|
int timeout_ms,
|
2012-07-17 20:41:54 -07:00
|
|
|
void (*user_cb)(DBusMessage*,
|
2012-06-02 11:23:16 -07:00
|
|
|
void*),
|
|
|
|
void *user,
|
|
|
|
const char *path,
|
|
|
|
const char *ifc,
|
|
|
|
const char *func,
|
|
|
|
int first_arg_type,
|
|
|
|
va_list args) {
|
2013-08-23 12:51:44 -07:00
|
|
|
DBusMessage *msg = nullptr;
|
2012-06-02 11:23:16 -07:00
|
|
|
/* Compose the command */
|
|
|
|
msg = dbus_message_new_method_call(BLUEZ_DBUS_BASE_IFC, path, ifc, func);
|
|
|
|
|
2013-08-23 12:51:44 -07:00
|
|
|
if (msg == nullptr) {
|
2012-06-02 11:23:16 -07:00
|
|
|
LOG("Could not allocate D-Bus message object!");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* append arguments */
|
|
|
|
if (!dbus_message_append_args_valist(msg, first_arg_type, args)) {
|
|
|
|
LOG("Could not append argument to method call!");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
return dbus_func_send_async(conn, msg, timeout_ms, user_cb, user);
|
2012-06-02 11:23:16 -07:00
|
|
|
done:
|
|
|
|
if (msg) dbus_message_unref(msg);
|
2012-07-17 20:41:54 -07:00
|
|
|
return FALSE;
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dbus_bool_t dbus_func_args_async(DBusConnection *conn,
|
|
|
|
int timeout_ms,
|
2012-07-17 20:41:54 -07:00
|
|
|
void (*reply)(DBusMessage *, void *),
|
2012-06-02 11:23:16 -07:00
|
|
|
void *user,
|
|
|
|
const char *path,
|
|
|
|
const char *ifc,
|
|
|
|
const char *func,
|
|
|
|
int first_arg_type,
|
|
|
|
...) {
|
|
|
|
dbus_bool_t ret;
|
|
|
|
va_list lst;
|
|
|
|
va_start(lst, first_arg_type);
|
|
|
|
|
|
|
|
ret = dbus_func_args_async_valist(conn,
|
|
|
|
timeout_ms,
|
2012-07-17 20:41:54 -07:00
|
|
|
reply, user,
|
2012-06-02 11:23:16 -07:00
|
|
|
path, ifc, func,
|
|
|
|
first_arg_type, lst);
|
|
|
|
va_end(lst);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:05:50 -08:00
|
|
|
//
|
|
|
|
// Sends a message and allows the dispatching thread to wait for the
|
|
|
|
// reply. Only run it in DBus thread.
|
|
|
|
//
|
|
|
|
class DBusConnectionSendAndBlockRunnable : public DBusConnectionSendSyncRunnable
|
|
|
|
{
|
2013-03-11 07:22:56 -07:00
|
|
|
private:
|
|
|
|
static void Notify(DBusPendingCall* aCall, void* aData)
|
|
|
|
{
|
|
|
|
DBusConnectionSendAndBlockRunnable* runnable(
|
|
|
|
static_cast<DBusConnectionSendAndBlockRunnable*>(aData));
|
|
|
|
|
|
|
|
runnable->mReply = dbus_pending_call_steal_reply(aCall);
|
|
|
|
|
|
|
|
bool success = !!runnable->mReply;
|
|
|
|
|
|
|
|
if (runnable->mError) {
|
|
|
|
success = success && !dbus_error_is_set(runnable->mError);
|
|
|
|
|
|
|
|
if (!dbus_set_error_from_message(runnable->mError, runnable->mReply)) {
|
|
|
|
dbus_error_init(runnable->mError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_pending_call_cancel(aCall);
|
|
|
|
dbus_pending_call_unref(aCall);
|
|
|
|
|
|
|
|
runnable->Completed(success);
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:05:50 -08:00
|
|
|
public:
|
|
|
|
DBusConnectionSendAndBlockRunnable(DBusConnection* aConnection,
|
|
|
|
DBusMessage* aMessage,
|
|
|
|
int aTimeout,
|
|
|
|
DBusError* aError)
|
|
|
|
: DBusConnectionSendSyncRunnable(aConnection, aMessage),
|
|
|
|
mError(aError),
|
2013-03-11 07:22:56 -07:00
|
|
|
mReply(nullptr),
|
|
|
|
mTimeout(aTimeout)
|
2013-03-01 06:05:50 -08:00
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2013-03-11 07:22:56 -07:00
|
|
|
DBusPendingCall* call = nullptr;
|
2013-03-01 06:05:50 -08:00
|
|
|
|
2013-03-11 07:22:56 -07:00
|
|
|
dbus_bool_t success = dbus_connection_send_with_reply(mConnection,
|
|
|
|
mMessage,
|
|
|
|
&call,
|
|
|
|
mTimeout);
|
|
|
|
if (!success) {
|
|
|
|
if (mError) {
|
|
|
|
if (!call) {
|
|
|
|
dbus_set_error(mError, DBUS_ERROR_DISCONNECTED, "Connection is closed");
|
|
|
|
} else {
|
|
|
|
dbus_error_init(mError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto done;
|
2013-03-01 06:05:50 -08:00
|
|
|
}
|
|
|
|
|
2013-03-11 07:22:56 -07:00
|
|
|
success = dbus_pending_call_set_notify(call, Notify, this, nullptr);
|
2013-03-01 06:05:50 -08:00
|
|
|
|
2013-03-11 07:22:56 -07:00
|
|
|
done:
|
|
|
|
dbus_message_unref(mMessage);
|
2013-03-01 06:05:50 -08:00
|
|
|
|
2013-03-11 07:22:56 -07:00
|
|
|
if (!success) {
|
|
|
|
Completed(false);
|
|
|
|
NS_ENSURE_TRUE(success == TRUE, NS_ERROR_FAILURE);
|
|
|
|
}
|
2013-03-01 06:05:50 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBusMessage* GetReply()
|
|
|
|
{
|
|
|
|
return mReply;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~DBusConnectionSendAndBlockRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DBusError* mError;
|
|
|
|
DBusMessage* mReply;
|
2013-03-11 07:22:56 -07:00
|
|
|
int mTimeout;
|
2013-03-01 06:05:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
dbus_bool_t
|
|
|
|
dbus_func_send_and_block(DBusConnection* aConnection,
|
|
|
|
int aTimeout,
|
|
|
|
DBusMessage** aReply,
|
|
|
|
DBusError* aError,
|
|
|
|
DBusMessage* aMessage)
|
|
|
|
{
|
|
|
|
nsRefPtr<DBusConnectionSendAndBlockRunnable> t(
|
|
|
|
new DBusConnectionSendAndBlockRunnable(aConnection, aMessage,
|
|
|
|
aTimeout, aError));
|
|
|
|
MOZ_ASSERT(t);
|
|
|
|
|
|
|
|
nsresult rv = DispatchToDBusThread(t);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (aMessage) {
|
|
|
|
dbus_message_unref(aMessage);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!t->WaitForCompletion()) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aReply) {
|
|
|
|
*aReply = t->GetReply();
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-08-23 12:51:44 -07:00
|
|
|
// If err is nullptr, then any errors will be LOG'd, and free'd and the reply
|
|
|
|
// will be nullptr.
|
|
|
|
// If err is not nullptr, then it is assumed that dbus_error_init was already
|
2012-06-02 11:23:16 -07:00
|
|
|
// called, and error's will be returned to the caller without logging. The
|
2013-08-23 12:51:44 -07:00
|
|
|
// return value is nullptr iff an error was set. The client must free the
|
|
|
|
// error if set.
|
2013-03-01 06:05:50 -08:00
|
|
|
DBusMessage* dbus_func_args_timeout_valist(DBusConnection* conn,
|
|
|
|
int timeout_ms,
|
|
|
|
DBusError* err,
|
|
|
|
const char* path,
|
|
|
|
const char* ifc,
|
|
|
|
const char* func,
|
|
|
|
int first_arg_type,
|
|
|
|
va_list args)
|
|
|
|
{
|
2012-06-02 11:23:16 -07:00
|
|
|
/* Compose the command */
|
2013-03-01 06:05:50 -08:00
|
|
|
DBusMessage* msg = dbus_message_new_method_call(BLUEZ_DBUS_BASE_IFC, path, ifc, func);
|
2012-06-02 11:23:16 -07:00
|
|
|
|
2013-03-01 06:05:50 -08:00
|
|
|
if (!msg) {
|
2012-06-02 11:23:16 -07:00
|
|
|
LOG("Could not allocate D-Bus message object!");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* append arguments */
|
|
|
|
if (!dbus_message_append_args_valist(msg, first_arg_type, args)) {
|
|
|
|
LOG("Could not append argument to method call!");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:05:50 -08:00
|
|
|
DBusMessage* reply;
|
|
|
|
|
|
|
|
return dbus_func_send_and_block(conn, timeout_ms, &reply, err, msg) ? reply : nullptr;
|
2012-06-02 11:23:16 -07:00
|
|
|
|
|
|
|
done:
|
2013-03-01 06:05:50 -08:00
|
|
|
if (msg) {
|
|
|
|
dbus_message_unref(msg);
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
2013-03-01 06:05:50 -08:00
|
|
|
return nullptr;
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DBusMessage * dbus_func_args_timeout(DBusConnection *conn,
|
|
|
|
int timeout_ms,
|
2012-08-08 10:46:39 -07:00
|
|
|
DBusError* err,
|
2012-06-02 11:23:16 -07:00
|
|
|
const char *path,
|
|
|
|
const char *ifc,
|
|
|
|
const char *func,
|
|
|
|
int first_arg_type,
|
|
|
|
...) {
|
|
|
|
DBusMessage *ret;
|
|
|
|
va_list lst;
|
|
|
|
va_start(lst, first_arg_type);
|
2012-08-08 10:46:39 -07:00
|
|
|
ret = dbus_func_args_timeout_valist(conn, timeout_ms, err,
|
2012-06-02 11:23:16 -07:00
|
|
|
path, ifc, func,
|
|
|
|
first_arg_type, lst);
|
|
|
|
va_end(lst);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBusMessage * dbus_func_args(DBusConnection *conn,
|
|
|
|
const char *path,
|
|
|
|
const char *ifc,
|
|
|
|
const char *func,
|
|
|
|
int first_arg_type,
|
|
|
|
...) {
|
|
|
|
DBusMessage *ret;
|
|
|
|
va_list lst;
|
|
|
|
va_start(lst, first_arg_type);
|
2013-08-23 12:51:44 -07:00
|
|
|
ret = dbus_func_args_timeout_valist(conn, -1, nullptr,
|
2012-06-02 11:23:16 -07:00
|
|
|
path, ifc, func,
|
|
|
|
first_arg_type, lst);
|
|
|
|
va_end(lst);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBusMessage * dbus_func_args_error(DBusConnection *conn,
|
|
|
|
DBusError *err,
|
|
|
|
const char *path,
|
|
|
|
const char *ifc,
|
|
|
|
const char *func,
|
|
|
|
int first_arg_type,
|
|
|
|
...) {
|
|
|
|
DBusMessage *ret;
|
|
|
|
va_list lst;
|
|
|
|
va_start(lst, first_arg_type);
|
|
|
|
ret = dbus_func_args_timeout_valist(conn, -1, err,
|
|
|
|
path, ifc, func,
|
|
|
|
first_arg_type, lst);
|
|
|
|
va_end(lst);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-01 06:05:50 -08:00
|
|
|
int dbus_returns_int32(DBusMessage *reply)
|
2012-08-07 21:19:07 -07:00
|
|
|
{
|
|
|
|
DBusError err;
|
2012-10-01 00:04:01 -07:00
|
|
|
int32_t ret = -1;
|
2012-08-07 21:19:07 -07:00
|
|
|
|
|
|
|
dbus_error_init(&err);
|
|
|
|
if (!dbus_message_get_args(reply, &err,
|
|
|
|
DBUS_TYPE_INT32, &ret,
|
|
|
|
DBUS_TYPE_INVALID)) {
|
|
|
|
LOG_AND_FREE_DBUS_ERROR_WITH_MSG(&err, reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-09 00:55:08 -07:00
|
|
|
void DBusReplyHandler::Callback(DBusMessage* aReply, void* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
|
|
|
|
nsRefPtr<DBusReplyHandler> handler =
|
|
|
|
already_AddRefed<DBusReplyHandler>(static_cast<DBusReplyHandler*>(aData));
|
|
|
|
|
|
|
|
handler->Handle(aReply);
|
|
|
|
}
|
|
|
|
|
2012-05-07 14:50:25 -07:00
|
|
|
}
|
|
|
|
}
|