From d3742ba65124dbee474aecab22b4eda597f3ab7f Mon Sep 17 00:00:00 2001 From: Tim Kourt Date: Wed, 25 Jul 2018 12:54:39 -0700 Subject: [PATCH] client: add property setter for proxy v2: renamed builder_append_value_variant -> append --- client/dbus-proxy.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ client/dbus-proxy.h | 5 ++++ 2 files changed, 73 insertions(+) diff --git a/client/dbus-proxy.c b/client/dbus-proxy.c index 402cf7b3..8e1eaafd 100644 --- a/client/dbus-proxy.c +++ b/client/dbus-proxy.c @@ -175,6 +175,74 @@ char *proxy_property_str_completion(const struct proxy_interface_type *type, return NULL; } +static const struct proxy_interface_property *proxy_property_find( + const struct proxy_interface_property *types, + const char *name) +{ + size_t i; + + for (i = 0; types[i].name; i++) { + if (strcmp(types[i].name, name)) + continue; + + return &types[i]; + } + + return NULL; +} + +bool proxy_property_set(const struct proxy_interface *proxy, const char *name, + const char *value_str, l_dbus_message_func_t callback) +{ + struct l_dbus_message_builder *builder; + struct l_dbus_message *msg; + const struct proxy_interface_property *property; + + if (!proxy || !name) + return false; + + property = proxy_property_find(proxy->type->properties, name); + if (!property) + return false; + + if (!property->is_read_write) + return false; + + if (!property->append) + return false; + + msg = l_dbus_message_new_method_call(dbus, IWD_SERVICE, proxy->path, + L_DBUS_INTERFACE_PROPERTIES, + "Set"); + if (!msg) + return false; + + + builder = l_dbus_message_builder_new(msg); + if (!builder) { + l_dbus_message_unref(msg); + return false; + } + + l_dbus_message_builder_append_basic(builder, 's', + proxy->type->interface); + l_dbus_message_builder_append_basic(builder, 's', property->name); + l_dbus_message_builder_enter_variant(builder, property->type); + + if (!property->append(builder, value_str)) { + l_dbus_message_unref(msg); + return false; + } + + l_dbus_message_builder_leave_variant(builder); + l_dbus_message_builder_finalize(builder); + l_dbus_message_builder_destroy(builder); + + l_dbus_send_with_reply(dbus, msg, callback, (void *) proxy, NULL); + + return true; +} + bool dbus_message_has_error(struct l_dbus_message *message) { const char *name; diff --git a/client/dbus-proxy.h b/client/dbus-proxy.h index e851e2da..955ad2ff 100644 --- a/client/dbus-proxy.h +++ b/client/dbus-proxy.h @@ -38,6 +38,8 @@ struct proxy_interface_property { void (*update)(void *data, struct l_dbus_message_iter *variant); const char *(*tostr)(const void *data); const bool is_read_write; + bool (*append)(struct l_dbus_message_builder *builder, + const char *value_str); }; struct proxy_interface_type_ops { @@ -57,6 +59,9 @@ struct proxy_interface_type { const struct proxy_interface_type_ops *ops; }; +bool proxy_property_set(const struct proxy_interface *proxy, const char *name, + const char *value_str, l_dbus_message_func_t callback); + struct proxy_interface *proxy_interface_find(const char *interface, const char *path);