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 "func_code_utils.h"
17 #include <edm_log.h>
18 #include <sstream>
19 #include <string_ex.h>
20
21 namespace OHOS {
22 namespace EDM {
ConvertSystemFlag(std::uint32_t flag)23 FuncFlag FuncCodeUtils::ConvertSystemFlag(std::uint32_t flag)
24 {
25 FuncFlag result = FuncFlag::UNKNOWN;
26 switch (flag) {
27 case static_cast<std::uint32_t>(FuncFlag::SERVICE_FLAG):
28 case static_cast<std::uint32_t>(FuncFlag::POLICY_FLAG):
29 result = static_cast<FuncFlag>(flag);
30 break;
31 default:
32 break;
33 }
34 return result;
35 }
36
GetSystemFlag(std::uint32_t funcCode)37 FuncFlag FuncCodeUtils::GetSystemFlag(std::uint32_t funcCode)
38 {
39 return ConvertSystemFlag(FUNC_TO_FLAG(funcCode));
40 }
41
ConvertOperateType(std::uint32_t type)42 FuncOperateType FuncCodeUtils::ConvertOperateType(std::uint32_t type)
43 {
44 FuncOperateType result = FuncOperateType::UNKNOWN;
45 switch (type) {
46 case static_cast<std::uint32_t>(FuncOperateType::GET):
47 case static_cast<std::uint32_t>(FuncOperateType::SET):
48 case static_cast<std::uint32_t>(FuncOperateType::REMOVE):
49 result = static_cast<FuncOperateType>(type);
50 break;
51 default:
52 break;
53 }
54 return result;
55 }
56
GetOperateType(std::uint32_t funcCode)57 FuncOperateType FuncCodeUtils::GetOperateType(std::uint32_t funcCode)
58 {
59 return ConvertOperateType(FUNC_TO_OPERATE(funcCode));
60 }
61
GetPolicyCode(std::uint32_t funcCode)62 std::uint32_t FuncCodeUtils::GetPolicyCode(std::uint32_t funcCode)
63 {
64 return FUNC_TO_POLICY(funcCode);
65 }
66
CreateFuncCode(std::uint32_t systemFlag,std::uint32_t operateType,std::uint32_t policyCode)67 std::uint32_t FuncCodeUtils::CreateFuncCode(std::uint32_t systemFlag, std::uint32_t operateType,
68 std::uint32_t policyCode)
69 {
70 return CREATE_FUNC_CODE(systemFlag, operateType, policyCode);
71 }
72
IsServiceFlag(std::uint32_t funcCode)73 bool FuncCodeUtils::IsServiceFlag(std::uint32_t funcCode)
74 {
75 return GetSystemFlag(funcCode) == FuncFlag::SERVICE_FLAG;
76 }
77
IsPolicyFlag(std::uint32_t funcCode)78 bool FuncCodeUtils::IsPolicyFlag(std::uint32_t funcCode)
79 {
80 return GetSystemFlag(funcCode) == FuncFlag::POLICY_FLAG;
81 }
82
PrintFuncCode(std::uint32_t funcCode)83 std::string FuncCodeUtils::PrintFuncCode(std::uint32_t funcCode)
84 {
85 std::ostringstream os;
86 os << (std::bitset<32>(funcCode)); // funcCode is 32 bits
87 EDMLOGD("PrintFuncCode bitset: %{public}s", os.str().c_str());
88 return os.str();
89 }
90
ArrayStringContains(const std::vector<std::string> & array,const std::string & findItem)91 bool ArrayPolicyUtils::ArrayStringContains(const std::vector<std::string> &array, const std::string &findItem)
92 {
93 return std::any_of(array.begin(), array.end(), [&findItem](const std::string &item) { return item == findItem; });
94 }
95
RemovePolicy(std::vector<std::string> & removeData,std::vector<std::string> & currentData)96 void ArrayPolicyUtils::RemovePolicy(std::vector<std::string> &removeData, std::vector<std::string> ¤tData)
97 {
98 auto it = currentData.begin();
99 while (it != currentData.end()) {
100 if (ArrayStringContains(removeData, *it)) {
101 it = currentData.erase(it);
102 } else {
103 it++;
104 }
105 }
106 }
107
RemovePolicy(std::vector<std::map<std::string,std::string>> & removeData,std::vector<std::map<std::string,std::string>> & currentData)108 void ArrayPolicyUtils::RemovePolicy(std::vector<std::map<std::string, std::string>> &removeData,
109 std::vector<std::map<std::string, std::string>> ¤tData)
110 {
111 auto it = currentData.begin();
112 while (it != currentData.end()) {
113 bool contains = std::any_of(removeData.begin(), removeData.end(),
114 [&it](const std::map<std::string, std::string> &item) { return item == *it; });
115 if (contains) {
116 it = currentData.erase(it);
117 } else {
118 it++;
119 }
120 }
121 }
122 } // namespace EDM
123 } // namespace OHOS