From 9e99b6aadda65550dd1b52ab151a9c98fbde2b29 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 19 Dec 2009 00:44:22 -0800 Subject: [PATCH] Add another D-Bus helper for properties with arrays --- include/dbus.h | 2 ++ src/dbus.c | 63 ++++++++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/include/dbus.h b/include/dbus.h index 09a0a6ce..d3e9a82e 100644 --- a/include/dbus.h +++ b/include/dbus.h @@ -51,6 +51,8 @@ DBusConnection *connman_dbus_get_connection(void); void connman_dbus_property_append_variant(DBusMessageIter *property, const char *key, int type, void *val); +void connman_dbus_property_append_variable_array(DBusMessageIter *dict, + const char *key, int type, connman_dbus_append_cb_t function); static inline void connman_dbus_dict_open(DBusMessageIter *iter, DBusMessageIter *dict) diff --git a/src/dbus.c b/src/dbus.c index c025979a..1b592d36 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -118,6 +118,39 @@ void connman_dbus_property_append_variant(DBusMessageIter *iter, dbus_message_iter_close_container(iter, &value); } +void connman_dbus_property_append_variable_array(DBusMessageIter *iter, + const char *key, int type, connman_dbus_append_cb_t function) +{ + DBusMessageIter value, array; + const char *variant_sig, *array_sig; + + switch (type) { + case DBUS_TYPE_STRING: + variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING; + array_sig = DBUS_TYPE_STRING_AS_STRING; + break; + case DBUS_TYPE_OBJECT_PATH: + variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING; + array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING; + break; + default: + return; + } + + dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key); + + dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, + variant_sig, &value); + + dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY, + array_sig, &array); + if (function) + function(&array); + dbus_message_iter_close_container(&value, &array); + + dbus_message_iter_close_container(iter, &value); +} + void connman_dbus_dict_append_variant(DBusMessageIter *dict, const char *key, int type, void *val) { @@ -167,37 +200,13 @@ void connman_dbus_dict_append_fixed_array(DBusMessageIter *dict, void connman_dbus_dict_append_variable_array(DBusMessageIter *dict, const char *key, int type, connman_dbus_append_cb_t function) { - DBusMessageIter entry, value, array; - const char *variant_sig, *array_sig; - - switch (type) { - case DBUS_TYPE_STRING: - variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING; - array_sig = DBUS_TYPE_STRING_AS_STRING; - break; - case DBUS_TYPE_OBJECT_PATH: - variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING; - array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING; - break; - default: - return; - } + DBusMessageIter entry; dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL, &entry); - dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key); - - dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, - variant_sig, &value); - - dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY, - array_sig, &array); - if (function) - function(&array); - dbus_message_iter_close_container(&value, &array); - - dbus_message_iter_close_container(&entry, &value); + connman_dbus_property_append_variable_array(&entry, key, + type, function); dbus_message_iter_close_container(dict, &entry); } -- 2.34.1