]> pilppa.org Git - libplp.git/blob - src/DeviceConfig.cc
api fixes and cleanups
[libplp.git] / src / DeviceConfig.cc
1 /*
2  * DeviceConfig.cc
3  *
4  *  Created on: Dec 9, 2010
5  *      Author: lamikr
6  */
7
8 #include "DeviceConfig.hh"
9
10 #include <string.h>
11 #include <malloc.h>
12
13 #include "DataReader.hh"
14 #include "FileUtil.hh"
15
16 #include "log.h"
17 #include "config.h"
18 #include "private/uci_config.h"
19
20 using namespace std;
21 using namespace plp;
22
23 string DeviceConfig::store_base_dir             = DEFAULT_STORAGE_BASE_DIR;
24 long DeviceConfig::read_interval_seconds        = 600;
25
26 ConfigHandle::ConfigHandle(uci_context *ctx_param,
27                         uci_package *pkg_param,
28                         const char *short_fname_param,
29                         const char *full_fname_param) {
30         _ctx            = ctx_param;
31         _pkg            = pkg_param;
32         short_fname     = strdup(short_fname_param);
33         full_fname      = strdup(full_fname_param);
34 }
35
36 ConfigHandle::~ConfigHandle() {
37         uci_unload(_ctx, _pkg);
38         uci_free_context(_ctx);
39         free(short_fname);
40         free(full_fname);
41 }
42
43 DeviceConfig::DeviceConfig(string device_id_param) {
44         bool    succ;
45
46         device_id       = device_id_param;
47         uci_handle      = load_device_config(device_id_param);
48         if (uci_handle != NULL) {
49                 succ    = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, device_type);
50                 if (succ == false) {
51                         log_error("Could not read device type from the configuration file.\n");
52                 }
53         }
54         else {
55                 log_error("Could not read device configuration.\n");
56         }
57 }
58
59 DeviceConfig::~DeviceConfig() {
60         bool    suc;
61
62         if (uci_handle != NULL) {
63                 suc     = uci_save_config_values(uci_handle->_ctx,
64                                         uci_handle->_pkg);
65                 if (suc == true) {
66                         log_debug("saved the configuration file: %s\n", uci_handle->full_fname);
67                 }
68                 delete(uci_handle);
69                 uci_handle      = NULL;
70         }
71 }
72
73 void DeviceConfig::set_base_dir_name(string store_param) {
74         int     pos;
75         int     b_count;
76
77         pos     = store_param.find_last_of("/");
78         b_count = store_param.length();
79         if (pos == (b_count - 1)) {
80                 store_base_dir  = store_param;
81         }
82         else {
83                 store_base_dir  = store_param + "/";
84         }
85 }
86
87 long DeviceConfig::get_read_interval_seconds() {
88         return read_interval_seconds;
89 }
90
91 void DeviceConfig::set_read_interval_seconds(long seconds_param) {
92         read_interval_seconds   = seconds_param;
93 }
94
95 string DeviceConfig::get_base_dir_name() {
96         return store_base_dir;
97 }
98
99 string DeviceConfig::get_config_path_name() {
100         string  ret_val;
101         string  path_name;
102
103         path_name       = DeviceConfig::get_base_dir_name();
104         ret_val         = FileUtil::concat_paths(path_name, device_id);
105         return ret_val;
106 }
107
108 string DeviceConfig::get_config_file_name() {
109         string  ret_val;
110         string  fname_base;
111
112         fname_base      = DEVICE_CONFIG__FILE_NAME;
113         ret_val         = get_config_path_name();
114         ret_val         = FileUtil::concat_paths(ret_val, fname_base);
115         return ret_val;
116 }
117
118 string DeviceConfig::get_pathless_config_file_name() {
119         return DEVICE_CONFIG__FILE_NAME;
120 }
121
122 bool DeviceConfig::get_config_value(string key, string& value) {
123         char    *ret;
124         bool    ret_val;
125
126         ret_val = false;
127         value.clear();
128         if ((uci_handle != NULL) &&
129             (uci_handle->_ctx != NULL) &&
130             (uci_handle->_pkg != NULL)) {
131                 if (key.empty() == false) {
132                         ret     = uci_get_config_value(uci_handle->_ctx,
133                                                 uci_handle->_pkg,
134                                                 DEVICE_CONFIG__SECTION_NAME,
135                                                 key.c_str());
136                         if (ret != NULL) {
137                                 ret_val = true;
138                                 value   = ret;
139                         }
140                 }
141                 else {
142                         log_error("Failed to read configuration value, key was empty string.\n");
143                 }
144         }
145         else {
146                 log_error("Failed to read configuration value for key: %s. Invalid handle to configuration file.\n", key.c_str());
147         }
148         return ret_val;
149 }
150
151 bool DeviceConfig::set_config_value(string key,
152                                 string value,
153                                 bool save_immediately) {
154         string  cfg_dir;
155         string  cfg_fl;
156         bool    ret_val;
157
158         cfg_dir = get_config_path_name();
159         cfg_fl  = DEVICE_CONFIG__FILE_NAME;
160         if (uci_handle != NULL) {
161                 if ((uci_handle->_ctx != NULL) &&
162                     (uci_handle->_pkg != NULL) &&
163                     (uci_handle->short_fname != NULL) &&
164                     (uci_handle->full_fname != NULL)) {
165                         //log_debug("uci_handle != null, short_name: %s, full_name: %s\n",
166                         //      uci_handle->short_fname, uci_handle->full_fname);
167                         //log_debug("key: %s, value: %s\n", key.c_str(), value.c_str());
168                         ret_val = uci_set_config_value(uci_handle->_ctx,
169                                                 uci_handle->_pkg,
170                                                 uci_handle->short_fname,
171                                                 uci_handle->full_fname,
172                                                 DEVICE_CONFIG__SECTION_TYPE,
173                                                 DEVICE_CONFIG__SECTION_NAME,
174                                                 key.c_str(),
175                                                 value.c_str(),
176                                                 save_immediately);
177                 }
178                 else {
179                         log_error("Could not set config value for device %s: key: %s, value: %s. Invalid filename in handle.\n",
180                                 device_id.c_str(), key.c_str(), value.c_str());
181                 }
182         }
183         else {
184                 log_error("Could not set config value for device %s: key: %s, value: %s. Invalid handle to config file\n",
185                         device_id.c_str(), key.c_str(), value.c_str());
186         }
187         return ret_val;
188 }
189
190 // TODO: This function should be moved else where or it should be dynamic...
191 EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
192         if (device_type.empty() == false) {
193                 if (device_type.compare("Counter Device") == 0) {
194                         return DELTA;
195                 }
196         }
197         return MEAN;
198 }
199
200 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
201         int                     err_flg;
202         struct uci_context      *ctx;
203         struct uci_package      *pkg;
204         string                  fname_base;
205         string                  fname_full;
206         string                  cfg_dir;
207         ConfigHandle            *ret_val;
208         FILE                    *fp;
209
210         ret_val = NULL;
211         cfg_dir = get_config_path_name();
212         if (cfg_dir.empty() == false) {
213                 fname_full      = get_config_file_name();
214                 if (FileUtil::file_exist(fname_full.c_str(), false) == false)
215                         FileUtil::mkfile(fname_full.c_str(), false);
216                 if (access(fname_full.c_str(), R_OK) == 0) {
217                         ctx     = uci_alloc_context();
218                         if (ctx != NULL) {
219                                 log_debug("configuration file: %s\n", fname_full.c_str());
220                                 uci_set_confdir(ctx, cfg_dir.c_str());
221                                 err_flg = uci_load(ctx, fname_full.c_str(), &pkg);
222                                 if (err_flg == UCI_OK) {
223                                         //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
224                                         fname_base      = get_pathless_config_file_name();
225                                         ret_val = new ConfigHandle(ctx,
226                                                                 pkg,
227                                                                 fname_base.c_str(),
228                                                                 fname_full.c_str());
229                                 }
230                                 else {
231                                         log_debug("Failed to load device configuration: %s, err code: %d.\n", fname_full.c_str(), UCI_OK);
232                                         fp      = fopen(fname_full.c_str(), "w+");
233                                         if (fp != NULL)
234                                                 fclose(fp);
235                                         //uci_free_context(ctx);
236                                 }
237                         }
238                         else {
239                                 log_error("Failed to load device configuration, memory allocation error.\n");
240                                 fp      = fopen(fname_full.c_str(), "w+");
241                                 if (fp != NULL)
242                                         fclose(fp);
243                         }
244                 }
245                 else {
246                         log_error("Failed to load device configuration, file does not exist: %s.\n", fname_full.c_str());
247                 }
248         }
249         return ret_val;
250 }
251
252 DeviceConfig *DeviceConfig::get_device_config(string device_id_param) {
253         DeviceConfig    *ret_val;
254
255         ret_val = new DeviceConfig(device_id_param);
256         return ret_val;
257 }