1 /*
2 * Copyright (c) 2021, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file     media_user_setting_configure.cpp
24 //! \brief    The interface of media user setting configure.
25 
26 #include "mos_os.h"
27 #include "media_user_setting_configure.h"
28 
29 namespace MediaUserSetting {
30 namespace Internal {
31 
32 const UFKEY_NEXT Configure::m_rootKey = UFKEY_INTERNAL_NEXT;
33 const char *Configure::m_configPath = USER_SETTING_CONFIG_PATH;
34 const char *Configure::m_reportPath = USER_SETTING_REPORT_PATH;
35 
Configure()36 Configure::Configure()
37 {
38 #if (_DEBUG || _RELEASE_INTERNAL)
39         m_isDebugMode = true;
40 #endif
41 
42     MosUtilities::MosInitializeReg();
43 }
44 
~Configure()45 Configure::~Configure()
46 {
47     MosUtilities::MosUninitializeReg();
48 }
49 
Register(const std::string & valueName,const Group & group,const Value & defaultValue,bool isReportKey,bool debugOnly,const std::string & customPath)50 MOS_STATUS Configure::Register(
51     const std::string &valueName,
52     const Group &group,
53     const Value &defaultValue,
54     bool isReportKey,
55     bool debugOnly,
56     const std::string &customPath)
57 {
58     m_mutexLock.Lock();
59 
60     if (IsDefinitionExist(valueName))
61     {
62         m_mutexLock.Unlock();
63         return MOS_STATUS_FILE_EXISTS;
64     }
65 
66     auto &defs = GetDefinitions(group);
67 
68     defs.insert(
69         std::make_pair(
70             MakeHash(valueName),
71             std::make_shared<Definition>(
72                 valueName,
73                 defaultValue,
74                 isReportKey,
75                 debugOnly,
76                 customPath)));
77 
78     m_mutexLock.Unlock();
79 
80     return MOS_STATUS_SUCCESS;
81 }
82 
Read(Value & value,const std::string & valueName,const Group & group,PMOS_CONTEXT mosContext,const Value & customValue,bool useCustomValue)83 MOS_STATUS Configure::Read(Value &value,
84     const std::string &valueName,
85     const Group &group,
86     PMOS_CONTEXT mosContext,
87     const Value &customValue,
88     bool useCustomValue)
89 {
90     int32_t ret = 0;
91 
92     auto &defs = GetDefinitions(group);
93 
94     auto def = defs[MakeHash(valueName)];
95     if (def == nullptr)
96     {
97         return MOS_STATUS_INVALID_HANDLE;
98     }
99 
100     if (def->IsDebugOnly() && !m_isDebugMode)
101     {
102         value = useCustomValue ? customValue : def->DefaultValue();
103         return MOS_STATUS_SUCCESS;
104     }
105 
106     std::string basePath = "";
107     MOS_USER_FEATURE_KEY_PATH_INFO *ufInfo = Mos_GetDeviceUfPathInfo(mosContext);
108     if (ufInfo != nullptr && ufInfo->Path != nullptr)
109     {
110         basePath = ufInfo->Path;
111     }
112 
113     std::string configPath = def->CustomPath();
114     if (configPath.empty())
115     {
116         configPath = m_configPath;
117     }
118     else
119     {
120         configPath = "\\" + configPath;
121     }
122 
123     std::string path = basePath + configPath;
124 
125     UFKEY_NEXT  key      = {};
126     std::string strValue = "";
127     uint32_t    size     = MOS_USER_CONTROL_MAX_DATA_SIZE;
128     uint32_t    type     = 0;
129 
130     // read env variable first, if env value is set, return
131     // else read the reg keys
132     MOS_STATUS status = MosUtilities::MosReadEnvVariable(key, valueName, &type, strValue, &size);
133 
134     if (status == MOS_STATUS_SUCCESS)
135     {
136         value = strValue;
137         return MOS_STATUS_SUCCESS;
138     }
139 
140     status = MosUtilities::MosOpenRegKey(m_rootKey, path, KEY_READ, &key);
141 
142     if (status == MOS_STATUS_SUCCESS)
143     {
144         strValue = "";
145         size     = MOS_USER_CONTROL_MAX_DATA_SIZE;
146         type     = 0;
147 
148         m_mutexLock.Lock();
149         status = MosUtilities::MosGetRegValue(key, valueName, &type, strValue, &size);
150         if (status == MOS_STATUS_SUCCESS)
151         {
152             value = strValue;
153         }
154         m_mutexLock.Unlock();
155 
156         MosUtilities::MosCloseRegKey(key);
157     }
158 
159     if (status != MOS_STATUS_SUCCESS)
160     {
161         value = useCustomValue ? customValue : def->DefaultValue();
162     }
163 
164     return MOS_STATUS_SUCCESS;
165 }
166 
Write(const std::string & valueName,const Value & value,const Group & group,PMOS_CONTEXT mosContext,bool isForReport)167 MOS_STATUS Configure::Write(
168     const std::string &valueName,
169     const Value &value,
170     const Group &group,
171     PMOS_CONTEXT mosContext,
172     bool isForReport)
173 {
174     auto &defs = GetDefinitions(group);
175 
176     auto def = defs[MakeHash(valueName)];
177     if (def == nullptr)
178     {
179         return MOS_STATUS_INVALID_HANDLE;
180     }
181 
182     if (def->IsDebugOnly() && !m_isDebugMode)
183     {
184         return MOS_STATUS_SUCCESS;
185     }
186 
187     if (!def->IsReportKey() && isForReport)
188     {
189         return MOS_STATUS_INVALID_PARAMETER;
190     }
191 
192     std::string basePath = "";
193     MOS_USER_FEATURE_KEY_PATH_INFO *ufInfo = Mos_GetDeviceUfPathInfo(mosContext);
194     if (ufInfo != nullptr && ufInfo->Path != nullptr)
195     {
196         basePath = ufInfo->Path;
197     }
198 
199     std::string path = basePath + (isForReport ? m_reportPath : m_configPath);
200 
201     UFKEY_NEXT key = {};
202     MOS_STATUS status = MOS_STATUS_UNKNOWN;
203 
204     m_mutexLock.Lock();
205     status = MosUtilities::MosCreateRegKey(m_rootKey, path, KEY_WRITE, &key);
206 
207     if (status == MOS_STATUS_SUCCESS)
208     {
209         uint32_t size = def->DefaultValue().Size();
210 
211         status = MosUtilities::MosSetRegValue(key, valueName, UF_SZ, value.ConstString());
212 
213         MosUtilities::MosCloseRegKey(key);
214     }
215     m_mutexLock.Unlock();
216 
217     if (status != MOS_STATUS_SUCCESS)
218     {
219         // When any fail happen, just print out a critical message, but not return error to break normal call sequence.
220         MOS_CRITICALMESSAGE(MOS_COMPONENT_OS, MOS_SUBCOMP_SELF, "Failed to write media user setting value.");
221     }
222 
223     return MOS_STATUS_SUCCESS;
224 }
225 
226 }}