| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gio/gio.h> |
| 5 |
|
| 6 |
#include "helpers.h" |
| 7 |
|
| 8 |
GDBusConnection *gpiodbus_test_get_dbus_connection(void) |
| 9 |
{ |
| 10 |
g_autoptr(GDBusConnection) con = NULL; |
| 11 |
g_autofree gchar *addr = NULL; |
| 12 |
g_autoptr(GError) err = NULL; |
| 13 |
|
| 14 |
addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err); |
| 15 |
if (!addr) |
| 16 |
g_error("Failed to get address on the bus: %s", err->message); |
| 17 |
|
| 18 |
con = g_dbus_connection_new_for_address_sync(addr, |
| 19 |
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | |
| 20 |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, |
| 21 |
NULL, NULL, &err); |
| 22 |
if (!con) |
| 23 |
g_error("Failed to get system bus connection: %s", |
| 24 |
err->message); |
| 25 |
|
| 26 |
return g_object_ref(con); |
| 27 |
} |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
gboolean *added; |
| 31 |
gchar *obj_path; |
| 32 |
} OnObjectAddedData; |
| 33 |
|
| 34 |
static void on_object_added(GDBusObjectManager *manager G_GNUC_UNUSED, |
| 35 |
GpiodbusObject *object, gpointer data) |
| 36 |
{ |
| 37 |
OnObjectAddedData *cb_data = data; |
| 38 |
const gchar *path; |
| 39 |
|
| 40 |
path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object)); |
| 41 |
|
| 42 |
if (g_strcmp0(path, cb_data->obj_path) == 0) |
| 43 |
*cb_data->added = TRUE; |
| 44 |
} |
| 45 |
|
| 46 |
static gboolean on_timeout(gpointer data G_GNUC_UNUSED) |
| 47 |
{ |
| 48 |
g_error("timeout reached waiting for the gpiochip interface to appear on the bus"); |
| 49 |
|
| 50 |
return G_SOURCE_REMOVE; |
| 51 |
} |
| 52 |
|
| 53 |
void gpiodbus_test_wait_for_sim_intf(GPIOSimChip *sim) |
| 54 |
{ |
| 55 |
g_autoptr(GDBusObjectManager) manager = NULL; |
| 56 |
g_autoptr(GDBusConnection) con = NULL; |
| 57 |
g_autoptr(GpiodbusObject) obj = NULL; |
| 58 |
g_autoptr(GError) err = NULL; |
| 59 |
g_autofree gchar *obj_path; |
| 60 |
OnObjectAddedData cb_data; |
| 61 |
gboolean added = FALSE; |
| 62 |
guint timeout_id; |
| 63 |
|
| 64 |
con = gpiodbus_test_get_dbus_connection(); |
| 65 |
if (!con) |
| 66 |
g_error("failed to obtain a bus connection: %s", err->message); |
| 67 |
|
| 68 |
obj_path = g_strdup_printf("/io/gpiod1/chips/%s", |
| 69 |
g_gpiosim_chip_get_name(sim)); |
| 70 |
|
| 71 |
cb_data.added = &added; |
| 72 |
cb_data.obj_path = obj_path; |
| 73 |
|
| 74 |
manager = gpiodbus_object_manager_client_new_sync(con, |
| 75 |
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, |
| 76 |
"io.gpiod1", "/io/gpiod1/chips", NULL, &err); |
| 77 |
if (!manager) |
| 78 |
g_error("failed to create the object manager client: %s", |
| 79 |
err->message); |
| 80 |
|
| 81 |
g_signal_connect(manager, "object-added", G_CALLBACK(on_object_added), |
| 82 |
&cb_data); |
| 83 |
|
| 84 |
obj = GPIODBUS_OBJECT(g_dbus_object_manager_get_object(manager, |
| 85 |
obj_path)); |
| 86 |
if (obj) { |
| 87 |
if (g_strcmp0(g_dbus_object_get_object_path(G_DBUS_OBJECT(obj)), |
| 88 |
obj_path) == 0) |
| 89 |
added = TRUE; |
| 90 |
} |
| 91 |
|
| 92 |
timeout_id = g_timeout_add_seconds(5, on_timeout, NULL); |
| 93 |
|
| 94 |
while (!added) |
| 95 |
g_main_context_iteration(NULL, TRUE); |
| 96 |
|
| 97 |
g_source_remove(timeout_id); |
| 98 |
} |
| 99 |
|
| 100 |
GVariant *gpiodbus_test_make_empty_request_config(void) |
| 101 |
{ |
| 102 |
GVariantBuilder builder; |
| 103 |
|
| 104 |
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}")); |
| 105 |
|
| 106 |
return g_variant_ref_sink(g_variant_builder_end(&builder)); |
| 107 |
} |