8 struct uci_context *uci_ctx;
9 static struct uci_package *uci_network;
10 bool config_init = false;
11 static struct blob_buf b;
14 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
15 const char *name, enum blobmsg_type type)
21 case BLOBMSG_TYPE_STRING:
22 blobmsg_add_string(b, name, str);
24 case BLOBMSG_TYPE_BOOL:
25 if (!strcmp(str, "true") || !strcmp(str, "1"))
27 else if (!strcmp(str, "false") || !strcmp(str, "0"))
32 blobmsg_add_u8(b, name, intval);
34 case BLOBMSG_TYPE_INT32:
35 intval = strtol(str, &err, 0);
39 blobmsg_add_u32(b, name, intval);
46 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
47 enum blobmsg_type type)
49 struct uci_element *e;
50 char *str, *next, *word;
52 if (o->type == UCI_TYPE_LIST) {
53 uci_foreach_element(&o->v.list, e) {
54 uci_attr_to_blob(b, e->name, NULL, type);
59 str = strdup(o->v.string);
62 while ((word = strsep(&next, " \t")) != NULL) {
66 uci_attr_to_blob(b, word, NULL, type);
72 static void __uci_to_blob(struct blob_buf *b, struct uci_section *s,
73 const struct config_param_list *p)
75 const struct blobmsg_policy *attr;
76 struct uci_element *e;
81 uci_foreach_element(&s->options, e) {
82 for (i = 0; i < p->n_params; i++) {
84 if (!strcmp(attr->name, e->name))
93 if (attr->type == BLOBMSG_TYPE_ARRAY) {
97 array = blobmsg_open_array(b, attr->name);
98 uci_array_to_blob(b, o, p->info[i].type);
99 blobmsg_close_array(b, array);
103 if (o->type == UCI_TYPE_LIST)
106 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
110 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
111 const struct config_param_list *p)
115 __uci_to_blob(b, s, p);
116 for (i = 0; i < p->n_next; i++)
117 uci_to_blob(b, s, p->next[i]);
121 config_parse_interface(struct uci_section *s)
123 DPRINTF("Create interface '%s'\n", s->e.name);
125 blob_buf_init(&b, 0);
126 uci_to_blob(&b, s, &interface_attr_list);
127 interface_alloc(s->e.name, s, b.head);
131 config_init_devices(void)
133 struct uci_element *e;
135 uci_foreach_element(&uci_network->sections, e) {
136 struct uci_section *s = uci_to_section(e);
138 if (strcmp(s->type, "device") != 0)
141 blob_buf_init(&b, 0);
142 uci_to_blob(&b, s, &device_attr_list);
143 device_create(b.head, s);
148 config_init_interfaces(const char *name)
150 struct uci_context *ctx;
151 struct uci_package *p = NULL;
152 struct uci_element *e;
154 ctx = uci_alloc_context();
157 uci_set_confdir(ctx, "./config");
159 if (uci_load(ctx, "network", &p)) {
160 fprintf(stderr, "Failed to load network config\n");
167 config_init_devices();
169 uci_foreach_element(&p->sections, e) {
170 struct uci_section *s = uci_to_section(e);
172 if (name && strcmp(s->e.name, name) != 0)
175 if (!strcmp(s->type, "interface"))
176 config_parse_interface(s);
181 interface_start_pending();