1 /*
2  * Copyright (c) 2017 - 2019, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  *       copyright notice, this list of conditions and the following
11  *       disclaimer in the documentation and/or other materials provided
12  *       with the distribution.
13  *     * Neither the name of The Linux Foundation nor the names of its
14  *       contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #define LOG_TAG "PlatformConfig"
31 
32 #include <errno.h>
33 #include <utils/Log.h>
34 #include <sys/mman.h>
35 #include "PlatformConfig.h"
36 #include "ConfigParser.h"
37 
38 #ifdef ENABLE_CONFIGSTORE
39 #include <vendor/qti/hardware/capabilityconfigstore/1.0/types.h>
40 #include <vendor/qti/hardware/capabilityconfigstore/1.0/ICapabilityConfigStore.h>
41 #endif
42 
43 namespace Platform {
44 
45 #define PLAT_CONFIG_FILE "/vendor/etc/system_properties.xml"
46 
47 Config* Config::mInstance;
48 
Config()49 Config::Config() {
50     Platform::ConfigParser::initAndParse(PLAT_CONFIG_FILE, mConfigMap);
51 }
52 
getInstance()53 Config* Config::getInstance() {
54     VIDC_PLAT_LOGH("%s: Enter", __func__);
55     if (!mInstance) {
56         mInstance = new Config();
57     }
58     return mInstance;
59 }
60 
getInt32(Config_t config,int32_t * value,const int32_t defaultValue)61 ConfigError_t Config::getInt32(Config_t config, int32_t *value,
62         const int32_t defaultValue) {
63      Config *conf = getInstance();
64     if (conf == nullptr) {
65         *value = defaultValue;
66         return FAIL;
67     }
68     if (conf->mConfigMap.find(configStrMap[config].name) == conf->mConfigMap.end()) {
69         VIDC_PLAT_LOGH("%s: Returning default", __func__);
70         *value = defaultValue;
71         return FAIL;
72     }
73     *value = (int32_t) atoi(conf->mConfigMap[configStrMap[config].name].c_str());
74     VIDC_PLAT_LOGH("%s Config name: %s value: %d",
75             __func__, configStrMap[config].name, *value);
76     return OK;
77 }
78 
isConfigStoreEnabled()79 bool Config::isConfigStoreEnabled()
80 {
81 #ifdef ENABLE_CONFIGSTORE
82     return true;
83 #else
84     return false;
85 #endif
86 }
87 
getConfigStoreBool(const char * area,const char * config,bool & value,const bool defaultValue)88 ConfigError_t Config::getConfigStoreBool(const char *area, const char *config,
89         bool &value, const bool defaultValue)
90 {
91 #ifdef ENABLE_CONFIGSTORE
92     using vendor::qti::hardware::capabilityconfigstore::V1_0::Result;
93     using vendor::qti::hardware::capabilityconfigstore::V1_0::CommandResult;
94     if (area == nullptr || config == nullptr) {
95         return FAIL;
96     }
97 
98     value = defaultValue;
99 
100     Config *conf = getInstance();
101     if (conf == nullptr) {
102         return FAIL;
103     }
104 
105     // load on demand
106     if (conf->mConfigStore == nullptr) {
107         conf->mConfigStore = ICapabilityConfigStore::tryGetService();
108     }
109 
110     if (conf->mConfigStore == nullptr) {
111         VIDC_PLAT_LOGH("%s: unable to get configstore service", __func__);
112         return FAIL;
113     }
114 
115     CommandResult result = {Result::NOT_FOUND, 0};
116     conf->mConfigStore->getConfig(area, config, [&](const CommandResult &res) {
117         result = res;
118     });
119 
120     if (result.result_type != Result::SUCCESS) {
121         VIDC_PLAT_LOGH("%s: %s/%s not found(%u)", __func__, area, config,
122                        result.result_type);
123         return FAIL;
124     }
125 
126     if (strncasecmp("true", result.value.c_str(), result.value.size()) == 0) {
127         value = true;
128     } else {
129         value = false;
130     }
131     VIDC_PLAT_LOGH("%s: %s/%s=%s, returning %s ", __func__, area, config,
132             result.value.c_str(), value ? "true" : "false");
133     return OK;
134 #else
135     (void)(area);
136     (void)(config);
137     value = defaultValue;
138     return OK;
139 #endif
140 }
141 
142 }
143