From ffddf23a6b108454ae357aa8bdc2d8b23222dd79 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 19 Dec 2009 13:03:19 -0800 Subject: [PATCH] Add helpers for adding dictionaries as property --- include/dbus.h | 18 +++++++++++++++++- src/dbus.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/include/dbus.h b/include/dbus.h index d8a1854..ab6cc07 100644 --- a/include/dbus.h +++ b/include/dbus.h @@ -48,8 +48,10 @@ typedef void (* connman_dbus_append_cb_t) (DBusMessageIter *iter); DBusConnection *connman_dbus_get_connection(void); -void connman_dbus_property_append_variant(DBusMessageIter *property, +void connman_dbus_property_append_variant(DBusMessageIter *iter, const char *key, int type, void *val); +void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key, + connman_dbus_append_cb_t function, void *user_data); void connman_dbus_property_append_fixed_array(DBusMessageIter *iter, const char *key, int type, void *val, int len); void connman_dbus_property_append_variable_array(DBusMessageIter *dict, @@ -58,6 +60,9 @@ void connman_dbus_property_append_variable_array(DBusMessageIter *dict, dbus_bool_t connman_dbus_property_changed_basic(const char *path, const char *interface, const char *key, int type, void *val); +dbus_bool_t connman_dbus_property_changed_dict(const char *path, + const char *interface, const char *key, + connman_dbus_append_cb_t function, void *user_data); static inline void connman_dbus_dict_open(DBusMessageIter *iter, DBusMessageIter *dict) @@ -85,6 +90,17 @@ static inline void connman_dbus_dict_append_variant(DBusMessageIter *dict, dbus_message_iter_close_container(dict, &entry); } +static inline void connman_dbus_dict_append_dict(DBusMessageIter *dict, + const char *key, connman_dbus_append_cb_t function) +{ + DBusMessageIter entry; + + dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, + NULL, &entry); + connman_dbus_property_append_dict(&entry, key, function, NULL); + dbus_message_iter_close_container(dict, &entry); +} + static inline void connman_dbus_dict_append_fixed_array(DBusMessageIter *dict, const char *key, int type, void *val, int len) { diff --git a/src/dbus.c b/src/dbus.c index d67b0f8..b2fb27b 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -118,6 +118,27 @@ void connman_dbus_property_append_variant(DBusMessageIter *iter, dbus_message_iter_close_container(iter, &value); } +void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key, + connman_dbus_append_cb_t function, void *user_data) +{ + DBusMessageIter value, dict; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key); + + dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, + DBUS_TYPE_ARRAY_AS_STRING + DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING + DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &value); + + connman_dbus_dict_open(&value, &dict); + if (function) + function(&dict); + connman_dbus_dict_close(&value, &dict); + + dbus_message_iter_close_container(iter, &value); +} + void connman_dbus_property_append_fixed_array(DBusMessageIter *iter, const char *key, int type, void *val, int len) { @@ -203,6 +224,28 @@ dbus_bool_t connman_dbus_property_changed_basic(const char *path, return TRUE; } +dbus_bool_t connman_dbus_property_changed_dict(const char *path, + const char *interface, const char *key, + connman_dbus_append_cb_t function, void *user_data) +{ + DBusMessage *signal; + DBusMessageIter iter; + + if (path == NULL) + return FALSE; + + signal = dbus_message_new_signal(path, interface, "PropertyChanged"); + if (signal == NULL) + return FALSE; + + dbus_message_iter_init_append(signal, &iter); + connman_dbus_property_append_dict(&iter, key, function, user_data); + + g_dbus_send_message(connection, signal); + + return TRUE; +} + DBusConnection *connman_dbus_get_connection(void) { if (connection == NULL) -- 2.7.4