]> pilppa.org Git - libplp.git/blob - src/DeviceConfig.cc
Updates to set_config_value parameter
[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 <stdbool.h>
11 #include <string.h>
12 #include <malloc.h>
13
14 #include "log.h"
15
16 #include "DataReader.hh"
17 #include "FileUtil.hh"
18
19 using namespace std;
20 using namespace plp;
21
22 string DeviceConfig::store_base_dir     = DEFAULT_STORAGE_BASE_DIR;
23
24 ConfigHandle::ConfigHandle(uci_context *ctx_param, uci_package *pkg_param) {
25         _ctx    = ctx_param;
26         _pkg    = pkg_param;
27 }
28
29 ConfigHandle::~ConfigHandle() {
30         uci_unload(_ctx, _pkg);
31         uci_free_context(_ctx);
32 }
33
34 DeviceConfig::DeviceConfig(string device_id_param) {
35         device_id       = device_id_param;
36         uci_handle      = load_device_config(device_id_param);
37         if (uci_handle != NULL) {
38                 device_type     = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
39         }
40         else {
41                 log_error("Could not read device configuration.\n");
42         }
43 }
44
45 DeviceConfig::~DeviceConfig() {
46         if (uci_handle != NULL) {
47                 delete(uci_handle);
48                 uci_handle      = NULL;
49         }
50 }
51
52 void DeviceConfig::set_base_dir_name(string store_param) {
53         int     pos;
54         int     b_count;
55
56         pos     = store_param.find_last_of("/");
57         b_count = store_param.length();
58         if (pos == (b_count - 1)) {
59                 store_base_dir  = store_param;
60         }
61         else {
62                 store_base_dir  = store_param + "/";
63         }
64 }
65
66 string DeviceConfig::get_base_dir_name() {
67         return store_base_dir;
68 }
69
70 string DeviceConfig::get_dir_name(string device_id_param) {
71         string  ret_val;
72         string  d_name;
73
74         d_name  = DeviceConfig::get_base_dir_name();
75         ret_val = FileUtil::concat_paths(d_name, device_id_param);
76         return ret_val;
77 }
78
79 string DeviceConfig::get_file_name(string device_id_param) {
80         string  ret_val;
81         string  fname;
82
83         fname   = DEVICE_CONFIG__FILE_NAME;
84         ret_val = get_dir_name(device_id_param);
85         ret_val = FileUtil::concat_paths(ret_val, fname);
86         return ret_val;
87 }
88
89 string DeviceConfig::get_config_value(string key) {
90         struct uci_section      *section;
91         struct uci_option       *option;
92         string                  ret_val;
93
94         if (uci_handle != NULL) {
95                 section = uci_lookup_section(uci_handle->_ctx, uci_handle->_pkg, DEVICE_CONFIG__SECTION_NAME);
96                 if (section != NULL) {
97                         option  = uci_lookup_option(uci_handle->_ctx, section, key.c_str());
98                         if (option != NULL) {
99                                 switch (option->type) {
100                                         case UCI_TYPE_STRING:
101                                                 //log_info("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
102                                                 ret_val = option->v.string;
103                                                 break;
104                                         default:
105                                                 log_error("key: %s Failed to read parameter value\n", key.c_str());
106                                                 break;
107                                 }
108                         }
109                         else {
110                                 log_error("key: %s Failed to read parameter value\n", key.c_str());
111                         }
112                 }
113         }
114         return ret_val;
115 }
116
117 void DeviceConfig::set_config_value(string key,
118                                 string value,
119                                 bool save_immediately) {
120         string  cfg_dir;
121         string  cfg_fl;
122
123         cfg_dir = get_dir_name(device_id);
124         cfg_fl  = DEVICE_CONFIG__FILE_NAME;
125         set_config_value_to_section(cfg_dir.c_str(),
126                         cfg_fl.c_str(),
127                         DEVICE_CONFIG__SECTION_TYPE,
128                         DEVICE_CONFIG__SECTION_NAME,
129                         key.c_str(),
130                         value.c_str(),
131                         save_immediately);
132 }
133
134 EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
135         EnumSummaryCalculationType      ret_val;
136
137         ret_val = MEAN;
138         if (device_type.empty() == false) {
139                 if (device_type.compare("Counter Device") == 0) {
140                         ret_val = DELTA;
141                 }
142         }
143         return ret_val;;
144 }
145
146 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
147         int                     err_flg;
148         struct uci_context      *ctx;
149         struct uci_package      *pkg;
150         string                  cfg_fl;
151         string                  cfg_dir;
152         ConfigHandle            *ret_val;
153
154         ret_val = NULL;
155         cfg_dir = get_dir_name(device_id_param);
156         if (cfg_dir.empty() == false) {
157                 if (access(cfg_dir.c_str(), W_OK) != 0) {
158                         FileUtil::mkdirs(cfg_dir.c_str());
159                 }
160                 cfg_fl  = get_file_name(device_id_param);
161                 if (access(cfg_fl.c_str(), R_OK) == 0) {
162                         ctx     = uci_alloc_context();
163                         if (ctx != NULL) {
164                                 //log_debug("uci_set_confdir: %s\n", cfg_dir.c_str());
165                                 uci_set_confdir(ctx, cfg_dir.c_str());
166                                 err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
167                                 if (err_flg == UCI_OK) {
168                                         //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
169                                         ret_val = new ConfigHandle(ctx, pkg);
170                                 }
171                                 else {
172                                         log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK);
173                                         set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "", true);
174                                         uci_free_context(ctx);
175                                 }
176                         }
177                         else {
178                                 log_error("Failed to load device device configuration, memory allocation error.\n");
179                                 set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "", true);
180                         }
181                 }
182                 else {
183                         log_error("Failed to load device device configuration, file does not exist: %s.\n", cfg_fl.c_str());
184                 }
185         }
186         return ret_val;
187 }
188
189 DeviceConfig *DeviceConfig::get_device_config(string device_id) {
190         DeviceConfig    *ret_val;
191
192         ret_val = new DeviceConfig(device_id);
193         return ret_val;
194 }