dbus: Define priv argument for object rather than for method

We don't actually need to define separate user_data argument for
each method handler and property getter/setter. Instead, we can define
one argument for the whole object. That will make it easier to register
objects like BSS or Networks which require allocating and freeing
memory for their arguments.
This commit is contained in:
Witold Sowa 2010-01-01 11:33:41 +02:00 committed by Jouni Malinen
parent d84c8f1413
commit 1fa5995b51
3 changed files with 104 additions and 105 deletions

View file

@ -942,16 +942,20 @@ struct wpas_dbus_signal {
static void wpas_dbus_register(struct wpa_dbus_object_desc *obj_desc, static void wpas_dbus_register(struct wpa_dbus_object_desc *obj_desc,
void *priv, void *priv,
WPADBusArgumentFreeFunction priv_free,
const struct wpas_dbus_method *methods, const struct wpas_dbus_method *methods,
const struct wpas_dbus_property *properties, const struct wpas_dbus_property *properties,
const struct wpas_dbus_signal *signals) const struct wpas_dbus_signal *signals)
{ {
int i; int i;
obj_desc->user_data = priv;
obj_desc->user_data_free_func = priv_free;
for (i = 0; methods && methods[i].name; i++) { for (i = 0; methods && methods[i].name; i++) {
wpa_dbus_method_register(obj_desc, methods[i].iface, wpa_dbus_method_register(obj_desc, methods[i].iface,
methods[i].name, methods[i].handler, methods[i].name, methods[i].handler,
priv, NULL, methods[i].args); methods[i].args);
} }
for (i = 0; properties && properties[i].name; i++) { for (i = 0; properties && properties[i].name; i++) {
@ -960,7 +964,7 @@ static void wpas_dbus_register(struct wpa_dbus_object_desc *obj_desc,
properties[i].type, properties[i].type,
properties[i].getter, properties[i].getter,
properties[i].setter, properties[i].setter,
priv, NULL, properties[i]._access); properties[i]._access);
} }
for (i = 0; signals && signals[i].name; i++) { for (i = 0; signals && signals[i].name; i++) {
@ -1059,7 +1063,8 @@ int wpas_dbus_ctrl_iface_init(struct wpas_dbus_priv *priv)
return -1; return -1;
} }
wpas_dbus_register(obj_desc, priv->global, wpas_dbus_global_methods, wpas_dbus_register(obj_desc, priv->global, NULL,
wpas_dbus_global_methods,
wpas_dbus_global_properties, wpas_dbus_global_properties,
wpas_dbus_global_signals); wpas_dbus_global_signals);
@ -1102,6 +1107,32 @@ static void wpa_dbus_free(void *ptr)
} }
static const struct wpas_dbus_property wpas_dbus_network_properties[] = {
{ "Properties", WPAS_DBUS_NEW_IFACE_NETWORK, "a{sv}",
(WPADBusPropertyAccessor) wpas_dbus_getter_network_properties,
(WPADBusPropertyAccessor) wpas_dbus_setter_network_properties,
RW
},
{ "Enabled", WPAS_DBUS_NEW_IFACE_NETWORK, "b",
(WPADBusPropertyAccessor) wpas_dbus_getter_enabled,
(WPADBusPropertyAccessor) wpas_dbus_setter_enabled,
RW
},
{ NULL, NULL, NULL, NULL, NULL, 0 }
};
static const struct wpas_dbus_signal wpas_dbus_network_signals[] = {
{ "PropertiesChanged", WPAS_DBUS_NEW_IFACE_NETWORK,
{
{ "properties", "a{sv}", ARG_OUT },
END_ARGS
}
},
{ NULL, NULL, { END_ARGS } }
};
/** /**
* wpas_dbus_register_network - Register a configured network with dbus * wpas_dbus_register_network - Register a configured network with dbus
* @wpa_s: wpa_supplicant interface structure * @wpa_s: wpa_supplicant interface structure
@ -1116,16 +1147,10 @@ static int wpas_dbus_register_network(struct wpa_supplicant *wpa_s,
struct wpas_dbus_priv *ctrl_iface; struct wpas_dbus_priv *ctrl_iface;
struct wpa_dbus_object_desc *obj_desc; struct wpa_dbus_object_desc *obj_desc;
struct network_handler_args *arg1 = NULL; struct network_handler_args *arg = NULL;
struct network_handler_args *arg2 = NULL;
char *net_obj_path; char *net_obj_path;
struct wpa_dbus_argument sargs[] = {
{ "properties", "a{sv}", ARG_OUT },
END_ARGS
};
/* Do nothing if the control interface is not turned on */ /* Do nothing if the control interface is not turned on */
if (wpa_s == NULL || wpa_s->global == NULL) if (wpa_s == NULL || wpa_s->global == NULL)
return 0; return 0;
@ -1150,45 +1175,19 @@ static int wpas_dbus_register_network(struct wpa_supplicant *wpa_s,
} }
/* allocate memory for handlers arguments */ /* allocate memory for handlers arguments */
arg1 = os_zalloc(sizeof(struct network_handler_args)); arg = os_zalloc(sizeof(struct network_handler_args));
if (!arg1) { if (!arg) {
wpa_printf(MSG_ERROR, "Not enough memory "
"to create arguments for method");
goto err;
}
arg2 = os_zalloc(sizeof(struct network_handler_args));
if (!arg2) {
wpa_printf(MSG_ERROR, "Not enough memory " wpa_printf(MSG_ERROR, "Not enough memory "
"to create arguments for method"); "to create arguments for method");
goto err; goto err;
} }
arg1->wpa_s = wpa_s; arg->wpa_s = wpa_s;
arg1->ssid = ssid; arg->ssid = ssid;
arg2->wpa_s = wpa_s;
arg2->ssid = ssid;
/* Enabled property */ wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
wpa_dbus_property_register(obj_desc, WPAS_DBUS_NEW_IFACE_NETWORK, wpas_dbus_network_properties,
"Enabled", "b", wpas_dbus_network_signals);
(WPADBusPropertyAccessor)
wpas_dbus_getter_enabled,
(WPADBusPropertyAccessor)
wpas_dbus_setter_enabled,
arg1, wpa_dbus_free, RW);
/* Properties property */
wpa_dbus_property_register(obj_desc, WPAS_DBUS_NEW_IFACE_NETWORK,
"Properties", "a{sv}",
(WPADBusPropertyAccessor)
wpas_dbus_getter_network_properties,
(WPADBusPropertyAccessor)
wpas_dbus_setter_network_properties,
arg2, wpa_dbus_free, RW);
/* PropertiesChanged signal */
wpa_dbus_signal_register(obj_desc, WPAS_DBUS_NEW_IFACE_NETWORK,
"PropertiesChanged", sargs);
if (wpa_dbus_register_object_per_iface(ctrl_iface, net_obj_path, if (wpa_dbus_register_object_per_iface(ctrl_iface, net_obj_path,
wpa_s->ifname, obj_desc)) wpa_s->ifname, obj_desc))
@ -1202,8 +1201,7 @@ static int wpas_dbus_register_network(struct wpa_supplicant *wpa_s,
err: err:
os_free(net_obj_path); os_free(net_obj_path);
os_free(obj_desc); os_free(obj_desc);
os_free(arg1); os_free(arg);
os_free(arg2);
return -1; return -1;
} }
@ -1248,6 +1246,27 @@ static int wpas_dbus_unregister_network(struct wpa_supplicant *wpa_s, int nid)
} }
static const struct wpas_dbus_property wpas_dbus_bss_properties[] = {
{ "Properties", WPAS_DBUS_NEW_IFACE_BSSID, "a{sv}",
(WPADBusPropertyAccessor) wpas_dbus_getter_bss_properties,
NULL,
R
},
{ NULL, NULL, NULL, NULL, NULL, 0 }
};
static const struct wpas_dbus_signal wpas_dbus_bss_signals[] = {
{ "PropertiesChanged", WPAS_DBUS_NEW_IFACE_BSSID,
{
{ "properties", "a{sv}", ARG_OUT },
END_ARGS
}
},
{ NULL, NULL, { END_ARGS } }
};
/** /**
* wpas_dbus_unregister_bss - Unregister a scanned BSS from dbus * wpas_dbus_unregister_bss - Unregister a scanned BSS from dbus
* @wpa_s: wpa_supplicant interface structure * @wpa_s: wpa_supplicant interface structure
@ -1344,12 +1363,9 @@ static int wpas_dbus_register_bss(struct wpa_supplicant *wpa_s,
arg->wpa_s = wpa_s; arg->wpa_s = wpa_s;
arg->id = id; arg->id = id;
/* Properties property */ wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
wpa_dbus_property_register(obj_desc, WPAS_DBUS_NEW_IFACE_BSSID, wpas_dbus_bss_properties,
"Properties", "a{sv}", wpas_dbus_bss_signals);
(WPADBusPropertyAccessor)
wpas_dbus_getter_bss_properties, NULL,
arg, wpa_dbus_free, R);
wpa_printf(MSG_DEBUG, "dbus: Register BSS object '%s'", wpa_printf(MSG_DEBUG, "dbus: Register BSS object '%s'",
bss_obj_path); bss_obj_path);
@ -1627,7 +1643,7 @@ static int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
goto err; goto err;
} }
wpas_dbus_register(obj_desc, wpa_s, wpas_dbus_interface_methods, wpas_dbus_register(obj_desc, wpa_s, NULL, wpas_dbus_interface_methods,
wpas_dbus_interface_properties, wpas_dbus_interface_properties,
wpas_dbus_interface_signals); wpas_dbus_interface_signals);

View file

@ -35,10 +35,6 @@ struct wpa_dbus_method_desc {
/* method handling function */ /* method handling function */
WPADBusMethodHandler method_handler; WPADBusMethodHandler method_handler;
/* handler function argument */
void *handler_argument;
/* function used to free handler argument */
WPADBusArgumentFreeFunction argument_free_func;
/* number of method arguments */ /* number of method arguments */
int args_num; int args_num;
@ -87,10 +83,6 @@ struct wpa_dbus_property_desc {
WPADBusPropertyAccessor getter; WPADBusPropertyAccessor getter;
/* property setter function */ /* property setter function */
WPADBusPropertyAccessor setter; WPADBusPropertyAccessor setter;
/* argument for getter and setter functions */
void *user_data;
/* function used to free accessors argument */
WPADBusArgumentFreeFunction user_data_free_func;
}; };
@ -629,13 +621,16 @@ static void recursive_iter_copy(DBusMessageIter *from, DBusMessageIter *to)
*/ */
static DBusMessage * get_all_properties( static DBusMessage * get_all_properties(
DBusMessage *message, char *interface, DBusMessage *message, char *interface,
struct wpa_dbus_property_desc *property_dsc) struct wpa_dbus_object_desc *obj_dsc)
{ {
/* Create and initialize the return message */ /* Create and initialize the return message */
DBusMessage *reply = dbus_message_new_method_return(message); DBusMessage *reply = dbus_message_new_method_return(message);
DBusMessage *getterReply = NULL; DBusMessage *getterReply = NULL;
DBusMessageIter iter, dict_iter, entry_iter, ret_iter; DBusMessageIter iter, dict_iter, entry_iter, ret_iter;
int counter = 0; int counter = 0;
struct wpa_dbus_property_desc *property_dsc;
property_dsc = obj_dsc->properties;
dbus_message_iter_init_append(reply, &iter); dbus_message_iter_init_append(reply, &iter);
@ -652,7 +647,7 @@ static DBusMessage * get_all_properties(
property_dsc->access != W && property_dsc->getter) { property_dsc->access != W && property_dsc->getter) {
getterReply = property_dsc->getter( getterReply = property_dsc->getter(
message, property_dsc->user_data); message, obj_dsc->user_data);
dbus_message_iter_init(getterReply, &ret_iter); dbus_message_iter_init(getterReply, &ret_iter);
dbus_message_iter_open_container(&dict_iter, dbus_message_iter_open_container(&dict_iter,
@ -719,20 +714,20 @@ static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
NULL); NULL);
return get_all_properties(message, interface, return get_all_properties(message, interface, obj_dsc);
obj_dsc->properties);
} }
static DBusMessage * properties_get(DBusMessage *message, static DBusMessage * properties_get(DBusMessage *message,
struct wpa_dbus_property_desc *dsc) struct wpa_dbus_property_desc *dsc,
void *user_data)
{ {
if (os_strcmp(dbus_message_get_signature(message), "ss")) if (os_strcmp(dbus_message_get_signature(message), "ss"))
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
NULL); NULL);
if (dsc->access != W && dsc->getter) if (dsc->access != W && dsc->getter)
return dsc->getter(message, dsc->user_data); return dsc->getter(message, user_data);
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
"Property is write-only"); "Property is write-only");
@ -740,14 +735,15 @@ static DBusMessage * properties_get(DBusMessage *message,
static DBusMessage * properties_set(DBusMessage *message, static DBusMessage * properties_set(DBusMessage *message,
struct wpa_dbus_property_desc *dsc) struct wpa_dbus_property_desc *dsc,
void *user_data)
{ {
if (os_strcmp(dbus_message_get_signature(message), "ssv")) if (os_strcmp(dbus_message_get_signature(message), "ssv"))
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
NULL); NULL);
if (dsc->access != R && dsc->setter) if (dsc->access != R && dsc->setter)
return dsc->setter(message, dsc->user_data); return dsc->setter(message, user_data);
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
"Property is read-only"); "Property is read-only");
@ -795,9 +791,10 @@ properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method, if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0) WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0)
return properties_get(message, property_dsc); return properties_get(message, property_dsc,
obj_dsc->user_data);
return properties_set(message, property_dsc); return properties_set(message, property_dsc, obj_dsc->user_data);
} }
@ -876,7 +873,7 @@ static DBusMessage * msg_method_handler(DBusMessage *message,
} }
return method_dsc->method_handler(message, return method_dsc->method_handler(message,
method_dsc->handler_argument); obj_dsc->user_data);
} }
@ -973,10 +970,6 @@ void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
os_free(tmp_met_dsc->args[i].type); os_free(tmp_met_dsc->args[i].type);
} }
if (tmp_met_dsc->argument_free_func)
tmp_met_dsc->argument_free_func(
tmp_met_dsc->handler_argument);
os_free(tmp_met_dsc); os_free(tmp_met_dsc);
} }
@ -1009,13 +1002,13 @@ void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
os_free(tmp_prop_dsc->dbus_property); os_free(tmp_prop_dsc->dbus_property);
os_free(tmp_prop_dsc->type); os_free(tmp_prop_dsc->type);
if (tmp_prop_dsc->user_data_free_func)
tmp_prop_dsc->user_data_free_func(
tmp_prop_dsc->user_data);
os_free(tmp_prop_dsc); os_free(tmp_prop_dsc);
} }
/* free handler's argument */
if (obj_dsc->user_data_free_func)
obj_dsc->user_data_free_func(obj_dsc->user_data);
os_free(obj_dsc); os_free(obj_dsc);
} }
@ -1159,24 +1152,19 @@ int wpa_dbus_unregister_object_per_iface(
* @dbus_interface: DBus interface under which method will be registered * @dbus_interface: DBus interface under which method will be registered
* @dbus_method: a name the method will be registered with * @dbus_method: a name the method will be registered with
* @method_handler: a function which will be called to handle this method call * @method_handler: a function which will be called to handle this method call
* @handler_argument: an additional argument passed to handler function
* @argument_free_func: function used to free handler argument
* @args: method arguments list * @args: method arguments list
* Returns: Zero on success and -1 on failure * Returns: Zero on success and -1 on failure
* *
* Registers DBus method under given name and interface for the object. * Registers DBus method under given name and interface for the object.
* Method calls will be handled with given handling function and optional * Method calls will be handled with given handling function.
* argument passed to this function. Handler function is required to return * Handler function is required to return a DBusMessage pointer which
* a DBusMessage pointer which will be response to method call. Any method * will be response to method call. Any method call before being handled
* call before being handled must have registered appropriate handler by * must have registered appropriate handler by using this function.
* using this function.
*/ */
int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc, int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc,
const char *dbus_interface, const char *dbus_interface,
const char *dbus_method, const char *dbus_method,
WPADBusMethodHandler method_handler, WPADBusMethodHandler method_handler,
void *handler_argument,
WPADBusArgumentFreeFunction argument_free_func,
const struct wpa_dbus_argument args[]) const struct wpa_dbus_argument args[])
{ {
struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods; struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
@ -1246,8 +1234,6 @@ int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc,
goto err; goto err;
method_dsc->method_handler = method_handler; method_dsc->method_handler = method_handler;
method_dsc->handler_argument = handler_argument;
method_dsc->argument_free_func = argument_free_func;
method_dsc->next = NULL; method_dsc->next = NULL;
return 0; return 0;
@ -1393,17 +1379,14 @@ err:
* @type: a property type signature in form of DBus type description * @type: a property type signature in form of DBus type description
* @getter: a function called in order to get property value * @getter: a function called in order to get property value
* @setter: a function called in order to set property value * @setter: a function called in order to set property value
* @user_data: additional argument passed to setter or getter
* @user_data_free_func: function used to free additional argument
* @access: property access permissions specifier (R, W or RW) * @access: property access permissions specifier (R, W or RW)
* Returns: Zero on success and -1 on failure * Returns: Zero on success and -1 on failure
* *
* Registers DBus property under given name and interface for the object. * Registers DBus property under given name and interface for the object.
* Property are set with giver setter function and get with getter. * Properties are set with giver setter function and get with getter.Getter
* Additional argument is passed to getter or setter. Getter or setter * or setter are required to return DBusMessage which is response to Set/Get
* are required to return DBusMessage which is response to Set/Get method * method calls. Every property must be registered by this function before
* calls. Every property must be registered by this function before being * being used.
* used.
*/ */
int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc, int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc,
const char *dbus_interface, const char *dbus_interface,
@ -1411,8 +1394,6 @@ int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc,
const char *type, const char *type,
WPADBusPropertyAccessor getter, WPADBusPropertyAccessor getter,
WPADBusPropertyAccessor setter, WPADBusPropertyAccessor setter,
void *user_data,
WPADBusArgumentFreeFunction user_data_free_func,
enum dbus_prop_access _access) enum dbus_prop_access _access)
{ {
struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties; struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
@ -1458,8 +1439,6 @@ int wpa_dbus_property_register(struct wpa_dbus_object_desc *obj_dsc,
property_dsc->getter = getter; property_dsc->getter = getter;
property_dsc->setter = setter; property_dsc->setter = setter;
property_dsc->user_data = user_data;
property_dsc->user_data_free_func = user_data_free_func;
property_dsc->access = _access; property_dsc->access = _access;
property_dsc->next = NULL; property_dsc->next = NULL;

View file

@ -27,9 +27,17 @@ typedef DBusMessage * (* WPADBusPropertyAccessor)(DBusMessage *message,
struct wpa_dbus_object_desc { struct wpa_dbus_object_desc {
DBusConnection *connection; DBusConnection *connection;
/* list of methods, properties and signals registered with object */
struct wpa_dbus_method_desc *methods; struct wpa_dbus_method_desc *methods;
struct wpa_dbus_signal_desc *signals; struct wpa_dbus_signal_desc *signals;
struct wpa_dbus_property_desc *properties; struct wpa_dbus_property_desc *properties;
/* argument for method handlers and properties
* getter and setter functions */
void *user_data;
/* function used to free above argument */
WPADBusArgumentFreeFunction user_data_free_func;
}; };
enum dbus_prop_access { R, W, RW }; enum dbus_prop_access { R, W, RW };
@ -86,8 +94,6 @@ int wpa_dbus_method_register(struct wpa_dbus_object_desc *obj_dsc,
const char *dbus_interface, const char *dbus_interface,
const char *dbus_method, const char *dbus_method,
WPADBusMethodHandler method_handler, WPADBusMethodHandler method_handler,
void *handler_argument,
WPADBusArgumentFreeFunction argument_free_func,
const struct wpa_dbus_argument args[]); const struct wpa_dbus_argument args[]);
int wpa_dbus_signal_register(struct wpa_dbus_object_desc *obj_dsc, int wpa_dbus_signal_register(struct wpa_dbus_object_desc *obj_dsc,
@ -101,8 +107,6 @@ int wpa_dbus_property_register(
const char *type, const char *type,
WPADBusPropertyAccessor getter, WPADBusPropertyAccessor getter,
WPADBusPropertyAccessor setter, WPADBusPropertyAccessor setter,
void *user_data,
WPADBusArgumentFreeFunction user_data_free_func,
enum dbus_prop_access _access); enum dbus_prop_access _access);
void wpa_dbus_signal_property_changed(struct wpas_dbus_priv *iface, void wpa_dbus_signal_property_changed(struct wpas_dbus_priv *iface,