commit 09b124f0734cea6da38fa539ec1c5c3b0c5aaf1e
parent 668b61ff3ab9d251ded59894b4c5ee783052cb5a
Author: James Prestwood <prestwoj@gmail.com>
Date: Tue, 12 Jan 2021 09:17:16 -0800
dbus: add helper for appending a dictionary
Arrays of dictionaries are quite common, and for basic
types this API makes things much more convenient by
putting all the enter/append/leave calls in one place.
Diffstat:
2 files changed, 29 insertions(+), 0 deletions(-)
diff --git a/src/dbus.c b/src/dbus.c
@@ -202,6 +202,32 @@ void dbus_pending_reply(struct l_dbus_message **msg,
*msg = NULL;
}
+/*
+ * Convenience helper for appending a dictionary "{sv}". This only works when
+ * the variant is a basic type.
+ */
+bool dbus_append_dict_basic(struct l_dbus_message_builder *builder,
+ const char *name, char type,
+ const void *data)
+{
+ char strtype[] = { type, '\0' };
+
+ if (!l_dbus_message_builder_enter_dict(builder, "sv"))
+ return false;
+ if (!l_dbus_message_builder_append_basic(builder, 's', name))
+ return false;
+ if (!l_dbus_message_builder_enter_variant(builder, strtype))
+ return false;
+ if (!l_dbus_message_builder_append_basic(builder, type, data))
+ return false;
+ if (!l_dbus_message_builder_leave_variant(builder))
+ return false;
+ if (!l_dbus_message_builder_leave_dict(builder))
+ return false;
+
+ return true;
+}
+
struct l_dbus *dbus_get_bus(void)
{
return g_dbus;
diff --git a/src/dbus.h b/src/dbus.h
@@ -51,6 +51,9 @@ struct l_dbus *dbus_get_bus(void);
void dbus_pending_reply(struct l_dbus_message **msg,
struct l_dbus_message *reply);
+bool dbus_append_dict_basic(struct l_dbus_message_builder *builder,
+ const char *name, char type,
+ const void *data);
const char *dbus_iftype_to_string(unsigned int iftype);