2 * Samsung Ukraine R&D Center (SRK under a contract between)
3 * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
20 * @brief Application settings
21 * @date Created Feb 14, 2018
22 * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
24 #include <boost/property_tree/ptree.hpp>
25 #include <boost/property_tree/ini_parser.hpp>
30 #include "samonitor_tag.h"
33 #if !defined(CONFIG_FILE_PATH)
34 # error "CONFIG_FILE_PATH must be defined"
37 #define CONFIG_FILE STRINGIFY(CONFIG_FILE_PATH)
42 const std::string defaultConfig{CONFIG_FILE};
43 const int DEFAULT_KEEPALIVE_SECONDS = 10;
44 const std::string DEFAULT_STRING_PROPERTY{"unknown"};
46 const std::string PROPKEY_SERVER_ADDR{"Server.URL"};
47 const std::string PROPKEY_KEEPALIVE{"Server.keepalive"};
48 const std::string PROPKEY_DUID{"Device.id"};
49 const std::string PROPKEY_LOCK{"Device.locked"};
56 Settings Settings::_instance;
61 , keepAliveTimeout(DEFAULT_KEEPALIVE_SECONDS)
68 bool Settings::_load(const std::string& fileName)
73 boost::property_tree::ptree properties;
74 boost::property_tree::read_ini(fileName, properties);
76 serverAddress = properties.get<std::string>(PROPKEY_SERVER_ADDR, std::string{});
77 deviceId = properties.get<std::string>(PROPKEY_DUID, std::string{});
78 keepAliveTimeout = std::chrono::milliseconds(properties.get<int>(PROPKEY_KEEPALIVE, DEFAULT_KEEPALIVE_SECONDS));
79 lock = properties.get<bool>(PROPKEY_LOCK, false);
81 } catch (std::exception& e) {
82 LOG_E(TAG, "Fail to load configuration from \"%s\". Error: %s", fileName.c_str(), e.what());
88 bool Settings::loadDefaults()
90 bool result = _load(defaultConfig);
96 if (saveFileName.empty()) {
97 LOG_E(TAG, "Can't load settings, settings file path not set.");
101 return _load(saveFileName);
104 bool Settings::save() const
106 if (saveFileName.empty()) {
107 LOG_E(TAG, "Can't save settings, settings file path not set.");
113 boost::property_tree::ptree properties;
114 properties.put(PROPKEY_SERVER_ADDR, serverAddress);
115 properties.put(PROPKEY_KEEPALIVE, keepAliveTimeout.count());
116 properties.put(PROPKEY_DUID, deviceId);
117 properties.put(PROPKEY_LOCK, lock);
119 boost::property_tree::write_ini(saveFileName, properties);
121 } catch (std::exception& e) {
122 LOG_E(TAG, "Failed to write configuration at %s.", saveFileName.c_str());