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 
MockEffectReleaseDesc(struct EffectControllerDescriptorVdi * desc)180 static void MockEffectReleaseDesc(struct EffectControllerDescriptorVdi *desc)
181 {
182     if (desc == NULL) {
183         return;
184     }
185 
186     OsalMemFree((void *)desc->effectId);
187     desc->effectId = NULL;
188 
189     OsalMemFree((void *)desc->effectName);
190     desc->effectName = NULL;
191 
192     OsalMemFree((void *)desc->libName);
193     desc->libName = NULL;
194 
195     OsalMemFree((void *)desc->supplier);
196     desc->supplier = NULL;
197 }
198 
MockCpyDesc(const char * src,char ** dest)199 static int32_t MockCpyDesc(const char *src, char **dest)
200 {
201     if (src == NULL || dest == NULL) {
202         HDF_LOGE("%{public}s: invalid parameter!", __func__);
203         return HDF_ERR_INVALID_PARAM;
204     }
205 
206     *dest = (char *)OsalMemCalloc(HDF_EFFECT_NAME_LEN * sizeof(char));
207     if (*dest == NULL) {
208         HDF_LOGE("%{public}s: out of memory!", __func__);
209         return HDF_ERR_MALLOC_FAIL;
210     }
211 
212     if (memcpy_s((void *)(*dest), HDF_EFFECT_NAME_LEN, src, strlen(src)) != EOK) {
213         HDF_LOGE("%{public}s: memcpy_s effect desc fail!", __func__);
214         OsalMemFree((void **)dest);
215         return HDF_FAILURE;
216     }
217     return HDF_SUCCESS;
218 }
219 
MockGetEffectDescriptorSub(struct EffectControllerDescriptorVdi * desc)220 static int32_t MockGetEffectDescriptorSub(struct EffectControllerDescriptorVdi *desc)
221 {
222     if (desc == NULL) {
223         HDF_LOGE("%{public}s: invailid input params", __func__);
224         return HDF_ERR_INVALID_PARAM;
225     }
226 
227     if (MockCpyDesc(g_mockEffectDescriptor.effectId, &(desc->effectId)) != HDF_SUCCESS) {
228         HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "effectId");
229         MockEffectReleaseDesc(desc);
230         return HDF_FAILURE;
231     }
232 
233     if (MockCpyDesc(g_mockEffectDescriptor.effectName, &(desc->effectName)) != HDF_SUCCESS) {
234         HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "effectName");
235         MockEffectReleaseDesc(desc);
236         return HDF_FAILURE;
237     }
238 
239     if (MockCpyDesc(g_mockEffectDescriptor.libName, &(desc->libName)) != HDF_SUCCESS) {
240         HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "libName");
241         MockEffectReleaseDesc(desc);
242         return HDF_FAILURE;
243     }
244 
245     if (MockCpyDesc(g_mockEffectDescriptor.supplier, &(desc->supplier)) != HDF_SUCCESS) {
246         HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "supplier");
247         MockEffectReleaseDesc(desc);
248         return HDF_FAILURE;
249     }
250 
251     return HDF_SUCCESS;
252 }
253 
MockGetEffectDescriptor(struct IEffectControlVdi * self,struct EffectControllerDescriptorVdi * desc)254 int32_t MockGetEffectDescriptor(struct IEffectControlVdi *self, struct EffectControllerDescriptorVdi *desc)
255 {
256     HDF_LOGD("enter to %{public}s", __func__);
257     if (self == NULL || desc == NULL) {
258         HDF_LOGE("%{public}s: invailid input params", __func__);
259         return HDF_ERR_INVALID_PARAM;
260     }
261 
262     if (MockGetEffectDescriptorSub(desc) != HDF_SUCCESS) {
263         HDF_LOGE("%{public}s: get descriptor fail!", __func__);
264         return HDF_FAILURE;
265     }
266 
267     HDF_LOGD("%{public}s: succ", __func__);
268     return HDF_SUCCESS;
269 }
270 
MockCreateController(struct EffectFactory * self,const struct EffectInfoVdi * info,struct IEffectControlVdi ** handle)271 static int32_t MockCreateController(struct EffectFactory *self, const struct EffectInfoVdi *info,
272                                     struct IEffectControlVdi **handle)
273 {
274     if (self == NULL || info == NULL || handle == NULL) {
275         HDF_LOGE("%{public}s: invailid input params", __func__);
276         return HDF_ERR_INVALID_PARAM;
277     }
278 
279     struct EffectHwControl *hwCtrl = (struct EffectHwControl *)OsalMemCalloc(sizeof(struct EffectHwControl));
280     if (hwCtrl == NULL) {
281         HDF_LOGE("%{public}s: hwCtrl is NULL", __func__);
282         return HDF_FAILURE;
283     }
284 
285     hwCtrl->impls.EffectProcess = MockEffectProcess;
286     hwCtrl->impls.SendCommand = MockSendCommand;
287     hwCtrl->impls.EffectReverse = MockEffectReverse;
288     hwCtrl->impls.GetEffectDescriptor = MockGetEffectDescriptor,
289     *handle = &hwCtrl->impls;
290 
291     return HDF_SUCCESS;
292 }
293 
MockDestroyController(struct EffectFactory * self,struct IEffectControlVdi * handle)294 static int32_t MockDestroyController(struct EffectFactory *self, struct IEffectControlVdi *handle)
295 {
296     if (self == NULL || handle == NULL) {
297         HDF_LOGE("%{public}s: invailid input params", __func__);
298         return HDF_ERR_INVALID_PARAM;
299     }
300 
301     struct EffectHwControl *hwCtrl = (struct EffectHwControl *)handle;
302     OsalMemFree(hwCtrl);
303     hwCtrl = NULL;
304 
305     return HDF_SUCCESS;
306 }
307 
MockGetDescriptor(struct EffectFactory * self,const char * uuid,struct EffectControllerDescriptorVdi * desc)308 static int32_t MockGetDescriptor(struct EffectFactory *self, const char *uuid,
309     struct EffectControllerDescriptorVdi *desc)
310 {
311     HDF_LOGD("enter to %{public}s", __func__);
312     if (self == NULL || uuid == NULL || desc == NULL) {
313         HDF_LOGE("%{public}s: invailid input params", __func__);
314         return HDF_ERR_INVALID_PARAM;
315     }
316 
317     if (strcmp(uuid, g_mockEffectDescriptor.effectId) != 0) {
318         HDF_LOGE("%{public}s: error effectId!", __func__);
319         return HDF_FAILURE;
320     }
321 
322     if (MockGetEffectDescriptorSub(desc) != HDF_SUCCESS) {
323         HDF_LOGE("%{public}s: get descriptor fail!", __func__);
324         return HDF_FAILURE;
325     }
326     return HDF_SUCCESS;
327 }
328 
329 struct EffectFactory g_mockFactoryLib = {
330     .version = 1,
331     .effectLibName = "libmock_effect_lib",
332     .supplier = "hdf",
333     .CreateController = MockCreateController,
334     .DestroyController = MockDestroyController,
335     .GetDescriptor = MockGetDescriptor,
336 };
337 
GetEffectoyFactoryLib(void)338 struct EffectFactory *GetEffectoyFactoryLib(void)
339 {
340     return &g_mockFactoryLib;
341 }
342