1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "AudioPolicyGlobalParser"
17 #endif
18
19 #include "audio_policy_global_parser.h"
20 #include "audio_errors.h"
21 #include "media_monitor_manager.h"
22
23 namespace OHOS {
24 namespace AudioStandard {
25
26 constexpr int32_t CHANGE_NUMBER_SYSTEM = 10;
27
LoadConfiguration()28 bool AudioPolicyGlobalParser::LoadConfiguration()
29 {
30 mDoc_ = xmlReadFile(POLICY_GLOBAL_CONFIG_FILE, nullptr, 0);
31 CHECK_AND_RETURN_RET_LOG(mDoc_ != nullptr, false, "AudioPolicyGlobalParser xmlReadFile failed");
32
33 return true;
34 }
35
Parse()36 bool AudioPolicyGlobalParser::Parse()
37 {
38 xmlNode *root = xmlDocGetRootElement(mDoc_);
39 CHECK_AND_RETURN_RET_LOG(root != nullptr, false, "xmlDocGetRootElement Failed");
40
41 xmlNode *currNode = nullptr;
42 if (root->xmlChildrenNode) {
43 currNode = root->xmlChildrenNode;
44 } else {
45 AUDIO_ERR_LOG("AudioPolicyGlobalParser Missing node");
46 return false;
47 }
48 if (!ParseInternal(currNode)) {
49 return false;
50 }
51 return true;
52 }
53
Destroy()54 void AudioPolicyGlobalParser::Destroy()
55 {
56 if (mDoc_ != nullptr) {
57 xmlFreeDoc(mDoc_);
58 }
59 }
60
ParseInternal(xmlNode * node)61 bool AudioPolicyGlobalParser::ParseInternal(xmlNode *node)
62 {
63 xmlNode *currNode = node;
64 while (currNode != nullptr) {
65 if (XML_ELEMENT_NODE == currNode->type &&
66 (!xmlStrcmp(currNode->name, reinterpret_cast<const xmlChar*>("attribute")))) {
67 ParserAttribute(currNode);
68 }
69 currNode = currNode->next;
70 }
71 return true;
72 }
73
ParserAttribute(xmlNode * currNode)74 void AudioPolicyGlobalParser::ParserAttribute(xmlNode *currNode)
75 {
76 while (currNode) {
77 std::string name;
78 uint32_t value;
79 xmlChar *attrName = xmlGetProp(currNode, reinterpret_cast<const xmlChar*>("name"));
80 xmlChar *attrValue = xmlGetProp(currNode, reinterpret_cast<const xmlChar*>("value"));
81 if (attrName == nullptr || attrValue == nullptr) {
82 currNode = currNode->next;
83 continue;
84 }
85 name = static_cast<char *>(reinterpret_cast<char *>(attrName));
86 value = static_cast<uint32_t>(std::strtol(reinterpret_cast<char *>(attrValue), nullptr, CHANGE_NUMBER_SYSTEM));
87 globalConfigs_[name] = value;
88 xmlFree(attrName);
89 xmlFree(attrValue);
90 currNode = currNode->next;
91 }
92 }
93
GetConfigByKeyName(std::string keyName,uint32_t & value)94 int32_t AudioPolicyGlobalParser::GetConfigByKeyName(std::string keyName, uint32_t &value)
95 {
96 if (!globalConfigs_.count(keyName)) {
97 AUDIO_ERR_LOG("GetConfigByKeyName key error keyName=%{public}s", keyName.c_str());
98 return ERR_CONFIG_NAME_ERROR;
99 }
100 value = globalConfigs_[keyName];
101 return SUCCESS;
102 }
103
104 } // namespace AudioStandard
105 } // namespace OHOS
106