Skip to content

Instantly share code, notes, and snippets.

@qwerty12
Created June 7, 2012 19:40
Show Gist options
  • Save qwerty12/2891109 to your computer and use it in GitHub Desktop.
Save qwerty12/2891109 to your computer and use it in GitHub Desktop.
Non-working GDBus BlueZ thing
//gcc -Wall bztest.c $(pkg-config --cflags --libs gio-2.0)
#include <stdlib.h>
#include <gio/gio.h>
#define NOPE(obj) \
G_STMT_START \
if (!obj) \
{ \
retval = EXIT_FAILURE; \
goto exit; \
} \
G_STMT_END
int main (void) {
int retval = EXIT_SUCCESS;
g_type_init();
GDBusConnection *sys = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
NOPE(sys);
GVariant *adapter_tuple = g_dbus_connection_call_sync(sys,
"org.bluez",
"/",
"org.bluez.Manager",
"DefaultAdapter",
NULL,
((const GVariantType *) "(o)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL);
NOPE(adapter_tuple);
GVariant *adapter = g_variant_get_child_value(adapter_tuple, 0);
NOPE(adapter);
GVariant *adapter_properties_tuple = g_dbus_connection_call_sync(sys,
"org.bluez",
g_variant_get_string(adapter, NULL),
"org.bluez.Adapter",
"GetProperties",
NULL,
((const GVariantType *) "(a{sv})"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL);
NOPE(adapter_properties_tuple);
GVariant *adapter_properties = g_variant_get_child_value(adapter_properties_tuple, 0);
NOPE(adapter_properties);
GVariant *registered_devices = g_variant_lookup_value(adapter_properties, "Devices", G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
NOPE(registered_devices);
gsize children = g_variant_n_children(registered_devices);
if (children > 0)
{
int i;
for (i = 0; i != children; ++i)
{
GVariant *dev = g_variant_get_child_value(registered_devices, i);
if (g_variant_is_of_type (dev, G_VARIANT_TYPE_OBJECT_PATH))
{
(void) g_dbus_connection_call_sync(sys,
"org.bluez",
g_variant_get_string(dev, NULL),
"org.bluez.Device",
"Disconnect",
NULL,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL);
}
g_variant_unref(dev);
}
}
(void) g_dbus_connection_call_sync(sys,
"org.bluez",
g_variant_get_string(adapter, NULL),
"org.bluez.Adapter",
"SetProperty",
g_variant_new("sv", "Powered", g_variant_new_variant(g_variant_new_boolean(FALSE))), //Possible leak: Check if g_variant_new_variant's floating reference is taken by g_variant_new
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL);
exit:
if (registered_devices)
g_variant_unref(registered_devices);
if (adapter_properties)
g_variant_unref(adapter_properties);
if (adapter_properties_tuple)
g_variant_unref(adapter_properties_tuple);
if (adapter)
g_variant_unref(adapter);
if (adapter_tuple)
g_variant_unref(adapter_tuple);
if (sys)
g_object_unref (sys);
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment