1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong DID 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 "codec_component_manager_stub.h"
16 #include <dlfcn.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_device_object.h>
19 #include <hdf_log.h>
20 #include <osal_mem.h>
21 #include <securec.h>
22 #include "codec_component_capability_config.h"
23 #include "codec_component_manager_service.h"
24 #define CODEC_SERVICE_IMPL "libcodec_hdi_omx_service_impl"
25 typedef void (*SERVICE_CONSTRUCT_FUNC)(struct OmxComponentManager *);
SerStubGetComponentNum(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)26 static int32_t SerStubGetComponentNum(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
27 struct HdfSBuf *reply)
28 {
29 int32_t num = serviceImpl->GetComponentNum();
30 if (!HdfSbufWriteInt32(reply, num)) {
31 HDF_LOGE("%{public}s: write num failed!", __func__);
32 return HDF_ERR_INVALID_PARAM;
33 }
34 return HDF_SUCCESS;
35 }
36
SerStubGetComponentCapablityList(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)37 static int32_t SerStubGetComponentCapablityList(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
38 struct HdfSBuf *reply)
39 {
40 int32_t count = 0;
41 int32_t err = HDF_SUCCESS;
42 CodecCompCapability *caps = NULL;
43 if (!HdfSbufReadInt32(data, &count) || (count <= 0)) {
44 HDF_LOGE("%{public}s: read count failed!", __func__);
45 return HDF_ERR_INVALID_PARAM;
46 }
47 caps = (CodecCompCapability *)OsalMemCalloc(sizeof(CodecCompCapability) * (count));
48 if (caps == NULL) {
49 HDF_LOGE("%{public}s: alloc caps failed!", __func__);
50 return HDF_ERR_INVALID_PARAM;
51 }
52 err = serviceImpl->GetComponentCapabilityList(caps, count);
53 if (err != HDF_SUCCESS) {
54 HDF_LOGE("%{public}s: call GetComponentCapabilityList function failed!", __func__);
55 return err;
56 }
57
58 for (int32_t i = 0; i < count; i++) {
59 if (!CodecCompCapabilityBlockMarshalling(reply, &caps[i])) {
60 HDF_LOGE("%{public}s: call CodecCompCapabilityBlockMarshalling function failed!", __func__);
61 err = HDF_ERR_INVALID_PARAM;
62 break;
63 }
64 }
65 OsalMemFree(caps);
66 return err;
67 }
68
ReadParamsForCreateComponent(struct HdfSBuf * data,char ** compName,int64_t * appData,struct CodecCallbackType ** callback)69 static int32_t ReadParamsForCreateComponent(struct HdfSBuf *data, char **compName, int64_t *appData,
70 struct CodecCallbackType **callback)
71 {
72 const char *compNameCp = HdfSbufReadString(data);
73 if (compNameCp == NULL) {
74 HDF_LOGE("%{public}s: read compNameCp failed!", __func__);
75 return HDF_ERR_INVALID_PARAM;
76 }
77
78 if (!HdfSbufReadInt64(data, appData)) {
79 HDF_LOGE("%{public}s: read appData failed!", __func__);
80 return HDF_ERR_INVALID_PARAM;
81 }
82 *compName = strdup(compNameCp);
83
84 struct HdfRemoteService *callbackRemote = HdfSbufReadRemoteService(data);
85 if (callbackRemote == NULL) {
86 HDF_LOGE("%{public}s: read callbackRemote failed!", __func__);
87 return HDF_ERR_INVALID_PARAM;
88 }
89 *callback = CodecCallbackTypeGet(callbackRemote);
90
91 return HDF_SUCCESS;
92 }
93
SerStubCreateComponent(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)94 static int32_t SerStubCreateComponent(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
95 struct HdfSBuf *reply)
96 {
97 int32_t ret = HDF_SUCCESS;
98 struct CodecComponentType *component = NULL;
99 uint32_t componentId = 0;
100 int64_t appData = 0;
101 struct CodecCallbackType *callback = NULL;
102 char *compName = NULL;
103
104 ret = ReadParamsForCreateComponent(data, &compName, &appData, &callback);
105 if (ret != HDF_SUCCESS) {
106 if (compName != NULL) {
107 OsalMemFree(compName);
108 compName = NULL;
109 }
110 return ret;
111 }
112 ret = serviceImpl->CreateComponent(&component, &componentId, compName, appData, callback);
113 if (compName != NULL) {
114 OsalMemFree(compName);
115 compName = NULL;
116 }
117 if (ret != HDF_SUCCESS) {
118 HDF_LOGE("%{public}s: call CreateComponent function failed!", __func__);
119 return ret;
120 }
121
122 if (HdfSbufWriteRemoteService(reply, component->AsObject(component)) != 0) {
123 HDF_LOGE("%{public}s: write component failed!", __func__);
124 return HDF_ERR_INVALID_PARAM;
125 }
126 if (!HdfSbufWriteUint32(reply, componentId)) {
127 HDF_LOGE("%{public}s: write componentId failed!", __func__);
128 return HDF_ERR_INVALID_PARAM;
129 }
130 return ret;
131 }
132
SerStubDestroyComponent(struct CodecComponentManager * serviceImpl,struct HdfSBuf * data,struct HdfSBuf * reply)133 static int32_t SerStubDestroyComponent(struct CodecComponentManager *serviceImpl, struct HdfSBuf *data,
134 struct HdfSBuf *reply)
135 {
136 uint32_t componentId = 0;
137 if (!HdfSbufReadUint32(data, &componentId)) {
138 HDF_LOGE("%{public}s: read componentId failed!", __func__);
139 return HDF_ERR_INVALID_PARAM;
140 }
141 int32_t ret = serviceImpl->DestroyComponent(componentId);
142 if (ret != HDF_SUCCESS) {
143 HDF_LOGE("%{public}s: call DestroyComponent function failed!", __func__);
144 }
145 return ret;
146 }
147
CodecComponentManagerServiceOnRemoteRequest(struct CodecComponentManager * serviceImpl,int32_t cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)148 static int32_t CodecComponentManagerServiceOnRemoteRequest(struct CodecComponentManager *serviceImpl, int32_t cmdId,
149 struct HdfSBuf *data, struct HdfSBuf *reply)
150 {
151 switch (cmdId) {
152 case CMD_CODEC_GET_COMPONENT_NUM:
153 return SerStubGetComponentNum(serviceImpl, data, reply);
154 case CMD_CODEC_GET_COMPONENT_CAPABILITY_LIST:
155 return SerStubGetComponentCapablityList(serviceImpl, data, reply);
156 case CMD_CREATE_COMPONENT:
157 return SerStubCreateComponent(serviceImpl, data, reply);
158 case CMD_DESTROY_COMPONENT:
159 return SerStubDestroyComponent(serviceImpl, data, reply);
160 default:
161 HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, cmdId);
162 return HDF_ERR_INVALID_PARAM;
163 }
164 }
165
CodecComponentManagerStubAsObject(struct CodecComponentManager * self)166 static struct HdfRemoteService *CodecComponentManagerStubAsObject(struct CodecComponentManager *self)
167 {
168 return NULL;
169 }
170
CodecComponentManagerStubConstruct(struct CodecComponentManagerStub * stub)171 bool CodecComponentManagerStubConstruct(struct CodecComponentManagerStub *stub)
172 {
173 if (stub == NULL) {
174 HDF_LOGE("%{public}s: stub is null!", __func__);
175 return false;
176 }
177
178 stub->OnRemoteRequest = CodecComponentManagerServiceOnRemoteRequest;
179 stub->interface.AsObject = CodecComponentManagerStubAsObject;
180 return true;
181 }