Remove build warnings and fixed svace issues
[platform/adaptation/ap_samsung/libomxil-e3250-v4l2.git] / openmax / core / Exynos_OMX_Component_Register.c
1 /*
2  *
3  * Copyright 2012 Samsung Electronics S.LSI Co. LTD
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*
19  * @file       Exynos_OMX_Component_Register.c
20  * @brief      Exynos OpenMAX IL Component Register
21  * @author     SeungBeom Kim (sbcrux.kim@samsung.com)
22  * @version    2.0.0
23  * @history
24  *    2012.02.20 : Create
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <dlfcn.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include <dirent.h>
35
36 #include "OMX_Component.h"
37 #include "Exynos_OSAL_Memory.h"
38 #include "Exynos_OSAL_ETC.h"
39 #include "Exynos_OSAL_Library.h"
40 #include "Exynos_OMX_Component_Register.h"
41 #include "Exynos_OMX_Macros.h"
42
43 #undef  EXYNOS_LOG_TAG
44 #define EXYNOS_LOG_TAG    "EXYNOS_COMP_REGS"
45 #define EXYNOS_LOG_OFF
46 #include "Exynos_OSAL_Log.h"
47
48 OMX_ERRORTYPE Exynos_OMX_Component_Register(EXYNOS_OMX_COMPONENT_REGLIST **compList, OMX_U32 *compNum)
49 {
50     OMX_ERRORTYPE  ret = OMX_ErrorNone;
51     int            componentNum = 0, totalCompNum = 0;
52     char          *libName;
53     const char    *errorMsg;
54     DIR           *dir;
55     struct dirent *d;
56
57     int (*Exynos_OMX_COMPONENT_Library_Register)(ExynosRegisterComponentType **exynosComponents);
58     ExynosRegisterComponentType **exynosComponentsTemp;
59     EXYNOS_OMX_COMPONENT_REGLIST *componentList;
60
61     FunctionIn();
62
63     dir = opendir(EXYNOS_OMX_INSTALL_PATH);
64     if (dir == NULL) {
65         ret = OMX_ErrorUndefined;
66         goto EXIT;
67     }
68
69     componentList = (EXYNOS_OMX_COMPONENT_REGLIST *)Exynos_OSAL_Malloc(sizeof(EXYNOS_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
70     Exynos_OSAL_Memset(componentList, 0, sizeof(EXYNOS_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
71     libName = Exynos_OSAL_Malloc(MAX_OMX_COMPONENT_LIBNAME_SIZE);
72
73     while ((d = readdir(dir)) != NULL) {
74         OMX_HANDLETYPE soHandle;
75         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "%s", d->d_name);
76
77         if (Exynos_OSAL_Strncmp(d->d_name, "libOMX.Exynos.", Exynos_OSAL_Strlen("libOMX.Exynos.")) == 0) {
78             Exynos_OSAL_Memset(libName, 0, MAX_OMX_COMPONENT_LIBNAME_SIZE);
79             Exynos_OSAL_Strcpy(libName, EXYNOS_OMX_INSTALL_PATH);
80             Exynos_OSAL_Strcat(libName, d->d_name);
81             Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "Path & libName : %s", libName);
82             if ((soHandle = Exynos_OSAL_dlopen(libName, RTLD_LAZY)) != NULL) {
83                 Exynos_OSAL_dlerror();    /* clear error*/
84                 if ((Exynos_OMX_COMPONENT_Library_Register = Exynos_OSAL_dlsym(soHandle, "Exynos_OMX_COMPONENT_Library_Register")) != NULL) {
85                     int i = 0;
86                     unsigned int j = 0;
87
88                     componentNum = (*Exynos_OMX_COMPONENT_Library_Register)(NULL);
89                     exynosComponentsTemp = (ExynosRegisterComponentType **)Exynos_OSAL_Malloc(sizeof(ExynosRegisterComponentType*) * componentNum);
90                     for (i = 0; i < componentNum; i++) {
91                         exynosComponentsTemp[i] = Exynos_OSAL_Malloc(sizeof(ExynosRegisterComponentType));
92                         Exynos_OSAL_Memset(exynosComponentsTemp[i], 0, sizeof(ExynosRegisterComponentType));
93                     }
94                     (*Exynos_OMX_COMPONENT_Library_Register)(exynosComponentsTemp);
95
96                     for (i = 0; i < componentNum; i++) {
97                         Exynos_OSAL_Strcpy(componentList[totalCompNum].component.componentName, exynosComponentsTemp[i]->componentName);
98                         for (j = 0; j < exynosComponentsTemp[i]->totalRoleNum; j++)
99                             Exynos_OSAL_Strcpy(componentList[totalCompNum].component.roles[j], exynosComponentsTemp[i]->roles[j]);
100                         componentList[totalCompNum].component.totalRoleNum = exynosComponentsTemp[i]->totalRoleNum;
101
102                         Exynos_OSAL_Strcpy(componentList[totalCompNum].libName, libName);
103
104                         totalCompNum++;
105                     }
106                     for (i = 0; i < componentNum; i++) {
107                         Exynos_OSAL_Free(exynosComponentsTemp[i]);
108                     }
109
110                     Exynos_OSAL_Free(exynosComponentsTemp);
111                 } else {
112                     if ((errorMsg = Exynos_OSAL_dlerror()) != NULL)
113                         Exynos_OSAL_Log(EXYNOS_LOG_WARNING, "dlsym failed: %s", errorMsg);
114                 }
115                 Exynos_OSAL_dlclose(soHandle);
116             } else {
117                 Exynos_OSAL_Log(EXYNOS_LOG_WARNING, "dlopen failed: %s", Exynos_OSAL_dlerror());
118             }
119         } else {
120             /* not a component name line. skip */
121             continue;
122         }
123     }
124
125     Exynos_OSAL_Free(libName);
126
127     closedir(dir);
128
129     *compList = componentList;
130     *compNum = totalCompNum;
131
132 EXIT:
133     FunctionOut();
134
135     return ret;
136 }
137
138 OMX_ERRORTYPE Exynos_OMX_Component_Unregister(EXYNOS_OMX_COMPONENT_REGLIST *componentList)
139 {
140     Exynos_OSAL_Memset(componentList, 0, sizeof(EXYNOS_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
141     Exynos_OSAL_Free(componentList);
142
143     return OMX_ErrorNone;
144 }
145
146 OMX_ERRORTYPE Exynos_OMX_ComponentAPICheck(OMX_COMPONENTTYPE *component)
147 {
148     OMX_ERRORTYPE ret = OMX_ErrorNone;
149
150     if ((NULL == component->GetComponentVersion)    ||
151         (NULL == component->SendCommand)            ||
152         (NULL == component->GetParameter)           ||
153         (NULL == component->SetParameter)           ||
154         (NULL == component->GetConfig)              ||
155         (NULL == component->SetConfig)              ||
156         (NULL == component->GetExtensionIndex)      ||
157         (NULL == component->GetState)               ||
158         (NULL == component->ComponentTunnelRequest) ||
159         (NULL == component->UseBuffer)              ||
160         (NULL == component->AllocateBuffer)         ||
161         (NULL == component->FreeBuffer)             ||
162         (NULL == component->EmptyThisBuffer)        ||
163         (NULL == component->FillThisBuffer)         ||
164         (NULL == component->SetCallbacks)           ||
165         (NULL == component->ComponentDeInit)        ||
166         (NULL == component->UseEGLImage)            ||
167         (NULL == component->ComponentRoleEnum))
168         ret = OMX_ErrorInvalidComponent;
169     else
170         ret = OMX_ErrorNone;
171
172     return ret;
173 }
174
175 OMX_ERRORTYPE Exynos_OMX_ComponentLoad(EXYNOS_OMX_COMPONENT *exynos_component)
176 {
177     OMX_ERRORTYPE      ret = OMX_ErrorNone;
178     OMX_HANDLETYPE     libHandle;
179     OMX_COMPONENTTYPE *pOMXComponent;
180
181     FunctionIn();
182
183     OMX_ERRORTYPE (*Exynos_OMX_ComponentInit)(OMX_HANDLETYPE hComponent, OMX_STRING componentName);
184
185     libHandle = Exynos_OSAL_dlopen((OMX_STRING)exynos_component->libName, RTLD_LAZY);
186     if (!libHandle) {
187         ret = OMX_ErrorInvalidComponentName;
188         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponentName, Line:%d", __LINE__);
189         goto EXIT;
190     }
191
192     Exynos_OMX_ComponentInit = Exynos_OSAL_dlsym(libHandle, "Exynos_OMX_ComponentInit");
193     if (!Exynos_OMX_ComponentInit) {
194         Exynos_OSAL_dlclose(libHandle);
195         ret = OMX_ErrorInvalidComponent;
196         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
197         goto EXIT;
198     }
199
200     pOMXComponent = (OMX_COMPONENTTYPE *)Exynos_OSAL_Malloc(sizeof(OMX_COMPONENTTYPE));
201     INIT_SET_SIZE_VERSION(pOMXComponent, OMX_COMPONENTTYPE);
202     ret = (*Exynos_OMX_ComponentInit)((OMX_HANDLETYPE)pOMXComponent, (OMX_STRING)exynos_component->componentName);
203     if (ret != OMX_ErrorNone) {
204         Exynos_OSAL_Free(pOMXComponent);
205         Exynos_OSAL_dlclose(libHandle);
206         ret = OMX_ErrorInvalidComponent;
207         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
208         goto EXIT;
209     } else {
210         if (Exynos_OMX_ComponentAPICheck(pOMXComponent) != OMX_ErrorNone) {
211             if (NULL != pOMXComponent->ComponentDeInit)
212                 pOMXComponent->ComponentDeInit(pOMXComponent);
213             Exynos_OSAL_Free(pOMXComponent);
214             Exynos_OSAL_dlclose(libHandle);
215             ret = OMX_ErrorInvalidComponent;
216             Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
217             goto EXIT;
218         }
219         exynos_component->libHandle = libHandle;
220         exynos_component->pOMXComponent = pOMXComponent;
221         ret = OMX_ErrorNone;
222     }
223
224 EXIT:
225     FunctionOut();
226
227     return ret;
228 }
229
230 OMX_ERRORTYPE Exynos_OMX_ComponentUnload(EXYNOS_OMX_COMPONENT *exynos_component)
231 {
232     OMX_ERRORTYPE ret = OMX_ErrorNone;
233     OMX_COMPONENTTYPE *pOMXComponent = NULL;
234
235     FunctionIn();
236
237     if (!exynos_component) {
238         ret = OMX_ErrorBadParameter;
239         goto EXIT;
240     }
241
242     pOMXComponent = exynos_component->pOMXComponent;
243     if (pOMXComponent != NULL) {
244         pOMXComponent->ComponentDeInit(pOMXComponent);
245         Exynos_OSAL_Free(pOMXComponent);
246         exynos_component->pOMXComponent = NULL;
247     }
248
249     if (exynos_component->libHandle != NULL) {
250         Exynos_OSAL_dlclose(exynos_component->libHandle);
251         exynos_component->libHandle = NULL;
252     }
253
254 EXIT:
255     FunctionOut();
256
257     return ret;
258 }
259