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