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 list_head list;
24 struct list_head alias;
32 struct ucimap_list *aliases;
36 struct list_head list;
39 struct uci_network *interface;
43 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
45 struct uci_network *net = section;
47 INIT_LIST_HEAD(&net->list);
48 INIT_LIST_HEAD(&net->alias);
49 net->name = s->e.name;
55 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
57 struct uci_alias *alias = section;
59 INIT_LIST_HEAD(&alias->list);
60 alias->name = s->e.name;
65 network_add_interface(struct uci_map *map, void *section)
67 struct uci_network *net = section;
69 list_add(&net->list, &ifs);
75 network_add_alias(struct uci_map *map, void *section)
77 struct uci_alias *a = section;
80 list_add(&a->list, &a->interface->alias);
85 static struct uci_sectmap network_interface;
86 static struct uci_sectmap network_alias;
88 static struct uci_optmap network_interface_options[] = {
90 UCIMAP_OPTION(struct uci_network, proto),
91 .type = UCIMAP_STRING,
96 UCIMAP_OPTION(struct uci_network, ifname),
97 .type = UCIMAP_STRING,
101 UCIMAP_OPTION(struct uci_network, ipaddr),
102 .type = UCIMAP_STRING,
106 UCIMAP_OPTION(struct uci_network, enabled),
111 UCIMAP_OPTION(struct uci_network, test),
116 UCIMAP_OPTION(struct uci_network, aliases),
117 .type = UCIMAP_LIST | UCIMAP_SECTION,
118 .data.sm = &network_alias
122 static struct uci_sectmap network_interface = {
124 .options = network_interface_options,
125 .alloc_len = sizeof(struct uci_network),
126 .init_section = network_init_interface,
127 .add_section = network_add_interface,
128 .n_options = ARRAY_SIZE(network_interface_options),
131 static struct uci_optmap network_alias_options[] = {
133 UCIMAP_OPTION(struct uci_alias, interface),
134 .type = UCIMAP_SECTION,
135 .data.sm = &network_interface
139 static struct uci_sectmap network_alias = {
141 .options = network_alias_options,
142 .alloc_len = sizeof(struct uci_network),
143 .init_section = network_init_alias,
144 .add_section = network_add_alias,
145 .n_options = ARRAY_SIZE(network_alias_options),
148 static struct uci_sectmap *network_smap[] = {
153 static struct uci_map network_map = {
154 .sections = network_smap,
155 .n_sections = ARRAY_SIZE(network_smap),
159 int main(int argc, char **argv)
161 struct uci_context *ctx;
162 struct uci_package *pkg;
163 struct list_head *p, *p2;
164 struct uci_network *net;
165 struct uci_alias *alias;
168 INIT_LIST_HEAD(&ifs);
169 ctx = uci_alloc_context();
170 ucimap_init(&network_map);
172 uci_load(ctx, "network", &pkg);
174 ucimap_parse(&network_map, pkg);
176 list_for_each(p, &ifs) {
177 net = list_entry(p, struct uci_network, list);
178 printf("New network section '%s'\n"
189 (net->enabled ? "on" : "off"));
191 for (i = 0; i < net->aliases->n_items; i++) {
192 alias = net->aliases->item[i].section;
193 printf("New alias: %s\n", alias->name);
196 net->ipaddr = "2.3.4.5";
197 ucimap_set_changed(net, &net->ipaddr);
198 ucimap_store_section(&network_map, pkg, net);
205 ucimap_cleanup(&network_map);
206 uci_free_context(ctx);