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