Rule management added.
[platform/core/security/suspicious-activity-monitor.git] / daemon / settings.cpp
1 /**
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.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18 /**
19  * @file   settings.cpp
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>
23  */
24 #include <boost/property_tree/ptree.hpp>
25 #include <boost/property_tree/ini_parser.hpp>
26 #include <fstream>
27 #include <random>
28 #include "settings.h"
29 #include "logging.h"
30 #include "samonitor_tag.h"
31 #include "macro.h"
32
33 #if !defined(CONFIG_FILE_PATH)
34 #   error "CONFIG_FILE_PATH must be defined"
35 #endif
36
37 #define CONFIG_FILE STRINGIFY(CONFIG_FILE_PATH)
38
39 // Defaults
40 namespace
41 {
42 const std::string defaultConfig{CONFIG_FILE};
43 const int DEFAULT_KEEPALIVE_SECONDS = 10;
44 const std::string DEFAULT_STRING_PROPERTY{"unknown"};
45
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"};
50
51 }
52
53 namespace agent
54 {
55
56 Settings Settings::_instance;
57
58 Settings::Settings()
59     : serverAddress()
60     , deviceId()
61     , keepAliveTimeout(DEFAULT_KEEPALIVE_SECONDS)
62     , saveFileName()
63     , loaded(false)
64     , lock(false)
65 {
66 }
67
68 bool Settings::_load(const std::string& fileName)
69 {
70     loaded = false;
71
72     try {
73         boost::property_tree::ptree properties;
74         boost::property_tree::read_ini(fileName, properties);
75
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);
80         loaded = true;
81     } catch (std::exception& e) {
82         LOG_E(TAG, "Fail to load configuration from \"%s\". Error: %s", fileName.c_str(), e.what());
83     }
84
85     return loaded;
86 }
87
88 bool Settings::loadDefaults()
89 {
90     bool result = _load(defaultConfig);
91     return result;
92 }
93
94 bool Settings::load()
95 {
96     if (saveFileName.empty()) {
97         LOG_E(TAG, "Can't load settings, settings file path not set.");
98         return false;
99     }
100
101     return _load(saveFileName);
102 }
103
104 bool Settings::save() const
105 {
106     if (saveFileName.empty()) {
107         LOG_E(TAG, "Can't save settings, settings file path not set.");
108         return false;
109     }
110
111     try
112     {
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);
118
119         boost::property_tree::write_ini(saveFileName, properties);
120         return true;
121     } catch (std::exception& e) {
122         LOG_E(TAG, "Failed to write configuration at %s.", saveFileName.c_str());
123     }
124
125     return false;
126 }
127
128 }