2009-04-21 01:27:01

by yesir yao

[permalink] [raw]
Subject: dbus of c program for adapter get/set properties

hi, all:

I need to use dbus api to control my bluetooth adapter in my C program.
After I had read doc/adapter-api.txt and writed the codes, It can't work.

my question is:
what is the different between:
dbus_connection_send_with_reply_and_block
and
dbus_connection_send_with_reply ?

in src/adapter.c, I found two methods defined:
static GDBusMethodTable adapter_methods[] = {
{ "GetProperties", "", "a{sv}", get_properties },
{ "SetProperty", "sv", "", set_property,
G_DBUS_METHOD_FLAG_ASYNC },

For Methods dict GetProperties(), we should use
dbus_connection_send_with_reply_and_block ?
For Methods void SetProperty(..), we should use
dbus_connection_send_with_reply ?
is it right or not? (because of the flag: G_DBUS_METHOD_FLAG_ASYNC ?

===================================
static LRESULT OnAdapterSetDiscov()
{
DBusConnection *conn;
DBusMessage *dmsg, *reply;
DBusError err;
int bOn = TRUE;
const char *PropName = "Discoverable";

conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
if (!conn) {
print("Can't get on session bus");
exit(1);
}

dmsg = dbus_message_new_method_call("org.bluez", "/org/bluez/hci0",
"org.bluez.Adapter", "SetProperty");
if (!dmsg) {
print("Can't allocate new method call");
return -1;
}

dbus_message_append_args(dmsg, DBUS_TYPE_STRING, &PropName,
DBUS_TYPE_BOOLEAN, &bOn,
DBUS_TYPE_INVALID);
#if 1
// can't SetProperty of Discoverable
if (dbus_connection_send_with_reply(conn, dmsg, NULL, -1) == FALSE) {
print("D-Bus send failed");
return (LRESULT)0;
}

#else
// print error: Method "SetProperty" with signature "sb" on interface
" org.bluez.Adapter" doesn't exist

dbus_error_init(&err);
reply = dbus_connection_send_with_reply_and_block(conn, dmsg, -1, &err);
if (!reply) {
if (dbus_error_is_set(&err)) {
print("%s", err.message);
dbus_error_free(&err);
}
return (LRESULT)0;
}
#endif

dbus_message_unref(dmsg);
dbus_message_unref(reply);
dbus_connection_flush(conn);

return (LRESULT)0;
}


2009-04-23 04:52:20

by yesir yao

[permalink] [raw]
Subject: Re: dbus of c program for adapter get/set properties

>>
>> I need to use dbus api to control my bluetooth adapter in my C program.
>> After I had read doc/adapter-api.txt and writed the codes, It can't work
>
> The answer is very simple, you are using fixed/hardcoded object path
> (/org/bluez/hci0) that is causing the problem. We made sure it won't
> work by adding the pid to the path so no one will have a change to
> hardcode an adapter path and the code still work after bluetoothd has
> restarted, the correct way of doing this is via '/' path (the only
> fixed path bluetoothd has) which implements org.bluez.Manager (see
> doc/manager-api.txt).
>

I have changed the code of bluez/src/manger.c,
and delete "getpid()" in func manager_init.
so the fixed object path (/org/bluez/hci0) works well.


> About the G_DBUS_METHOD_FLAG_ASYNC are really not for clients,
> although they hint the behavior of the daemon the client should know
> better whether to block or not.


the siample question is:
1) for dbus_connection_send_with_reply_and_block and
dbus_connection_send_with_reply,
which one I should use for the sample code in the first email ?

2) for func dbus_message_append_args, the parm I had set is right or not?

thx a lot for you helps.

2009-04-21 02:50:26

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: dbus of c program for adapter get/set properties

Hi

On Mon, Apr 20, 2009 at 10:27 PM, yesir yao <[email protected]> wrote:
> hi, all:
>
> I need to use dbus api to control my bluetooth adapter in my C program.
> After I had read doc/adapter-api.txt and writed the codes, It can't work

The answer is very simple, you are using fixed/hardcoded object path
(/org/bluez/hci0) that is causing the problem. We made sure it won't
work by adding the pid to the path so no one will have a change to
hardcode an adapter path and the code still work after bluetoothd has
restarted, the correct way of doing this is via '/' path (the only
fixed path bluetoothd has) which implements org.bluez.Manager (see
doc/manager-api.txt).

About the G_DBUS_METHOD_FLAG_ASYNC are really not for clients,
although they hint the behavior of the daemon the client should know
better whether to block or not.

ps: check this http://smcv.pseudorandom.co.uk/2008/11/nonblocking/


--
Luiz Augusto von Dentz
Engenheiro de Computa??o