#endif
#include <errno.h>
+#include <stdarg.h>
#include <glib.h>
#include <gdbus.h>
static gchar *device_filter = NULL;
+static struct {
+ enum connman_property_id id;
+ int type;
+ const char *name;
+ const void *value;
+} propid_table[] = {
+ { CONNMAN_PROPERTY_ID_IPV4_METHOD,
+ DBUS_TYPE_STRING, "IPv4.Method", "dhcp" },
+ { CONNMAN_PROPERTY_ID_IPV4_ADDRESS,
+ DBUS_TYPE_STRING, "IPv4.Address" },
+ { CONNMAN_PROPERTY_ID_IPV4_NETMASK,
+ DBUS_TYPE_STRING, "IPv4.Netmask" },
+ { CONNMAN_PROPERTY_ID_IPV4_GATEWAY,
+ DBUS_TYPE_STRING, "IPv4.Gateway" },
+ { CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
+ DBUS_TYPE_STRING, "IPv4.Nameserver" },
+ { }
+};
+
+static int propid2type(enum connman_property_id id)
+{
+ int i;
+
+ for (i = 0; propid_table[i].name; i++) {
+ if (propid_table[i].id == id)
+ return propid_table[i].type;
+ }
+
+ return DBUS_TYPE_INVALID;
+}
+
+static const char *propid2name(enum connman_property_id id)
+{
+ int i;
+
+ for (i = 0; propid_table[i].name; i++) {
+ if (propid_table[i].id == id)
+ return propid_table[i].name;
+ }
+
+ return NULL;
+}
+
static const char *type2string(enum connman_element_type type)
{
switch (type) {
static void append_property(DBusMessageIter *dict,
struct connman_property *property)
{
- if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC) {
- append_entry(dict, property->name, property->type,
- &property->value);
+ if (property->value == NULL)
return;
- }
+
+ append_entry(dict, property->name, property->type, &property->value);
}
static DBusMessage *get_properties(DBusConnection *conn,
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
- if (element->parent != NULL)
+ if (element->parent != NULL &&
+ element->parent->type != CONNMAN_ELEMENT_TYPE_ROOT) {
append_entry(&dict, "Parent",
DBUS_TYPE_OBJECT_PATH, &element->parent->path);
+ }
str = type2string(element->type);
if (str != NULL)
append_entry(&dict, "IPv4.Gateway",
DBUS_TYPE_STRING, &element->ipv4.gateway);
+ connman_element_lock(element);
+
for (list = element->properties; list; list = list->next) {
struct connman_property *property = list->data;
append_property(&dict, property);
}
+ connman_element_unlock(element);
+
dbus_message_iter_close_container(&array, &dict);
return reply;
static DBusMessage *set_property(DBusConnection *conn,
DBusMessage *msg, void *data)
{
+ struct connman_element *element = data;
+ DBusMessageIter iter;
+ DBusMessageIter value;
+ const char *name;
+ GSList *list;
+
+ DBG("conn %p", conn);
+
+ if (dbus_message_iter_init(msg, &iter) == FALSE)
+ return __connman_error_invalid_arguments(msg);
+
+ dbus_message_iter_get_basic(&iter, &name);
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
+
+ if (__connman_security_check_privileges(msg) < 0)
+ return __connman_error_permission_denied(msg);
+
+ connman_element_lock(element);
+
+ for (list = element->properties; list; list = list->next) {
+ struct connman_property *property = list->data;
+ const char *str;
+
+ if (g_str_equal(property->name, name) == FALSE)
+ continue;
+
+ if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
+ continue;
+
+ property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
+
+ if (property->type == DBUS_TYPE_STRING) {
+ dbus_message_iter_get_basic(&value, &str);
+ g_free(property->value);
+ property->value = g_strdup(str);
+ } else
+ property->value = NULL;
+ }
+
+ connman_element_unlock(element);
+
+ return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+}
+
+static DBusMessage *clear_property(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct connman_element *element = data;
+ const char *name;
+ GSList *list;
+
+ DBG("conn %p", conn);
+
+ if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID) == FALSE)
+ return __connman_error_invalid_arguments(msg);
+
+ if (__connman_security_check_privileges(msg) < 0)
+ return __connman_error_permission_denied(msg);
+
+ connman_element_lock(element);
+
+ for (list = element->properties; list; list = list->next) {
+ struct connman_property *property = list->data;
+
+ if (g_str_equal(property->name, name) == FALSE)
+ continue;
+
+ if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
+ continue;
+
+ if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
+ continue;
+
+ property->flags |= CONNMAN_PROPERTY_FLAG_REFERENCE;
+
+ if (property->type == DBUS_TYPE_STRING)
+ g_free(property->value);
+
+ property->value = NULL;
+ }
+
+ connman_element_unlock(element);
+
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}
static GDBusMethodTable element_methods[] = {
{ "GetProperties", "", "a{sv}", get_properties },
{ "SetProperty", "sv", "", set_property },
+ { "ClearProperty", "s", "", clear_property },
{ "Update", "", "", do_update },
{ "Enable", "", "", do_enable },
{ "Disable", "", "", do_disable },
g_static_rw_lock_writer_unlock(&element_lock);
}
-struct connman_element *connman_element_create(void)
+/**
+ * connman_element_create:
+ * @name: element name
+ *
+ * Allocate a new element and assign the given #name to it. If the name
+ * is #NULL, it will be later on created based on the element type.
+ *
+ * Returns: a newly-allocated #connman_element structure
+ */
+struct connman_element *connman_element_create(const char *name)
{
struct connman_element *element;
- element = g_new0(struct connman_element, 1);
+ element = g_try_new0(struct connman_element, 1);
+ if (element == NULL)
+ return NULL;
DBG("element %p", element);
g_static_mutex_init(&element->mutex);
+ element->name = g_strdup(name);
element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
element->subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
element->state = CONNMAN_ELEMENT_STATE_CLOSED;
-
+ element->index = -1;
element->enabled = FALSE;
- element->netdev.index = -1;
-
return element;
}
return element;
}
-void connman_element_unref(struct connman_element *element)
+static void free_properties(struct connman_element *element)
{
- DBG("element %p name %s refcount %d", element, element->name,
- g_atomic_int_get(&element->refcount) - 1);
+ GSList *list;
- if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
- GSList *list;
+ DBG("element %p name %s", element, element->name);
+
+ connman_element_lock(element);
- for (list = element->properties; list; list = list->next) {
- struct connman_property *property = list->data;
- if ((property->flags & CONNMAN_PROPERTY_FLAG_STATIC) &&
- property->type == DBUS_TYPE_STRING)
+ for (list = element->properties; list; list = list->next) {
+ struct connman_property *property = list->data;
+
+ if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)) {
+ if (property->type == DBUS_TYPE_STRING)
g_free(property->value);
- g_free(property);
- list->data = NULL;
}
- g_slist_free(element->properties);
+ g_free(property);
+ }
+
+ g_slist_free(element->properties);
+
+ element->properties = NULL;
+
+ connman_element_unlock(element);
+}
+
+void connman_element_unref(struct connman_element *element)
+{
+ DBG("element %p name %s refcount %d", element, element->name,
+ g_atomic_int_get(&element->refcount) - 1);
+
+ if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
+ free_properties(element);
g_free(element->ipv4.address);
g_free(element->ipv4.netmask);
g_free(element->ipv4.gateway);
g_free(element->ipv4.broadcast);
g_free(element->ipv4.nameserver);
g_free(element->network.identifier);
- g_free(element->netdev.name);
g_free(element->path);
g_free(element->name);
g_free(element);
return -ENOMEM;
property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
-
- property->name = g_strdup(name);
- property->type = type;
+ property->id = CONNMAN_PROPERTY_ID_INVALID;
+ property->name = g_strdup(name);
+ property->type = type;
DBG("name %s type %d value %p", name, type, value);
return 0;
}
+static void *get_reference_value(struct connman_element *element,
+ enum connman_property_id id)
+{
+ GSList *list;
+
+ DBG("element %p name %s", element, element->name);
+
+ for (list = element->properties; list; list = list->next) {
+ struct connman_property *property = list->data;
+
+ if (property->id != id)
+ continue;
+
+ if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
+ return property->value;
+ }
+
+ if (element->parent == NULL)
+ return NULL;
+
+ return get_reference_value(element->parent, id);
+}
+
+static void set_reference_properties(struct connman_element *element)
+{
+ GSList *list;
+
+ DBG("element %p name %s", element, element->name);
+
+ for (list = element->properties; list; list = list->next) {
+ struct connman_property *property = list->data;
+
+ if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
+ continue;
+
+ property->value = get_reference_value(element->parent,
+ property->id);
+ }
+}
+
+static struct connman_property *create_property(struct connman_element *element,
+ enum connman_property_id id)
+{
+ struct connman_property *property;
+ GSList *list;
+
+ DBG("element %p name %s", element, element->name);
+
+ connman_element_lock(element);
+
+ for (list = element->properties; list; list = list->next) {
+ property = list->data;
+
+ if (property->id == id)
+ goto unlock;
+ }
+
+ property = g_try_new0(struct connman_property, 1);
+ if (property == NULL)
+ goto unlock;
+
+ property->flags = CONNMAN_PROPERTY_FLAG_REFERENCE;
+ property->id = id;
+ property->name = g_strdup(propid2name(id));
+ property->type = propid2type(id);
+
+ if (property->name == NULL) {
+ g_free(property);
+ property = NULL;
+ goto unlock;
+ }
+
+ element->properties = g_slist_append(element->properties, property);
+
+unlock:
+ connman_element_unlock(element);
+
+ return property;
+}
+
+static void create_default_properties(struct connman_element *element)
+{
+ struct connman_property *property;
+ int i;
+
+ DBG("element %p name %s", element, element->name);
+
+ for (i = 0; propid_table[i].name; i++) {
+ DBG("property %s", propid_table[i].name);
+
+ property = create_property(element, propid_table[i].id);
+
+ property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
+
+ if (propid_table[i].type != DBUS_TYPE_STRING)
+ continue;
+
+ if (propid_table[i].value)
+ property->value = g_strdup(propid_table[i].value);
+ else
+ property->value = g_strdup("");
+ }
+}
+
+static int define_properties_valist(struct connman_element *element,
+ va_list args)
+{
+ enum connman_property_id id;
+
+ DBG("element %p name %s", element, element->name);
+
+ id = va_arg(args, enum connman_property_id);
+
+ while (id != CONNMAN_PROPERTY_ID_INVALID) {
+
+ DBG("property %d", id);
+
+ create_property(element, id);
+
+ id = va_arg(args, enum connman_property_id);
+ }
+
+ return 0;
+}
+
+/**
+ * connman_element_define_properties:
+ * @element: an element
+ * @varargs: list of property identifiers
+ *
+ * Define the valid properties for an element.
+ *
+ * Returns: %0 on success
+ */
+int connman_element_define_properties(struct connman_element *element, ...)
+{
+ va_list args;
+ int err;
+
+ DBG("element %p name %s", element, element->name);
+
+ va_start(args, element);
+
+ err = define_properties_valist(element, args);
+
+ va_end(args);
+
+ return err;
+}
+
+int connman_element_create_property(struct connman_element *element,
+ const char *name, int type)
+{
+ return -EIO;
+}
+
int connman_element_set_property(struct connman_element *element,
- enum connman_property_type type, const void *value)
+ enum connman_property_id id, const void *value)
{
+#if 0
switch (type) {
case CONNMAN_PROPERTY_TYPE_INVALID:
return -EINVAL;
connman_element_unlock(element);
break;
}
+#endif
g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
CONNMAN_MANAGER_INTERFACE, "ElementUpdated",
}
int connman_element_get_value(struct connman_element *element,
- enum connman_property_type type, void *value)
+ enum connman_property_id id, void *value)
{
if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
return -EINVAL;
+#if 0
switch (type) {
case CONNMAN_PROPERTY_TYPE_INVALID:
return -EINVAL;
connman_element_unlock(element);
break;
}
+#endif
return 0;
}
+/**
+ * connman_element_register:
+ * @element: the element to register
+ * @parent: the parent to register the element with
+ *
+ * Register an element with the core. It will be register under the given
+ * parent of if %NULL is provided under the root element.
+ *
+ * Returns: %0 on success
+ */
int connman_element_register(struct connman_element *element,
struct connman_element *parent)
{
DBG("element %p name %s parent %p", element, element->name, parent);
if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE) {
- if (g_str_equal(device_filter, element->netdev.name) == FALSE)
- return -EINVAL;
+ if (g_str_equal(device_filter, element->name) == FALSE)
+ return -EPERM;
}
if (connman_element_ref(element) == NULL)
if (element->name == NULL) {
element->name = g_strdup(type2string(element->type));
- if (element->name == NULL)
+ if (element->name == NULL) {
+ connman_element_unlock(element);
return -EINVAL;
+ }
}
element->parent = parent;
if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
element->subtype = element->parent->subtype;
} else {
+ element->parent = element_root->data;
+
node = element_root;
basepath = "";
}
element->path = g_strdup_printf("%s/%s", basepath, element->name);
+ set_reference_properties(element);
+
connman_element_unlock(element);
DBG("element %p path %s", element, element->path);
g_static_rw_lock_writer_lock(&element_lock);
- element = connman_element_create();
+ element = connman_element_create("root");
- element->name = g_strdup("root");
element->path = g_strdup("/");
element->type = CONNMAN_ELEMENT_TYPE_ROOT;
+ create_default_properties(element);
+
element_root = g_node_new(element);
g_static_rw_lock_writer_unlock(&element_lock);