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 "telephony_call_policy_serializer.h"
17
18 #include <unordered_set>
19 #include "cJSON.h"
20 #include "cjson_check.h"
21
22 namespace OHOS {
23 namespace EDM {
24
25 const char* const POLICY_FLAG = "policyFlag";
26 const char* const NUMBER_LIST = "numberList";
27
Deserialize(const std::string & policy,std::map<std::string,TelephonyCallPolicyType> & dataObj)28 bool TelephonyCallPolicySerializer::Deserialize(const std::string &policy,
29 std::map<std::string, TelephonyCallPolicyType> &dataObj)
30 {
31 if (policy.empty()) {
32 return true;
33 }
34 cJSON* root = cJSON_Parse(policy.c_str());
35 if (root == nullptr) {
36 return false;
37 }
38 cJSON* mapItem;
39 cJSON_ArrayForEach(mapItem, root) {
40 cJSON* policyFlag = cJSON_GetObjectItem(mapItem, POLICY_FLAG);
41 cJSON* numberList = cJSON_GetObjectItem(mapItem, NUMBER_LIST);
42 if (!cJSON_IsArray(numberList) || !cJSON_IsNumber(policyFlag)) {
43 cJSON_Delete(root);
44 return false;
45 }
46 std::vector<std::string> numberVector;
47 cJSON* vectorItem;
48 cJSON_ArrayForEach(vectorItem, numberList) {
49 char* policyValue = cJSON_GetStringValue(vectorItem);
50 if (policyValue == nullptr) {
51 cJSON_Delete(root);
52 return false;
53 }
54 numberVector.push_back(std::string(policyValue));
55 }
56 dataObj[mapItem->string].numberList = numberVector;
57 dataObj[mapItem->string].policyFlag = policyFlag->valueint;
58 }
59 cJSON_Delete(root);
60 return true;
61 }
62
CreateArray(const std::vector<std::string> & numberList)63 cJSON* TelephonyCallPolicySerializer::CreateArray(const std::vector<std::string> &numberList)
64 {
65 cJSON* array = cJSON_CreateArray();
66 if (array == nullptr) {
67 return nullptr;
68 }
69 for (auto& vectorIt : numberList) {
70 cJSON* policyValue = cJSON_CreateString(vectorIt.c_str());
71 if (policyValue == nullptr) {
72 cJSON_Delete(array);
73 return nullptr;
74 }
75 cJSON_AddItemToArray(array, policyValue);
76 }
77
78 return array;
79 }
80
Serialize(const std::map<std::string,TelephonyCallPolicyType> & dataObj,std::string & policy)81 bool TelephonyCallPolicySerializer::Serialize(const std::map<std::string, TelephonyCallPolicyType> &dataObj,
82 std::string &policy)
83 {
84 cJSON* root = nullptr;
85 CJSON_CREATE_OBJECT_AND_CHECK(root, false);
86 for (auto& mapIt : dataObj) {
87 cJSON* policyObject = nullptr;
88 CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(policyObject, false, root);
89 cJSON_AddNumberToObject(policyObject, POLICY_FLAG, mapIt.second.policyFlag);
90 cJSON* array = CreateArray(mapIt.second.numberList);
91 if (array == nullptr) {
92 cJSON_Delete(root);
93 cJSON_Delete(policyObject);
94 return false;
95 }
96 if (!cJSON_AddItemToObject(policyObject, NUMBER_LIST, array)) {
97 cJSON_Delete(root);
98 cJSON_Delete(policyObject);
99 cJSON_Delete(array);
100 return false;
101 }
102 if (!cJSON_AddItemToObject(root, mapIt.first.c_str(), policyObject)) {
103 cJSON_Delete(root);
104 cJSON_Delete(policyObject);
105 return false;
106 }
107 }
108 char* jsonStr = cJSON_Print(root);
109 if (jsonStr == nullptr) {
110 cJSON_Delete(root);
111 return false;
112 }
113 policy = std::string(jsonStr);
114 cJSON_free(jsonStr);
115 cJSON_Delete(root);
116 return true;
117 }
118
GetPolicy(MessageParcel & data,std::map<std::string,TelephonyCallPolicyType> & result)119 bool TelephonyCallPolicySerializer::GetPolicy(MessageParcel &data,
120 std::map<std::string, TelephonyCallPolicyType> &result)
121 {
122 return true;
123 }
124
WritePolicy(MessageParcel & reply,std::map<std::string,TelephonyCallPolicyType> & result)125 bool TelephonyCallPolicySerializer::WritePolicy(MessageParcel &reply,
126 std::map<std::string, TelephonyCallPolicyType> &result)
127 {
128 return true;
129 }
130
MergePolicy(std::vector<std::map<std::string,TelephonyCallPolicyType>> & data,std::map<std::string,TelephonyCallPolicyType> & result)131 bool TelephonyCallPolicySerializer::MergePolicy(std::vector<std::map<std::string, TelephonyCallPolicyType>> &data,
132 std::map<std::string, TelephonyCallPolicyType> &result)
133 {
134 for (auto policyMap : data) {
135 for (auto iter : policyMap) {
136 if (result.find(iter.first) == result.end()) {
137 result[iter.first] = iter.second;
138 continue;
139 }
140 std::unordered_set<std::string> uset;
141 uset.insert(result[iter.first].numberList.begin(), result[iter.first].numberList.end());
142 uset.insert(iter.second.numberList.begin(), iter.second.numberList.end());
143 std::vector<std::string> mergeResult(uset.begin(), uset.end());
144 result[iter.first].numberList = mergeResult;
145 result[iter.first].policyFlag = iter.second.policyFlag;
146 }
147 }
148 return true;
149 }
150 } // namespace EDM
151 } // namespace OHOS