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
);
111 config_parse_interface(struct uci_section
*s
)
113 DPRINTF("Create interface '%s'\n", s
->e
.name
);
115 blob_buf_init(&b
, 0);
116 uci_to_blob(&b
, s
, &interface_attr_list
);
117 alloc_interface(s
->e
.name
, s
, b
.head
);
121 config_device_inits(void)
123 struct uci_element
*e
;
125 uci_foreach_element(&uci_network
->sections
, e
) {
126 struct uci_section
*s
= uci_to_section(e
);
128 if (strcmp(s
->type
, "device") != 0)
131 blob_buf_init(&b
, 0);
132 uci_to_blob(&b
, s
, &device_attr_list
);
133 device_create(b
.head
, s
);
138 config_init_interfaces(const char *name
)
140 struct uci_context
*ctx
;
141 struct uci_package
*p
= NULL
;
142 struct uci_element
*e
;
144 ctx
= uci_alloc_context();
147 uci_set_confdir(ctx
, "./config");
149 if (uci_load(ctx
, "network", &p
)) {
150 fprintf(stderr
, "Failed to load network config\n");
157 config_device_inits();
159 uci_foreach_element(&p
->sections
, e
) {
160 struct uci_section
*s
= uci_to_section(e
);
162 if (name
&& strcmp(s
->e
.name
, name
) != 0)
165 if (!strcmp(s
->type
, "interface"))
166 config_parse_interface(s
);
171 start_pending_interfaces();