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.
7 * @file indexed_property_policy.cpp
8 * @brief Implementation of indexed property policy parsing and applying
9 * @date Created March 22, 2018
10 * @author Mail to: <A HREF="mailto:i.metelytsia@samsung.com">Iurii Metelytsia, i.metelytsia@samsung.com</A>
13 #include "indexed_property_policy.h"
14 #include <jsoncpp/json/reader.h>
16 #include "samonitor_tag.h"
21 IndexedPropertyPolicy::IndexedPropertyPolicy(PolicyEnforce& policy_enforce, const std::string& policy_name, IdxPropSetter setter, IdxPropGetter getter)
22 : IPolicy(policy_enforce, policy_name), m_setter(setter), m_getter(getter)
25 throw std::runtime_error("Setter is empty.");
28 int IndexedPropertyPolicy::apply(device_policy_manager_h hdpm, const Json::Value& policy)
30 int res = TIZEN_ERROR_NONE;
32 LOG_D(TAG, "IndexedPropertyPolicy::apply");
34 if (policy.empty() || !policy.isMember("name") || !policy.isMember("key") || !policy.isMember("value")) {
35 LOG_E(TAG, "Empty policy or invalid policy format");
36 return TIZEN_ERROR_INVALID_PARAMETER;
39 std::string name = policy.get("name", "").asString();
40 std::string key = policy.get("key", "").asString();
41 int value = policy.get("value", 0).asInt();
42 LOG_D(TAG, "Enforce policy [%s] with key [%s] to state %d", name.c_str(), key.c_str(), value);
45 if ((res = m_setter(hdpm, key.c_str(), value)) != TIZEN_ERROR_NONE) {
46 LOG_E(TAG, "Policy [%s] enforce failed with error: %d", name.c_str(), res);
48 LOG_D(TAG, "Policy [%s] enforced", name.c_str());
50 } catch (std::exception& e) {
51 LOG_E(TAG, "Policy [%s] enforce failed with exception: %s", name.c_str(), e.what());
52 res = TIZEN_ERROR_UNKNOWN;
58 int IndexedPropertyPolicy::getState(device_policy_manager_h hdpm, Json::Value& policy)
60 int res = TIZEN_ERROR_NONE;
62 LOG_D(TAG, "IndexedPropertyPolicy::getState");
64 if (policy.empty() || !policy.isMember("name") || !policy.isMember("key")) {
65 LOG_E(TAG, "Empty policy or invalid policy format");
66 return TIZEN_ERROR_INVALID_PARAMETER;
69 std::string name = policy.get("name", "").asString();
70 std::string key = policy.get("key", "").asString();
71 policy["supported"] = 0;
75 LOG_E(TAG, "Getting policy [%s] state with key [%s] is not supported", name.c_str(), key.c_str());
76 return TIZEN_ERROR_NOT_SUPPORTED;
81 if((res = m_getter(hdpm, key.c_str(), &value)) != TIZEN_ERROR_NONE) {
82 LOG_E(TAG, "Getting policy [%s] state failed with error: %d", name.c_str(), res);
85 LOG_D(TAG, "Policy [%s] with key [%s] state [%d]", name.c_str(), key.c_str(), value);
86 policy["supported"] = 1;
87 policy["value"] = value;
89 } catch (std::exception& e) {
90 LOG_E(TAG, "Getting policy [%s] state failed with exception: %s", name.c_str(), e.what());
91 res = TIZEN_ERROR_UNKNOWN;