9 struct uci_context *uci_ctx;
10 static struct uci_package *uci_network;
11 bool config_init = false;
12 static struct blob_buf b;
15 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
16 const char *name, enum blobmsg_type type)
22 case BLOBMSG_TYPE_STRING:
23 blobmsg_add_string(b, name, str);
25 case BLOBMSG_TYPE_BOOL:
26 if (!strcmp(str, "true") || !strcmp(str, "1"))
28 else if (!strcmp(str, "false") || !strcmp(str, "0"))
33 blobmsg_add_u8(b, name, intval);
35 case BLOBMSG_TYPE_INT32:
36 intval = strtol(str, &err, 0);
40 blobmsg_add_u32(b, name, intval);
47 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
48 enum blobmsg_type type)
50 struct uci_element *e;
51 char *str, *next, *word;
53 if (o->type == UCI_TYPE_LIST) {
54 uci_foreach_element(&o->v.list, e) {
55 uci_attr_to_blob(b, e->name, NULL, type);
60 str = strdup(o->v.string);
63 while ((word = strsep(&next, " \t")) != NULL) {
67 uci_attr_to_blob(b, word, NULL, type);
73 static void __uci_to_blob(struct blob_buf *b, struct uci_section *s,
74 const struct config_param_list *p)
76 const struct blobmsg_policy *attr = NULL;
77 struct uci_element *e;
82 uci_foreach_element(&s->options, e) {
83 for (i = 0; i < p->n_params; i++) {
85 if (!strcmp(attr->name, e->name))
94 if (attr->type == BLOBMSG_TYPE_ARRAY) {
98 array = blobmsg_open_array(b, attr->name);
99 uci_array_to_blob(b, o, p->info[i].type);
100 blobmsg_close_array(b, array);
104 if (o->type == UCI_TYPE_LIST)
107 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
111 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
112 const struct config_param_list *p)
116 __uci_to_blob(b, s, p);
117 for (i = 0; i < p->n_next; i++)
118 uci_to_blob(b, s, p->next[i]);
122 config_parse_bridge_interface(struct uci_section *s)
126 name = alloca(strlen(s->e.name) + 4);
127 sprintf(name, "br-%s", s->e.name);
128 blobmsg_add_string(&b, "name", name);
130 uci_to_blob(&b, s, bridge_device_type.config_params);
131 if (!bridge_device_type.create(b.head)) {
132 DPRINTF("Failed to create bridge for interface '%s'\n", s->e.name);
136 blob_buf_init(&b, 0);
137 blobmsg_add_string(&b, "ifname", name);
142 config_parse_interface(struct uci_section *s)
144 struct interface *iface;
147 DPRINTF("Create interface '%s'\n", s->e.name);
149 blob_buf_init(&b, 0);
151 type = uci_lookup_option_string(uci_ctx, s, "type");
152 if (type && !strcmp(type, "bridge"))
153 if (config_parse_bridge_interface(s))
156 uci_to_blob(&b, s, &interface_attr_list);
157 iface = interface_alloc(s->e.name, b.head);
161 blob_buf_init(&b, 0);
162 if (iface->proto_handler && iface->proto_handler->config_params)
163 uci_to_blob(&b, s, iface->proto_handler->config_params);
165 proto_init_interface(iface, b.head);
169 config_init_devices(void)
171 struct uci_element *e;
173 uci_foreach_element(&uci_network->sections, e) {
174 struct uci_section *s = uci_to_section(e);
175 const struct device_type *devtype;
178 if (strcmp(s->type, "device") != 0)
181 blob_buf_init(&b, 0);
182 type = uci_lookup_option_string(uci_ctx, s, "type");
183 if (type && !strcmp(type, "bridge"))
184 devtype = &bridge_device_type;
186 devtype = &simple_device_type;
188 uci_to_blob(&b, s, devtype->config_params);
189 devtype->create(b.head);
194 config_init_interfaces(const char *name)
196 struct uci_context *ctx;
197 struct uci_package *p = NULL;
198 struct uci_element *e;
200 ctx = uci_alloc_context();
203 uci_set_confdir(ctx, "./config");
205 if (uci_load(ctx, "network", &p)) {
206 fprintf(stderr, "Failed to load network config\n");
213 config_init_devices();
215 uci_foreach_element(&p->sections, e) {
216 struct uci_section *s = uci_to_section(e);
218 if (name && strcmp(s->e.name, name) != 0)
221 if (!strcmp(s->type, "interface"))
222 config_parse_interface(s);
227 interface_start_pending();