e329bdaf305f448d436850a84b867ec0c7af87d1
[platform/core/uifw/multi-assistant-service.git] / plugins / wakeup-manager / src / dependency_resolver.cpp
1 /*
2  * Copyright 2018-2019 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <tizen.h>
18 #include <dlfcn.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <vconf.h>
24
25 #include <stdexcept>
26
27 #include "multi_assistant_main.h"
28 #include "dependency_resolver.h"
29
30 #ifdef LOG_TAG
31 #undef LOG_TAG
32 #endif
33 #define LOG_TAG "dependency-resolver"
34
35 static void *g_handle = NULL;
36 static mas_dependency_module_interface g_mas_dependency = { NULL, };
37
38 int dependency_resolver_initialize(mas_dependency_plugin_proxy_interface interface)
39 {
40         MAS_LOGD("[Enter]");
41
42         const int FILEPATH_LEN = 512;
43         char filepath[FILEPATH_LEN] = {'\0', };
44         char *vconf_str = vconf_get_str(MAS_DEPENDENCY_MODULE_PATH);
45         if (vconf_str) {
46                 snprintf(filepath, FILEPATH_LEN - 1, "%s", vconf_str);
47                 free(vconf_str);
48         } else {
49                 const char *default_path = MAS_DEPENDENCY_DEFAULT_PATH;
50                 snprintf(filepath, FILEPATH_LEN - 1, "%s/%s", default_path, MAS_DEPENDENCY_DEFAULT_FILENAME);
51         }
52         filepath[FILEPATH_LEN - 1] = '\0';
53
54         char *error;
55         g_handle = NULL;
56         g_handle = dlopen(filepath, RTLD_LAZY);
57         if (NULL != (error = dlerror())) {
58                 MAS_LOGE("[ERROR] Fail to dlopen(%s), error(%s)", filepath, error);
59                 return -1; //MAS_ERROR_OPERATION_FAILED;
60         }
61
62         g_mas_dependency.initialize =
63                 (mas_dependency_initialize)dlsym(g_handle,
64                 MAS_DEPENDENCY_FUNC_INITIALIZE);
65         g_mas_dependency.deinitialize =
66                 (mas_dependency_deinitialize)dlsym(g_handle,
67                 MAS_DEPENDENCY_FUNC_DEINITIALIZE);
68         g_mas_dependency.set_error_callback =
69                 (mas_dependency_set_error_callback)dlsym(g_handle,
70                 MAS_DEPENDENCY_FUNC_SET_ERROR_CALLBACK);
71         g_mas_dependency.start_recording =
72                 (mas_dependency_start_recording)dlsym(g_handle,
73                 MAS_DEPENDENCY_FUNC_START_RECORDING);
74         g_mas_dependency.stop_recording =
75                 (mas_dependency_stop_recording)dlsym(g_handle,
76                 MAS_DEPENDENCY_FUNC_STOP_RECORDING);
77         g_mas_dependency.set_recording_session =
78                 (mas_dependency_set_recording_session)dlsym(g_handle,
79                 MAS_DEPENDENCY_FUNC_SET_RECORDING_SESSION);
80         g_mas_dependency.set_background_volume =
81                 (mas_dependency_set_background_volume)dlsym(g_handle,
82                 MAS_DEPENDENCY_FUNC_SET_BACKGROUND_VOLUME);
83         g_mas_dependency.get_audio_format =
84                 (mas_dependency_get_audio_format)dlsym(g_handle,
85                 MAS_DEPENDENCY_FUNC_GET_AUDIO_FORMAT);
86         g_mas_dependency.get_audio_source_type =
87                 (mas_dependency_get_audio_source_type)dlsym(g_handle,
88                 MAS_DEPENDENCY_FUNC_GET_AUDIO_SOURCE_TYPE);
89         g_mas_dependency.process_wakeup_engine_command =
90                 (mas_dependency_process_wakeup_engine_command)dlsym(g_handle,
91                 MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND);
92
93         int ret = -1;
94         int dependency_version = 0;
95         if (NULL != g_handle) {
96                 mas_dependency_initialize func = g_mas_dependency.initialize;
97
98                 if (NULL == func) {
99                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_INITIALIZE);
100                 } else {
101                         try {
102                                 ret = func(interface, &dependency_version);
103                         } catch (const std::exception& e) {
104                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
105                                         MAS_DEPENDENCY_FUNC_INITIALIZE, e.what());
106                         }
107                         if (0 != ret) {
108                                 MAS_LOGE("[ERROR] Fail to initialize, ret(%d)", ret);
109                         }
110                 }
111         } else {
112                 MAS_LOGE("[ERROR] g_handle is not valid");
113         }
114         MAS_LOGD("g_handle : %p, dependency_version %d", g_handle, dependency_version);
115         return ret;
116 }
117
118 int dependency_resolver_deinitialize(void)
119 {
120         MAS_LOGD("g_handle : %p", g_handle);
121         int ret = -1;
122         if (NULL != g_handle) {
123                 mas_dependency_deinitialize func = g_mas_dependency.deinitialize;
124                 if (NULL == func) {
125                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_DEINITIALIZE);
126                 } else {
127                         try {
128                                 ret = func();
129                         } catch (const std::exception& e) {
130                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
131                                         MAS_DEPENDENCY_FUNC_DEINITIALIZE, e.what());
132                         }
133                         if (0 != ret) {
134                                 MAS_LOGE("[ERROR] Fail to deinitialize, ret(%d)", ret);
135                         }
136                 }
137
138                 dlclose(g_handle);
139                 g_handle = NULL;
140         } else {
141                 MAS_LOGE("[ERROR] g_handle is not valid");
142         }
143
144         return ret;
145 }
146
147 int dependency_resolver_set_error_callback(mas_error_cb callback, void* user_data)
148 {
149         int ret = -1;
150         if (NULL != g_handle) {
151                 mas_dependency_set_error_callback func = g_mas_dependency.set_error_callback;
152                 if (NULL == func) {
153                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_SET_ERROR_CALLBACK);
154                 } else {
155                         try {
156                                 ret = func(callback, user_data);
157                         } catch (const std::exception& e) {
158                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
159                                         MAS_DEPENDENCY_FUNC_SET_ERROR_CALLBACK, e.what());
160                         }
161                         if (0 != ret) {
162                                 MAS_LOGE("[ERROR] Fail to set error callback(%p, %p), ret(%d)", callback, user_data, ret);
163                         }
164                 }
165         } else {
166                 MAS_LOGE("[ERROR] g_handle is not valid");
167         }
168         return ret;
169 }
170
171 int dependency_resolver_start_recording(void)
172 {
173         int ret = -1;
174         MAS_LOGD("g_handle : %p", g_handle);
175         if (NULL != g_handle) {
176                 mas_dependency_start_recording func = g_mas_dependency.start_recording;
177                 if (NULL == func) {
178                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_START_RECORDING);
179                 } else {
180                         try {
181                                 ret = func();
182                         } catch (const std::exception& e) {
183                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
184                                         MAS_DEPENDENCY_FUNC_START_RECORDING, e.what());
185                         }
186                         if (0 != ret) {
187                                 MAS_LOGE("[ERROR] Fail to start recording, ret(%d)", ret);
188                         }
189                 }
190         } else {
191                 MAS_LOGE("[ERROR] g_handle is not valid");
192         }
193
194         return ret;
195 }
196
197 int dependency_resolver_stop_recording(void)
198 {
199         int ret = -1;
200         if (NULL != g_handle) {
201                 mas_dependency_stop_recording func = g_mas_dependency.stop_recording;
202                 if (NULL == func) {
203                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_STOP_RECORDING);
204                 } else {
205                         try {
206                                 ret = func();
207                         } catch (const std::exception& e) {
208                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
209                                         MAS_DEPENDENCY_FUNC_STOP_RECORDING, e.what());
210                         }
211                         if (0 != ret) {
212                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
213                         }
214                 }
215         } else {
216                 MAS_LOGE("[ERROR] g_handle is not valid");
217         }
218
219         return ret;
220 }
221
222 int dependency_resolver_set_recording_session(unsigned int session)
223 {
224         int ret = -1;
225         MAS_LOGD("g_handle : %p", g_handle);
226         if (NULL != g_handle) {
227                 mas_dependency_set_recording_session func = g_mas_dependency.set_recording_session;
228                 if (NULL == func) {
229                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_SET_RECORDING_SESSION);
230                 } else {
231                         try {
232                                 ret = func(session);
233                         } catch (const std::exception& e) {
234                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
235                                         MAS_DEPENDENCY_FUNC_SET_RECORDING_SESSION, e.what());
236                         }
237                         if (0 != ret) {
238                                 MAS_LOGE("[ERROR] Fail to set recording session, ret(%d)", ret);
239                         }
240                 }
241         } else {
242                 MAS_LOGE("[ERROR] g_handle is not valid");
243         }
244
245         return ret;
246 }
247
248 int dependency_resolver_set_background_volume(double ratio)
249 {
250         int ret = -1;
251         if (NULL != g_handle) {
252                 mas_dependency_set_background_volume func = g_mas_dependency.set_background_volume;
253                 if (NULL == func) {
254                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_SET_BACKGROUND_VOLUME);
255                 } else {
256                         try {
257                                 ret = func(ratio);
258                         } catch (const std::exception& e) {
259                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
260                                         MAS_DEPENDENCY_FUNC_SET_BACKGROUND_VOLUME, e.what());
261                         }
262                         if (0 != ret) {
263                                 MAS_LOGE("[ERROR] Fail to set background volume to %f, ret(%d)", ratio, ret);
264                         }
265                 }
266         } else {
267                 MAS_LOGE("[ERROR] g_handle is not valid");
268         }
269
270         return ret;
271 }
272
273 int dependency_resolver_get_audio_format(int* rate, int* channel, int* audio_type)
274 {
275         int ret = -1;
276         if (NULL != g_handle) {
277                 mas_dependency_get_audio_format func = g_mas_dependency.get_audio_format;
278                 if (NULL == func) {
279                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_GET_AUDIO_FORMAT);
280                 } else {
281                         try {
282                                 ret = func(rate, channel, audio_type);
283                         } catch (const std::exception& e) {
284                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
285                                         MAS_DEPENDENCY_FUNC_GET_AUDIO_FORMAT, e.what());
286                         }
287                         if (0 != ret) {
288                                 MAS_LOGE("[ERROR] Fail to get audio format, ret(%d)", ret);
289                         }
290                 }
291         } else {
292                 MAS_LOGE("[ERROR] g_handle is not valid");
293         }
294
295         return ret;
296 }
297
298 int dependency_resolver_get_audio_source_type(char** type)
299 {
300         int ret = -1;
301         if (NULL != g_handle) {
302                 mas_dependency_get_audio_source_type func = g_mas_dependency.get_audio_source_type;
303                 if (NULL == func) {
304                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_GET_AUDIO_SOURCE_TYPE);
305                 } else {
306                         try {
307                                 ret = func(type);
308                         } catch (const std::exception& e) {
309                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
310                                         MAS_DEPENDENCY_FUNC_GET_AUDIO_SOURCE_TYPE, e.what());
311                         }
312                         if (0 != ret) {
313                                 MAS_LOGE("[ERROR] Fail to get audio source type, ret(%d)", ret);
314                         }
315                 }
316         } else {
317                 MAS_LOGE("[ERROR] g_handle is not valid");
318         }
319
320         return ret;
321 }
322
323 int dependency_resolver_process_wakeup_engine_command(const char* engine, const char* command)
324 {
325         int ret = -1;
326         if (NULL != g_handle) {
327                 mas_dependency_process_wakeup_engine_command func = g_mas_dependency.process_wakeup_engine_command;
328                 if (NULL == func) {
329                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND);
330                 } else {
331                         try {
332                                 ret = func(engine, command);
333                         } catch (const std::exception& e) {
334                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
335                                         MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND, e.what());
336                         }
337                         if (0 != ret) {
338                                 MAS_LOGE("[ERROR] Fail to process wakeup engine command, ret(%d)", ret);
339                         }
340                 }
341         } else {
342                 MAS_LOGE("[ERROR] g_handle is not valid");
343         }
344
345         return ret;
346 }