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
16 #include "drag_data_packer.h"
17
18 #include "devicestatus_common.h"
19 #include "devicestatus_define.h"
20 #include "devicestatus_errors.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "DragDataPacker"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
MarshallingDetailedSummarys(const DragData & dragData,Parcel & data)28 int32_t DragDataPacker::MarshallingDetailedSummarys(const DragData &dragData, Parcel &data)
29 {
30 if (SummaryPacker::Marshalling(dragData.detailedSummarys, data) != RET_OK) {
31 FI_HILOGE("Marshalling detailedSummarys failed");
32 return RET_ERR;
33 }
34 return RET_OK;
35 }
36
UnMarshallingDetailedSummarys(Parcel & data,DragData & dragData)37 int32_t DragDataPacker::UnMarshallingDetailedSummarys(Parcel &data, DragData &dragData)
38 {
39 if (SummaryPacker::UnMarshalling(data, dragData.detailedSummarys) != RET_OK) {
40 FI_HILOGE("UnMarshalling detailedSummarys failed");
41 return RET_ERR;
42 }
43 return RET_OK;
44 }
45
MarshallingSummaryExpanding(const DragData & dragData,Parcel & data)46 int32_t DragDataPacker::MarshallingSummaryExpanding(const DragData &dragData, Parcel &data)
47 {
48 if (SummaryFormat::Marshalling(dragData.summaryFormat, data) != RET_OK) {
49 FI_HILOGE("Marshalling summaryFormat failed");
50 return RET_ERR;
51 }
52 WRITEINT32(data, dragData.summaryVersion, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
53 WRITEINT64(data, dragData.summaryTotalSize, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
54 return RET_OK;
55 }
56
UnMarshallingSummaryExpanding(Parcel & data,DragData & dragData)57 int32_t DragDataPacker::UnMarshallingSummaryExpanding(Parcel &data, DragData &dragData)
58 {
59 if (SummaryFormat::UnMarshalling(data, dragData.summaryFormat) != RET_OK) {
60 FI_HILOGE("UnMarshalling summaryFormat failed");
61 return RET_ERR;
62 }
63 if (!(data).ReadInt32(dragData.summaryVersion)) {
64 FI_HILOGE("ReadInt32 summaryVersion failed");
65 return RET_ERR;
66 }
67 if (!(data).ReadInt64(dragData.summaryTotalSize)) {
68 FI_HILOGE("ReadInt64 summaryTotalSize failed");
69 return RET_ERR;
70 }
71 return RET_OK;
72 }
73
Marshalling(const DragData & dragData,Parcel & data,bool isCross)74 int32_t DragDataPacker::Marshalling(const DragData &dragData, Parcel &data, bool isCross)
75 {
76 CALL_DEBUG_ENTER;
77 if (ShadowPacker::Marshalling(dragData.shadowInfos, data, isCross) != RET_OK) {
78 FI_HILOGE("Marshalling shadowInfos failed");
79 return RET_ERR;
80 }
81 WRITEUINT8VECTOR(data, dragData.buffer, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
82 WRITESTRING(data, dragData.udKey, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
83 WRITESTRING(data, dragData.extraInfo, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
84 WRITESTRING(data, dragData.filterInfo, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
85 WRITEINT32(data, dragData.sourceType, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
86 WRITEINT32(data, dragData.dragNum, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
87 WRITEINT32(data, dragData.pointerId, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
88 WRITEINT32(data, dragData.displayX, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
89 WRITEINT32(data, dragData.displayY, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
90 WRITEINT32(data, dragData.displayId, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
91 WRITEINT32(data, dragData.mainWindow, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
92 WRITEBOOL(data, dragData.hasCanceledAnimation, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
93 WRITEBOOL(data, dragData.hasCoordinateCorrected, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
94 if (SummaryPacker::Marshalling(dragData.summarys, data) != RET_OK) {
95 FI_HILOGE("Marshalling summary failed");
96 return RET_ERR;
97 }
98
99 if (!isCross && !data.WriteBool(dragData.isDragDelay)) {
100 FI_HILOGE("Marshalling isDragDelay failed");
101 }
102 return RET_OK;
103 }
104
UnMarshalling(Parcel & data,DragData & dragData,bool isCross)105 int32_t DragDataPacker::UnMarshalling(Parcel &data, DragData &dragData, bool isCross)
106 {
107 CALL_DEBUG_ENTER;
108 if (ShadowPacker::UnMarshalling(data, dragData.shadowInfos, isCross) != RET_OK) {
109 FI_HILOGE("UnMarshalling shadowInfos failed");
110 return RET_ERR;
111 }
112 READUINT8VECTOR(data, dragData.buffer, E_DEVICESTATUS_READ_PARCEL_ERROR);
113 READSTRING(data, dragData.udKey, E_DEVICESTATUS_READ_PARCEL_ERROR);
114 READSTRING(data, dragData.extraInfo, E_DEVICESTATUS_READ_PARCEL_ERROR);
115 READSTRING(data, dragData.filterInfo, E_DEVICESTATUS_READ_PARCEL_ERROR);
116 READINT32(data, dragData.sourceType, E_DEVICESTATUS_READ_PARCEL_ERROR);
117 READINT32(data, dragData.dragNum, E_DEVICESTATUS_READ_PARCEL_ERROR);
118 READINT32(data, dragData.pointerId, E_DEVICESTATUS_READ_PARCEL_ERROR);
119 READINT32(data, dragData.displayX, E_DEVICESTATUS_READ_PARCEL_ERROR);
120 READINT32(data, dragData.displayY, E_DEVICESTATUS_READ_PARCEL_ERROR);
121 READINT32(data, dragData.displayId, E_DEVICESTATUS_READ_PARCEL_ERROR);
122 READINT32(data, dragData.mainWindow, E_DEVICESTATUS_READ_PARCEL_ERROR);
123 READBOOL(data, dragData.hasCanceledAnimation, E_DEVICESTATUS_READ_PARCEL_ERROR);
124 READBOOL(data, dragData.hasCoordinateCorrected, E_DEVICESTATUS_READ_PARCEL_ERROR);
125 if (SummaryPacker::UnMarshalling(data, dragData.summarys) != RET_OK) {
126 FI_HILOGE("Unmarshalling summary failed");
127 return RET_ERR;
128 }
129
130 if (!isCross && !data.ReadBool(dragData.isDragDelay)) {
131 FI_HILOGE("Unmarshalling isDragDelay failed");
132 }
133 return RET_OK;
134 }
135
CheckDragData(const DragData & dragData)136 int32_t DragDataPacker::CheckDragData(const DragData &dragData)
137 {
138 for (const auto& shadowInfo : dragData.shadowInfos) {
139 if (ShadowPacker::CheckShadowInfo(shadowInfo) != RET_OK) {
140 FI_HILOGE("CheckShadowInfo failed");
141 return RET_ERR;
142 }
143 }
144 if ((dragData.dragNum <= 0) || (dragData.buffer.size() > MAX_BUFFER_SIZE) ||
145 (dragData.displayX < 0) || (dragData.displayY < 0)) {
146 FI_HILOGE("Start drag invalid parameter, dragNum:%{public}d, bufferSize:%{public}zu, "
147 "displayX:%{private}d, displayY:%{private}d",
148 dragData.dragNum, dragData.buffer.size(), dragData.displayX, dragData.displayY);
149 return RET_ERR;
150 }
151 return RET_OK;
152 }
153
Marshalling(const std::vector<ShadowInfo> & shadowInfos,Parcel & data,bool isCross)154 int32_t ShadowPacker::Marshalling(const std::vector<ShadowInfo> &shadowInfos, Parcel &data, bool isCross)
155 {
156 CALL_DEBUG_ENTER;
157 if (shadowInfos.empty()) {
158 FI_HILOGE("Invalid parameter shadowInfos");
159 return ERR_INVALID_VALUE;
160 }
161 int32_t shadowNum = static_cast<int32_t>(shadowInfos.size());
162 if (shadowNum > SHADOW_NUM_LIMIT) {
163 FI_HILOGW("Only %{public}d shadowInfos allowed at most, now %{public}d", SHADOW_NUM_LIMIT, shadowNum);
164 shadowNum = SHADOW_NUM_LIMIT;
165 }
166 WRITEINT32(data, shadowNum, ERR_INVALID_VALUE);
167 for (int32_t i = 0; i < shadowNum; i++) {
168 if (PackUpShadowInfo(shadowInfos[i], data, isCross) != RET_OK) {
169 FI_HILOGE("PackUpShadowInfo No.%{public}d failed", i);
170 return RET_ERR;
171 }
172 }
173 return RET_OK;
174 }
175
UnMarshalling(Parcel & data,std::vector<ShadowInfo> & shadowInfos,bool isCross)176 int32_t ShadowPacker::UnMarshalling(Parcel &data, std::vector<ShadowInfo> &shadowInfos, bool isCross)
177 {
178 CALL_DEBUG_ENTER;
179 int32_t shadowNum { 0 };
180 READINT32(data, shadowNum, E_DEVICESTATUS_READ_PARCEL_ERROR);
181 if (shadowNum <= 0 || shadowNum > SHADOW_NUM_LIMIT) {
182 FI_HILOGE("Invalid shadowNum:%{public}d", shadowNum);
183 return RET_ERR;
184 }
185 for (int32_t i = 0; i < shadowNum; i++) {
186 ShadowInfo shadowInfo;
187 if (UnPackShadowInfo(data, shadowInfo, isCross) != RET_OK) {
188 FI_HILOGE("UnPackShadowInfo No.%{public}d failed", i);
189 return RET_ERR;
190 }
191 CHKPR(shadowInfo.pixelMap, RET_ERR);
192 shadowInfos.push_back(shadowInfo);
193 }
194 return RET_OK;
195 }
196
PackUpShadowInfo(const ShadowInfo & shadowInfo,Parcel & data,bool isCross)197 int32_t ShadowPacker::PackUpShadowInfo(const ShadowInfo &shadowInfo, Parcel &data, bool isCross)
198 {
199 CALL_DEBUG_ENTER;
200 CHKPR(shadowInfo.pixelMap, RET_ERR);
201 if (isCross) {
202 FI_HILOGD("By EncodeTlv");
203 std::vector<uint8_t> pixelBuffer;
204 if (!shadowInfo.pixelMap->EncodeTlv(pixelBuffer)) {
205 FI_HILOGE("EncodeTlv pixelMap failed");
206 return ERR_INVALID_VALUE;
207 }
208 WRITEUINT8VECTOR(data, pixelBuffer, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
209 } else {
210 FI_HILOGD("By Marshalling");
211 if (!shadowInfo.pixelMap->Marshalling(data)) {
212 FI_HILOGE("Marshalling pixelMap failed");
213 return ERR_INVALID_VALUE;
214 }
215 }
216 WRITEINT32(data, shadowInfo.x, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
217 WRITEINT32(data, shadowInfo.y, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
218 return RET_OK;
219 }
220
UnPackShadowInfo(Parcel & data,ShadowInfo & shadowInfo,bool isCross)221 int32_t ShadowPacker::UnPackShadowInfo(Parcel &data, ShadowInfo &shadowInfo, bool isCross)
222 {
223 CALL_DEBUG_ENTER;
224 Media::PixelMap *rawPixelMap = nullptr;
225 if (isCross) {
226 FI_HILOGD("By DecodeTlv");
227 std::vector<uint8_t> pixelBuffer;
228 READUINT8VECTOR(data, pixelBuffer, ERR_INVALID_VALUE);
229 rawPixelMap = Media::PixelMap::DecodeTlv(pixelBuffer);
230 } else {
231 FI_HILOGD("By UnMarshalling");
232 rawPixelMap = OHOS::Media::PixelMap::Unmarshalling(data);
233 }
234 CHKPR(rawPixelMap, RET_ERR);
235 shadowInfo.pixelMap = std::shared_ptr<Media::PixelMap>(rawPixelMap);
236 CHKPR(shadowInfo.pixelMap, RET_ERR);
237 READINT32(data, shadowInfo.x, E_DEVICESTATUS_READ_PARCEL_ERROR);
238 READINT32(data, shadowInfo.y, E_DEVICESTATUS_READ_PARCEL_ERROR);
239 return RET_OK;
240 }
241
CheckShadowInfo(const ShadowInfo & shadowInfo)242 int32_t ShadowPacker::CheckShadowInfo(const ShadowInfo &shadowInfo)
243 {
244 CHKPR(shadowInfo.pixelMap, RET_ERR);
245 if ((shadowInfo.x > 0) || (shadowInfo.y > 0) ||
246 (shadowInfo.x < -shadowInfo.pixelMap->GetWidth()) || (shadowInfo.y < -shadowInfo.pixelMap->GetHeight())) {
247 FI_HILOGE("Invalid parameter, shadowInfoX:%{private}d, shadowInfoY:%{private}d", shadowInfo.x, shadowInfo.y);
248 return RET_ERR;
249 }
250 return RET_OK;
251 }
252
Marshalling(const SummaryMap & val,Parcel & parcel)253 int32_t SummaryPacker::Marshalling(const SummaryMap &val, Parcel &parcel)
254 {
255 WRITEINT32(parcel, static_cast<int32_t>(val.size()), ERR_INVALID_VALUE);
256 for (auto const &[k, v] : val) {
257 WRITESTRING(parcel, k, ERR_INVALID_VALUE);
258 WRITEINT64(parcel, v, ERR_INVALID_VALUE);
259 }
260 return RET_OK;
261 }
262
UnMarshalling(Parcel & parcel,SummaryMap & val)263 int32_t SummaryPacker::UnMarshalling(Parcel &parcel, SummaryMap &val)
264 {
265 size_t readAbleSize = parcel.GetReadableBytes();
266 int32_t size = 0;
267 READINT32(parcel, size, E_DEVICESTATUS_READ_PARCEL_ERROR);
268 if (size < 0 || (static_cast<size_t>(size) > readAbleSize) || static_cast<size_t>(size) > val.max_size()) {
269 FI_HILOGE("Invalid size:%{public}d", size);
270 return RET_ERR;
271 }
272 for (int32_t i = 0; i < size; ++i) {
273 std::string key;
274 READSTRING(parcel, key, E_DEVICESTATUS_READ_PARCEL_ERROR);
275 READINT64(parcel, val[key], E_DEVICESTATUS_READ_PARCEL_ERROR);
276 }
277 return RET_OK;
278 }
279
Marshalling(const ShadowOffset & shadowOffset,Parcel & parcel)280 int32_t ShadowOffsetPacker::Marshalling(const ShadowOffset&shadowOffset, Parcel &parcel)
281 {
282 WRITEINT32(parcel, shadowOffset.offsetX, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
283 WRITEINT32(parcel, shadowOffset.offsetY, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
284 WRITEINT32(parcel, shadowOffset.width, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
285 WRITEINT32(parcel, shadowOffset.height, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
286 return RET_OK;
287 }
288
UnMarshalling(Parcel & parcel,ShadowOffset & shadowOffset)289 int32_t ShadowOffsetPacker::UnMarshalling(Parcel &parcel, ShadowOffset&shadowOffset)
290 {
291 READINT32(parcel, shadowOffset.offsetX, E_DEVICESTATUS_READ_PARCEL_ERROR);
292 READINT32(parcel, shadowOffset.offsetY, E_DEVICESTATUS_READ_PARCEL_ERROR);
293 READINT32(parcel, shadowOffset.width, E_DEVICESTATUS_READ_PARCEL_ERROR);
294 READINT32(parcel, shadowOffset.height, E_DEVICESTATUS_READ_PARCEL_ERROR);
295 return RET_OK;
296 }
297
Marshalling(const std::map<std::string,std::vector<int32_t>> & val,Parcel & parcel)298 int32_t SummaryFormat::Marshalling(const std::map<std::string, std::vector<int32_t>> &val, Parcel &parcel)
299 {
300 WRITEINT32(parcel, static_cast<int32_t>(val.size()), ERR_INVALID_VALUE);
301 for (auto const &[k, v] : val) {
302 WRITESTRING(parcel, k, ERR_INVALID_VALUE);
303 WRITEINT32VECTOR(parcel, v, ERR_INVALID_VALUE);
304 }
305 return RET_OK;
306 }
307
UnMarshalling(Parcel & parcel,std::map<std::string,std::vector<int32_t>> & val)308 int32_t SummaryFormat::UnMarshalling(Parcel &parcel, std::map<std::string, std::vector<int32_t>> &val)
309 {
310 size_t readAbleSize = parcel.GetReadableBytes();
311 int32_t size = 0;
312 READINT32(parcel, size, E_DEVICESTATUS_READ_PARCEL_ERROR);
313 if (size < 0 || (static_cast<size_t>(size) > readAbleSize) || static_cast<size_t>(size) > val.max_size()) {
314 FI_HILOGE("Invalid size:%{public}d", size);
315 return RET_ERR;
316 }
317 for (int32_t i = 0; i < size; ++i) {
318 std::string key;
319 READSTRING(parcel, key, E_DEVICESTATUS_READ_PARCEL_ERROR);
320 READINT32VECTOR(parcel, val[key], E_DEVICESTATUS_READ_PARCEL_ERROR);
321 }
322 return RET_OK;
323 }
324 } // namespace DeviceStatus
325 } // namespace Msdp
326 } // namespace OHOS
327