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 #include "common_msgproc.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24
25 #define HDF_LOG_TAG codec_hdi_proxy
26
CodecCapabilityBaseUnmarshalling(struct HdfSBuf * reply,CodecCapability * cap)27 static bool CodecCapabilityBaseUnmarshalling(struct HdfSBuf *reply, CodecCapability *cap)
28 {
29 if (!HdfSbufReadUint32(reply, (uint32_t*)&cap->mime)) {
30 HDF_LOGE("%{public}s: read cap->mime failed!", __func__);
31 return false;
32 }
33 if (!HdfSbufReadUint32(reply, (uint32_t*)&cap->type)) {
34 HDF_LOGE("%{public}s: read cap->type failed!", __func__);
35 return false;
36 }
37 for (uint32_t i = 0; i < NAME_LENGTH; i++) {
38 if (!HdfSbufReadUint8(reply, (uint8_t *)&(cap->name)[i])) {
39 HDF_LOGE("%{public}s: read name[i] failed!", __func__);
40 return false;
41 }
42 }
43 for (uint32_t j = 0; j < PROFILE_NUM; j++) {
44 if (!HdfSbufReadInt32(reply, &(cap->supportProfiles)[j])) {
45 HDF_LOGE("%{public}s: read supportProfiles[i] failed!", __func__);
46 return false;
47 }
48 }
49 if (!HdfSbufReadInt8(reply, (int8_t *)&cap->isSoftwareCodec)) {
50 HDF_LOGE("%{public}s: read cap->isSoftwareCodec failed!", __func__);
51 return false;
52 }
53 if (!HdfSbufReadInt32(reply, &cap->processModeMask)) {
54 HDF_LOGE("%{public}s: read cap->processModeMask failed!", __func__);
55 return false;
56 }
57 if (!HdfSbufReadUint32(reply, &cap->capsMask)) {
58 HDF_LOGE("%{public}s: read cap->capsMask failed!", __func__);
59 return false;
60 }
61 if (!HdfSbufReadUint32(reply, &cap->allocateMask)) {
62 HDF_LOGE("%{public}s: read cap->allocateMask failed!", __func__);
63 return false;
64 }
65 return true;
66 }
67
CodecCapabilityRangeValueUnmarshalling(struct HdfSBuf * reply,RangeValue * dataBlock)68 static bool CodecCapabilityRangeValueUnmarshalling(struct HdfSBuf *reply, RangeValue *dataBlock)
69 {
70 if (dataBlock == NULL) {
71 return false;
72 }
73 if (!HdfSbufReadInt32(reply, &dataBlock->min)) {
74 HDF_LOGE("%{public}s: read dataBlock->min failed!", __func__);
75 return false;
76 }
77 if (!HdfSbufReadInt32(reply, &dataBlock->max)) {
78 HDF_LOGE("%{public}s: read dataBlock->max failed!", __func__);
79 return false;
80 }
81 return true;
82 }
83
CodecCapabilityPortUnmarshalling(struct HdfSBuf * reply,CodecCapability * cap)84 static bool CodecCapabilityPortUnmarshalling(struct HdfSBuf *reply, CodecCapability *cap)
85 {
86 int32_t ret;
87 if (cap->type < AUDIO_DECODER) {
88 const VideoPortCap *video = (const VideoPortCap *)HdfSbufReadUnpadBuffer(reply, sizeof(VideoPortCap));
89 if (video == NULL) {
90 HDF_LOGE("%{public}s: read video failed!", __func__);
91 return false;
92 }
93 ret = memcpy_s(&cap->port, sizeof(VideoPortCap), video, sizeof(VideoPortCap));
94 if (ret != EOK) {
95 HDF_LOGE("%{public}s: memcpy_s video failed, error code: %{public}d", __func__, ret);
96 return false;
97 }
98 } else if (cap->type < INVALID_TYPE) {
99 const AudioPortCap *audio = (const AudioPortCap *)HdfSbufReadUnpadBuffer(reply, sizeof(AudioPortCap));
100 if (audio == NULL) {
101 HDF_LOGE("%{public}s: read audio failed!", __func__);
102 return false;
103 }
104 ret = memcpy_s(&cap->port, sizeof(AudioPortCap), audio, sizeof(AudioPortCap));
105 if (ret != EOK) {
106 HDF_LOGE("%{public}s: memcpy_s audio failed, error code: %{public}d", __func__, ret);
107 return false;
108 }
109 } else {
110 ret = memset_s(&cap->port, sizeof(cap->port), 0, sizeof(cap->port));
111 if (ret != EOK) {
112 HDF_LOGE("%{public}s: memset_s cap->port failed, error code: %{public}d", __func__, ret);
113 return false;
114 }
115 }
116 return true;
117 }
118
CodecProxyParseGottenCapability(struct HdfSBuf * reply,CodecCapability * cap)119 int32_t CodecProxyParseGottenCapability(struct HdfSBuf *reply, CodecCapability *cap)
120 {
121 if (reply == NULL || cap == NULL) {
122 return HDF_ERR_INVALID_PARAM;
123 }
124 if (!CodecCapabilityBaseUnmarshalling(reply, cap)) {
125 return HDF_FAILURE;
126 }
127 if (!CodecCapabilityRangeValueUnmarshalling(reply, &cap->inputBufferNum)) {
128 HDF_LOGE("%{public}s: read &cap->inputBufferNum failed!", __func__);
129 return HDF_FAILURE;
130 }
131 if (!CodecCapabilityRangeValueUnmarshalling(reply, &cap->outputBufferNum)) {
132 HDF_LOGE("%{public}s: read &cap->outputBufferNum failed!", __func__);
133 return HDF_FAILURE;
134 }
135 if (!CodecCapabilityRangeValueUnmarshalling(reply, &cap->bitRate)) {
136 HDF_LOGE("%{public}s: read &cap->bitRate failed!", __func__);
137 return HDF_FAILURE;
138 }
139 if (!HdfSbufReadInt32(reply, &cap->inputBufferSize)) {
140 HDF_LOGE("%{public}s: read cap->inputBufferSize failed!", __func__);
141 return HDF_FAILURE;
142 }
143 if (!HdfSbufReadInt32(reply, &cap->outputBufferSize)) {
144 HDF_LOGE("%{public}s: read cap->outputBufferSize failed!", __func__);
145 return HDF_FAILURE;
146 }
147 if (!CodecCapabilityPortUnmarshalling(reply, cap)) {
148 HDF_LOGE("%{public}s: read cap->port failed!", __func__);
149 return HDF_FAILURE;
150 }
151
152 return HDF_SUCCESS;
153 }
154
CodecProxyPackParam(struct HdfSBuf * data,const Param * param)155 int32_t CodecProxyPackParam(struct HdfSBuf *data, const Param *param)
156 {
157 if (data == NULL || param == NULL) {
158 HDF_LOGE("%{public}s: params NULL!", __func__);
159 return HDF_ERR_INVALID_PARAM;
160 }
161
162 if (!HdfSbufWriteInt32(data, (int32_t)param->key)) {
163 HDF_LOGE("%{public}s: write param->key failed!", __func__);
164 return false;
165 }
166
167 if (!HdfSbufWriteInt32(data, param->size)) {
168 HDF_LOGE("%{public}s: write param->size failed!", __func__);
169 return false;
170 }
171 for (int32_t i = 0; i < param->size; i++) {
172 if (!HdfSbufWriteInt8(data, ((int8_t*)(param->val))[i])) {
173 HDF_LOGE("%{public}s: write (param->val)[i] failed!", __func__);
174 return false;
175 }
176 }
177
178 return HDF_SUCCESS;
179 }
180
CodecProxyParseParam(struct HdfSBuf * reply,Param * param)181 int32_t CodecProxyParseParam(struct HdfSBuf *reply, Param *param)
182 {
183 if (reply == NULL || param == NULL) {
184 HDF_LOGE("%{public}s: params NULL!", __func__);
185 return HDF_ERR_INVALID_PARAM;
186 }
187
188 if (!HdfSbufReadInt32(reply, (int32_t*)¶m->key)) {
189 HDF_LOGE("%{public}s: read param->key failed!", __func__);
190 return HDF_FAILURE;
191 }
192
193 int8_t* valCp = NULL;
194 int32_t valCpLen = 0;
195 if (!HdfSbufReadInt32(reply, &valCpLen)) {
196 HDF_LOGE("%{public}s: read size failed!", __func__);
197 return HDF_FAILURE;
198 }
199 if (valCpLen > 0) {
200 valCp = (int8_t*)OsalMemCalloc(sizeof(int8_t) * valCpLen);
201 if (valCp == NULL) {
202 HDF_LOGE("%{public}s: alloc mem failed!", __func__);
203 return HDF_FAILURE;
204 }
205 for (int32_t i = 0; i < valCpLen; i++) {
206 if (!HdfSbufReadInt8(reply, &valCp[i])) {
207 HDF_LOGE("%{public}s: read valCp[i] failed!", __func__);
208 OsalMemFree(valCp);
209 return HDF_FAILURE;
210 }
211 }
212 }
213 param->val = (void*)valCp;
214 param->size = valCpLen;
215
216 return HDF_SUCCESS;
217 }
218
CodecProxyPackBufferInfo(struct HdfSBuf * data,const CodecBufferInfo * buffer)219 static int32_t CodecProxyPackBufferInfo(struct HdfSBuf *data, const CodecBufferInfo *buffer)
220 {
221 if (data == NULL || buffer == NULL) {
222 HDF_LOGE("%{public}s: params NULL!", __func__);
223 return HDF_ERR_INVALID_PARAM;
224 }
225 if (!HdfSbufWriteUint32(data, (uint32_t)buffer->type)) {
226 HDF_LOGE("%{public}s: Write BufferType failed!", __func__);
227 return HDF_FAILURE;
228 }
229 if (buffer->type == BUFFER_TYPE_VIRTUAL) {
230 if (!HdfSbufWriteBuffer(data, (void *)buffer->buf, buffer->length)) {
231 HDF_LOGE("%{public}s: Write addr failed!", __func__);
232 return HDF_FAILURE;
233 }
234 } else if (buffer->type == BUFFER_TYPE_FD) {
235 if (!HdfSbufWriteFileDescriptor(data, (int32_t)buffer->buf)) {
236 HDF_LOGE("%{public}s: Write fd failed!", __func__);
237 return HDF_FAILURE;
238 }
239 } else if (buffer->type == BUFFER_TYPE_HANDLE) {
240 if (!PackBufferHandle(data, (BufferHandle *)buffer->buf)) {
241 return HDF_FAILURE;
242 }
243 } else {
244 HDF_LOGE("%{public}s: buffer->type is err!", __func__);
245 return HDF_FAILURE;
246 }
247 if (!HdfSbufWriteUint32(data, buffer->offset)) {
248 HDF_LOGE("%{public}s: Write offset failed!", __func__);
249 return HDF_FAILURE;
250 }
251 if (!HdfSbufWriteUint32(data, buffer->length)) {
252 HDF_LOGE("%{public}s: Write length failed!", __func__);
253 return HDF_FAILURE;
254 }
255 if (!HdfSbufWriteUint32(data, buffer->capacity)) {
256 HDF_LOGE("%{public}s: Write size failed!", __func__);
257 return HDF_FAILURE;
258 }
259 return HDF_SUCCESS;
260 }
261
CodecProxyParseBufferInfo(struct HdfSBuf * reply,CodecBufferInfo * buffer)262 static int32_t CodecProxyParseBufferInfo(struct HdfSBuf *reply, CodecBufferInfo *buffer)
263 {
264 uint32_t readLen = 0;
265 if (reply == NULL || buffer == NULL) {
266 HDF_LOGE("%{public}s: buffer null!", __func__);
267 return HDF_ERR_INVALID_PARAM;
268 }
269 if (!HdfSbufReadUint32(reply, (uint32_t *)&buffer->type)) {
270 HDF_LOGE("%{public}s: read type failed!", __func__);
271 return HDF_FAILURE;
272 }
273 if (buffer->type == BUFFER_TYPE_VIRTUAL) {
274 void *buf = (void *)buffer->buf;
275 if (!HdfSbufReadBuffer(reply, (const void **)&buf, &readLen)) {
276 HDF_LOGE("%{public}s: read addr failed!", __func__);
277 return HDF_FAILURE;
278 }
279 } else if (buffer->type == BUFFER_TYPE_FD) {
280 buffer->buf = (intptr_t)HdfSbufReadFileDescriptor(reply);
281 if (buffer->buf < 0) {
282 HDF_LOGE("%{public}s: read fd failed!", __func__);
283 return HDF_FAILURE;
284 }
285 } else if (buffer->type == BUFFER_TYPE_HANDLE) {
286 if (!ParseBufferHandle(reply, (BufferHandle **)&buffer->buf)) {
287 return HDF_FAILURE;
288 }
289 }
290 if (!HdfSbufReadUint32(reply, &buffer->offset)) {
291 HDF_LOGE("%{public}s: read offset failed!", __func__);
292 return HDF_FAILURE;
293 }
294 if (!HdfSbufReadUint32(reply, &buffer->length)) {
295 HDF_LOGE("%{public}s: read length failed!", __func__);
296 return HDF_FAILURE;
297 }
298 if (!HdfSbufReadUint32(reply, &buffer->capacity)) {
299 HDF_LOGE("%{public}s: read size failed!", __func__);
300 return HDF_FAILURE;
301 }
302 return HDF_SUCCESS;
303 }
304
CodecProxyParseCodecBuffer(struct HdfSBuf * reply,CodecBuffer * codecBuffer)305 int32_t CodecProxyParseCodecBuffer(struct HdfSBuf *reply, CodecBuffer *codecBuffer)
306 {
307 if (reply == NULL || codecBuffer == NULL) {
308 HDF_LOGE("%{public}s: params error!", __func__);
309 return HDF_ERR_INVALID_PARAM;
310 }
311 if (!HdfSbufReadUint32(reply, &codecBuffer->bufferId)) {
312 HDF_LOGE("%{public}s: read sequence failed!", __func__);
313 return HDF_FAILURE;
314 }
315 if (!HdfSbufReadInt64(reply, &codecBuffer->timeStamp)) {
316 HDF_LOGE("%{public}s: read timeStamp failed!", __func__);
317 return HDF_FAILURE;
318 }
319 if (!HdfSbufReadUint32(reply, &codecBuffer->flag)) {
320 HDF_LOGE("%{public}s: read flag failed!", __func__);
321 return HDF_FAILURE;
322 }
323 for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
324 if (CodecProxyParseBufferInfo(reply, &codecBuffer->buffer[i])) {
325 HDF_LOGE("%{public}s: read buffers failed!", __func__);
326 return HDF_FAILURE;
327 }
328 }
329 return HDF_SUCCESS;
330 }
331
CodecProxyPackCodecBuffer(struct HdfSBuf * data,const CodecBuffer * codecBuffer)332 int32_t CodecProxyPackCodecBuffer(struct HdfSBuf *data, const CodecBuffer *codecBuffer)
333 {
334 if (data == NULL || codecBuffer == NULL) {
335 HDF_LOGE("%{public}s: params error!", __func__);
336 return HDF_ERR_INVALID_PARAM;
337 }
338 if (!HdfSbufWriteUint32(data, codecBuffer->bufferCnt)) {
339 HDF_LOGE("%{public}s: write bufferCnt failed!", __func__);
340 return HDF_FAILURE;
341 }
342 if (!HdfSbufWriteUint32(data, codecBuffer->bufferId)) {
343 HDF_LOGE("%{public}s: write bufferId failed!", __func__);
344 return HDF_FAILURE;
345 }
346 if (!HdfSbufWriteInt64(data, codecBuffer->timeStamp)) {
347 HDF_LOGE("%{public}s: write timeStamp failed!", __func__);
348 return HDF_FAILURE;
349 }
350 if (!HdfSbufWriteUint32(data, codecBuffer->flag)) {
351 HDF_LOGE("%{public}s: write flag failed!", __func__);
352 return HDF_FAILURE;
353 }
354 for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
355 if (CodecProxyPackBufferInfo(data, &codecBuffer->buffer[i])) {
356 HDF_LOGE("%{public}s: write buffers failed!", __func__);
357 return HDF_FAILURE;
358 }
359 }
360 return HDF_SUCCESS;
361 }
362
CodecProxyParseFenceFd(struct HdfSBuf * reply,int32_t * fenceFd)363 int32_t CodecProxyParseFenceFd(struct HdfSBuf *reply, int32_t *fenceFd)
364 {
365 uint8_t validFd = 0;
366 if (!HdfSbufReadUint8(reply, &validFd)) {
367 HDF_LOGE("%{public}s: read validFd failed!", __func__);
368 return HDF_FAILURE;
369 }
370 if (validFd != 0) {
371 *fenceFd = HdfSbufReadFileDescriptor(reply);
372 } else {
373 *fenceFd = -1;
374 }
375 return HDF_SUCCESS;
376 }
377
CodecProxyPackFenceFd(struct HdfSBuf * data,int fenceFd)378 int32_t CodecProxyPackFenceFd(struct HdfSBuf *data, int fenceFd)
379 {
380 uint8_t validFd = fenceFd >= 0;
381 if (!HdfSbufWriteUint8(data, validFd)) {
382 HDF_LOGE("%{public}s: write validFd flag failed!", __func__);
383 return HDF_ERR_INVALID_PARAM;
384 }
385 if (validFd != 0 && !HdfSbufWriteFileDescriptor(data, fenceFd)) {
386 HDF_LOGE("%{public}s: write fenceFd failed!", __func__);
387 return HDF_ERR_INVALID_PARAM;
388 }
389 return HDF_SUCCESS;
390 }
391
392 #ifdef __cplusplus
393 }
394 #endif /* __cplusplus */
395