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