0

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?

deltamind106
  • 638
  • 7
  • 19

1 Answers1

1

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.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • Okay that's what I was assuming I could avoid. I figured that among the plethora of gdbus API functions, surely there would be a way to discover an object's child nodes without resorting to parsing a giant XML string, with piles of irrelevant data. If gdbus API designers decided that this should be the best-practice way to discover child nodes in a tree, then boy they must need a lesson in KISS design. – deltamind106 Jan 10 '19 at 14:37
  • Of course if this is the best way to discover child nodes, then that leads to the next question of the best-practice method for keeping updated as child nodes appear/vanish. Of course there is the signal_subscribe API, but this only tells you when new nodes appear (it doesn't tell you about pre-existing nodes). But that is another question, so I will ask it separately. – deltamind106 Jan 10 '19 at 14:41