2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <vision_source_interface.h>
23 #include <vision_source.h>
25 #include <iniparser.h>
26 #include "vision_source_private.h"
28 #define MAX_BACKEND_NAME 32
30 typedef struct vision_source_internal
34 vision_source_func_s funcs;
35 } vision_source_internal_s;
37 int _vision_source_dlsym(vision_source_internal_s *handle)
39 void (*attach_backend)(vision_source_func_s *) =
40 dlsym(handle->dl_handle, "attach_backend");
42 char *error = dlerror();
44 LOGE("Failed dlsym : %s\n", error);
45 return VISION_SOURCE_ERROR_INTERNAL;
47 attach_backend(&(handle->funcs));
49 int ret = handle->funcs.init(&(handle->backend_handle));
50 if (ret != VISION_SOURCE_ERROR_NONE) {
53 return VISION_SOURCE_ERROR_NONE;
56 int _vision_source_attach(const char *backend_name,
57 vision_source_internal_s *handle)
60 LOGI("backend %s connected", backend_name);
61 handle->dl_handle = dlopen(backend_name, RTLD_LAZY);
62 VISION_SOURCE_NULL_ARG_CHECK(handle->dl_handle);
64 int ret = _vision_source_dlsym(handle);
65 if (ret != VISION_SOURCE_ERROR_NONE) {
66 dlclose(handle->dl_handle);
70 return VISION_SOURCE_ERROR_NONE;
73 int _vision_source_get_backend(char *name)
77 dictionary *ini = iniparser_load(HAL_INI_PATH);
79 LOGI("hal ini not exist path : %s", HAL_INI_PATH);
80 ini = iniparser_load(INI_PATH);
82 LOGE("ini not exist path : %s", INI_PATH);
83 return VISION_SOURCE_ERROR_INVALID_PARAMETER;
87 const char *backend_name = iniparser_getstring(ini, "common:name", NULL);
89 if (backend_name == NULL) {
90 iniparser_freedict(ini);
91 return VISION_SOURCE_ERROR_INVALID_PARAMETER;
94 snprintf(name, MAX_BACKEND_NAME, "libvision-source-%s.so", backend_name);
96 iniparser_freedict(ini);
98 return VISION_SOURCE_ERROR_NONE;
101 int vision_source_init(vision_source_h *handle)
104 VISION_SOURCE_NULL_ARG_CHECK(handle);
106 vision_source_internal_s *source_handle = NULL;
108 char backend_tmp[MAX_BACKEND_NAME];
109 int ret = _vision_source_get_backend(backend_tmp);
110 if (ret != VISION_SOURCE_ERROR_NONE) {
114 source_handle = malloc(sizeof(vision_source_internal_s));
115 VISION_SOURCE_NULL_ARG_CHECK(source_handle);
117 const char *backend_name = backend_tmp;
118 ret = _vision_source_attach(backend_name, source_handle);
119 if (ret != VISION_SOURCE_ERROR_NONE) {
123 *handle = source_handle;
126 return VISION_SOURCE_ERROR_NONE;
129 int vision_source_exit(vision_source_h handle)
132 VISION_SOURCE_NULL_ARG_CHECK(handle);
133 vision_source_internal_s *source_handle =
134 (vision_source_internal_s *) handle;
136 int ret = source_handle->funcs.exit(source_handle->backend_handle);
137 if (ret != VISION_SOURCE_ERROR_NONE) {
141 VISION_SOURCE_NULL_ARG_CHECK(source_handle->dl_handle);
142 ret = dlclose(source_handle->dl_handle);
144 LOGE("Failed to close shared object : %s", dlerror());
145 return VISION_SOURCE_ERROR_INTERNAL;
150 return VISION_SOURCE_ERROR_NONE;
153 int vision_source_open_device(vision_source_h handle, int device_index)
155 VISION_SOURCE_NULL_ARG_CHECK(handle);
156 vision_source_internal_s *source_handle =
157 (vision_source_internal_s *) handle;
158 return source_handle->funcs.open_device(source_handle->backend_handle,
161 int vision_source_close_device(vision_source_h handle)
163 VISION_SOURCE_NULL_ARG_CHECK(handle);
164 vision_source_internal_s *source_handle =
165 (vision_source_internal_s *) handle;
166 return source_handle->funcs.close_device(source_handle->backend_handle);
168 int vision_source_start_stream(vision_source_h handle, stream_cb callback,
171 VISION_SOURCE_NULL_ARG_CHECK(handle);
172 vision_source_internal_s *source_handle =
173 (vision_source_internal_s *) handle;
174 return source_handle->funcs.start_stream(source_handle->backend_handle,
175 callback, user_data);
177 int vision_source_stop_stream(vision_source_h handle)
179 VISION_SOURCE_NULL_ARG_CHECK(handle);
180 vision_source_internal_s *source_handle =
181 (vision_source_internal_s *) handle;
182 return source_handle->funcs.stop_stream(source_handle->backend_handle);
185 int vision_source_enumerate_devices(vision_source_h handle,
186 vision_source_device_info_list_s *info_list)
188 VISION_SOURCE_NULL_ARG_CHECK(handle);
189 vision_source_internal_s *source_handle =
190 (vision_source_internal_s *) handle;
191 return source_handle->funcs.enumerate_devices(source_handle->backend_handle,
195 int vision_source_set_stream_format(vision_source_h handle,
196 vision_source_format_s *format)
198 VISION_SOURCE_NULL_ARG_CHECK(handle);
199 vision_source_internal_s *source_handle =
200 (vision_source_internal_s *) handle;
201 return source_handle->funcs.set_stream_format(source_handle->backend_handle,
204 int vision_source_get_capture_frame(vision_source_h handle,
205 vision_source_buffer_s *buffer)
207 VISION_SOURCE_NULL_ARG_CHECK(handle);
208 vision_source_internal_s *source_handle =
209 (vision_source_internal_s *) handle;
210 return source_handle->funcs.get_capture_frame(source_handle->backend_handle,
214 int vision_source_release_capture_frame(vision_source_h handle,
217 VISION_SOURCE_NULL_ARG_CHECK(handle);
218 vision_source_internal_s *source_handle =
219 (vision_source_internal_s *) handle;
220 return source_handle->funcs.release_capture_frame(
221 source_handle->backend_handle, buffer_index);