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 proto_init_interface(iface
, s
);
165 config_init_devices(void)
167 struct uci_element
*e
;
169 uci_foreach_element(&uci_network
->sections
, e
) {
170 struct uci_section
*s
= uci_to_section(e
);
171 const struct device_type
*devtype
;
174 if (strcmp(s
->type
, "device") != 0)
177 blob_buf_init(&b
, 0);
178 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
179 if (type
&& !strcmp(type
, "bridge"))
180 devtype
= &bridge_device_type
;
182 devtype
= &simple_device_type
;
184 uci_to_blob(&b
, s
, devtype
->config_params
);
185 devtype
->create(b
.head
);
190 config_init_interfaces(const char *name
)
192 struct uci_context
*ctx
;
193 struct uci_package
*p
= NULL
;
194 struct uci_element
*e
;
196 ctx
= uci_alloc_context();
199 uci_set_confdir(ctx
, "./config");
201 if (uci_load(ctx
, "network", &p
)) {
202 fprintf(stderr
, "Failed to load network config\n");
209 config_init_devices();
211 uci_foreach_element(&p
->sections
, e
) {
212 struct uci_section
*s
= uci_to_section(e
);
214 if (name
&& strcmp(s
->e
.name
, name
) != 0)
217 if (!strcmp(s
->type
, "interface"))
218 config_parse_interface(s
);
223 interface_start_pending();