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