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