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 "display_device_common.h"
17 #undef HDF_LOG_TAG
18 #define HDF_LOG_TAG DisplayCommon
19
20 using namespace OHOS;
21 using OHOS::MessageParcel;
22
Convert2PowerStatus(int32_t x)23 DispPowerStatus Convert2PowerStatus(int32_t x)
24 {
25 return ((x < POWER_STATUS_ON) || (x > POWER_STATUS_BUTT)) ? POWER_STATUS_BUTT : static_cast<DispPowerStatus>(x);
26 }
Convert2CompositionType(int32_t x)27 CompositionType Convert2CompositionType(int32_t x)
28 {
29 return ((x < COMPOSITION_CLIENT) || (x > COMPOSITION_BUTT)) ? COMPOSITION_BUTT : static_cast<CompositionType>(x);
30 }
Convert2TransformType(int32_t x)31 TransformType Convert2TransformType(int32_t x)
32 {
33 return ((x < ROTATE_NONE) || (x > ROTATE_BUTT)) ? ROTATE_BUTT : static_cast<TransformType>(x);
34 }
35
Convert2BlendTypeType(int32_t x)36 BlendType Convert2BlendTypeType(int32_t x)
37 {
38 return ((x < BLEND_NONE) || (x > BLEND_BUTT)) ? BLEND_BUTT : static_cast<BlendType>(x);
39 }
40
DisplayDeviceWriteUint32(MessageParcel * parcel,uint32_t data)41 bool DisplayDeviceWriteUint32(MessageParcel *parcel, uint32_t data)
42 {
43 if (parcel == nullptr) {
44 DISPLAY_LOG("error: MessageParcel is nullptr");
45 return false;
46 }
47 if (!parcel->WriteUint32(sizeof data)) {
48 DISPLAY_LOG("error: write data length into parcel failed");
49 return false;
50 }
51 if (!parcel->WriteUint32(data)) {
52 DISPLAY_LOG("error: write data into parcel failed");
53 return false;
54 }
55
56 return true;
57 }
DisplayDeviceReadUint32(uint32_t * data,OHOS::MessageParcel * parcel)58 bool DisplayDeviceReadUint32(uint32_t *data, OHOS::MessageParcel *parcel)
59 {
60 uint32_t retValueLen = 0;
61 if (sizeof(uint32_t) != (retValueLen = parcel->ReadUint32())) {
62 DISPLAY_LOG("error: wrong data length of proxy received: %{public}u, expected: %{public}zu", retValueLen,
63 sizeof(uint32_t));
64 return false;
65 }
66 *data = parcel->ReadUint32();
67
68 return true;
69 }
70
DisplayDeviceWriteInt32(OHOS::MessageParcel * parcel,int32_t data)71 bool DisplayDeviceWriteInt32(OHOS::MessageParcel *parcel, int32_t data)
72 {
73 if (parcel == nullptr) {
74 DISPLAY_LOG("error: MessageParcel is nullptr");
75 return false;
76 }
77 if (!parcel->WriteUint32(sizeof data)) {
78 DISPLAY_LOG("error: write data length into parcel failed");
79 return false;
80 }
81 if (!parcel->WriteInt32(data)) {
82 DISPLAY_LOG("error: write data into parcel failed");
83 return false;
84 }
85
86 return true;
87 }
DisplayDeviceReadInt32(int32_t * data,OHOS::MessageParcel * parcel)88 bool DisplayDeviceReadInt32(int32_t *data, OHOS::MessageParcel *parcel)
89 {
90 uint32_t retValueLen = 0;
91 if (sizeof(int32_t) != (retValueLen = parcel->ReadUint32())) {
92 DISPLAY_LOG("error: wrong data length of proxy received: %{public}u, expected: %{public}zu", retValueLen,
93 sizeof(int32_t));
94 return false;
95 }
96 *data = parcel->ReadInt32();
97 return true;
98 }
99
DisplayDeviceWriteBool(OHOS::MessageParcel * parcel,bool data)100 bool DisplayDeviceWriteBool(OHOS::MessageParcel *parcel, bool data)
101 {
102 if (parcel == nullptr) {
103 DISPLAY_LOG("error: MessageParcel is nullptr");
104 return false;
105 }
106 if (!parcel->WriteUint32(sizeof data)) {
107 DISPLAY_LOG("error: write data length into parcel failed");
108 return false;
109 }
110 if (!parcel->WriteInt32(data)) {
111 DISPLAY_LOG("error: write data into parcel failed");
112 return false;
113 }
114 return true;
115 }
DisplayDeviceReadBool(bool * data,OHOS::MessageParcel * parcel)116 bool DisplayDeviceReadBool(bool *data, OHOS::MessageParcel *parcel)
117 {
118 uint32_t retValueLen = 0;
119 if (sizeof(bool) != (retValueLen = parcel->ReadUint32())) {
120 DISPLAY_LOG(
121 "error: wrong data length of proxy received: %{public}u, expected: %{public}zu", retValueLen, sizeof(bool));
122 return false;
123 }
124 *data = parcel->ReadBool();
125
126 return true;
127 }
128
DisplayDeviceWriteUint64(OHOS::MessageParcel * parcel,uint64_t data)129 bool DisplayDeviceWriteUint64(OHOS::MessageParcel *parcel, uint64_t data)
130 {
131 if (parcel == nullptr) {
132 DISPLAY_LOG("error: MessageParcel is nullptr");
133 return false;
134 }
135 if (!parcel->WriteUint32(sizeof data)) {
136 DISPLAY_LOG("error: write data length into parcel failed");
137 return false;
138 }
139 if (!parcel->WriteUint64(data)) {
140 DISPLAY_LOG("error: write data into parcel failed");
141 return false;
142 }
143
144 return true;
145 }
146
DisplayDeviceReadUint64(uint64_t * data,OHOS::MessageParcel * parcel)147 bool DisplayDeviceReadUint64(uint64_t *data, OHOS::MessageParcel *parcel)
148 {
149 uint32_t retValueLen = 0;
150 if (sizeof(uint64_t) != (retValueLen = parcel->ReadUint32())) {
151 DISPLAY_LOG("error: wrong data length of proxy received: %{public}u, expected: %{public}zu", retValueLen,
152 sizeof(uint64_t));
153 return false;
154 }
155 *data = parcel->ReadUint64();
156
157 return true;
158 }
159
DisplayDeviceWritePtr(OHOS::MessageParcel * parcel,uintptr_t data)160 bool DisplayDeviceWritePtr(OHOS::MessageParcel *parcel, uintptr_t data)
161 {
162 if (parcel == nullptr) {
163 DISPLAY_LOG("error: MessageParcel is nullptr");
164 return false;
165 }
166 if (!parcel->WriteUint32(sizeof data)) {
167 DISPLAY_LOG("error: write data length into parcel failed");
168 return false;
169 }
170 if (!parcel->WritePointer(data)) {
171 DISPLAY_LOG("error: write data into parcel failed");
172 return false;
173 }
174
175 return true;
176 }
DisplayDeviceReadPtr(uintptr_t * & data,OHOS::MessageParcel * parcel)177 bool DisplayDeviceReadPtr(uintptr_t *&data, OHOS::MessageParcel *parcel)
178 {
179 uint32_t retValueLen = 0;
180 if (sizeof(uintptr_t) != (retValueLen = parcel->ReadUint32())) {
181 DISPLAY_LOG("error: wrong data length of proxy received: %{public}u, expected: %{public}zu", retValueLen,
182 sizeof(uintptr_t));
183 return false;
184 }
185 *data = parcel->ReadPointer();
186
187 return true;
188 }
189
DisplayDeviceWriteCmdId(MessageParcel * parcel,DisplayDeviceCommandId cmdId)190 bool DisplayDeviceWriteCmdId(MessageParcel *parcel, DisplayDeviceCommandId cmdId)
191 {
192 if (parcel == nullptr) {
193 DISPLAY_LOG("error: MessageParcel is nullptr");
194 return false;
195 }
196 if (!parcel->WriteUint32(cmdId)) {
197 DISPLAY_LOG("error: write cmdId into parcel failed");
198 return false;
199 }
200
201 return true;
202 }
203
DisplayDeviceReadCmdId(MessageParcel * parcel)204 DisplayDeviceCommandId DisplayDeviceReadCmdId(MessageParcel *parcel)
205 {
206 uint32_t enumTmp = parcel->ReadUint32();
207 enumTmp &= ~BATCH_CMD_FLAG;
208
209 return ((enumTmp > DSP_CMD_EXECUTECMD && enumTmp < DSP_CMD_RESERVED_1001)
210 || (enumTmp > DSP_CMD_SET_PROXY_REMOTE_CALLBACK && enumTmp < DSP_CMD_RESERVED_2001)
211 || (enumTmp > DSP_CMD_SETLAYERBLENDTYPE))
212 ? DSP_CMD_INVALID
213 : static_cast<DisplayDeviceCommandId>(enumTmp);
214 }
215
DisplayDeviceWriteBufHdl(MessageParcel * parcel,const BufferHandle * data)216 bool DisplayDeviceWriteBufHdl(MessageParcel *parcel, const BufferHandle *data)
217 {
218 if (parcel == nullptr) {
219 DISPLAY_LOG("error: MessageParcel iss nullptr");
220 return false;
221 }
222 if (!WriteBufferHandle(*parcel, *data)) {
223 DISPLAY_LOG("error: write data into parcel failed");
224 return false;
225 }
226
227 return true;
228 }
DisplayDeviceReadBufHdl(BufferHandle * & data,OHOS::MessageParcel * parcel)229 bool DisplayDeviceReadBufHdl(BufferHandle *&data, OHOS::MessageParcel *parcel)
230 {
231 data = ReadBufferHandle(*parcel);
232 if (data == nullptr) {
233 DISPLAY_LOG("read reply data failed");
234 return false;
235 }
236
237 return true;
238 }
239
DisplayDeviceWriteFileDescriptor(MessageParcel * parcel,const int32_t fd)240 bool DisplayDeviceWriteFileDescriptor(MessageParcel *parcel, const int32_t fd)
241 {
242 bool validFd = (fd >= 0);
243 if (!parcel->WriteBool(validFd)) {
244 DISPLAY_LOG("error: %{public}s parcel->WriteBool failed", __func__);
245 return false;
246 }
247 if (validFd && !parcel->WriteFileDescriptor(fd)) {
248 DISPLAY_LOG("error: %{public}s parcel->WriteFileDescriptor fd failed", __func__);
249 return false;
250 } else if (!validFd) {
251 DISPLAY_LOG("%{public}s Write invalid FileDescriptor", __func__);
252 return true;
253 }
254 return true;
255 }
256
DisplayDeviceReadFileDescriptor(int32_t * fd,MessageParcel * parcel)257 bool DisplayDeviceReadFileDescriptor(int32_t *fd, MessageParcel *parcel)
258 {
259 bool validFd = false;
260 if (!parcel->ReadBool(validFd)) {
261 DISPLAY_LOG("error: %{public}s ReadBool validFd failed", __func__);
262 return false;
263 }
264 if (validFd) {
265 *fd = parcel->ReadFileDescriptor();
266 if (*fd == -1) {
267 DISPLAY_LOG("error: %{public}s ReadFileDescriptor fd failed", __func__);
268 return false;
269 }
270 } else {
271 *fd = -1;
272 DISPLAY_LOG("%{public}s Read invalid FileDescriptor", __func__);
273 }
274 return true;
275 }
276
DisplayDeviceWriteFileDescriptorArray(MessageParcel * parcel,const int * fd,uint32_t num)277 bool DisplayDeviceWriteFileDescriptorArray(MessageParcel *parcel, const int *fd, uint32_t num)
278 {
279 for (uint32_t i = 0; i < num; i++) {
280 DISPLAY_LOG("%{public}s(%{public}d), fence fd:%{public}d", __func__, __LINE__, fd[i]);
281 if (!DisplayDeviceWriteFileDescriptor(parcel, fd[i])) {
282 DISPLAY_LOG(
283 "error: %{public}s parcel->WriteFileDescriptor fence failed, fence fd:%{public}d", __func__, fd[i]);
284 return false;
285 }
286 }
287 return true;
288 }
289
DisplayDeviceReadFileDescriptorArray(int * fd,MessageParcel * parcel,uint32_t num)290 bool DisplayDeviceReadFileDescriptorArray(int *fd, MessageParcel *parcel, uint32_t num)
291 {
292 for (uint32_t i = 0; i < num; i++) {
293 if (!DisplayDeviceReadFileDescriptor(&fd[i], parcel)) {
294 DISPLAY_LOG("error: %{public}s parcel->ReadFileDescriptor fence failed", __func__);
295 return false;
296 }
297 }
298 return true;
299 }
300