linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lucas De Marchi <lucas.demarchi@profusion.mobi>
To: linux-bluetooth@vger.kernel.org
Cc: Lucas De Marchi <lucas.demarchi@profusion.mobi>
Subject: [PATCH BlueZ v4 06/15] gdbus: Implement DBus.Properties.Set method
Date: Thu,  4 Oct 2012 04:26:30 -0300	[thread overview]
Message-ID: <1349335599-12443-7-git-send-email-lucas.de.marchi@gmail.com> (raw)
In-Reply-To: <1349335599-12443-1-git-send-email-lucas.de.marchi@gmail.com>

From: Lucas De Marchi <lucas.demarchi@profusion.mobi>

Contrary to Get() and GetAll(), Set() is asynchronous so we pass an id
to the setter so later it can declare the Set() as successful or
otherwise.
---
 gdbus/gdbus.h  |  16 +++++++
 gdbus/object.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 157 insertions(+), 1 deletion(-)

diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h
index b2e78c4..ec7b1ef 100644
--- a/gdbus/gdbus.h
+++ b/gdbus/gdbus.h
@@ -69,6 +69,12 @@ typedef DBusMessage * (* GDBusMethodFunction) (DBusConnection *connection,
 typedef gboolean (*GDBusPropertyGetter)(const GDBusPropertyTable *property,
 					DBusMessageIter *iter, void *data);
 
+typedef guint32 GDBusPendingPropertySet;
+
+typedef void (*GDBusPropertySetter)(const GDBusPropertyTable *property,
+			DBusMessageIter *value, GDBusPendingPropertySet id,
+			void *data);
+
 typedef gboolean (*GDBusPropertyExists)(const GDBusPropertyTable *property,
 								void *data);
 
@@ -123,6 +129,7 @@ struct GDBusPropertyTable {
 	const char *name;
 	const char *type;
 	GDBusPropertyGetter get;
+	GDBusPropertySetter set;
 	GDBusPropertyExists exists;
 	GDBusPropertyFlags flags;
 };
@@ -239,6 +246,15 @@ guint g_dbus_add_signal_watch(DBusConnection *connection,
 gboolean g_dbus_remove_watch(DBusConnection *connection, guint tag);
 void g_dbus_remove_all_watches(DBusConnection *connection);
 
+void g_dbus_pending_property_success(DBusConnection *connection,
+						GDBusPendingPropertySet id);
+void g_dbus_pending_property_error_valist(DBusConnection *connection,
+					GDBusPendingReply id, const char *name,
+					const char *format, va_list args);
+void g_dbus_pending_property_error(DBusConnection *connection,
+					GDBusPendingReply id, const char *name,
+					const char *format, ...);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/gdbus/object.c b/gdbus/object.c
index c6e4a53..4509f76 100644
--- a/gdbus/object.c
+++ b/gdbus/object.c
@@ -59,6 +59,11 @@ struct security_data {
 	void *iface_user_data;
 };
 
+struct property_data {
+	GDBusPendingPropertySet id;
+	DBusMessage *message;
+};
+
 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
 						const char *direction)
 {
@@ -380,6 +385,79 @@ static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
 	return FALSE;
 }
 
+static GDBusPendingPropertySet next_pending_property = 1;
+static GSList *pending_property_set;
+
+static struct property_data *remove_pending_property_data(
+						GDBusPendingPropertySet id)
+{
+	struct property_data *propdata;
+	GSList *l;
+
+        for (l = pending_property_set; l != NULL; l = l->next) {
+		propdata = l->data;
+		if (propdata->id != id)
+			continue;
+	}
+
+	if (l == NULL)
+		return NULL;
+
+	pending_property_set = g_slist_delete_link(pending_property_set, l);
+
+	return propdata;
+}
+
+void g_dbus_pending_property_success(DBusConnection *connection,
+						GDBusPendingPropertySet id)
+{
+	struct property_data *propdata;
+
+	propdata = remove_pending_property_data(id);
+	if (propdata == NULL)
+		return;
+
+	g_dbus_send_reply(connection, propdata->message, DBUS_TYPE_INVALID);
+	dbus_message_unref(propdata->message);
+	g_free(propdata);
+}
+
+void g_dbus_pending_property_error_valist(DBusConnection *connection,
+					GDBusPendingReply id, const char *name,
+					const char *format, va_list args)
+{
+	struct property_data *propdata;
+	DBusMessage *reply;
+
+	propdata = remove_pending_property_data(id);
+	if (propdata == NULL)
+		return;
+
+	reply = g_dbus_create_error_valist(propdata->message, name, format,
+									args);
+	if (reply != NULL) {
+		dbus_connection_send(connection, reply, NULL);
+		dbus_message_unref(reply);
+	}
+
+	dbus_message_unref(propdata->message);
+	g_free(propdata);
+}
+
+void g_dbus_pending_property_error(DBusConnection *connection,
+					GDBusPendingReply id, const char *name,
+					const char *format, ...)
+{
+	va_list args;
+
+	va_start(args, format);
+
+	g_dbus_pending_property_error_valist(connection, id, name, format,
+									args);
+
+	va_end(args);
+}
+
 static void generic_unregister(DBusConnection *connection, void *user_data)
 {
 	struct generic_data *data = user_data;
@@ -636,7 +714,69 @@ static DBusMessage *properties_get_all(DBusConnection *connection,
 static DBusMessage *properties_set(DBusConnection *connection,
 					DBusMessage *message, void *user_data)
 {
-	return dbus_message_new_method_return(message);
+	struct generic_data *data = user_data;
+	DBusMessageIter iter, sub;
+	struct interface_data *iface;
+	const GDBusPropertyTable *property;
+	const char *name, *interface;
+	struct property_data *propdata;
+
+	if (!dbus_message_iter_init(message, &iter))
+		return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
+							"No arguments given");
+
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
+		return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
+					"Invalid argument type: '%c'",
+					dbus_message_iter_get_arg_type(&iter));
+
+	dbus_message_iter_get_basic(&iter, &name);
+	dbus_message_iter_next(&iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
+		return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
+					"Invalid argument type: '%c'",
+					dbus_message_iter_get_arg_type(&iter));
+
+	dbus_message_iter_get_basic(&iter, &interface);
+	dbus_message_iter_next(&iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
+		return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
+					"Invalid argument type: '%c'",
+					dbus_message_iter_get_arg_type(&iter));
+
+	dbus_message_iter_recurse(&iter, &sub);
+
+	iface = find_interface(data->interfaces, interface);
+	if (iface == NULL)
+		return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
+					"No such interface '%s'", interface);
+
+	property = find_property(iface->properties, name);
+	if (property == NULL)
+		return g_dbus_create_error(message,
+						DBUS_ERROR_UNKNOWN_PROPERTY,
+						"No such property '%s'", name);
+
+	if (property->set == NULL)
+		return g_dbus_create_error(message,
+					DBUS_ERROR_PROPERTY_READ_ONLY,
+					"Property '%s' is not writable", name);
+
+	if (property->exists != NULL &&
+			!property->exists(property, iface->user_data))
+		return g_dbus_create_error(message,
+						DBUS_ERROR_UNKNOWN_PROPERTY,
+						"No such property '%s'", name);
+
+	propdata = g_new(struct property_data, 1);
+	propdata->id = next_pending_property++;
+	propdata->message = dbus_message_ref(message);
+
+	property->set(property, &sub, propdata->id, iface->user_data);
+
+	return NULL;
 }
 
 static const GDBusMethodTable properties_methods[] = {
-- 
1.7.12.2


  parent reply	other threads:[~2012-10-04  7:26 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-04  7:26 [PATCH BlueZ v4 00/15] Properties + ObjectManager Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 01/15] gdbus: Move typedefs up Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 02/15] gdbus: Use macros to add annotations Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 03/15] gdbus: Add skeleton of DBus.Properties interface Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 04/15] gdbus: Implement DBus.Properties.Get method Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 05/15] gdbus: Implement DBus.Properties.GetAll method Lucas De Marchi
2012-10-04  7:26 ` Lucas De Marchi [this message]
2012-10-04  7:26 ` [PATCH BlueZ v4 07/15] gdbus: Add properties into Introspectable interface Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 08/15] gdbus: Add support for org.freedesktop.DBus.ObjectManager interface Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 09/15] gdbus: Group interface changes to reduce the amount of signals emitted Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 10/15] gdbus: Only export ObjectManager interface on root path Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 11/15] gdbus: Integrates ObjectManager with Properties interface Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 12/15] gdbus: Simplify code for appending properties Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 13/15] gdbus: Implement PropertiesChanged signal Lucas De Marchi
2012-10-04  7:26 ` [PATCH BlueZ v4 14/15] Use DBus.Properties on Control interface Lucas De Marchi
2012-10-04 11:42   ` Anderson Lizardo
2012-10-04 14:22     ` Johan Hedberg
2012-10-04  7:26 ` [PATCH BlueZ v4 15/15] Use DBus.Properties on Manager interface Lucas De Marchi
2012-10-04 14:17 ` [PATCH BlueZ v4 00/15] Properties + ObjectManager Johan Hedberg
2012-10-04 14:29   ` Lucas De Marchi
2012-10-04 18:11     ` Marcel Holtmann
2012-10-04 18:27       ` Lucas De Marchi
2012-10-04 19:15         ` Marcel Holtmann
2012-10-04 19:23           ` Lucas De Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1349335599-12443-7-git-send-email-lucas.de.marchi@gmail.com \
    --to=lucas.demarchi@profusion.mobi \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).