]> pilppa.org Git - libplp.git/blob - src_test/test_config2.c
add read_interval DeviceConfig methods
[libplp.git] / src_test / test_config2.c
1 /*
2  * test_w1.cc
3  *
4  *  Created on: Oct 20, 2010
5  *      Author: lamikr
6  */
7 #include <string.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <uci.h>
12
13 #include "log.h"
14 #include "config.h"
15
16 /*
17 static int uci_do_add(int argc, char **argv)
18 {
19         struct uci_package *p = NULL;
20         struct uci_section *s = NULL;
21         int ret;
22
23         if (argc != 3)
24                 return 255;
25
26         ret = uci_load(ctx, argv[1], &p);
27         if (ret != UCI_OK)
28                 goto done;
29
30         ret = uci_add_section(ctx, p, argv[2], &s);
31         if (ret != UCI_OK)
32                 goto done;
33
34         ret = uci_save(ctx, p);
35
36 done:
37         if (ret != UCI_OK)
38                 cli_perror();
39         else if (s)
40                 fprintf(stdout, "%s\n", s->e.name);
41         return ret;
42 }
43 */
44
45 static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name)
46 {
47         struct uci_ptr          ptr;
48         int                     ret_val;
49         char                    *cmd_data;
50         int                     len;
51
52         ret_val = -1;
53         if ((ctx != NULL) &&
54             (conf_file_name != NULL) &&
55             (section_type != NULL) &&
56             (section_name != NULL)) {
57                 len             = strlen(conf_file_name);
58                 len             = len + 1;
59                 len             = len + strlen(section_type);
60                 len             = len + 1;
61                 len             = len + strlen(section_name);
62                 len             = len + 1;
63                 cmd_data        = malloc(len);
64                 if (cmd_data != NULL) {
65                         snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
66                         if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
67                                 ret_val = uci_set(ctx, &ptr);
68                                 if (ret_val == UCI_OK) {
69                                 }
70                         }
71                         free(cmd_data);
72                 }
73         }
74         return ret_val;
75 }
76
77 bool set_config_value2(const char *conf_dir_name,
78                         const char *conf_file_name,
79                         const char *section_type,
80                         const char *section_name,
81                         const char *key,
82                         const char *value) {
83         struct uci_context      *ctx;
84         struct uci_package      *pkg;
85         struct uci_section      *sct;
86         struct uci_section      *tmp_sct;
87         int                     err_flg;
88         char                    *fname;
89         int                     b_count;
90         struct uci_element      *elem;
91         struct uci_ptr          ptr;
92         FILE                    *fp;
93         bool                    ret_val;
94
95         ret_val = false;
96         if ((conf_dir_name != NULL) &&
97             (conf_file_name != NULL) &&
98             (section_type != NULL) &&
99             (section_name != NULL) &&
100             (key != NULL) &&
101             (value != NULL)) {
102                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
103                 fname   = (char *)calloc(1, b_count);
104                 if (fname != NULL) {
105                         strncpy(fname, conf_dir_name, b_count);
106                         strncat(fname, "/", 1);
107                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
108                         ctx     = uci_alloc_context();
109                         if (ctx != NULL) {
110                                 sct     = NULL;
111                                 uci_set_confdir(ctx, conf_dir_name);
112                                 if (access(fname, W_OK) != 0) {
113                                         if (access(fname, F_OK) != 0) {
114                                                 fp      = fopen(fname, "w+");
115                                                 fclose(fp);
116                                         }
117                                 }
118                                 if (access(fname, W_OK) == 0) {
119                                         err_flg = uci_load(ctx, fname, &pkg);
120                                         uci_foreach_element(&pkg->sections, elem) {
121                                                 tmp_sct = uci_to_section(elem);
122                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
123                                                         sct     = tmp_sct;
124                                                         break;
125                                                 }
126                                         }
127                                         if (sct == NULL) {
128                                                 //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
129                                                 //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
130                                                 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
131                                                 if (err_flg == UCI_OK) {
132                                                         uci_foreach_element(&pkg->sections, elem) {
133                                                                 tmp_sct = uci_to_section(elem);
134                                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
135                                                                         sct     = tmp_sct;
136                                                                         break;
137                                                                 }
138                                                         }
139                                                 }
140                                         }
141                                         if (err_flg == 0) {
142                                                 memset(&ptr, 0, sizeof(ptr));
143                                                 ptr.package     = pkg->e.name;
144                                                 ptr.section     = sct->e.name;
145                                                 ptr.option      = key;
146
147                                                 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
148                                                         ptr.value       = value;
149                                                         uci_set(ctx, &ptr);
150                                                         uci_save(ctx, pkg);
151                                                         ret_val = true;
152                                                         log_debug("Created configuration section %s to configuration file: %s\n", section_name, fname);
153                                                 }
154                                                 else {
155                                                         log_error("Could not write to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name);
156                                                 }
157                                                 uci_free_context(ctx);
158                                         }
159                                         else {
160                                                 log_error("Could not write to configuration file: %s\n. Could not create section %s.\n", fname, section_name);
161                                         }
162                                 }
163                                 else {
164                                         log_error("Could not write to configuration file: %s. File does not exist or is not writable.\n", fname);
165                                 }
166                         }
167                         free(fname);
168                 }
169                 else {
170                         log_error("Could not change config value, out of memory\n");
171                 }
172         }
173         else {
174                 log_error("Could not change config value, invalid parameters\n");
175         }
176         return ret_val;
177 }
178
179 void test_config() {
180         char work_dir[FILENAME_MAX];
181
182         getcwd(work_dir, sizeof(work_dir));
183         printf("working directory: %s\n", work_dir);
184
185         set_config_value2(work_dir,
186                         "dev_cfg_txt",
187                         "mysection_type",
188                         "mysection_name",
189                         "myoption_name",
190                         "my_option_value");
191         set_config_value2(work_dir,
192                         "dev_cfg_txt",
193                         "mysection_type",
194                         "mysection_name",
195                         "myoption_name",
196                         "my_option_value2");
197 }
198
199 int main(int argc, char** argv) {
200         test_config();
201         return 0;
202 }