cdbusbluezgdbus

What gdbus function returns a list of child object nodes?


I am able to introspect a DBus node, and get some XML which includes information about the child nodes. However, this requires me to parse XML, and I'm trying to keep the application lightweight. What gdbus function can I use to simply get a list of child node object names?

Here is the code that fetches the XML.

#include <stdio.h>
#include <stdlib.h>
#include <gio/gio.h>

int main(int argc,char *argv[])
{
GError *err=NULL;
GVariant *result;
GDBusConnection *c;
const char *xml;

    if ((c = g_bus_get_sync(G_BUS_TYPE_SYSTEM,NULL,&err)) == NULL) {
        if (err) fprintf(stderr,"g_bus_get error: %s\n",err->message);
        exit(1);
    } /* if */
    result = g_dbus_connection_call_sync(c,"org.bluez","/org/bluez",
                        "org.freedesktop.DBus.Introspectable",
                        "Introspect",NULL,G_VARIANT_TYPE("(s)"),
                        G_DBUS_CALL_FLAGS_NONE,3000,NULL,&err);
    if (result==NULL) {
        if (err) fprintf(stderr,"gbus_connection_call error: %s\n",
            err->message);
        exit(1);
    } /* if */
    g_variant_get(result,"(&s)",&xml);
    printf("%s\n",xml);
    exit(0);
}

So the above code works. Deep down in the returned XML there are elements describing the children of the org.bluez object node. In my case there is an element like this:

<node name="hci0"></node>.

However, I don't want to parse the XML to find this. What other gdbus function can be used to simply retrieve the names of the children of the org.bluez, without requiring an XML parser?


Solution

  • I think your best bet is to use the built-in XML parser. It's how the gdbus introspect command line tool is implemented.

    Call the g_dbus_node_info_new_for_xml function to parse the XML. This give you back a GDBusNodeInfo, which you must free with g_dbus_node_info_unref(). The best example I can find of how to use it is here, which parses the XML, then loops through the nodes element of the struct that's returned.