1 /*
2 * Copyright (c) 2023 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 "osal_mem.h"
17 #include "securec.h"
18 #include "v1_0/effect_types_vdi.h"
19 #include "v1_0/ieffect_control_vdi.h"
20 #include "v1_0/effect_factory.h"
21 #include "audio_uhdf_log.h"
22
23 #define HDF_EFFECT_NAME_LEN 64
24 #define HDF_LOG_TAG HDF_AUDIO_EFFECT
25 struct EffectHwControl {
26 struct IEffectControlVdi impls;
27 };
28
29 struct EffectControllerDescriptorVdi g_mockEffectDescriptor = {
30 .effectId = "aaaabbbb-8888-9999-6666-aabbccdd9966ff",
31 .effectName = "mock_effect",
32 .libName = "libmock_effect_lib",
33 .supplier = "mock"
34 };
35
MockEffectInitController(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)36 static int32_t MockEffectInitController(const int8_t *commandData, uint32_t cmdDataLen,
37 int8_t *replyData, uint32_t *replyDataLen)
38 {
39 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
40 return HDF_ERR_INVALID_PARAM;
41 }
42 (void)commandData;
43 (void)cmdDataLen;
44 return HDF_SUCCESS;
45 }
46
MockEffectSetConfig(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)47 static int32_t MockEffectSetConfig(const int8_t *commandData, uint32_t cmdDataLen,
48 int8_t *replyData, uint32_t *replyDataLen)
49 {
50 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
51 return HDF_ERR_INVALID_PARAM;
52 }
53 (void)commandData;
54 (void)cmdDataLen;
55 return HDF_SUCCESS;
56 }
57
MockEffectGetCofig(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)58 static int32_t MockEffectGetCofig(const int8_t *commandData, uint32_t cmdDataLen,
59 int8_t *replyData, uint32_t *replyDataLen)
60 {
61 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
62 return HDF_ERR_INVALID_PARAM;
63 }
64 (void)commandData;
65 (void)cmdDataLen;
66 return HDF_SUCCESS;
67 }
68
MockEffectReset(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)69 static int32_t MockEffectReset(const int8_t *commandData, uint32_t cmdDataLen,
70 int8_t *replyData, uint32_t *replyDataLen)
71 {
72 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
73 return HDF_ERR_INVALID_PARAM;
74 }
75 (void)commandData;
76 (void)cmdDataLen;
77 return HDF_SUCCESS;
78 }
79
MockEffectEnable(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)80 static int32_t MockEffectEnable(const int8_t *commandData, uint32_t cmdDataLen,
81 int8_t *replyData, uint32_t *replyDataLen)
82 {
83 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
84 return HDF_ERR_INVALID_PARAM;
85 }
86 (void)commandData;
87 (void)cmdDataLen;
88 return HDF_SUCCESS;
89 }
90
MockEffectDisable(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)91 static int32_t MockEffectDisable(const int8_t *commandData, uint32_t cmdDataLen,
92 int8_t *replyData, uint32_t *replyDataLen)
93 {
94 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
95 return HDF_ERR_INVALID_PARAM;
96 }
97 (void)commandData;
98 (void)cmdDataLen;
99 return HDF_SUCCESS;
100 }
101
MockEffectSetparams(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)102 static int32_t MockEffectSetparams(const int8_t *commandData, uint32_t cmdDataLen,
103 int8_t *replyData, uint32_t *replyDataLen)
104 {
105 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
106 return HDF_ERR_INVALID_PARAM;
107 }
108 (void)commandData;
109 (void)cmdDataLen;
110 return HDF_SUCCESS;
111 }
112
MockEffectGetParams(const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)113 static int32_t MockEffectGetParams(const int8_t *commandData, uint32_t cmdDataLen,
114 int8_t *replyData, uint32_t *replyDataLen)
115 {
116 if (commandData == NULL || replyData == NULL || replyDataLen == NULL) {
117 return HDF_ERR_INVALID_PARAM;
118 }
119 (void)commandData;
120 (void)cmdDataLen;
121 return HDF_SUCCESS;
122 }
123
124 static struct EffectCommandTable g_effectCommandTable[] = {
125 {AUDIO_EFFECT_COMMAND_VDI_INIT_CONTOLLER, MockEffectInitController},
126 {AUDIO_EFFECT_COMMAND_VDI_SET_CONFIG, MockEffectSetConfig},
127 {AUDIO_EFFECT_COMMAND_VDI_GET_CONFIG, MockEffectGetCofig},
128 {AUDIO_EFFECT_COMMAND_VDI_RESET, MockEffectReset},
129 {AUDIO_EFFECT_COMMAND_VDI_ENABLE, MockEffectEnable},
130 {AUDIO_EFFECT_COMMAND_VDI_DISABLE, MockEffectDisable},
131 {AUDIO_EFFECT_COMMAND_VDI_SET_PARAM, MockEffectSetparams},
132 {AUDIO_EFFECT_COMMAND_VDI_GET_PARAM, MockEffectGetParams},
133 };
134
MockEffectProcess(struct IEffectControlVdi * self,const struct AudioEffectBufferVdi * input,struct AudioEffectBufferVdi * output)135 static int32_t MockEffectProcess(struct IEffectControlVdi *self, const struct AudioEffectBufferVdi *input,
136 struct AudioEffectBufferVdi *output)
137 {
138 if (self == NULL || input == NULL || output == NULL) {
139 HDF_LOGE("%{public}s: invailid input params", __func__);
140 return HDF_ERR_INVALID_PARAM;
141 }
142
143 return HDF_SUCCESS;
144 }
145
MockSendCommand(struct IEffectControlVdi * self,enum EffectCommandTableIndexVdi cmdId,const int8_t * commandData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)146 static int32_t MockSendCommand(struct IEffectControlVdi *self, enum EffectCommandTableIndexVdi cmdId,
147 const int8_t *commandData, uint32_t cmdDataLen, int8_t *replyData, uint32_t *replyDataLen)
148 {
149 if (self == NULL || commandData == NULL || replyData == NULL || replyDataLen == NULL) {
150 HDF_LOGE("%{public}s: invailid input params", __func__);
151 return HDF_ERR_INVALID_PARAM;
152 }
153
154 struct EffectCommandTable *cmdTable = g_effectCommandTable;
155
156 if (cmdId >= (sizeof(g_effectCommandTable) / sizeof(struct EffectCommandTable))) {
157 HDF_LOGE("%{public}s: the index of the table is invailied", __func__);
158 return HDF_ERR_INVALID_PARAM;
159 }
160
161 if (cmdTable[cmdId].func == NULL) {
162 HDF_LOGE("%{public}s: the corresponding command function is null", __func__);
163 return HDF_FAILURE;
164 }
165
166 return cmdTable[cmdId].func(commandData, cmdDataLen, replyData, replyDataLen);
167 }
168
MockEffectReverse(struct IEffectControlVdi * self,const struct AudioEffectBufferVdi * input,struct AudioEffectBufferVdi * output)169 static int32_t MockEffectReverse(struct IEffectControlVdi *self, const struct AudioEffectBufferVdi *input,
170 struct AudioEffectBufferVdi *output)
171 {
172 if (self == NULL || input == NULL || output == NULL) {
173 HDF_LOGE("%{public}s: invailid input params", __func__);
174 return HDF_ERR_INVALID_PARAM;
175 }
176
177 return HDF_SUCCESS;
178 }
179
MockCpyDesc(const char * src,char ** dest)180 static int32_t MockCpyDesc(const char *src, char **dest)
181 {
182 if (src == NULL || dest == NULL) {
183 HDF_LOGE("%{public}s: invalid parameter!", __func__);
184 return HDF_ERR_INVALID_PARAM;
185 }
186
187 if (*dest == NULL) {
188 HDF_LOGE("%{public}s: out of memory!", __func__);
189 return HDF_ERR_MALLOC_FAIL;
190 }
191
192 if (memcpy_s((void *)(*dest), HDF_EFFECT_NAME_LEN, src, strlen(src)) != EOK) {
193 HDF_LOGE("%{public}s: memcpy_s effect desc fail!", __func__);
194 return HDF_FAILURE;
195 }
196 return HDF_SUCCESS;
197 }
198
MockGetEffectDescriptorSub(struct EffectControllerDescriptorVdi * desc)199 static int32_t MockGetEffectDescriptorSub(struct EffectControllerDescriptorVdi *desc)
200 {
201 if (desc == NULL) {
202 HDF_LOGE("%{public}s: invailid input params", __func__);
203 return HDF_ERR_INVALID_PARAM;
204 }
205
206 if (MockCpyDesc(g_mockEffectDescriptor.effectId, &(desc->effectId)) != HDF_SUCCESS) {
207 HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "effectId");
208 return HDF_FAILURE;
209 }
210
211 if (MockCpyDesc(g_mockEffectDescriptor.effectName, &(desc->effectName)) != HDF_SUCCESS) {
212 HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "effectName");
213 return HDF_FAILURE;
214 }
215
216 if (MockCpyDesc(g_mockEffectDescriptor.libName, &(desc->libName)) != HDF_SUCCESS) {
217 HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "libName");
218 return HDF_FAILURE;
219 }
220
221 if (MockCpyDesc(g_mockEffectDescriptor.supplier, &(desc->supplier)) != HDF_SUCCESS) {
222 HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "supplier");
223 return HDF_FAILURE;
224 }
225
226 return HDF_SUCCESS;
227 }
228
MockGetEffectDescriptor(struct IEffectControlVdi * self,struct EffectControllerDescriptorVdi * desc)229 int32_t MockGetEffectDescriptor(struct IEffectControlVdi *self, struct EffectControllerDescriptorVdi *desc)
230 {
231 HDF_LOGD("enter to %{public}s", __func__);
232 if (self == NULL || desc == NULL) {
233 HDF_LOGE("%{public}s: invailid input params", __func__);
234 return HDF_ERR_INVALID_PARAM;
235 }
236
237 if (MockGetEffectDescriptorSub(desc) != HDF_SUCCESS) {
238 HDF_LOGE("%{public}s: get descriptor fail!", __func__);
239 return HDF_FAILURE;
240 }
241
242 HDF_LOGD("%{public}s: succ", __func__);
243 return HDF_SUCCESS;
244 }
245
MockCreateController(struct EffectFactory * self,const struct EffectInfoVdi * info,struct IEffectControlVdi ** handle)246 static int32_t MockCreateController(struct EffectFactory *self, const struct EffectInfoVdi *info,
247 struct IEffectControlVdi **handle)
248 {
249 if (self == NULL || info == NULL || handle == NULL) {
250 HDF_LOGE("%{public}s: invailid input params", __func__);
251 return HDF_ERR_INVALID_PARAM;
252 }
253
254 struct EffectHwControl *hwCtrl = (struct EffectHwControl *)OsalMemCalloc(sizeof(struct EffectHwControl));
255 if (hwCtrl == NULL) {
256 HDF_LOGE("%{public}s: hwCtrl is NULL", __func__);
257 return HDF_FAILURE;
258 }
259
260 hwCtrl->impls.EffectProcess = MockEffectProcess;
261 hwCtrl->impls.SendCommand = MockSendCommand;
262 hwCtrl->impls.EffectReverse = MockEffectReverse;
263 hwCtrl->impls.GetEffectDescriptor = MockGetEffectDescriptor,
264 *handle = &hwCtrl->impls;
265
266 return HDF_SUCCESS;
267 }
268
MockDestroyController(struct EffectFactory * self,struct IEffectControlVdi * handle)269 static int32_t MockDestroyController(struct EffectFactory *self, struct IEffectControlVdi *handle)
270 {
271 if (self == NULL || handle == NULL) {
272 HDF_LOGE("%{public}s: invailid input params", __func__);
273 return HDF_ERR_INVALID_PARAM;
274 }
275
276 struct EffectHwControl *hwCtrl = (struct EffectHwControl *)handle;
277 OsalMemFree(hwCtrl);
278 hwCtrl = NULL;
279
280 return HDF_SUCCESS;
281 }
282
MockGetDescriptor(struct EffectFactory * self,const char * uuid,struct EffectControllerDescriptorVdi * desc)283 static int32_t MockGetDescriptor(struct EffectFactory *self, const char *uuid,
284 struct EffectControllerDescriptorVdi *desc)
285 {
286 HDF_LOGD("enter to %{public}s", __func__);
287 if (self == NULL || uuid == NULL || desc == NULL) {
288 HDF_LOGE("%{public}s: invailid input params", __func__);
289 return HDF_ERR_INVALID_PARAM;
290 }
291
292 if (strcmp(uuid, g_mockEffectDescriptor.effectId) != 0) {
293 HDF_LOGE("%{public}s: error effectId!", __func__);
294 return HDF_FAILURE;
295 }
296
297 if (MockGetEffectDescriptorSub(desc) != HDF_SUCCESS) {
298 HDF_LOGE("%{public}s: get descriptor fail!", __func__);
299 return HDF_FAILURE;
300 }
301 return HDF_SUCCESS;
302 }
303
304 struct EffectFactory g_mockFactoryLib = {
305 .version = 1,
306 .effectLibName = "libmock_effect_lib",
307 .supplier = "hdf",
308 .CreateController = MockCreateController,
309 .DestroyController = MockDestroyController,
310 .GetDescriptor = MockGetDescriptor,
311 };
312
GetEffectoyFactoryLib(void)313 struct EffectFactory *GetEffectoyFactoryLib(void)
314 {
315 return &g_mockFactoryLib;
316 }
317