1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "UdmfTypesUtil"
16 #include "udmf_types_util.h"
17
18 #include "logger.h"
19 #include "tlv_util.h"
20 #include "udmf_conversion.h"
21
22 namespace OHOS {
23 namespace ITypesUtil {
24 using namespace UDMF;
25 template<>
Marshalling(const UnifiedData & input,MessageParcel & parcel)26 bool Marshalling(const UnifiedData &input, MessageParcel &parcel)
27 {
28 std::vector<uint8_t> dataBytes;
29 auto recordTlv = TLVObject(dataBytes);
30 if (!TLVUtil::Writing(input, recordTlv, TAG::TAG_UNIFIED_DATA)) {
31 LOG_ERROR(UDMF_SERVICE, "TLV writing failed!");
32 return false;
33 }
34 if (!parcel.WriteInt32(static_cast<int32_t>(dataBytes.size()))
35 || !parcel.WriteRawData(dataBytes.data(), dataBytes.size())) {
36 LOG_ERROR(UDMF_SERVICE, "Marshall unified data failed!");
37 return false;
38 }
39 return true;
40 }
41
42 template<>
Unmarshalling(UnifiedData & output,MessageParcel & parcel)43 bool Unmarshalling(UnifiedData &output, MessageParcel &parcel)
44 {
45 auto size = parcel.ReadInt32();
46 if (size == 0) {
47 LOG_ERROR(UDMF_SERVICE, "UnifiedData is empty!");
48 return false;
49 }
50 auto rawData = parcel.ReadRawData(size);
51 if (rawData == nullptr) {
52 LOG_ERROR(UDMF_SERVICE, "RawData is null!");
53 return false;
54 }
55 const uint8_t *data = reinterpret_cast<const uint8_t *>(rawData);
56 std::vector<uint8_t> dataBytes(data, data + size);
57 auto dataTlv = TLVObject(dataBytes);
58 if (!TLVUtil::ReadTlv(output, dataTlv, TAG::TAG_UNIFIED_DATA)) {
59 LOG_ERROR(UDMF_SERVICE, "Unmarshall unified data failed!");
60 return false;
61 }
62 UdmfConversion::ConvertRecordToSubclass(output);
63 return true;
64 }
65
66 template<>
Marshalling(const std::vector<UnifiedData> & input,MessageParcel & parcel)67 bool Marshalling(const std::vector<UnifiedData> &input, MessageParcel &parcel)
68 {
69 std::vector<uint8_t> dataSetBytes;
70 auto recordTlv = TLVObject(dataSetBytes);
71 if (!TLVUtil::Writing(input, recordTlv, TAG::TAG_UNIFIED_DATA)) {
72 LOG_ERROR(UDMF_SERVICE, "TLV writing failed!");
73 return false;
74 }
75 if (!parcel.WriteInt32(static_cast<int32_t>(dataSetBytes.size()))
76 || !parcel.WriteRawData(dataSetBytes.data(), dataSetBytes.size())) {
77 LOG_ERROR(UDMF_SERVICE, "Marshall unified data set failed!");
78 return false;
79 }
80 return true;
81 }
82
83 template<>
Unmarshalling(std::vector<UnifiedData> & output,MessageParcel & parcel)84 bool Unmarshalling(std::vector<UnifiedData> &output, MessageParcel &parcel)
85 {
86 auto size = parcel.ReadInt32();
87 if (size == 0) {
88 LOG_ERROR(UDMF_SERVICE, "UnifiedDataSet is empty!");
89 return false;
90 }
91 const uint8_t *rawData = reinterpret_cast<const uint8_t *>(parcel.ReadRawData(size));
92 if (rawData == nullptr) {
93 LOG_ERROR(UDMF_SERVICE, "RawData is null!");
94 return false;
95 }
96 std::vector<uint8_t> dataSetBytes(rawData, rawData + size);
97 auto dataTlv = TLVObject(dataSetBytes);
98 if (!TLVUtil::ReadTlv(output, dataTlv, TAG::TAG_UNIFIED_DATA)) {
99 LOG_ERROR(UDMF_SERVICE, "Unmarshall unified data set failed!");
100 return false;
101 }
102 UdmfConversion::ConvertRecordToSubclass(output);
103 return true;
104 }
105
106 template<>
Marshalling(const Summary & input,MessageParcel & parcel)107 bool Marshalling(const Summary &input, MessageParcel &parcel)
108 {
109 return ITypesUtil::Marshal(parcel, input.summary, input.totalSize);
110 }
111
112 template<>
Unmarshalling(Summary & output,MessageParcel & parcel)113 bool Unmarshalling(Summary &output, MessageParcel &parcel)
114 {
115 return ITypesUtil::Unmarshal(parcel, output.summary, output.totalSize);
116 }
117
118 template<>
Marshalling(const Privilege & input,MessageParcel & parcel)119 bool Marshalling(const Privilege &input, MessageParcel &parcel)
120 {
121 return ITypesUtil::Marshal(parcel, input.tokenId, input.readPermission, input.writePermission);
122 }
123
124 template<>
Unmarshalling(Privilege & output,MessageParcel & parcel)125 bool Unmarshalling(Privilege &output, MessageParcel &parcel)
126 {
127 return ITypesUtil::Unmarshal(parcel, output.tokenId, output.readPermission, output.writePermission);
128 }
129
130 template<>
Marshalling(const CustomOption & input,MessageParcel & parcel)131 bool Marshalling(const CustomOption &input, MessageParcel &parcel)
132 {
133 return ITypesUtil::Marshal(parcel, input.intention);
134 }
135
136 template<>
Unmarshalling(CustomOption & output,MessageParcel & parcel)137 bool Unmarshalling(CustomOption &output, MessageParcel &parcel)
138 {
139 return ITypesUtil::Unmarshal(parcel, output.intention);
140 }
141
142 template<>
Marshalling(const QueryOption & input,MessageParcel & parcel)143 bool Marshalling(const QueryOption &input, MessageParcel &parcel)
144 {
145 return ITypesUtil::Marshal(parcel, input.key, input.intention);
146 }
147
148 template<>
Unmarshalling(QueryOption & output,MessageParcel & parcel)149 bool Unmarshalling(QueryOption &output, MessageParcel &parcel)
150 {
151 return ITypesUtil::Unmarshal(parcel, output.key, output.intention);
152 }
153
154 template<>
Marshalling(const UDType & input,MessageParcel & parcel)155 bool Marshalling(const UDType &input, MessageParcel &parcel)
156 {
157 int32_t type = input;
158 return ITypesUtil::Marshal(parcel, type);
159 }
160
161 template<>
Unmarshalling(UDType & output,MessageParcel & parcel)162 bool Unmarshalling(UDType &output, MessageParcel &parcel)
163 {
164 int32_t type;
165 if (!ITypesUtil::Unmarshal(parcel, type)) {
166 LOG_ERROR(UDMF_FRAMEWORK, "Unmarshal UDType failed!");
167 return false;
168 }
169 if (type < ENTITY || type >= UD_BUTT) {
170 LOG_ERROR(UDMF_FRAMEWORK, "invalid UDType!");
171 return false;
172 }
173 output = static_cast<UDType>(type);
174 return true;
175 }
176
177 template<>
Marshalling(const Intention & input,MessageParcel & parcel)178 bool Marshalling(const Intention &input, MessageParcel &parcel)
179 {
180 int32_t intention = input;
181 return ITypesUtil::Marshal(parcel, intention);
182 }
183
184 template<>
Unmarshalling(Intention & output,MessageParcel & parcel)185 bool Unmarshalling(Intention &output, MessageParcel &parcel)
186 {
187 int32_t intention;
188 if (!ITypesUtil::Unmarshal(parcel, intention)) {
189 LOG_ERROR(UDMF_FRAMEWORK, "Unmarshal Intention failed!");
190 return false;
191 }
192 if (intention < UD_INTENTION_BASE || intention > UD_INTENTION_BUTT) {
193 LOG_ERROR(UDMF_FRAMEWORK, "invalid Intention!");
194 return false;
195 }
196 output = static_cast<Intention>(intention);
197 return true;
198 }
199 } // namespace ITypesUtil
200 } // namespace OHOS