2 * ucimap-example - sample code for the ucimap library
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
23 struct ucimap_section_data map
;
24 struct list_head list
;
25 struct list_head alias
;
30 unsigned char ipaddr
[4];
33 struct ucimap_list
*aliases
;
37 struct ucimap_section_data map
;
38 struct list_head list
;
41 struct uci_network
*interface
;
45 network_parse_ip(void *section
, struct uci_optmap
*om
, union ucimap_data
*data
, const char *str
)
47 struct uci_network
*net
= section
;
48 unsigned char *target
= (unsigned char *) data
->s
;
52 if (sscanf(str
, "%d.%d.%d.%d", &tmp
[0], &tmp
[1], &tmp
[2], &tmp
[3]) != 4)
55 for (i
= 0; i
< 4; i
++)
56 target
[i
] = (char) tmp
[i
];
62 network_init_interface(struct uci_map
*map
, void *section
, struct uci_section
*s
)
64 struct uci_network
*net
= section
;
66 INIT_LIST_HEAD(&net
->list
);
67 INIT_LIST_HEAD(&net
->alias
);
68 net
->name
= s
->e
.name
;
74 network_init_alias(struct uci_map
*map
, void *section
, struct uci_section
*s
)
76 struct uci_alias
*alias
= section
;
78 INIT_LIST_HEAD(&alias
->list
);
79 alias
->name
= s
->e
.name
;
84 network_add_interface(struct uci_map
*map
, void *section
)
86 struct uci_network
*net
= section
;
88 list_add(&net
->list
, &ifs
);
94 network_add_alias(struct uci_map
*map
, void *section
)
96 struct uci_alias
*a
= section
;
99 list_add(&a
->list
, &a
->interface
->alias
);
104 static struct ucimap_section_data
*
105 network_allocate(struct uci_map
*map
, struct uci_sectionmap
*sm
, struct uci_section
*s
)
107 struct uci_network
*p
= malloc(sizeof(struct uci_network
));
108 memset(p
, 0, sizeof(struct uci_network
));
113 struct uci_optmap map
;
117 static struct uci_sectionmap network_interface
;
118 static struct uci_sectionmap network_alias
;
120 static struct my_optmap network_interface_options
[] = {
123 UCIMAP_OPTION(struct uci_network
, proto
),
124 .type
= UCIMAP_STRING
,
131 UCIMAP_OPTION(struct uci_network
, ifname
),
132 .type
= UCIMAP_STRING
,
138 UCIMAP_OPTION(struct uci_network
, ipaddr
),
139 .type
= UCIMAP_CUSTOM
,
141 .parse
= network_parse_ip
,
146 UCIMAP_OPTION(struct uci_network
, enabled
),
153 UCIMAP_OPTION(struct uci_network
, test
),
160 UCIMAP_OPTION(struct uci_network
, aliases
),
161 .type
= UCIMAP_LIST
| UCIMAP_SECTION
,
162 .data
.sm
= &network_alias
167 static struct uci_sectionmap network_interface
= {
168 UCIMAP_SECTION(struct uci_network
, map
),
170 .alloc
= network_allocate
,
171 .init
= network_init_interface
,
172 .add
= network_add_interface
,
173 .options
= &network_interface_options
[0].map
,
174 .n_options
= ARRAY_SIZE(network_interface_options
),
175 .options_size
= sizeof(struct my_optmap
)
178 static struct uci_optmap network_alias_options
[] = {
180 UCIMAP_OPTION(struct uci_alias
, interface
),
181 .type
= UCIMAP_SECTION
,
182 .data
.sm
= &network_interface
186 static struct uci_sectionmap network_alias
= {
187 UCIMAP_SECTION(struct uci_alias
, map
),
189 .options
= network_alias_options
,
190 .init
= network_init_alias
,
191 .add
= network_add_alias
,
192 .n_options
= ARRAY_SIZE(network_alias_options
),
195 static struct uci_sectionmap
*network_smap
[] = {
200 static struct uci_map network_map
= {
201 .sections
= network_smap
,
202 .n_sections
= ARRAY_SIZE(network_smap
),
206 int main(int argc
, char **argv
)
208 struct uci_context
*ctx
;
209 struct uci_package
*pkg
;
210 struct list_head
*p
, *p2
;
211 struct uci_network
*net
;
212 struct uci_alias
*alias
;
215 INIT_LIST_HEAD(&ifs
);
216 ctx
= uci_alloc_context();
217 ucimap_init(&network_map
);
219 uci_load(ctx
, "network", &pkg
);
221 ucimap_parse(&network_map
, pkg
);
223 list_for_each(p
, &ifs
) {
224 net
= list_entry(p
, struct uci_network
, list
);
225 printf("New network section '%s'\n"
228 " ipaddr: %d.%d.%d.%d\n"
234 net
->ipaddr
[0], net
->ipaddr
[1],
235 net
->ipaddr
[2], net
->ipaddr
[3],
237 (net
->enabled
? "on" : "off"));
239 for (i
= 0; i
< net
->aliases
->n_items
; i
++) {
240 alias
= net
->aliases
->item
[i
].section
;
241 printf("New alias: %s\n", alias
->name
);
244 net
->ipaddr
= "2.3.4.5";
245 ucimap_set_changed(net
, &net
->ipaddr
);
246 ucimap_store_section(&network_map
, pkg
, net
);
253 ucimap_cleanup(&network_map
);
254 uci_free_context(ctx
);