1 /*
2 * Copyright (c) 2022 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
16 #include "para_config.h"
17
18 #undef LOG_TAG
19 #define LOG_TAG "ueaServer-ParaConfig"
20
21 namespace OHOS {
22 namespace RME {
23 namespace {
24 constexpr int FPS_MAX_VALUE = 120;
25 constexpr int RENDER_TYPE_MAX_VALUE = 2;
26 }
27
28 std::map<std::string, std::string> ParaConfig::m_generalConfig;
29 std::map<std::string, std::map<std::string, int>> ParaConfig::m_subEventConfig;
30 std::vector<int> ParaConfig::m_fpsList;
31 std::vector<int> ParaConfig::m_renderTypeList;
32
IsXmlPrepared(const std::string & filePath)33 bool ParaConfig::IsXmlPrepared(const std::string& filePath)
34 {
35 xmlDocPtr docPtr = xmlReadFile(filePath.c_str(), nullptr, XML_PARSE_NOBLANKS);
36 if (docPtr == nullptr) {
37 RME_LOGE("[IsXmlPrepared]:load xml error!");
38 return false;
39 }
40
41 xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
42 if (rootPtr == nullptr || rootPtr->name == nullptr) {
43 RME_LOGE("[IsXmlPrepared]: get root element failed!");
44 xmlFreeDoc(docPtr);
45 return false;
46 }
47 for (xmlNodePtr curNodePtr = rootPtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
48 if (IsValidNode(*curNodePtr)) {
49 RME_LOGE("[IsXmlPrepared]: invalid node!");
50 continue;
51 }
52 auto nodeName = curNodePtr->name;
53 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("log_open")) ||
54 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("enable")) ||
55 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("SOC")) ||
56 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("frame_sched_reset_count"))) {
57 ReadConfigInfo(curNodePtr);
58 continue;
59 }
60 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("fps_list"))) {
61 ReadFpsConfig(curNodePtr);
62 continue;
63 }
64 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("render_type"))) {
65 ReadRenderType(curNodePtr);
66 continue;
67 }
68 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("framedetect"))) {
69 ReadFrameConfig(curNodePtr);
70 continue;
71 }
72 }
73 xmlFreeDoc(docPtr);
74 return true;
75 }
76
IsValidNode(const xmlNode & currNode)77 bool ParaConfig::IsValidNode(const xmlNode& currNode)
78 {
79 if (currNode.name == nullptr || currNode.type == XML_COMMENT_NODE) {
80 return true;
81 }
82 return false;
83 }
84
ReadConfigInfo(const xmlNodePtr & root)85 void ParaConfig::ReadConfigInfo(const xmlNodePtr& root)
86 {
87 xmlChar* context = xmlNodeGetContent(root);
88 if (context == nullptr) {
89 RME_LOGE("[GetConfigInfo]:read xml node error: nodeName:(%{public}s)", root->name);
90 return;
91 }
92 std::string nodeName = reinterpret_cast<const char *>(root->name);
93 m_generalConfig[nodeName] = std::string(reinterpret_cast<const char*>(context));
94 xmlFree(context);
95 }
96
ReadFpsConfig(const xmlNodePtr & root)97 void ParaConfig::ReadFpsConfig(const xmlNodePtr& root)
98 {
99 xmlChar* context = xmlNodeGetContent(root);
100 if (context == nullptr) {
101 RME_LOGE("[GetFpsConfig]: fps read failed!");
102 return;
103 }
104
105 SplitString(std::string(reinterpret_cast<const char*>(context)), " ", m_fpsList, FPS_MAX_VALUE, "fpsList");
106 xmlFree(context);
107 }
108
ReadRenderType(const xmlNodePtr & root)109 void ParaConfig::ReadRenderType(const xmlNodePtr& root)
110 {
111 xmlChar* context = xmlNodeGetContent(root);
112 if (context == nullptr) {
113 RME_LOGE("[GetRenderType]: renderType read failed!");
114 return;
115 }
116 SplitString(std::string(reinterpret_cast<const char*>(context)), " ",
117 m_renderTypeList, RENDER_TYPE_MAX_VALUE, "renderType");
118 xmlFree(context);
119 }
120
SplitString(const std::string & context,const std::string & character,std::vector<int> & mList,const int maxVal,const std::string & attrName)121 void ParaConfig::SplitString(const std::string& context, const std::string& character, std::vector<int> &mList,
122 const int maxVal, const std::string& attrName)
123 {
124 if (context == "") {
125 return;
126 }
127
128 std::string toSplitStr = context + character;
129 size_t pos = toSplitStr.find(character);
130
131 while (pos != toSplitStr.npos) {
132 int curVal = atoi(toSplitStr.substr(0, pos).c_str());
133 if (curVal <= 0 && curVal > maxVal) {
134 RME_LOGE("[SplitString]:get data error! attr name:%{public}s", attrName.c_str());
135 return;
136 }
137 mList.push_back(curVal);
138 toSplitStr = toSplitStr.substr(pos + 1, toSplitStr.size());
139 pos = toSplitStr.find(character);
140 }
141 for (auto m : mList) {
142 RME_LOGI("[SplitString]: attrName: %{public}s, list_val:%{public}d", attrName.c_str(), m);
143 }
144 RME_LOGI("[SplitString]:get data success!attr name:%{public}s", attrName.c_str());
145 }
146
ReadFrameConfig(const xmlNodePtr & root)147 void ParaConfig::ReadFrameConfig(const xmlNodePtr& root)
148 {
149 // to need abnormal process! what if the xml has problem when traversal?
150 for (xmlNodePtr curNode = root->xmlChildrenNode; curNode != nullptr; curNode = curNode->next) {
151 if (IsValidNode(*curNode)) {
152 RME_LOGE("[IsXmlPrepared]: invalid node!");
153 continue;
154 }
155 std::string key1 = "";
156 std::string key2 = "";
157 ReadAttr(curNode, "render_type", key1);
158 ReadAttr(curNode, "fps_list", key2);
159 std::string key = key1 + " " + key2;
160 RME_LOGI("ReadFrameConfig-key:%{public}s", key.c_str());
161 std::map<std::string, int> frameConfigTmp;
162 xmlNodePtr curSubNode = curNode->xmlChildrenNode;
163 for (; curSubNode != nullptr; curSubNode = curSubNode->next) {
164 std::string nodeName = reinterpret_cast<const char*>(curSubNode->name); // char* to string
165 xmlChar* context = xmlNodeGetContent(curSubNode);
166 if (context == nullptr) { // if one config wrong then this config dilscard.
167 RME_LOGE("[GetFrameConfig]: frame config get error! nodeName:%{public}s, key:%{public}s",
168 nodeName.c_str(), key.c_str());
169 xmlFree(context);
170 break;
171 }
172 frameConfigTmp[nodeName] = atoi(reinterpret_cast<const char*>(context));
173 RME_LOGI("[GetFrameConfig]: nodeName:%{public}s, val:%{public}s",
174 nodeName.c_str(), reinterpret_cast<const char*>(context));
175 xmlFree(context);
176 }
177 m_subEventConfig[key] = frameConfigTmp;
178 }
179 RME_LOGI("[GetFrameConfig]:read frameconfig success!");
180 }
181
ReadAttr(xmlNodePtr & root,const std::string & attrName,std::string & res)182 void ParaConfig::ReadAttr(xmlNodePtr& root, const std::string& attrName, std::string& res)
183 {
184 xmlAttrPtr attrPtr = root->properties;
185 while (attrPtr != nullptr) {
186 if (!xmlStrcmp(attrPtr->name, reinterpret_cast<const xmlChar*>(attrName.c_str()))) {
187 xmlChar* resAttr = xmlGetProp(root, reinterpret_cast<const xmlChar*>(attrName.c_str()));
188 res = std::to_string(atoi((char*)resAttr));
189 RME_LOGI("[ReadAttr]: attr <%{public}s> read res: %{public}s!", attrName.c_str(), res.c_str());
190 xmlFree(resAttr);
191 }
192 attrPtr = attrPtr->next;
193 }
194 }
195
GetGeneralConfig()196 std::map<std::string, std::string> ParaConfig::GetGeneralConfig()
197 {
198 return m_generalConfig;
199 }
200
GetSubEventConfig()201 std::map<std::string, std::map<std::string, int>> ParaConfig::GetSubEventConfig()
202 {
203 return m_subEventConfig;
204 }
205
GetFpsList()206 std::vector<int> ParaConfig::GetFpsList()
207 {
208 return m_fpsList;
209 }
210
GetRenderTypeList()211 std::vector<int> ParaConfig::GetRenderTypeList()
212 {
213 return m_renderTypeList;
214 }
215 } // namespace RME
216 } // namespace OHOS
217