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_bridge_interface(struct uci_section *s)
125 name = alloca(strlen(s->e.name) + 4);
126 sprintf(name, "br-%s", s->e.name);
127 blobmsg_add_string(&b, "name", name);
129 uci_to_blob(&b, s, bridge_device_type.config_params);
130 if (!bridge_device_type.create(b.head)) {
131 DPRINTF("Failed to create bridge for interface '%s'\n", s->e.name);
135 blob_buf_init(&b, 0);
136 blobmsg_add_string(&b, "ifname", name);
141 config_parse_interface(struct uci_section *s)
144 DPRINTF("Create interface '%s'\n", s->e.name);
146 blob_buf_init(&b, 0);
148 type = uci_lookup_option_string(uci_ctx, s, "type");
149 if (type && !strcmp(type, "bridge"))
150 if (config_parse_bridge_interface(s))
153 uci_to_blob(&b, s, &interface_attr_list);
154 interface_alloc(s->e.name, s, b.head);
158 config_init_devices(void)
160 struct uci_element *e;
162 uci_foreach_element(&uci_network->sections, e) {
163 struct uci_section *s = uci_to_section(e);
164 const struct device_type *devtype;
167 if (strcmp(s->type, "device") != 0)
170 blob_buf_init(&b, 0);
171 type = uci_lookup_option_string(uci_ctx, s, "type");
172 if (type && !strcmp(type, "bridge"))
173 devtype = &bridge_device_type;
175 devtype = &simple_device_type;
177 uci_to_blob(&b, s, devtype->config_params);
178 devtype->create(b.head);
183 config_init_interfaces(const char *name)
185 struct uci_context *ctx;
186 struct uci_package *p = NULL;
187 struct uci_element *e;
189 ctx = uci_alloc_context();
192 uci_set_confdir(ctx, "./config");
194 if (uci_load(ctx, "network", &p)) {
195 fprintf(stderr, "Failed to load network config\n");
202 config_init_devices();
204 uci_foreach_element(&p->sections, e) {
205 struct uci_section *s = uci_to_section(e);
207 if (name && strcmp(s->e.name, name) != 0)
210 if (!strcmp(s->type, "interface"))
211 config_parse_interface(s);
216 interface_start_pending();