summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@openwrt.org>2011-07-29 14:09:05 +0200
committerFelix Fietkau <nbd@openwrt.org>2011-07-29 14:09:05 +0200
commitf2382b059a30759d41b248c6235f32de8c26047f (patch)
tree8f87fbcb2a7bd5ffe5f42f1b851a599db43d2cfa /config.c
parentf6fb6bee2c29f31d13d0b0288f4f680b6b56e9ba (diff)
downloadnetifd-f2382b059a30759d41b248c6235f32de8c26047f.tar.gz
implement uci-to-blobmsg conversion as an abstraction between uci and the rest of netifd
Diffstat (limited to 'config.c')
-rw-r--r--config.c125
1 files changed, 94 insertions, 31 deletions
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,