From f2382b059a30759d41b248c6235f32de8c26047f Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 29 Jul 2011 14:09:05 +0200 Subject: [PATCH] implement uci-to-blobmsg conversion as an abstraction between uci and the rest of netifd --- config.c | 125 +++++++++++++++++++++++++++++++++++++++++++++--------------- config.h | 24 ++++++++++++ interface.c | 45 +++++++++++++++++++++- interface.h | 5 ++- 4 files changed, 166 insertions(+), 33 deletions(-) create mode 100644 config.h diff --git a/config.c b/config.c index 94a7fdb..9104a9c 100644 --- a/config.c +++ b/config.c @@ -13,51 +13,114 @@ struct uci_context *uci_ctx; static struct uci_package *uci_network; bool config_init = false; -enum { - SIF_TYPE, - SIF_IFNAME, - __SIF_MAX, -}; +static void uci_attr_to_blob(struct blob_buf *b, const char *str, + const char *name, enum blobmsg_type type) +{ + char *err; + int intval; + + switch (type) { + case BLOBMSG_TYPE_STRING: + blobmsg_add_string(b, name, str); + break; + case BLOBMSG_TYPE_BOOL: + if (!strcmp(str, "true") || !strcmp(str, "1")) + intval = 1; + else if (!strcmp(str, "false") || !strcmp(str, "0")) + intval = 0; + else + return; -static const struct uci_parse_option if_opts[__SIF_MAX] = { - [SIF_TYPE] = { "type", UCI_TYPE_STRING }, - [SIF_IFNAME] = { "ifname", UCI_TYPE_STRING }, -}; + blobmsg_add_u8(b, name, intval); + break; + case BLOBMSG_TYPE_INT32: + intval = strtol(str, &err, 0); + if (*err) + return; -static void -config_parse_interface(struct uci_section *s) -{ - struct uci_option *opts[__SIF_MAX]; - struct interface *iface; - struct device *dev; - const char *type; + blobmsg_add_u32(b, name, intval); + break; + default: + break; + } +} - DPRINTF("Create interface '%s'\n", s->e.name); +static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o, + enum blobmsg_type type) +{ + struct uci_element *e; + char *str, *next, *word; - iface = alloc_interface(s->e.name, s); - if (!iface) + if (o->type == UCI_TYPE_LIST) { + uci_foreach_element(&o->v.list, e) { + uci_attr_to_blob(b, e->name, NULL, type); + } return; + } - uci_parse_section(s, if_opts, __SIF_MAX, opts); + str = strdup(o->v.string); + next = str; - if (opts[SIF_TYPE]) { - type = opts[SIF_TYPE]->v.string; + while ((word = strsep(&next, " \t")) != NULL) { + if (!*word) + continue; - if (!strcmp(type, "bridge")) { - interface_attach_bridge(iface, s); - return; - } + uci_attr_to_blob(b, word, NULL, type); } - if (opts[SIF_IFNAME]) { - dev = get_device(opts[SIF_IFNAME]->v.string, true); - if (!dev) - return; + free(str); +} + +static void uci_to_blob(struct blob_buf *b, struct uci_section *s, + const struct config_param_list *p) +{ + const struct blobmsg_policy *attr; + struct uci_element *e; + struct uci_option *o; + void *array; + int i; + + uci_foreach_element(&s->options, e) { + for (i = 0; i < p->n_params; i++) { + attr = &p->params[i]; + if (!strcmp(attr->name, e->name)) + break; + } + + if (i == p->n_params) + continue; + + o = uci_to_option(e); + + if (attr->type == BLOBMSG_TYPE_ARRAY) { + if (!p->info) + continue; + + array = blobmsg_open_array(b, attr->name); + uci_array_to_blob(b, o, p->info[i].type); + blobmsg_close_array(b, array); + continue; + } + + if (o->type == UCI_TYPE_LIST) + continue; - add_device_user(&iface->main_dev, dev); + uci_attr_to_blob(b, o->v.string, attr->name, attr->type); } } +static void +config_parse_interface(struct uci_section *s) +{ + struct blob_buf b = {}; + + DPRINTF("Create interface '%s'\n", s->e.name); + + blob_buf_init(&b, 0); + uci_to_blob(&b, s, &interface_attr_list); + alloc_interface(s->e.name, s, b.head); +} + enum { SDEV_NAME, SDEV_TYPE, diff --git a/config.h b/config.h new file mode 100644 index 0000000..6403e1f --- /dev/null +++ b/config.h @@ -0,0 +1,24 @@ +#ifndef __NETIFD_CONFIG_H +#define __NETIFD_CONFIG_H + +#include + +enum config_param_type { + CONFIG_PARAM_TYPE_SIMPLE, + CONFIG_PARAM_TYPE_LIST, + CONFIG_PARAM_TYPE_SECTION, +}; + +union config_param_info { + enum blobmsg_type type; + struct config_params *section; +}; + +struct config_param_list { + const struct config_param_list *next; + int n_params; + const struct blobmsg_policy *params; + const union config_param_info *info; +}; + +#endif diff --git a/interface.c b/interface.c index dd303ef..c448e4d 100644 --- a/interface.c +++ b/interface.c @@ -7,9 +7,35 @@ #include "interface.h" #include "proto.h" #include "ubus.h" +#include "config.h" static LIST_HEAD(interfaces); +enum { + IFACE_ATTR_TYPE, + IFACE_ATTR_IFNAME, + IFACE_ATTR_PROTO, + IFACE_ATTR_AUTO, + IFACE_ATTR_MAX +}; + +static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = { + [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING, +}; + +static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = { + [IFACE_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }, + [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING }, + [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_ARRAY }, + [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL }, +}; + +const struct config_param_list interface_attr_list = { + .n_params = IFACE_ATTR_MAX, + .params = iface_attrs, + .info = iface_attr_info, +}; + static void clear_interface_errors(struct interface *iface) { @@ -177,9 +203,12 @@ void interface_set_proto_state(struct interface *iface, struct interface_proto_s } struct interface * -alloc_interface(const char *name, struct uci_section *s) +alloc_interface(const char *name, struct uci_section *s, struct blob_attr *attr) { struct interface *iface; + struct blob_attr *tb[IFACE_ATTR_MAX]; + struct blob_attr *cur; + struct device *dev; iface = get_interface(name); if (iface) @@ -198,6 +227,20 @@ alloc_interface(const char *name, struct uci_section *s) netifd_ubus_add_interface(iface); + blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, + blob_data(attr), blob_len(attr)); + + if ((cur = tb[IFACE_ATTR_TYPE])) { + if (!strcmp(blobmsg_data(cur), "bridge")) + interface_attach_bridge(iface, s); + } + + if ((cur = tb[IFACE_ATTR_IFNAME])) { + dev = get_device(blobmsg_data(cur), true); + if (dev) + add_device_user(&iface->main_dev, dev); + } + return iface; } diff --git a/interface.h b/interface.h index 480f759..d30bd36 100644 --- a/interface.h +++ b/interface.h @@ -2,6 +2,7 @@ #define __NETIFD_INTERFACE_H #include "device.h" +#include "config.h" struct interface; struct interface_proto_state; @@ -56,8 +57,10 @@ struct interface { struct ubus_object ubus; }; +extern const struct config_param_list interface_attr_list; + struct interface *get_interface(const char *name); -struct interface *alloc_interface(const char *name, struct uci_section *s); +struct interface *alloc_interface(const char *name, struct uci_section *s, struct blob_attr *attr); void free_interface(struct interface *iface); void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state); -- 2.11.0