4 * Created on: Aug 2, 2012
8 #include "uci_config.h"
10 #include "../retval.h"
12 int uci_create_named_section(struct uci_context *ctx,
13 const char *conf_fname_base,
14 const char *section_type,
15 const char *section_name,
16 bool save_immediately)
25 (conf_fname_base != NULL) &&
26 (section_type != NULL) &&
27 (section_name != NULL)) {
28 len = strlen(conf_fname_base);
30 len = len + strlen(section_type);
32 len = len + strlen(section_name);
34 cmd_data = malloc(len);
35 if (cmd_data != NULL) {
42 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
43 ret_val = uci_set(ctx, &ptr);
44 if (ret_val == UCI_OK) {
45 if (save_immediately) {
46 ret_val = uci_save(ctx, ptr.p);
56 bool uci_set_config_value(struct uci_context *ctx,
57 struct uci_package *pkg,
58 const char *conf_fname_base,
59 const char *conf_fname_full,
60 const char *section_type,
61 const char *section_name,
64 const bool save_immediately) {
65 struct uci_section *sct;
66 struct uci_section *tmp_sct;
68 struct uci_element *elem;
76 (section_type != NULL) &&
77 (section_name != NULL) &&
81 uci_foreach_element(&pkg->sections, elem) {
82 tmp_sct = uci_to_section(elem);
83 if (tmp_sct != NULL) {
84 if (strcmp(tmp_sct->type, section_type) == 0) {
89 //TODO: uci_free_section(tmp_sct); ?, or will uci_free_contect(ctx) also free the section
94 //log_debug("Creating new section %s to configuration file: %s\n", section_name, conf_fname_full);
95 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
96 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
97 err_flg = uci_create_named_section(ctx,
102 if (err_flg == UCI_OK) {
103 uci_foreach_element(&pkg->sections, elem) {
104 tmp_sct = uci_to_section(elem);
105 if (strcmp(tmp_sct->type, section_type) == 0) {
111 //TODO: uci_free_section(sct); ?, or will uci_free_contect(ctx) also free the section
113 if (err_flg == UCI_OK) {
114 memset(&ptr, 0, sizeof(ptr));
115 ptr.package = pkg->e.name;
116 ptr.section = sct->e.name;
118 err_flg = uci_lookup_ptr(ctx, &ptr, NULL, false);
119 if (err_flg == UCI_OK) {
121 err_flg = uci_set(ctx, &ptr);
122 log_debug("file: %s, section_key: %s/%s: value: %s.\n",
127 if (err_flg == UCI_OK) {
128 if (save_immediately == true) {
129 err_flg = uci_save(ctx, pkg);
130 if (err_flg == UCI_OK) {
134 log_error("Failed to set value to configuration file: %s. Could not save the file.\n", conf_fname_full);
142 log_error("Failed to set value to configuration file: %s. Could not set new value.\n", conf_fname_full);
146 log_error("Failed to set value to configuration file: %s. Could not look-up pointer for package %s section %s.\n",
153 log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", conf_fname_full, section_name);
157 log_error("Failed to set value to configuration file, invalid parameters\n");
162 char *uci_get_config_value(struct uci_context *ctx,
163 struct uci_package *pkg,
164 const char *section_name,
165 const char *key_name,
167 struct uci_section *section;
168 struct uci_option *option;
175 section = uci_lookup_section(ctx,
178 if (section != NULL) {
179 option = uci_lookup_option(ctx,
182 if (option != NULL) {
183 switch (option->type) {
184 case UCI_TYPE_STRING:
185 //log_debug("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
186 ret_val = option->v.string;
189 log_error("Failed to read configuration value for key: %s\n", key_name);
190 *err_flg = PLP_ERR_IO;
195 *err_flg = PLP_ERR_DATA_NOT_FOUND;
196 //log_error("Failed to find configuration key: %s\n", key_name);
200 *err_flg = PLP_ERR_DATA_NOT_FOUND;
201 //log_error("Failed to find configuration section name: %s\n", section_name);
207 bool uci_save_config_values(struct uci_context *ctx,
208 struct uci_package *pkg) {
214 err_flg = uci_save(ctx, pkg);
215 if (err_flg == UCI_OK) {
219 log_error("Could not save configuration file.\n");