1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "drag_data_packer.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include "devicestatus_common.h"
19f857971dSopenharmony_ci#include "devicestatus_define.h"
20f857971dSopenharmony_ci#include "devicestatus_errors.h"
21f857971dSopenharmony_ci
22f857971dSopenharmony_ci#undef LOG_TAG
23f857971dSopenharmony_ci#define LOG_TAG "DragDataPacker"
24f857971dSopenharmony_ci
25f857971dSopenharmony_cinamespace OHOS {
26f857971dSopenharmony_cinamespace Msdp {
27f857971dSopenharmony_cinamespace DeviceStatus {
28f857971dSopenharmony_ci
29f857971dSopenharmony_ciint32_t DragDataPacker::Marshalling(const DragData &dragData, Parcel &data, bool isCross)
30f857971dSopenharmony_ci{
31f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
32f857971dSopenharmony_ci    if (ShadowPacker::Marshalling(dragData.shadowInfos, data, isCross) != RET_OK) {
33f857971dSopenharmony_ci        FI_HILOGE("Marshalling shadowInfos failed");
34f857971dSopenharmony_ci        return RET_ERR;
35f857971dSopenharmony_ci    }
36f857971dSopenharmony_ci    WRITEUINT8VECTOR(data, dragData.buffer, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
37f857971dSopenharmony_ci    WRITESTRING(data, dragData.udKey, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
38f857971dSopenharmony_ci    WRITESTRING(data, dragData.extraInfo, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
39f857971dSopenharmony_ci    WRITESTRING(data, dragData.filterInfo, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
40f857971dSopenharmony_ci    WRITEINT32(data, dragData.sourceType, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
41f857971dSopenharmony_ci    WRITEINT32(data, dragData.dragNum, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
42f857971dSopenharmony_ci    WRITEINT32(data, dragData.pointerId, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
43f857971dSopenharmony_ci    WRITEINT32(data, dragData.displayX, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
44f857971dSopenharmony_ci    WRITEINT32(data, dragData.displayY, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
45f857971dSopenharmony_ci    WRITEINT32(data, dragData.displayId, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
46f857971dSopenharmony_ci    WRITEINT32(data, dragData.mainWindow, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
47f857971dSopenharmony_ci    WRITEBOOL(data, dragData.hasCanceledAnimation, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
48f857971dSopenharmony_ci    WRITEBOOL(data, dragData.hasCoordinateCorrected, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
49f857971dSopenharmony_ci    if (SummaryPacker::Marshalling(dragData.summarys, data) != RET_OK) {
50f857971dSopenharmony_ci        FI_HILOGE("Marshalling summary failed");
51f857971dSopenharmony_ci        return RET_ERR;
52f857971dSopenharmony_ci    }
53f857971dSopenharmony_ci    return RET_OK;
54f857971dSopenharmony_ci}
55f857971dSopenharmony_ci
56f857971dSopenharmony_ciint32_t DragDataPacker::UnMarshalling(Parcel &data, DragData &dragData, bool isCross)
57f857971dSopenharmony_ci{
58f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
59f857971dSopenharmony_ci    if (ShadowPacker::UnMarshalling(data, dragData.shadowInfos, isCross) != RET_OK) {
60f857971dSopenharmony_ci        FI_HILOGE("UnMarshalling shadowInfos failed");
61f857971dSopenharmony_ci        return RET_ERR;
62f857971dSopenharmony_ci    }
63f857971dSopenharmony_ci    READUINT8VECTOR(data, dragData.buffer, E_DEVICESTATUS_READ_PARCEL_ERROR);
64f857971dSopenharmony_ci    READSTRING(data, dragData.udKey, E_DEVICESTATUS_READ_PARCEL_ERROR);
65f857971dSopenharmony_ci    READSTRING(data, dragData.extraInfo, E_DEVICESTATUS_READ_PARCEL_ERROR);
66f857971dSopenharmony_ci    READSTRING(data, dragData.filterInfo, E_DEVICESTATUS_READ_PARCEL_ERROR);
67f857971dSopenharmony_ci    READINT32(data, dragData.sourceType, E_DEVICESTATUS_READ_PARCEL_ERROR);
68f857971dSopenharmony_ci    READINT32(data, dragData.dragNum, E_DEVICESTATUS_READ_PARCEL_ERROR);
69f857971dSopenharmony_ci    READINT32(data, dragData.pointerId, E_DEVICESTATUS_READ_PARCEL_ERROR);
70f857971dSopenharmony_ci    READINT32(data, dragData.displayX, E_DEVICESTATUS_READ_PARCEL_ERROR);
71f857971dSopenharmony_ci    READINT32(data, dragData.displayY, E_DEVICESTATUS_READ_PARCEL_ERROR);
72f857971dSopenharmony_ci    READINT32(data, dragData.displayId, E_DEVICESTATUS_READ_PARCEL_ERROR);
73f857971dSopenharmony_ci    READINT32(data, dragData.mainWindow, E_DEVICESTATUS_READ_PARCEL_ERROR);
74f857971dSopenharmony_ci    READBOOL(data, dragData.hasCanceledAnimation, E_DEVICESTATUS_READ_PARCEL_ERROR);
75f857971dSopenharmony_ci    READBOOL(data, dragData.hasCoordinateCorrected, E_DEVICESTATUS_READ_PARCEL_ERROR);
76f857971dSopenharmony_ci    if (SummaryPacker::UnMarshalling(data, dragData.summarys) != RET_OK) {
77f857971dSopenharmony_ci        FI_HILOGE("Unmarshalling summary failed");
78f857971dSopenharmony_ci        return RET_ERR;
79f857971dSopenharmony_ci    }
80f857971dSopenharmony_ci    return RET_OK;
81f857971dSopenharmony_ci}
82f857971dSopenharmony_ci
83f857971dSopenharmony_ciint32_t DragDataPacker::CheckDragData(const DragData &dragData)
84f857971dSopenharmony_ci{
85f857971dSopenharmony_ci    for (const auto& shadowInfo : dragData.shadowInfos) {
86f857971dSopenharmony_ci        if (ShadowPacker::CheckShadowInfo(shadowInfo) != RET_OK) {
87f857971dSopenharmony_ci            FI_HILOGE("CheckShadowInfo failed");
88f857971dSopenharmony_ci            return RET_ERR;
89f857971dSopenharmony_ci        }
90f857971dSopenharmony_ci    }
91f857971dSopenharmony_ci    if ((dragData.dragNum <= 0) || (dragData.buffer.size() > MAX_BUFFER_SIZE) ||
92f857971dSopenharmony_ci        (dragData.displayX < 0) || (dragData.displayY < 0)) {
93f857971dSopenharmony_ci        FI_HILOGE("Start drag invalid parameter, dragNum:%{public}d, bufferSize:%{public}zu, "
94f857971dSopenharmony_ci            "displayX:%{private}d, displayY:%{private}d",
95f857971dSopenharmony_ci            dragData.dragNum, dragData.buffer.size(), dragData.displayX, dragData.displayY);
96f857971dSopenharmony_ci        return RET_ERR;
97f857971dSopenharmony_ci    }
98f857971dSopenharmony_ci    return RET_OK;
99f857971dSopenharmony_ci}
100f857971dSopenharmony_ci
101f857971dSopenharmony_ciint32_t ShadowPacker::Marshalling(const std::vector<ShadowInfo> &shadowInfos, Parcel &data, bool isCross)
102f857971dSopenharmony_ci{
103f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
104f857971dSopenharmony_ci    if (shadowInfos.empty()) {
105f857971dSopenharmony_ci        FI_HILOGE("Invalid parameter shadowInfos");
106f857971dSopenharmony_ci        return ERR_INVALID_VALUE;
107f857971dSopenharmony_ci    }
108f857971dSopenharmony_ci    int32_t shadowNum = static_cast<int32_t>(shadowInfos.size());
109f857971dSopenharmony_ci    if (shadowNum > SHADOW_NUM_LIMIT) {
110f857971dSopenharmony_ci        FI_HILOGW("Only %{public}d shadowInfos allowed at most, now %{public}d", SHADOW_NUM_LIMIT, shadowNum);
111f857971dSopenharmony_ci        shadowNum = SHADOW_NUM_LIMIT;
112f857971dSopenharmony_ci    }
113f857971dSopenharmony_ci    WRITEINT32(data, shadowNum, ERR_INVALID_VALUE);
114f857971dSopenharmony_ci    for (int32_t i = 0; i < shadowNum; i++) {
115f857971dSopenharmony_ci        if (PackUpShadowInfo(shadowInfos[i], data, isCross) != RET_OK) {
116f857971dSopenharmony_ci            FI_HILOGE("PackUpShadowInfo No.%{public}d failed", i);
117f857971dSopenharmony_ci            return RET_ERR;
118f857971dSopenharmony_ci        }
119f857971dSopenharmony_ci    }
120f857971dSopenharmony_ci    return RET_OK;
121f857971dSopenharmony_ci}
122f857971dSopenharmony_ci
123f857971dSopenharmony_ciint32_t ShadowPacker::UnMarshalling(Parcel &data, std::vector<ShadowInfo> &shadowInfos, bool isCross)
124f857971dSopenharmony_ci{
125f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
126f857971dSopenharmony_ci    int32_t shadowNum { 0 };
127f857971dSopenharmony_ci    READINT32(data, shadowNum, E_DEVICESTATUS_READ_PARCEL_ERROR);
128f857971dSopenharmony_ci    if (shadowNum <= 0 || shadowNum > SHADOW_NUM_LIMIT) {
129f857971dSopenharmony_ci        FI_HILOGE("Invalid shadowNum:%{public}d", shadowNum);
130f857971dSopenharmony_ci        return RET_ERR;
131f857971dSopenharmony_ci    }
132f857971dSopenharmony_ci    for (int32_t i = 0; i < shadowNum; i++) {
133f857971dSopenharmony_ci        ShadowInfo shadowInfo;
134f857971dSopenharmony_ci        if (UnPackShadowInfo(data, shadowInfo, isCross) != RET_OK) {
135f857971dSopenharmony_ci            FI_HILOGE("UnPackShadowInfo No.%{public}d failed", i);
136f857971dSopenharmony_ci            return RET_ERR;
137f857971dSopenharmony_ci        }
138f857971dSopenharmony_ci        CHKPR(shadowInfo.pixelMap, RET_ERR);
139f857971dSopenharmony_ci        shadowInfos.push_back(shadowInfo);
140f857971dSopenharmony_ci    }
141f857971dSopenharmony_ci    return RET_OK;
142f857971dSopenharmony_ci}
143f857971dSopenharmony_ci
144f857971dSopenharmony_ciint32_t ShadowPacker::PackUpShadowInfo(const ShadowInfo &shadowInfo, Parcel &data, bool isCross)
145f857971dSopenharmony_ci{
146f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
147f857971dSopenharmony_ci    CHKPR(shadowInfo.pixelMap, RET_ERR);
148f857971dSopenharmony_ci    if (isCross) {
149f857971dSopenharmony_ci        FI_HILOGD("By EncodeTlv");
150f857971dSopenharmony_ci        std::vector<uint8_t> pixelBuffer;
151f857971dSopenharmony_ci        if (!shadowInfo.pixelMap->EncodeTlv(pixelBuffer)) {
152f857971dSopenharmony_ci            FI_HILOGE("EncodeTlv pixelMap failed");
153f857971dSopenharmony_ci            return ERR_INVALID_VALUE;
154f857971dSopenharmony_ci        }
155f857971dSopenharmony_ci        WRITEUINT8VECTOR(data, pixelBuffer, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
156f857971dSopenharmony_ci    } else {
157f857971dSopenharmony_ci        FI_HILOGD("By Marshalling");
158f857971dSopenharmony_ci        if (!shadowInfo.pixelMap->Marshalling(data)) {
159f857971dSopenharmony_ci            FI_HILOGE("Marshalling pixelMap failed");
160f857971dSopenharmony_ci            return ERR_INVALID_VALUE;
161f857971dSopenharmony_ci        }
162f857971dSopenharmony_ci    }
163f857971dSopenharmony_ci    WRITEINT32(data, shadowInfo.x, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
164f857971dSopenharmony_ci    WRITEINT32(data, shadowInfo.y, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
165f857971dSopenharmony_ci    return RET_OK;
166f857971dSopenharmony_ci}
167f857971dSopenharmony_ci
168f857971dSopenharmony_ciint32_t ShadowPacker::UnPackShadowInfo(Parcel &data, ShadowInfo &shadowInfo, bool isCross)
169f857971dSopenharmony_ci{
170f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
171f857971dSopenharmony_ci    Media::PixelMap *rawPixelMap = nullptr;
172f857971dSopenharmony_ci    if (isCross) {
173f857971dSopenharmony_ci        FI_HILOGD("By DecodeTlv");
174f857971dSopenharmony_ci        std::vector<uint8_t> pixelBuffer;
175f857971dSopenharmony_ci        READUINT8VECTOR(data, pixelBuffer, ERR_INVALID_VALUE);
176f857971dSopenharmony_ci        rawPixelMap = Media::PixelMap::DecodeTlv(pixelBuffer);
177f857971dSopenharmony_ci    } else {
178f857971dSopenharmony_ci        FI_HILOGD("By UnMarshalling");
179f857971dSopenharmony_ci        rawPixelMap = OHOS::Media::PixelMap::Unmarshalling(data);
180f857971dSopenharmony_ci    }
181f857971dSopenharmony_ci    CHKPR(rawPixelMap, RET_ERR);
182f857971dSopenharmony_ci    shadowInfo.pixelMap = std::shared_ptr<Media::PixelMap>(rawPixelMap);
183f857971dSopenharmony_ci    CHKPR(shadowInfo.pixelMap, RET_ERR);
184f857971dSopenharmony_ci    READINT32(data, shadowInfo.x, E_DEVICESTATUS_READ_PARCEL_ERROR);
185f857971dSopenharmony_ci    READINT32(data, shadowInfo.y, E_DEVICESTATUS_READ_PARCEL_ERROR);
186f857971dSopenharmony_ci    return RET_OK;
187f857971dSopenharmony_ci}
188f857971dSopenharmony_ci
189f857971dSopenharmony_ciint32_t ShadowPacker::CheckShadowInfo(const ShadowInfo &shadowInfo)
190f857971dSopenharmony_ci{
191f857971dSopenharmony_ci    CHKPR(shadowInfo.pixelMap, RET_ERR);
192f857971dSopenharmony_ci    if ((shadowInfo.x > 0) || (shadowInfo.y > 0) ||
193f857971dSopenharmony_ci        (shadowInfo.x < -shadowInfo.pixelMap->GetWidth()) || (shadowInfo.y < -shadowInfo.pixelMap->GetHeight())) {
194f857971dSopenharmony_ci        FI_HILOGE("Invalid parameter, shadowInfoX:%{private}d, shadowInfoY:%{private}d", shadowInfo.x, shadowInfo.y);
195f857971dSopenharmony_ci        return RET_ERR;
196f857971dSopenharmony_ci    }
197f857971dSopenharmony_ci    return RET_OK;
198f857971dSopenharmony_ci}
199f857971dSopenharmony_ci
200f857971dSopenharmony_ciint32_t SummaryPacker::Marshalling(const SummaryMap &val, Parcel &parcel)
201f857971dSopenharmony_ci{
202f857971dSopenharmony_ci    WRITEINT32(parcel, static_cast<int32_t>(val.size()), ERR_INVALID_VALUE);
203f857971dSopenharmony_ci    for (auto const &[k, v] : val) {
204f857971dSopenharmony_ci        WRITESTRING(parcel, k, ERR_INVALID_VALUE);
205f857971dSopenharmony_ci        WRITEINT64(parcel, v, ERR_INVALID_VALUE);
206f857971dSopenharmony_ci    }
207f857971dSopenharmony_ci    return RET_OK;
208f857971dSopenharmony_ci}
209f857971dSopenharmony_ci
210f857971dSopenharmony_ciint32_t SummaryPacker::UnMarshalling(Parcel &parcel, SummaryMap &val)
211f857971dSopenharmony_ci{
212f857971dSopenharmony_ci    size_t readAbleSize = parcel.GetReadableBytes();
213f857971dSopenharmony_ci    int32_t size = 0;
214f857971dSopenharmony_ci    READINT32(parcel, size, E_DEVICESTATUS_READ_PARCEL_ERROR);
215f857971dSopenharmony_ci    if (size < 0 || (static_cast<size_t>(size) > readAbleSize) || static_cast<size_t>(size) > val.max_size()) {
216f857971dSopenharmony_ci        FI_HILOGE("Invalid size:%{public}d", size);
217f857971dSopenharmony_ci        return RET_ERR;
218f857971dSopenharmony_ci    }
219f857971dSopenharmony_ci    for (int32_t i = 0; i < size; ++i) {
220f857971dSopenharmony_ci        std::string key;
221f857971dSopenharmony_ci        READSTRING(parcel, key, E_DEVICESTATUS_READ_PARCEL_ERROR);
222f857971dSopenharmony_ci        READINT64(parcel, val[key], E_DEVICESTATUS_READ_PARCEL_ERROR);
223f857971dSopenharmony_ci    }
224f857971dSopenharmony_ci    return RET_OK;
225f857971dSopenharmony_ci}
226f857971dSopenharmony_ci
227f857971dSopenharmony_ciint32_t ShadowOffsetPacker::Marshalling(const ShadowOffset&shadowOffset, Parcel &parcel)
228f857971dSopenharmony_ci{
229f857971dSopenharmony_ci    WRITEINT32(parcel, shadowOffset.offsetX, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
230f857971dSopenharmony_ci    WRITEINT32(parcel, shadowOffset.offsetY, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
231f857971dSopenharmony_ci    WRITEINT32(parcel, shadowOffset.width, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
232f857971dSopenharmony_ci    WRITEINT32(parcel, shadowOffset.height, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
233f857971dSopenharmony_ci    return RET_OK;
234f857971dSopenharmony_ci}
235f857971dSopenharmony_ci
236f857971dSopenharmony_ciint32_t ShadowOffsetPacker::UnMarshalling(Parcel &parcel, ShadowOffset&shadowOffset)
237f857971dSopenharmony_ci{
238f857971dSopenharmony_ci    READINT32(parcel, shadowOffset.offsetX, E_DEVICESTATUS_READ_PARCEL_ERROR);
239f857971dSopenharmony_ci    READINT32(parcel, shadowOffset.offsetY, E_DEVICESTATUS_READ_PARCEL_ERROR);
240f857971dSopenharmony_ci    READINT32(parcel, shadowOffset.width, E_DEVICESTATUS_READ_PARCEL_ERROR);
241f857971dSopenharmony_ci    READINT32(parcel, shadowOffset.height, E_DEVICESTATUS_READ_PARCEL_ERROR);
242f857971dSopenharmony_ci    return RET_OK;
243f857971dSopenharmony_ci}
244f857971dSopenharmony_ci} // namespace DeviceStatus
245f857971dSopenharmony_ci} // namespace Msdp
246f857971dSopenharmony_ci} // namespace OHOS
247