]> pilppa.org Git - libplp.git/blob - src/private/uci_config.c
cleanups for devices with no data
[libplp.git] / src / private / uci_config.c
1 /*
2  * uci_config.c
3  *
4  *  Created on: Aug 2, 2012
5  *      Author: lamikr
6  */
7
8 #include "uci_config.h"
9 #include "../log.h"
10 #include "../retval.h"
11
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)
17 {
18         struct uci_ptr  ptr;
19         int             ret_val;
20         char            *cmd_data;
21         int             len;
22
23         ret_val = -1;
24         if ((ctx != NULL) &&
25             (conf_fname_base != NULL) &&
26             (section_type != NULL) &&
27             (section_name != NULL)) {
28                 len             = strlen(conf_fname_base);
29                 len             = len + 1;
30                 len             = len + strlen(section_type);
31                 len             = len + 1;
32                 len             = len + strlen(section_name);
33                 len             = len + 1;
34                 cmd_data        = malloc(len);
35                 if (cmd_data != NULL) {
36                         snprintf(cmd_data,
37                                 len,
38                                 "%s.%s=%s",
39                                 conf_fname_base,
40                                 section_name,
41                                 section_type);
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);
47                                         }
48                                 }
49                         }
50                         free(cmd_data);
51                 }
52         }
53         return ret_val;
54 }
55
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,
62                         const char *key,
63                         const char *value,
64                         const bool save_immediately) {
65         struct uci_section      *sct;
66         struct uci_section      *tmp_sct;
67         int                     err_flg;
68         struct uci_element      *elem;
69         struct uci_ptr          ptr;
70         bool                    ret_val;
71
72         ret_val = false;
73         err_flg = UCI_OK;
74         if ((ctx != NULL) &&
75             (pkg != NULL) &&
76             (section_type != NULL) &&
77             (section_name != NULL) &&
78             (key != NULL) &&
79             (value != NULL)) {
80                 sct     = 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) {
85                                         sct     = tmp_sct;
86                                         break;
87                                 }
88                                 else {
89                                         //TODO: uci_free_section(tmp_sct); ?, or will uci_free_contect(ctx) also free the section
90                                 }
91                         }
92                 }
93                 if (sct == NULL) {
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,
98                                                 conf_fname_base,
99                                                 section_type,
100                                                 section_name,
101                                                 false);
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) {
106                                                 sct     = tmp_sct;
107                                                 break;
108                                         }
109                                 }
110                         }
111                         //TODO: uci_free_section(sct); ?, or will uci_free_contect(ctx) also free the section
112                 }
113                 if (err_flg == UCI_OK) {
114                         memset(&ptr, 0, sizeof(ptr));
115                         ptr.package     = pkg->e.name;
116                         ptr.section     = sct->e.name;
117                         ptr.option      = key;
118                         err_flg         = uci_lookup_ptr(ctx, &ptr, NULL, false);
119                         if (err_flg == UCI_OK) {
120                                 ptr.value       = value;
121                                 err_flg         = uci_set(ctx, &ptr);
122                                 log_debug("file: %s, section_key: %s/%s: value: %s.\n",
123                                         conf_fname_full,
124                                         section_name,
125                                         key,
126                                         value);
127                                 if (err_flg == UCI_OK) {
128                                         if (save_immediately == true) {
129                                                 err_flg = uci_save(ctx, pkg);
130                                                 if (err_flg == UCI_OK) {
131                                                         ret_val = true;
132                                                 }
133                                                 else {
134                                                         log_error("Failed to set value to configuration file: %s. Could not save the file.\n", conf_fname_full);
135                                                 }
136                                         }
137                                         else {
138                                                 ret_val = true;
139                                         }
140                                 }
141                                 else {
142                                         log_error("Failed to set value to configuration file: %s. Could not set new value.\n", conf_fname_full);
143                                 }
144                         }
145                         else {
146                                 log_error("Failed to set value to configuration file: %s. Could not look-up pointer for package %s section %s.\n",
147                                         conf_fname_full,
148                                         pkg->e.name,
149                                         sct->e.name);
150                         }
151                 }
152                 else {
153                         log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", conf_fname_full, section_name);
154                 }
155         }
156         else {
157                 log_error("Failed to set value to configuration file, invalid parameters\n");
158         }
159         return ret_val;
160 }
161
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,
166                         int *err_flg) {
167         struct uci_section      *section;
168         struct uci_option       *option;
169         char                    *ret_val;
170
171         ret_val = NULL;
172         *err_flg        = PLP_OK;
173         if ((ctx != NULL) &&
174             (pkg != NULL)) {
175                 section = uci_lookup_section(ctx,
176                                         pkg,
177                                         section_name);
178                 if (section != NULL) {
179                         option  = uci_lookup_option(ctx,
180                                                 section,
181                                                 key_name);
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;
187                                                 break;
188                                         default:
189                                                 log_error("Failed to read configuration value for key: %s\n", key_name);
190                                                 *err_flg        = PLP_ERR_IO;
191                                                 break;
192                                 }
193                         }
194                         else {
195                                 *err_flg        = PLP_ERR_DATA_NOT_FOUND;
196                                 //log_error("Failed to find configuration key: %s\n", key_name);
197                         }
198                 }
199                 else {
200                         *err_flg        = PLP_ERR_DATA_NOT_FOUND;
201                         //log_error("Failed to find configuration section name: %s\n", section_name);
202                 }
203         }
204         return ret_val;
205 }
206
207 bool uci_save_config_values(struct uci_context *ctx,
208                         struct uci_package *pkg) {
209         bool    ret_val;
210         int     err_flg;
211
212         if ((ctx != NULL) &&
213             (pkg != NULL)) {
214                 err_flg = uci_save(ctx, pkg);
215                 if (err_flg == UCI_OK) {
216                         ret_val = true;
217                 }
218                 else {
219                         log_error("Could not save configuration file.\n");
220                 }
221         }
222         return ret_val;
223 }