4 * Created on: Aug 2, 2012
8 #include "uci_config.h"
11 int uci_create_named_section(struct uci_context *ctx,
12 const char *conf_fname_base,
13 const char *section_type,
14 const char *section_name,
15 bool save_immediately)
24 (conf_fname_base != NULL) &&
25 (section_type != NULL) &&
26 (section_name != NULL)) {
27 len = strlen(conf_fname_base);
29 len = len + strlen(section_type);
31 len = len + strlen(section_name);
33 cmd_data = malloc(len);
34 if (cmd_data != NULL) {
41 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
42 ret_val = uci_set(ctx, &ptr);
43 if (ret_val == UCI_OK) {
44 if (save_immediately) {
45 ret_val = uci_save(ctx, ptr.p);
55 bool uci_set_config_value(struct uci_context *ctx,
56 struct uci_package *pkg,
57 const char *conf_fname_base,
58 const char *conf_fname_full,
59 const char *section_type,
60 const char *section_name,
63 const bool save_immediately) {
64 struct uci_section *sct;
65 struct uci_section *tmp_sct;
67 struct uci_element *elem;
75 (section_type != NULL) &&
76 (section_name != NULL) &&
80 uci_foreach_element(&pkg->sections, elem) {
81 tmp_sct = uci_to_section(elem);
82 if (tmp_sct != NULL) {
83 if (strcmp(tmp_sct->type, section_type) == 0) {
88 //TODO: uci_free_section(tmp_sct); ?, or will uci_free_contect(ctx) also free the section
93 //log_debug("Creating new section %s to configuration file: %s\n", section_name, conf_fname_full);
94 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
95 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
96 err_flg = uci_create_named_section(ctx,
101 if (err_flg == UCI_OK) {
102 uci_foreach_element(&pkg->sections, elem) {
103 tmp_sct = uci_to_section(elem);
104 if (strcmp(tmp_sct->type, section_type) == 0) {
110 //TODO: uci_free_section(sct); ?, or will uci_free_contect(ctx) also free the section
112 if (err_flg == UCI_OK) {
113 memset(&ptr, 0, sizeof(ptr));
114 ptr.package = pkg->e.name;
115 ptr.section = sct->e.name;
117 err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false);
118 if (err_flg == UCI_OK) {
120 err_flg = uci_set(ctx, &ptr);
121 log_debug("file: %s, section_key: %s/%s: value: %s.\n",
126 if (err_flg == UCI_OK) {
127 if (save_immediately == true) {
128 err_flg = uci_save(ctx, pkg);
129 if (err_flg == UCI_OK) {
133 log_error("Failed to set value to configuration file: %s. Could not save the file.\n", conf_fname_full);
141 log_error("Failed to set value to configuration file: %s. Could not set new value.\n", conf_fname_full);
145 log_error("Failed to set value to configuration file: %s. Could not look-up pointer for package %s section %s.\n",
152 log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", conf_fname_full, section_name);
156 log_error("Failed to set value to configuration file, invalid parameters\n");
161 char *uci_get_config_value(struct uci_context *ctx,
162 struct uci_package *pkg,
163 const char *section_name,
164 const char *key_name) {
165 struct uci_section *section;
166 struct uci_option *option;
172 section = uci_lookup_section(ctx,
175 if (section != NULL) {
176 option = uci_lookup_option(ctx,
179 if (option != NULL) {
180 switch (option->type) {
181 case UCI_TYPE_STRING:
182 //log_debug("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
183 ret_val = option->v.string;
186 log_error("Failed to read configuration value for key: %s\n", key_name);
191 log_error("Failed to find configuration key: %s\n", key_name);
195 log_error("Failed to find configuration section name: %s\n", section_name);
201 bool uci_save_config_values(struct uci_context *ctx,
202 struct uci_package *pkg) {
208 err_flg = uci_save(ctx, pkg);
209 if (err_flg == UCI_OK) {
213 log_error("Could not save configuration file.\n");