1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/feature_validator.h"
7 #include <system_info.h>
12 namespace common_installer {
14 FeatureValidator::FeatureValidator(
15 const std::map<std::string, std::string>& features)
16 : features_(features) {
19 bool FeatureValidator::Validate(std::string* error) {
20 for (auto& pair : features_) {
21 const auto& feature = pair.first;
23 system_info_type_e type;
24 int ret = system_info_get_platform_type(feature.c_str(), &type);
25 if (ret != SYSTEM_INFO_ERROR_NONE) {
26 *error = std::string("Unknown feature: ") + feature;
31 case SYSTEM_INFO_BOOL:
32 ok = ValidateBoolean(feature, error);
35 ok = ValidateInteger(feature, error);
37 case SYSTEM_INFO_STRING:
38 ok = ValidateString(feature, error);
40 case SYSTEM_INFO_DOUBLE:
41 // There is no double typed feature on platform, no way to interpret it
46 *error = "Unknown type of feature";
54 bool FeatureValidator::ValidateBoolean(
55 const std::string& feature, std::string* error) {
56 bool supported = false;
57 int ret = system_info_get_platform_bool(feature.c_str(), &supported);
58 if (ret != SYSTEM_INFO_ERROR_NONE) {
59 *error = std::string("Failed to call system_info_get_platform_bool()") +
60 " for " + feature + ", error code: " + std::to_string(ret);
66 bool FeatureValidator::ValidateInteger(
67 const std::string& feature, std::string* error) {
68 int platform_value = 0;
69 int ret = system_info_get_platform_int(feature.c_str(), &platform_value);
70 if (ret != SYSTEM_INFO_ERROR_NONE) {
71 *error = std::string("Failed to call system_info_get_platform_int()") +
72 " for " + feature + ", error code: " + std::to_string(ret);
78 bool FeatureValidator::ValidateString(
79 const std::string& feature, std::string* error) {
81 std::unique_ptr<char, decltype(&std::free)> text_deleter(text, std::free);
82 int ret = system_info_get_platform_string(feature.c_str(), &text);
83 if (ret != SYSTEM_INFO_ERROR_NONE) {
84 *error = std::string("Failed to call system_info_get_platform_string()") +
85 " for " + feature + ", error code: " + std::to_string(ret);
91 } // namespace common_installer