| 1 | /* |
| 2 | * Copyright (c) 2021-2022, 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 | |
| 36 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 37 | std::set<std::string> Configure::m_nonPidRegPaths = {}; |
| 38 | #endif |
| 39 | |
| 40 | Configure::Configure(MOS_USER_FEATURE_KEY_PATH_INFO *keyPathInfo):Configure() |
| 41 | { |
| 42 | m_keyPathInfo = keyPathInfo; |
| 43 | |
| 44 | std::string subPath = ""; |
| 45 | |
| 46 | if (m_keyPathInfo != nullptr && m_keyPathInfo->Path != nullptr) |
| 47 | { |
| 48 | subPath = m_keyPathInfo->Path; |
| 49 | } |
| 50 | |
| 51 | //when statePath set, will init m_statedConfigPath and m_statedReportPath with m_keyPathInfo |
| 52 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 53 | m_pidPath = "\\" + std::to_string(MosUtilities::MosGetPid()); |
| 54 | #endif |
| 55 | m_statedConfigPath = subPath + m_configPath; |
| 56 | m_statedReportPath = subPath + m_reportPath; |
| 57 | } |
| 58 | |
| 59 | Configure::Configure() |
| 60 | { |
| 61 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 62 | m_isDebugMode = true; |
| 63 | #endif |
| 64 | m_statedConfigPath = m_configPath; |
| 65 | m_statedReportPath = m_reportPath; |
| 66 | |
| 67 | MosUtilities::MosInitializeReg(m_regBufferMap); |
| 68 | } |
| 69 | |
| 70 | Configure::~Configure() |
| 71 | { |
| 72 | MosUtilities::MosUninitializeReg(m_regBufferMap); |
| 73 | } |
| 74 | |
| 75 | MOS_STATUS Configure::Register( |
| 76 | const std::string &valueName, |
| 77 | const Group &group, |
| 78 | const Value &defaultValue, |
| 79 | bool isReportKey, |
| 80 | bool debugOnly, |
| 81 | bool useCustomPath, |
| 82 | const std::string &customPath, |
| 83 | bool statePath) |
| 84 | { |
| 85 | m_mutexLock.Lock(); |
| 86 | |
| 87 | if (IsDefinitionExist(valueName)) |
| 88 | { |
| 89 | m_mutexLock.Unlock(); |
| 90 | return MOS_STATUS_FILE_EXISTS; |
| 91 | } |
| 92 | |
| 93 | auto &defs = GetDefinitions(group); |
| 94 | std::string subPath = ""; |
| 95 | if (useCustomPath) |
| 96 | { |
| 97 | if (statePath && m_keyPathInfo != nullptr && m_keyPathInfo->Path != nullptr) |
| 98 | { |
| 99 | subPath = m_keyPathInfo->Path; |
| 100 | } |
| 101 | subPath += customPath; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | if (statePath) |
| 106 | { |
| 107 | subPath = m_statedConfigPath; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | subPath = m_configPath; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | defs.insert( |
| 116 | std::make_pair( |
| 117 | MakeHash(valueName), |
| 118 | std::make_shared<Definition>( |
| 119 | valueName, |
| 120 | defaultValue, |
| 121 | isReportKey, |
| 122 | debugOnly, |
| 123 | useCustomPath, |
| 124 | subPath, |
| 125 | m_rootKey, |
| 126 | statePath))); |
| 127 | |
| 128 | m_mutexLock.Unlock(); |
| 129 | |
| 130 | return MOS_STATUS_SUCCESS; |
| 131 | } |
| 132 | |
| 133 | MOS_STATUS Configure::Read(Value &value, |
| 134 | const std::string &valueName, |
| 135 | const Group &group, |
| 136 | const Value &customValue, |
| 137 | bool useCustomValue, |
| 138 | uint32_t option) |
| 139 | { |
| 140 | int32_t ret = 0; |
| 141 | MOS_STATUS status = MOS_STATUS_UNKNOWN; |
| 142 | auto &defs = GetDefinitions(group); |
| 143 | auto def = defs[MakeHash(valueName)]; |
| 144 | if (def == nullptr) |
| 145 | { |
| 146 | return MOS_STATUS_INVALID_HANDLE; |
| 147 | } |
| 148 | auto defaultType = def->DefaultValue().ValueType(); |
| 149 | |
| 150 | if (def->IsDebugOnly() && !m_isDebugMode) |
| 151 | { |
| 152 | value = useCustomValue ? customValue : def->DefaultValue(); |
| 153 | return MOS_STATUS_SUCCESS; |
| 154 | } |
| 155 | std::string path = GetReadPath(def, option); |
| 156 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 157 | //First, Read pid path user setting. If succeed, return; |
| 158 | if (m_nonPidRegPaths.find(path) == m_nonPidRegPaths.end()) |
| 159 | { |
| 160 | std::string pathPidSuffix = path + m_pidPath; |
| 161 | UFKEY_NEXT keyPidSuffix = {}; |
| 162 | m_mutexLock.Lock(); |
| 163 | status = MosUtilities::MosOpenRegKey(m_rootKey, pathPidSuffix, KEY_READ, &keyPidSuffix, m_regBufferMap); |
| 164 | if (status == MOS_STATUS_SUCCESS) |
| 165 | { |
| 166 | status = MosUtilities::MosGetRegValue(keyPidSuffix, valueName, defaultType, value, m_regBufferMap); |
| 167 | MosUtilities::MosCloseRegKey(keyPidSuffix); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | // Record the reg path which does not have inner pid reg path. |
| 172 | m_nonPidRegPaths.insert(path); |
| 173 | } |
| 174 | m_mutexLock.Unlock(); |
| 175 | } |
| 176 | //Second, if reading pid path failed, read non-pid path. If succeed, return; |
| 177 | if (status != MOS_STATUS_SUCCESS) |
| 178 | #endif |
| 179 | { |
| 180 | UFKEY_NEXT key = {}; |
| 181 | |
| 182 | m_mutexLock.Lock(); |
| 183 | |
| 184 | status = MosUtilities::MosOpenRegKey(m_rootKey, path, KEY_READ, &key, m_regBufferMap); |
| 185 | if (status == MOS_STATUS_SUCCESS) |
| 186 | { |
| 187 | status = MosUtilities::MosGetRegValue(key, valueName, defaultType, value, m_regBufferMap); |
| 188 | MosUtilities::MosCloseRegKey(key); |
| 189 | } |
| 190 | |
| 191 | m_mutexLock.Unlock(); |
| 192 | } |
| 193 | |
| 194 | //Third, if reading key failed, read envionment variable. External user setting does not set env varaible now. |
| 195 | if (status != MOS_STATUS_SUCCESS && option == MEDIA_USER_SETTING_INTERNAL) |
| 196 | { |
| 197 | // read env variable if no user setting set |
| 198 | status = MosUtilities::MosReadEnvVariable(def->ItemEnvName(), defaultType, value); |
| 199 | } |
| 200 | |
| 201 | if (status != MOS_STATUS_SUCCESS) |
| 202 | { |
| 203 | // customValue is only for internal user setting Read |
| 204 | if (option == MEDIA_USER_SETTING_INTERNAL) |
| 205 | { |
| 206 | value = useCustomValue ? customValue : def->DefaultValue(); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | // For external user setting, no customValue |
| 211 | if (useCustomValue == true) |
| 212 | { |
| 213 | MOS_OS_ASSERTMESSAGE("External user setting %s customValue will not be used.", valueName.c_str()); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return status; |
| 219 | } |
| 220 | |
| 221 | MOS_STATUS Configure::Write( |
| 222 | const std::string &valueName, |
| 223 | const Value &value, |
| 224 | const Group &group, |
| 225 | bool isForReport, |
| 226 | uint32_t option) |
| 227 | { |
| 228 | auto &defs = GetDefinitions(group); |
| 229 | |
| 230 | auto def = defs[MakeHash(valueName)]; |
| 231 | if (def == nullptr) |
| 232 | { |
| 233 | return MOS_STATUS_INVALID_HANDLE; |
| 234 | } |
| 235 | |
| 236 | if (def->IsDebugOnly() && !m_isDebugMode) |
| 237 | { |
| 238 | return MOS_STATUS_SUCCESS; |
| 239 | } |
| 240 | |
| 241 | if (!def->IsReportKey() && isForReport) |
| 242 | { |
| 243 | return MOS_STATUS_INVALID_PARAMETER; |
| 244 | } |
| 245 | |
| 246 | std::string path = GetReportPath(def, option); |
| 247 | |
| 248 | MOS_STATUS status = MOS_STATUS_UNKNOWN; |
| 249 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 250 | if (m_nonPidRegPaths.find(path) == m_nonPidRegPaths.end()) |
| 251 | { |
| 252 | std::string pathPidSuffix = path + m_pidPath; |
| 253 | UFKEY_NEXT keyPidSuffix = {}; |
| 254 | |
| 255 | m_mutexLock.Lock(); |
| 256 | status = MosUtilities::MosOpenRegKey(m_rootKey, pathPidSuffix, KEY_WRITE, &keyPidSuffix, m_regBufferMap); |
| 257 | |
| 258 | if (status == MOS_STATUS_SUCCESS) |
| 259 | { |
| 260 | status = MosUtilities::MosSetRegValue(keyPidSuffix, valueName, value, m_regBufferMap); |
| 261 | |
| 262 | MosUtilities::MosCloseRegKey(keyPidSuffix); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | // Record the reg path which does not have inner pid reg path. |
| 267 | m_nonPidRegPaths.insert(path); |
| 268 | } |
| 269 | m_mutexLock.Unlock(); |
| 270 | } |
| 271 | |
| 272 | if (status != MOS_STATUS_SUCCESS) |
| 273 | #endif |
| 274 | { |
| 275 | UFKEY_NEXT key = {}; |
| 276 | m_mutexLock.Lock(); |
| 277 | status = MosUtilities::MosCreateRegKey(m_rootKey, path, KEY_WRITE, &key, m_regBufferMap); |
| 278 | |
| 279 | if (status == MOS_STATUS_SUCCESS) |
| 280 | { |
| 281 | status = MosUtilities::MosSetRegValue(key, valueName, value, m_regBufferMap); |
| 282 | |
| 283 | MosUtilities::MosCloseRegKey(key); |
| 284 | } |
| 285 | m_mutexLock.Unlock(); |
| 286 | } |
| 287 | |
| 288 | if (status != MOS_STATUS_SUCCESS) |
| 289 | { |
| 290 | // When any fail happen, just print out a critical message, but not return error to break normal call sequence. |
| 291 | MOS_OS_NORMALMESSAGE("Failed to write media user setting %s value.", valueName.c_str()); |
| 292 | } |
| 293 | |
| 294 | return MOS_STATUS_SUCCESS; |
| 295 | } |
| 296 | |
| 297 | std::string Configure::GetReadPath( |
| 298 | std::shared_ptr<Definition> def, |
| 299 | uint32_t option) |
| 300 | { |
| 301 | std::string path = ""; |
| 302 | if (def == nullptr) |
| 303 | { |
| 304 | return path; |
| 305 | } |
| 306 | |
| 307 | if (option == MEDIA_USER_SETTING_INTERNAL) |
| 308 | { |
| 309 | return def->GetSubPath(); |
| 310 | } |
| 311 | else if (option == MEDIA_USER_SETTING_INTERNAL_REPORT) |
| 312 | { |
| 313 | return m_statedReportPath; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | return GetExternalPath(option); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | std::string Configure::GetReportPath( |
| 322 | std::shared_ptr<Definition> def, |
| 323 | uint32_t option) |
| 324 | { |
| 325 | std::string path = ""; |
| 326 | if (def == nullptr) |
| 327 | { |
| 328 | return path; |
| 329 | } |
| 330 | if (option == MEDIA_USER_SETTING_INTERNAL) |
| 331 | { |
| 332 | return m_statedReportPath; |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | return GetExternalPath(option); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | std::string Configure::GetExternalPath(uint32_t option) |
| 341 | { |
| 342 | std::string path = ""; |
| 343 | auto it = m_pathOption.find(option); |
| 344 | if (it != m_pathOption.end()) |
| 345 | { |
| 346 | if (it->second.bStated && m_keyPathInfo != nullptr && m_keyPathInfo->Path != nullptr) |
| 347 | { |
| 348 | path = m_keyPathInfo->Path; |
| 349 | } |
| 350 | path += it->second.subPath; |
| 351 | return path; |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | MOS_OS_ASSERTMESSAGE("Invalid Option"); |
| 356 | return ""; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | }} |