1 /*
2 * Copyright (c) 2021 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 #include "proxy_msgproc.h"
16 #include <hdf_log.h>
17 #include <osal_mem.h>
18 #include <securec.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif /* __cplusplus */
CodecProxyParseAlignment(struct HdfSBuf * reply,Alginment * alginment)23 int32_t CodecProxyParseAlignment(struct HdfSBuf *reply, Alginment *alginment)
24 {
25 if (reply == NULL || alginment == NULL) {
26 HDF_LOGE("%{public}s: params NULL!", __func__);
27 return HDF_ERR_INVALID_PARAM;
28 }
29 if (!HdfSbufReadInt32(reply, &alginment->widthAlginment)) {
30 HDF_LOGE("%{public}s: read widthAlginment failed!", __func__);
31 return HDF_FAILURE;
32 }
33 if (!HdfSbufReadInt32(reply, &alginment->heightAlginment)) {
34 HDF_LOGE("%{public}s: read heightAlginment failed!", __func__);
35 return HDF_FAILURE;
36 }
37 return HDF_SUCCESS;
38 }
39
CodecProxyParseRect(struct HdfSBuf * reply,Rect * rectangle)40 int32_t CodecProxyParseRect(struct HdfSBuf *reply, Rect *rectangle)
41 {
42 if (reply == NULL || rectangle == NULL) {
43 HDF_LOGE("%{public}s: params NULL!", __func__);
44 return HDF_ERR_INVALID_PARAM;
45 }
46 if (!HdfSbufReadInt32(reply, &rectangle->width)) {
47 HDF_LOGE("%{public}s: read width failed!", __func__);
48 return HDF_FAILURE;
49 }
50 if (!HdfSbufReadInt32(reply, &rectangle->height)) {
51 HDF_LOGE("%{public}s: read height failed!", __func__);
52 return HDF_FAILURE;
53 }
54 return HDF_SUCCESS;
55 }
56
CodecProxyParseArray(struct HdfSBuf * reply,ResizableArray * resArr)57 int32_t CodecProxyParseArray(struct HdfSBuf *reply, ResizableArray *resArr)
58 {
59 if (reply == NULL || resArr == NULL) {
60 HDF_LOGE("%{public}s: params NULL!", __func__);
61 return HDF_ERR_INVALID_PARAM;
62 }
63
64 resArr->actualLen = 0;
65 if (!HdfSbufReadUint32(reply, &resArr->actualLen)) {
66 HDF_LOGE("%{public}s: read actualLen failed!", __func__);
67 return HDF_FAILURE;
68 }
69 for (uint32_t i = 0; i < resArr->actualLen; i++) {
70 if (!HdfSbufReadUint32(reply, &resArr->element[i])) {
71 HDF_LOGE("%{public}s: read element failed!", __func__);
72 return HDF_FAILURE;
73 }
74 }
75 return HDF_SUCCESS;
76 }
77
CodecProxyParseGottenCapbility(struct HdfSBuf * reply,CodecCapbility * cap)78 int32_t CodecProxyParseGottenCapbility(struct HdfSBuf *reply, CodecCapbility *cap)
79 {
80 if (reply == NULL || cap == NULL) {
81 return HDF_ERR_INVALID_PARAM;
82 }
83 if (!HdfSbufReadUint32(reply, (uint32_t*)&cap->mime)) {
84 return HDF_FAILURE;
85 }
86 if (!HdfSbufReadUint32(reply, (uint32_t*)&cap->type)) {
87 return HDF_FAILURE;
88 }
89 if (CodecProxyParseAlignment(reply, &cap->whAlignment) != HDF_SUCCESS) {
90 return HDF_FAILURE;
91 }
92 if (CodecProxyParseRect(reply, &cap->minSize) != HDF_SUCCESS) {
93 return HDF_FAILURE;
94 }
95 if (CodecProxyParseRect(reply, &cap->maxSize) != HDF_SUCCESS) {
96 return HDF_FAILURE;
97 }
98 if (!HdfSbufReadUint64(reply, &cap->minBitRate)) {
99 return HDF_FAILURE;
100 }
101 if (!HdfSbufReadUint64(reply, &cap->maxBitRate)) {
102 return HDF_FAILURE;
103 }
104 if (CodecProxyParseArray(reply, &cap->supportProfiles) != HDF_SUCCESS) {
105 return HDF_FAILURE;
106 }
107 if (CodecProxyParseArray(reply, &cap->supportLevels) != HDF_SUCCESS) {
108 return HDF_FAILURE;
109 }
110 if (CodecProxyParseArray(reply, &cap->supportPixelFormats) != HDF_SUCCESS) {
111 return HDF_FAILURE;
112 }
113 if (!HdfSbufReadUint32(reply, &cap->minInputBufferNum)) {
114 return HDF_FAILURE;
115 }
116 if (!HdfSbufReadUint32(reply, &cap->minOutputBufferNum)) {
117 return HDF_FAILURE;
118 }
119 if (!HdfSbufReadUint32(reply, &cap->allocateMask)) {
120 return HDF_FAILURE;
121 }
122 if (!HdfSbufReadUint32(reply, &cap->capsMask)) {
123 return HDF_FAILURE;
124 }
125
126 return HDF_SUCCESS;
127 }
128
129 // todo val 类型,决定read类型
CodecProxyPackParam(struct HdfSBuf * data,const Param * param)130 int32_t CodecProxyPackParam(struct HdfSBuf *data, const Param *param)
131 {
132 if (data == NULL || param == NULL) {
133 HDF_LOGE("%{public}s: params NULL!", __func__);
134 return HDF_ERR_INVALID_PARAM;
135 }
136 if (!HdfSbufWriteUint32(data, (uint32_t)param->key)) {
137 HDF_LOGE("%{public}s: Write key failed!", __func__);
138 return HDF_FAILURE;
139 }
140 if (!HdfSbufWriteUint32(data, (uint32_t)¶m->val)) {
141 HDF_LOGE("%{public}s: Write val failed!", __func__);
142 return HDF_FAILURE;
143 }
144 if (!HdfSbufWriteInt32(data, param->size)) {
145 HDF_LOGE("%{public}s: Write size failed!", __func__);
146 return HDF_FAILURE;
147 }
148 return HDF_SUCCESS;
149 }
150 // todo val 类型,决定read类型
CodecProxyParseParam(struct HdfSBuf * reply,Param * param)151 int32_t CodecProxyParseParam(struct HdfSBuf *reply, Param *param)
152 {
153 uint32_t tempKey = 0;
154 if (reply == NULL || param == NULL) {
155 HDF_LOGE("%{public}s: params NULL!", __func__);
156 return HDF_ERR_INVALID_PARAM;
157 }
158 if (!HdfSbufReadUint32(reply, &tempKey)) {
159 HDF_LOGE("%{public}s: read tempKey failed!", __func__);
160 return HDF_FAILURE;
161 }
162 param->key = (ParamKey)tempKey;
163 if (!HdfSbufReadUint32(reply, (uint32_t *)¶m->val)) {
164 HDF_LOGE("%{public}s: Read val failed!", __func__);
165 return HDF_FAILURE;
166 }
167 if (!HdfSbufReadInt32(reply, (int32_t*)¶m->size)) {
168 HDF_LOGE("%{public}s: read size failed!", __func__);
169 return HDF_FAILURE;
170 }
171 return HDF_SUCCESS;
172 }
173
CodecProxyPackBufferHandle(struct HdfSBuf * data,CodecBufferHandle * bufferHandle)174 int32_t CodecProxyPackBufferHandle(struct HdfSBuf *data, CodecBufferHandle *bufferHandle)
175 {
176 if (data == NULL || bufferHandle == NULL) {
177 HDF_LOGE("%{public}s: params NULL!", __func__);
178 return HDF_ERR_INVALID_PARAM;
179 }
180 if (!HdfSbufWriteUint8(data, (uint8_t)&bufferHandle->virAddr)) {
181 HDF_LOGE("%{public}s: Write virAddr failed!", __func__);
182 return HDF_FAILURE;
183 }
184 if (!HdfSbufWriteUint32(data, (uint32_t)bufferHandle->handle)) {
185 HDF_LOGE("%{public}s: Write handle failed!", __func__);
186 return HDF_FAILURE;
187 }
188 return HDF_SUCCESS;
189 }
190
CodecProxyPackBufferInfo(struct HdfSBuf * data,CodecBufferInfo * buffers)191 int32_t CodecProxyPackBufferInfo(struct HdfSBuf *data, CodecBufferInfo *buffers)
192 {
193 if (data == NULL || buffers == NULL) {
194 HDF_LOGE("%{public}s: params NULL!", __func__);
195 return HDF_ERR_INVALID_PARAM;
196 }
197 if (!HdfSbufWriteUint32(data, (uint32_t)buffers->type)) {
198 HDF_LOGE("%{public}s: Write tempType failed!", __func__);
199 return HDF_FAILURE;
200 }
201 if (buffers->type == BUFFER_TYPE_VIRTUAL) {
202 if (!HdfSbufWriteUint8(data, (uint8_t)&buffers->addr)) {
203 HDF_LOGE("%{public}s: Write addr failed!", __func__);
204 return HDF_FAILURE;
205 }
206 } else if (buffers->type == BUFFER_TYPE_FD) {
207 if (!HdfSbufWriteInt32(data, buffers->fd)) {
208 HDF_LOGE("%{public}s: Write fd failed!", __func__);
209 return HDF_FAILURE;
210 }
211 } else if (buffers->type == BUFFER_TYPE_HANDLE) {
212 if (CodecProxyPackBufferHandle(data, &buffers->handle)) {
213 HDF_LOGE("%{public}s: Write handle failed!", __func__);
214 return HDF_FAILURE;
215 }
216 } else {
217 HDF_LOGE("%{public}s: buffers->type is err!", __func__);
218 return HDF_FAILURE;
219 }
220 if (!HdfSbufWriteUint32(data, buffers->offset)) {
221 HDF_LOGE("%{public}s: Write offset failed!", __func__);
222 return HDF_FAILURE;
223 }
224 if (!HdfSbufWriteUint32(data, buffers->length)) {
225 HDF_LOGE("%{public}s: Write length failed!", __func__);
226 return HDF_FAILURE;
227 }
228 if (!HdfSbufWriteUint32(data, buffers->size)) {
229 HDF_LOGE("%{public}s: Write size failed!", __func__);
230 return HDF_FAILURE;
231 }
232 return HDF_SUCCESS;
233 }
234
CodecProxyPackInputInfo(struct HdfSBuf * data,const InputInfo * inputData)235 int32_t CodecProxyPackInputInfo(struct HdfSBuf *data, const InputInfo *inputData)
236 {
237 if (data == NULL || inputData == NULL || inputData->buffers == NULL) {
238 HDF_LOGE("%{public}s: params NULL!", __func__);
239 return HDF_ERR_INVALID_PARAM;
240 }
241 if (!HdfSbufWriteUint32(data, inputData->bufferCnt)) {
242 HDF_LOGE("%{public}s: Write bufferCnt failed!", __func__);
243 return HDF_FAILURE;
244 }
245 for (uint32_t i = 0; i < inputData->bufferCnt; i++) {
246 if (CodecProxyPackBufferInfo(data, &inputData->buffers[i])) {
247 HDF_LOGE("%{public}s: Write buffers failed!", __func__);
248 return HDF_FAILURE;
249 }
250 }
251 if (!HdfSbufWriteInt64(data, inputData->pts)) {
252 HDF_LOGE("%{public}s: Write pts failed!", __func__);
253 return HDF_FAILURE;
254 }
255 if (!HdfSbufWriteInt32(data, inputData->flag)) {
256 HDF_LOGE("%{public}s: Write flag failed!", __func__);
257 return HDF_FAILURE;
258 }
259 return HDF_SUCCESS;
260 }
261
CodecProxyParseBufferHandle(struct HdfSBuf * reply,CodecBufferHandle * bufferHandle)262 int32_t CodecProxyParseBufferHandle(struct HdfSBuf *reply, CodecBufferHandle *bufferHandle)
263 {
264 if (reply == NULL || bufferHandle == NULL) {
265 HDF_LOGE("%{public}s: params NULL!", __func__);
266 return HDF_ERR_INVALID_PARAM;
267 }
268 if (!HdfSbufReadUint8(reply, bufferHandle->virAddr)) {
269 HDF_LOGE("%{public}s: read virAddr failed!", __func__);
270 return HDF_FAILURE;
271 }
272 if (!HdfSbufReadUint32(reply, (uint32_t *)&bufferHandle->handle)) {
273 HDF_LOGE("%{public}s: read handle failed!", __func__);
274 return HDF_FAILURE;
275 }
276 return HDF_SUCCESS;
277 }
278
CodecProxyParseBufferInfo(struct HdfSBuf * reply,CodecBufferInfo * buffers)279 int32_t CodecProxyParseBufferInfo(struct HdfSBuf *reply, CodecBufferInfo *buffers)
280 {
281 uint32_t tempType = 0;
282 if (reply == NULL || buffers == NULL) {
283 HDF_LOGE("%{public}s: buffers null!", __func__);
284 return HDF_ERR_INVALID_PARAM;
285 }
286 if (!HdfSbufReadUint32(reply, &tempType)) {
287 HDF_LOGE("%{public}s: read type failed!", __func__);
288 return HDF_FAILURE;
289 }
290 buffers->type = (BufferType)tempType;
291 if (buffers->type == BUFFER_TYPE_VIRTUAL) {
292 if (!HdfSbufReadUint8(reply, buffers->addr)) {
293 HDF_LOGE("%{public}s: read addr failed!", __func__);
294 return HDF_FAILURE;
295 }
296 } else if (buffers->type == BUFFER_TYPE_FD) {
297 if (!HdfSbufReadInt32(reply, &buffers->fd)) {
298 HDF_LOGE("%{public}s: read fd failed!", __func__);
299 return HDF_FAILURE;
300 }
301 } else if (buffers->type == BUFFER_TYPE_HANDLE) {
302 if (CodecProxyParseBufferHandle(reply, &buffers->handle)) {
303 HDF_LOGE("%{public}s: read handle failed!", __func__);
304 return HDF_FAILURE;
305 }
306 }
307 if (!HdfSbufReadUint32(reply, &buffers->offset)) {
308 HDF_LOGE("%{public}s: read offset failed!", __func__);
309 return HDF_FAILURE;
310 }
311 if (!HdfSbufReadUint32(reply, &buffers->length)) {
312 HDF_LOGE("%{public}s: read length failed!", __func__);
313 return HDF_FAILURE;
314 }
315 if (!HdfSbufReadUint32(reply, &buffers->size)) {
316 HDF_LOGE("%{public}s: read size failed!", __func__);
317 return HDF_FAILURE;
318 }
319 return HDF_SUCCESS;
320 }
321
CodecProxyParseInputInfo(struct HdfSBuf * reply,InputInfo * inputData)322 int32_t CodecProxyParseInputInfo(struct HdfSBuf *reply, InputInfo *inputData)
323 {
324 if (reply == NULL || inputData == NULL || inputData->buffers == NULL) {
325 HDF_LOGE("%{public}s: params error!", __func__);
326 return HDF_ERR_INVALID_PARAM;
327 }
328 for (uint32_t i = 0; i < inputData->bufferCnt; i++) {
329 if (CodecProxyParseBufferInfo(reply, &inputData->buffers[i])) {
330 HDF_LOGE("%{public}s: read buffers failed!", __func__);
331 return HDF_FAILURE;
332 }
333 }
334 if (!HdfSbufReadInt64(reply, &inputData->pts)) {
335 HDF_LOGE("%{public}s: read pts failed!", __func__);
336 return HDF_FAILURE;
337 }
338 if (!HdfSbufReadInt32(reply, &inputData->flag)) {
339 HDF_LOGE("%{public}s: read flag failed!", __func__);
340 return HDF_FAILURE;
341 }
342 return HDF_SUCCESS;
343 }
344
CodecProxyParseOutputInfo(struct HdfSBuf * reply,OutputInfo * outInfo)345 int32_t CodecProxyParseOutputInfo(struct HdfSBuf *reply, OutputInfo *outInfo)
346 {
347 if (reply == NULL || outInfo == NULL || outInfo->buffers == NULL) {
348 HDF_LOGE("%{public}s: params error!", __func__);
349 return HDF_ERR_INVALID_PARAM;
350 }
351 for (uint32_t i = 0; i < outInfo->bufferCnt; i++) {
352 if (CodecProxyParseBufferInfo(reply, &outInfo->buffers[i])) {
353 HDF_LOGE("%{public}s: read buffers failed!", __func__);
354 return HDF_FAILURE;
355 }
356 }
357 if (!HdfSbufReadInt64(reply, &outInfo->timeStamp)) {
358 HDF_LOGE("%{public}s: read timeStamp failed!", __func__);
359 return HDF_FAILURE;
360 }
361 if (!HdfSbufReadUint32(reply, &outInfo->sequence)) {
362 HDF_LOGE("%{public}s: read sequence failed!", __func__);
363 return HDF_FAILURE;
364 }
365 if (!HdfSbufReadUint32(reply, &outInfo->flag)) {
366 HDF_LOGE("%{public}s: read flag failed!", __func__);
367 return HDF_FAILURE;
368 }
369 uint32_t tempType = 0;
370 if (!HdfSbufReadUint32(reply, &tempType)) {
371 HDF_LOGE("%{public}s: read tempType failed!", __func__);
372 return HDF_FAILURE;
373 }
374 outInfo->type = (CodecType)tempType;
375 if (!HdfSbufReadUint64(reply, (uint64_t *)&outInfo->vendorPrivate)) {
376 HDF_LOGE("%{public}s: read vendorPrivate failed!", __func__);
377 return HDF_FAILURE;
378 }
379 return HDF_SUCCESS;
380 }
381
CodecProxyPackOutputInfo(struct HdfSBuf * data,OutputInfo * outInfo)382 int32_t CodecProxyPackOutputInfo(struct HdfSBuf *data, OutputInfo *outInfo)
383 {
384 if (data == NULL || outInfo == NULL || outInfo->buffers == NULL) {
385 HDF_LOGE("%{public}s: params error!", __func__);
386 return HDF_ERR_INVALID_PARAM;
387 }
388 if (!HdfSbufWriteUint32(data, outInfo->bufferCnt)) {
389 HDF_LOGE("%{public}s: write bufferCnt failed!", __func__);
390 return HDF_FAILURE;
391 }
392 for (uint32_t i = 0; i < outInfo->bufferCnt; i++) {
393 if (CodecProxyPackBufferInfo(data, &outInfo->buffers[i])) {
394 HDF_LOGE("%{public}s: write buffers failed!", __func__);
395 return HDF_FAILURE;
396 }
397 }
398 if (!HdfSbufWriteInt64(data, outInfo->timeStamp)) {
399 HDF_LOGE("%{public}s: write timeStamp failed!", __func__);
400 return HDF_FAILURE;
401 }
402 if (!HdfSbufWriteUint32(data, outInfo->sequence)) {
403 HDF_LOGE("%{public}s: write sequence failed!", __func__);
404 return HDF_FAILURE;
405 }
406 if (!HdfSbufWriteUint32(data, outInfo->flag)) {
407 HDF_LOGE("%{public}s: write flag failed!", __func__);
408 return HDF_FAILURE;
409 }
410 if (!HdfSbufWriteUint32(data, outInfo->type)) {
411 HDF_LOGE("%{public}s: write outInfo->type failed!", __func__);
412 return HDF_FAILURE;
413 }
414 if (!HdfSbufWriteUint64(data, (uint64_t)&outInfo->vendorPrivate)) {
415 HDF_LOGE("%{public}s: write vendorPrivate failed!", __func__);
416 return HDF_FAILURE;
417 }
418 return HDF_SUCCESS;
419 }
420
421 #ifdef __cplusplus
422 }
423 #endif /* __cplusplus */