Add wakeup policy support in dependency module
[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 mas_dependency_module_interface* dependency_resolver_get_interface()
39 {
40         return &g_mas_dependency;
41 }
42
43 int dependency_resolver_initialize(mas_dependency_plugin_proxy_interface interface)
44 {
45         MAS_LOGD("[Enter]");
46
47         const int FILEPATH_LEN = 512;
48         char filepath[FILEPATH_LEN] = {'\0', };
49         char *vconf_str = vconf_get_str(MAS_DEPENDENCY_MODULE_PATH);
50         if (vconf_str) {
51                 snprintf(filepath, FILEPATH_LEN - 1, "%s", vconf_str);
52                 free(vconf_str);
53         } else {
54                 const char *default_path = MAS_DEPENDENCY_DEFAULT_PATH;
55                 snprintf(filepath, FILEPATH_LEN - 1, "%s/%s", default_path, MAS_DEPENDENCY_DEFAULT_FILENAME);
56         }
57         filepath[FILEPATH_LEN - 1] = '\0';
58
59         char *error;
60         g_handle = NULL;
61         g_handle = dlopen(filepath, RTLD_LAZY);
62         if (NULL != (error = dlerror())) {
63                 MAS_LOGE("[ERROR] Fail to dlopen(%s), error(%s)", filepath, error);
64                 return -1; //MAS_ERROR_OPERATION_FAILED;
65         }
66
67         g_mas_dependency.initialize =
68                 (mas_dependency_initialize)dlsym(g_handle,
69                 MAS_DEPENDENCY_FUNC_INITIALIZE);
70         g_mas_dependency.deinitialize =
71                 (mas_dependency_deinitialize)dlsym(g_handle,
72                 MAS_DEPENDENCY_FUNC_DEINITIALIZE);
73         g_mas_dependency.set_error_callback =
74                 (mas_dependency_set_error_callback)dlsym(g_handle,
75                 MAS_DEPENDENCY_FUNC_SET_ERROR_CALLBACK);
76         g_mas_dependency.start_recording =
77                 (mas_dependency_start_recording)dlsym(g_handle,
78                 MAS_DEPENDENCY_FUNC_START_RECORDING);
79         g_mas_dependency.stop_recording =
80                 (mas_dependency_stop_recording)dlsym(g_handle,
81                 MAS_DEPENDENCY_FUNC_STOP_RECORDING);
82         g_mas_dependency.set_recording_session =
83                 (mas_dependency_set_recording_session)dlsym(g_handle,
84                 MAS_DEPENDENCY_FUNC_SET_RECORDING_SESSION);
85         g_mas_dependency.set_background_volume =
86                 (mas_dependency_set_background_volume)dlsym(g_handle,
87                 MAS_DEPENDENCY_FUNC_SET_BACKGROUND_VOLUME);
88         g_mas_dependency.get_audio_format =
89                 (mas_dependency_get_audio_format)dlsym(g_handle,
90                 MAS_DEPENDENCY_FUNC_GET_AUDIO_FORMAT);
91         g_mas_dependency.get_audio_source_type =
92                 (mas_dependency_get_audio_source_type)dlsym(g_handle,
93                 MAS_DEPENDENCY_FUNC_GET_AUDIO_SOURCE_TYPE);
94         g_mas_dependency.process_wakeup_engine_command =
95                 (mas_dependency_process_wakeup_engine_command)dlsym(g_handle,
96                 MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND);
97         g_mas_dependency.process_wakeup_candidate =
98                 (mas_dependency_process_wakeup_candidate)dlsym(g_handle,
99                 MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_CANDIDATE);
100
101         int ret = -1;
102         int dependency_version = 0;
103         if (NULL != g_handle) {
104                 mas_dependency_initialize func = g_mas_dependency.initialize;
105
106                 if (NULL == func) {
107                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_INITIALIZE);
108                 } else {
109                         try {
110                                 ret = func(interface, &dependency_version);
111                         } catch (const std::exception& e) {
112                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
113                                         MAS_DEPENDENCY_FUNC_INITIALIZE, e.what());
114                         }
115                         if (0 != ret) {
116                                 MAS_LOGE("[ERROR] Fail to initialize, ret(%d)", ret);
117                         }
118                 }
119         } else {
120                 MAS_LOGE("[ERROR] g_handle is not valid");
121         }
122         MAS_LOGD("g_handle : %p, dependency_version %d", g_handle, dependency_version);
123         return ret;
124 }
125
126 int dependency_resolver_deinitialize(void)
127 {
128         MAS_LOGD("g_handle : %p", g_handle);
129         int ret = -1;
130         if (NULL != g_handle) {
131                 mas_dependency_deinitialize func = g_mas_dependency.deinitialize;
132                 if (NULL == func) {
133                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_DEINITIALIZE);
134                 } else {
135                         try {
136                                 ret = func();
137                         } catch (const std::exception& e) {
138                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
139                                         MAS_DEPENDENCY_FUNC_DEINITIALIZE, e.what());
140                         }
141                         if (0 != ret) {
142                                 MAS_LOGE("[ERROR] Fail to deinitialize, ret(%d)", ret);
143                         }
144                 }
145
146                 dlclose(g_handle);
147                 g_handle = NULL;
148         } else {
149                 MAS_LOGE("[ERROR] g_handle is not valid");
150         }
151
152         return ret;
153 }
154
155 int dependency_resolver_set_error_callback(mas_error_cb callback, void* user_data)
156 {
157         int ret = -1;
158         if (NULL != g_handle) {
159                 mas_dependency_set_error_callback func = g_mas_dependency.set_error_callback;
160                 if (NULL == func) {
161                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_SET_ERROR_CALLBACK);
162                 } else {
163                         try {
164                                 ret = func(callback, user_data);
165                         } catch (const std::exception& e) {
166                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
167                                         MAS_DEPENDENCY_FUNC_SET_ERROR_CALLBACK, e.what());
168                         }
169                         if (0 != ret) {
170                                 MAS_LOGE("[ERROR] Fail to set error callback(%p, %p), ret(%d)", callback, user_data, ret);
171                         }
172                 }
173         } else {
174                 MAS_LOGE("[ERROR] g_handle is not valid");
175         }
176         return ret;
177 }
178
179 int dependency_resolver_start_recording(void)
180 {
181         int ret = -1;
182         MAS_LOGD("g_handle : %p", g_handle);
183         if (NULL != g_handle) {
184                 mas_dependency_start_recording func = g_mas_dependency.start_recording;
185                 if (NULL == func) {
186                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_START_RECORDING);
187                 } else {
188                         try {
189                                 ret = func();
190                         } catch (const std::exception& e) {
191                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
192                                         MAS_DEPENDENCY_FUNC_START_RECORDING, e.what());
193                         }
194                         if (0 != ret) {
195                                 MAS_LOGE("[ERROR] Fail to start recording, ret(%d)", ret);
196                         }
197                 }
198         } else {
199                 MAS_LOGE("[ERROR] g_handle is not valid");
200         }
201
202         return ret;
203 }
204
205 int dependency_resolver_stop_recording(void)
206 {
207         int ret = -1;
208         if (NULL != g_handle) {
209                 mas_dependency_stop_recording func = g_mas_dependency.stop_recording;
210                 if (NULL == func) {
211                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_STOP_RECORDING);
212                 } else {
213                         try {
214                                 ret = func();
215                         } catch (const std::exception& e) {
216                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
217                                         MAS_DEPENDENCY_FUNC_STOP_RECORDING, e.what());
218                         }
219                         if (0 != ret) {
220                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
221                         }
222                 }
223         } else {
224                 MAS_LOGE("[ERROR] g_handle is not valid");
225         }
226
227         return ret;
228 }
229
230 int dependency_resolver_set_recording_session(unsigned int session)
231 {
232         int ret = -1;
233         MAS_LOGD("g_handle : %p", g_handle);
234         if (NULL != g_handle) {
235                 mas_dependency_set_recording_session func = g_mas_dependency.set_recording_session;
236                 if (NULL == func) {
237                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_SET_RECORDING_SESSION);
238                 } else {
239                         try {
240                                 ret = func(session);
241                         } catch (const std::exception& e) {
242                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
243                                         MAS_DEPENDENCY_FUNC_SET_RECORDING_SESSION, e.what());
244                         }
245                         if (0 != ret) {
246                                 MAS_LOGE("[ERROR] Fail to set recording session, ret(%d)", ret);
247                         }
248                 }
249         } else {
250                 MAS_LOGE("[ERROR] g_handle is not valid");
251         }
252
253         return ret;
254 }
255
256 int dependency_resolver_set_background_volume(double ratio)
257 {
258         int ret = -1;
259         if (NULL != g_handle) {
260                 mas_dependency_set_background_volume func = g_mas_dependency.set_background_volume;
261                 if (NULL == func) {
262                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_SET_BACKGROUND_VOLUME);
263                 } else {
264                         try {
265                                 ret = func(ratio);
266                         } catch (const std::exception& e) {
267                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
268                                         MAS_DEPENDENCY_FUNC_SET_BACKGROUND_VOLUME, e.what());
269                         }
270                         if (0 != ret) {
271                                 MAS_LOGE("[ERROR] Fail to set background volume to %f, ret(%d)", ratio, ret);
272                         }
273                 }
274         } else {
275                 MAS_LOGE("[ERROR] g_handle is not valid");
276         }
277
278         return ret;
279 }
280
281 int dependency_resolver_get_audio_format(int* rate, int* channel, int* audio_type)
282 {
283         int ret = -1;
284         if (NULL != g_handle) {
285                 mas_dependency_get_audio_format func = g_mas_dependency.get_audio_format;
286                 if (NULL == func) {
287                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_GET_AUDIO_FORMAT);
288                 } else {
289                         try {
290                                 ret = func(rate, channel, audio_type);
291                         } catch (const std::exception& e) {
292                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
293                                         MAS_DEPENDENCY_FUNC_GET_AUDIO_FORMAT, e.what());
294                         }
295                         if (0 != ret) {
296                                 MAS_LOGE("[ERROR] Fail to get audio format, ret(%d)", ret);
297                         }
298                 }
299         } else {
300                 MAS_LOGE("[ERROR] g_handle is not valid");
301         }
302
303         return ret;
304 }
305
306 int dependency_resolver_get_audio_source_type(char** type)
307 {
308         int ret = -1;
309         if (NULL != g_handle) {
310                 mas_dependency_get_audio_source_type func = g_mas_dependency.get_audio_source_type;
311                 if (NULL == func) {
312                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_GET_AUDIO_SOURCE_TYPE);
313                 } else {
314                         try {
315                                 ret = func(type);
316                         } catch (const std::exception& e) {
317                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
318                                         MAS_DEPENDENCY_FUNC_GET_AUDIO_SOURCE_TYPE, e.what());
319                         }
320                         if (0 != ret) {
321                                 MAS_LOGE("[ERROR] Fail to get audio source type, ret(%d)", ret);
322                         }
323                 }
324         } else {
325                 MAS_LOGE("[ERROR] g_handle is not valid");
326         }
327
328         return ret;
329 }
330
331 int dependency_resolver_process_wakeup_engine_command(const char* engine, const char* command)
332 {
333         int ret = -1;
334         if (NULL != g_handle) {
335                 mas_dependency_process_wakeup_engine_command func = g_mas_dependency.process_wakeup_engine_command;
336                 if (NULL == func) {
337                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND);
338                 } else {
339                         try {
340                                 ret = func(engine, command);
341                         } catch (const std::exception& e) {
342                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
343                                         MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND, e.what());
344                         }
345                         if (0 != ret) {
346                                 MAS_LOGE("[ERROR] Fail to process wakeup engine command, ret(%d)", ret);
347                         }
348                 }
349         } else {
350                 MAS_LOGE("[ERROR] g_handle is not valid");
351         }
352
353         return ret;
354 }
355
356 int dependency_resolver_process_wakeup_candidate(mas_wakeup_event_info* info)
357 {
358         int ret = -1;
359         if (NULL != g_handle) {
360                 mas_dependency_process_wakeup_candidate func = g_mas_dependency.process_wakeup_candidate;
361                 if (NULL == func) {
362                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_CANDIDATE);
363                 } else {
364                         try {
365                                 ret = func(info);
366                         } catch (const std::exception& e) {
367                                 MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
368                                         MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_CANDIDATE, e.what());
369                         }
370                         if (0 != ret) {
371                                 MAS_LOGE("[ERROR] Fail to process wakeup candidate, ret(%d)", ret);
372                         }
373                 }
374         } else {
375                 MAS_LOGE("[ERROR] g_handle is not valid");
376         }
377
378         return ret;
379 }