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
16 #include "camera_rotate_strategy_parser.h"
17 #include "camera_log.h"
18 #include "camera_util.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 static const int8_t DECIMAL = 10;
LoadConfiguration()23 bool CameraRotateStrategyParser::LoadConfiguration()
24 {
25 curNode_ = CameraXmlNode::Create();
26 int32_t ret = curNode_->Config(DEVICE_CONFIG_FILE, nullptr, 0);
27 if (ret != CAMERA_OK) {
28 MEDIA_ERR_LOG("Not found camera_rotate_strategy.xml!");
29 return false;
30 }
31 {
32 std::lock_guard<std::mutex> lock(strategyInfosMutex_);
33 cameraRotateStrategyInfos_.clear();
34 }
35 bool result = ParseInternal(curNode_->GetCopyNode());
36 CHECK_ERROR_RETURN_RET_LOG(!result, false, "Camera rotate strategy xml parse failed.");
37 return true;
38 }
39
Destroy()40 void CameraRotateStrategyParser::Destroy()
41 {
42 curNode_->FreeDoc();
43 curNode_->CleanUpParser();
44 }
45
ParseInternal(std::shared_ptr<CameraXmlNode> curNode)46 bool CameraRotateStrategyParser::ParseInternal(std::shared_ptr<CameraXmlNode> curNode)
47 {
48 for (; curNode->IsNodeValid(); curNode->MoveToNext()) {
49 if (!curNode->IsElementNode()) {
50 continue;
51 }
52 if (curNode->CompareName("strategy")) {
53 ParserStrategyInfo(curNode->GetCopyNode());
54 } else {
55 ParseInternal(curNode->GetChildrenNode());
56 }
57 }
58 return true;
59 }
60
ParserStrategyInfo(std::shared_ptr<CameraXmlNode> curNode)61 void CameraRotateStrategyParser::ParserStrategyInfo(std::shared_ptr<CameraXmlNode> curNode)
62 {
63 std::lock_guard<std::mutex> lock(strategyInfosMutex_);
64 if (curNode->IsNodeValid() && curNode->IsElementNode()) {
65 CameraRotateStrategyInfo info = {};
66 curNode->GetProp("bundleName", info.bundleName);
67
68 std::string pValue;
69 float wideValue = -1.0;
70 curNode->GetProp("wideValue", pValue);
71 char* endPtr;
72 wideValue = std::strtof(pValue.c_str(), &endPtr);
73 if (*endPtr != '\0' || pValue.empty()) {
74 MEDIA_ERR_LOG("The wideValue parameter is invalid.");
75 wideValue = -1.0;
76 }
77 info.wideValue = wideValue;
78 endPtr = nullptr;
79
80 int16_t rotateDegree = -1;
81 curNode->GetProp("rotateDegree", pValue);
82 long result = strtol(pValue.c_str(), &endPtr, DECIMAL);
83
84 if (*endPtr != '\0' || pValue.empty()) {
85 MEDIA_ERR_LOG("The rotateDegree parameter is invalid.");
86 rotateDegree = -1;
87 } else {
88 rotateDegree = static_cast<int16_t>(result);
89 }
90 info.rotateDegree = rotateDegree;
91
92 int16_t fps = -1;
93 curNode->GetProp("fps", pValue);
94 endPtr = nullptr;
95 result = strtol(pValue.c_str(), &endPtr, DECIMAL);
96
97 if (*endPtr != '\0' || pValue.empty()) {
98 MEDIA_ERR_LOG("The fps parameter is invalid.");
99 fps = -1;
100 } else {
101 fps = static_cast<int16_t>(result);
102 }
103 info.fps = fps;
104 cameraRotateStrategyInfos_.push_back(info);
105 MEDIA_INFO_LOG("ParserStrategyInfo: bundleName:%{public}s, wideValue:%{public}f, "
106 "rotateDegree:%{public}d, fps:%{public}d",
107 info.bundleName.c_str(), info.wideValue, info.rotateDegree, info.fps);
108 }
109 }
110 } // namespace CameraStandard
111 } // namespace OHOS
112