]> pilppa.org Git - lib1wire.git/blob - src/DeviceConfig.cc
Support for querying the list devices having data stored.
[lib1wire.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 <plp/log.h>
14 #include "W1Util.hh"
15 #include "W1Configure.hh"
16
17 #include "Factory.hh"
18
19 using namespace std;
20 using namespace w1;
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_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
39         }
40         else {
41                 log_error("Could not read device config\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 = W1Util::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);
85         ret_val = W1Util::concat_paths(ret_val, fname);
86         return ret_val;
87 }
88
89 string DeviceConfig::get_cfg_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                         switch (option->type) {
99                                 case UCI_TYPE_STRING:
100                                         log_info("config file: 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("config file: key: %s can not parse parameter value\n", key.c_str());
105                                         break;
106                         }
107                 }
108         }
109         return ret_val;
110 }
111
112 void DeviceConfig::set_cfg_value(string key, string value) {
113         string cfg_dir;
114         string cfg_fl;
115
116         cfg_dir = get_dir_name(device_id);
117         cfg_fl  = DEVICE_CONFIG__FILE_NAME;
118         set_config_value(cfg_dir.c_str(),
119                         cfg_fl.c_str(),
120                         DEVICE_CONFIG__SECTION_TYPE,
121                         DEVICE_CONFIG__SECTION_NAME,
122                         key.c_str(),
123                         value.c_str());
124 }
125
126 enum_summary_calculation DeviceConfig::get_summary_calculation_type() {
127         enum_summary_calculation        ret_val;
128
129         ret_val = MEAN;
130         if (device_type.empty() == false) {
131                 if (device_type.compare("counter") == 0) {
132                         ret_val = DELTA;
133                 }
134         }
135         return ret_val;;
136 }
137
138 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
139         int                     err_flg;
140         struct uci_context      *ctx;
141         struct uci_package      *pkg;
142         string                  cfg_fl;
143         string                  cfg_dir;
144         ConfigHandle            *ret_val;
145
146         ret_val = NULL;
147         cfg_dir = get_dir_name(device_id_param);
148         if (cfg_dir.empty() == false) {
149                 if (access(cfg_dir.c_str(), W_OK) != 0) {
150                         W1Util::mkdirs(cfg_dir.c_str());
151                 }
152                 cfg_fl  = get_file_name(device_id_param);
153                 ctx     = uci_alloc_context();
154                 if (ctx != NULL) {
155                         log_debug("uci_set_confdir: %s\n", cfg_dir.c_str());
156                         uci_set_confdir(ctx, cfg_dir.c_str());
157                         if (access(cfg_fl.c_str(), R_OK) == 0) {
158                                 log_debug("loading file: %s\n", cfg_fl.c_str());
159                                 err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
160                                 if (err_flg == UCI_OK) {
161                                         log_debug("Loaded device configuration: %s, UCI_OK: %d, err flg: %d\n.", cfg_fl.c_str(), UCI_OK, err_flg);
162                                         ret_val = new ConfigHandle(ctx, pkg);
163                                 }
164                                 else {
165                                         log_debug("Failed to load file: %s, UCI_OK: %d, err flg: %d\n.", cfg_fl.c_str(), UCI_OK, err_flg);
166                                         set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "unknowntype");
167                                 }
168                         }
169                         else {
170                                 log_error("Failed to load device device configuration, file does not exit: %s.\n", cfg_fl.c_str());
171                         }
172                 }
173                 else {
174                         log_error("Failed to load device device configuration, invalid device id: %s.\n", cfg_dir.c_str());
175                 }
176         }
177         return ret_val;
178 }