Add support for dynamic dedicated wakeup engine loading
authorJi-hoon Lee <dalton.lee@samsung.com>
Mon, 25 Mar 2019 08:15:33 +0000 (17:15 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Tue, 26 Mar 2019 07:26:24 +0000 (16:26 +0900)
Change-Id: Iea927f95db769cabfa84ee81a263e8f21b200fb3

plugins/wakeup-manager/inc/wakeup_manager.h
plugins/wakeup-manager/src/wakeup_manager.cpp

index c5093dd..962f8b0 100644 (file)
@@ -135,6 +135,8 @@ EXPORT_API int wakeup_manager_set_error_callback(wakeup_service_error_cb callbac
  *** Definitions for wakeup engine interface
  *************************************************************************************/
 #define MA_WAKEUP_ENGINE_PATH                                  tzplatform_mkpath(tzplatform_getid("TZ_SYS_RO_SHARE"), "multiassistant/engines")
+/* Need to check whether this dedicated engine path also needs to be configurable */
+#define MA_WAKEUP_DEDICATED_ENGINE_PATH                        "shared/lib/libwakeup-engine.so"
 
 typedef enum {
        MA_PLUGIN_EVENT_VOICE_KEY_PRESSED = 0,
index 7c01ee6..e37305d 100644 (file)
@@ -23,6 +23,8 @@
 #include <memory>
 #include <algorithm>
 
+#include <pkgmgr-info.h>
+
 #include "wakeup_manager_main.h"
 #include "wakeup_manager.h"
 #include "wakeup_settings.h"
@@ -267,6 +269,7 @@ static void wakeup_engine_audio_data_require_status_cb(bool require, void* user_
                        audio_data_require_count++;
                }
        }
+       MWR_LOGD("audio_data_require_count : %d", audio_data_require_count);
        if (audio_data_require_count > 0) {
                g_audio_data_required = true;
                if (g_audio_data_required != prev_audio_data_required &&
@@ -283,6 +286,110 @@ static void wakeup_engine_audio_data_require_status_cb(bool require, void* user_
        // UNLOCK REQUIRED
 }
 
+static void wakeup_engine_add_library(const char* name, const char* path)
+{
+       if (g_engine_count >= MAX_WAKEUP_ENGINE_NUM) return;
+       if (nullptr == name || nullptr == path) return;
+
+       MWR_LOGD("Name (%s), Filepath(%s)", name, path);
+       int index = g_engine_count;
+
+       char* error = NULL;
+       void* handle = dlopen(path, RTLD_LAZY);
+       if (NULL != (error = dlerror()) || NULL == handle) {
+               MWR_LOGD("[ERROR] Fail to dlopen(%s), error(%s)", path, error);
+       } else {
+               g_wakeup_engine_info[index].interface.initialize =
+                       (wakeup_engine_initialize)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_INITIALIZE);
+               g_wakeup_engine_info[index].interface.deinitialize =
+                       (wakeup_engine_deinitialize)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_DEINITIALIZE);
+               g_wakeup_engine_info[index].interface.activate =
+                       (wakeup_engine_activate)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_ACTIVATE);
+               g_wakeup_engine_info[index].interface.deactivate =
+                       (wakeup_engine_deactivate)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_DEACTIVATE);
+               g_wakeup_engine_info[index].interface.add_wakeup_word =
+                       (wakeup_engine_add_wakeup_word)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_ADD_WAKEUP_WORD);
+               g_wakeup_engine_info[index].interface.add_language =
+                       (wakeup_engine_add_language)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_ADD_LANGUAGE);
+               g_wakeup_engine_info[index].interface.set_language =
+                       (wakeup_engine_set_language)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_SET_LANGUAGE);
+               g_wakeup_engine_info[index].interface.update_manager_state =
+                       (wakeup_engine_update_manager_state)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_UPDATE_MANAGER_STATE);
+               g_wakeup_engine_info[index].interface.set_audio_format =
+                       (wakeup_engine_set_audio_format)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_FORMAT);
+               g_wakeup_engine_info[index].interface.get_audio_format =
+                       (wakeup_engine_get_audio_format)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_GET_AUDIO_FORMAT);
+               g_wakeup_engine_info[index].interface.feed_audio_data =
+                       (wakeup_engine_feed_audio_data)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_FEED_AUDIO_DATA);
+               g_wakeup_engine_info[index].interface.get_utterance_data_count =
+                       (wakeup_engine_get_utterance_data_count)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA_COUNT);
+               g_wakeup_engine_info[index].interface.get_utterance_data =
+                       (wakeup_engine_get_utterance_data)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA);
+               g_wakeup_engine_info[index].interface.set_assistant_specific_command =
+                       (wakeup_engine_set_assistant_specific_command)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND);
+               g_wakeup_engine_info[index].interface.set_wakeup_event_callback =
+                       (wakeup_engine_set_wakeup_event_callback)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_EVENT_CALLBACK);
+               g_wakeup_engine_info[index].interface.set_speech_status_callback =
+                       (wakeup_engine_set_speech_status_callback)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_SET_SPEECH_STATUS_CALLBACK);
+               g_wakeup_engine_info[index].interface.set_error_callback =
+                       (wakeup_engine_set_error_callback)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_SET_ERROR_CALLBACK);
+               g_wakeup_engine_info[index].interface.set_audio_data_require_status_callback =
+                       (wakeup_engine_set_audio_data_require_status_callback)dlsym(handle,
+                       MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_DATA_REQUIRE_STATUS_CALLBACK);
+       }
+       strncpy(g_wakeup_engine_info[index].engine_path, path, _POSIX_PATH_MAX);
+       strncpy(g_wakeup_engine_info[index].engine_name, name, _POSIX_PATH_MAX);
+
+       g_wakeup_engine_info[index].active = false;
+       /* We'll need to check vconf for enabled wakeup engines */
+       g_wakeup_engine_info[index].enabled = true;
+       g_wakeup_engine_info[index].audio_data_require_status = false;
+
+       /* All the necessary information has already been set properly */
+       ++g_engine_count;
+
+       MWR_LOGD("Initializing wakeup engine : %s %p",
+               g_wakeup_engine_info[index].engine_path,
+               g_wakeup_engine_info[index].interface.initialize);
+
+       if (g_wakeup_engine_info[index].interface.initialize) {
+               g_wakeup_engine_info[index].interface.initialize();
+       }
+       if (g_wakeup_engine_info[index].interface.set_wakeup_event_callback) {
+               g_wakeup_engine_info[index].interface.set_wakeup_event_callback(
+                       wakeup_engine_wakeup_event_cb, g_wakeup_engine_info[index].engine_name);
+       }
+       if (g_wakeup_engine_info[index].interface.set_speech_status_callback) {
+               g_wakeup_engine_info[index].interface.set_speech_status_callback(
+                       wakeup_engine_speech_status_cb, g_wakeup_engine_info[index].engine_name);
+       }
+       if (g_wakeup_engine_info[index].interface.set_error_callback) {
+               g_wakeup_engine_info[index].interface.set_error_callback(
+                       wakeup_engine_error_cb, g_wakeup_engine_info[index].engine_name);
+       }
+       if (g_wakeup_engine_info[index].interface.set_audio_data_require_status_callback) {
+               g_wakeup_engine_info[index].interface.set_audio_data_require_status_callback(
+                       wakeup_engine_audio_data_require_status_cb, g_wakeup_engine_info[index].engine_name);
+       }
+}
+
 static void wakeup_engine_add_directory(const char* name, const char* path)
 {
        if (NULL == path) return;
@@ -315,71 +422,7 @@ static void wakeup_engine_add_directory(const char* name, const char* path)
                                memset(filepath, '\0', _POSIX_PATH_MAX);
                                snprintf(filepath, _POSIX_PATH_MAX, "%s/%s", path, dirp->d_name);
 
-                               MWR_LOGD("Name (%s), Filepath(%s)", name, filepath);
-                               strncpy(g_wakeup_engine_info[g_engine_count].engine_path, filepath, _POSIX_PATH_MAX);
-                               strncpy(g_wakeup_engine_info[g_engine_count].engine_name, name, _POSIX_PATH_MAX);
-
-                               char* error = NULL;
-                               void* handle = dlopen(filepath, RTLD_LAZY);
-                               if (NULL != (error = dlerror()) || NULL == handle) {
-                                       MWR_LOGD("[ERROR] Fail to dlopen(%s), error(%s)", filepath, error);
-                               } else {
-                                       g_wakeup_engine_info[g_engine_count].interface.initialize =
-                                               (wakeup_engine_initialize)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_INITIALIZE);
-                                       g_wakeup_engine_info[g_engine_count].interface.deinitialize =
-                                               (wakeup_engine_deinitialize)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_DEINITIALIZE);
-                                       g_wakeup_engine_info[g_engine_count].interface.activate =
-                                               (wakeup_engine_activate)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_ACTIVATE);
-                                       g_wakeup_engine_info[g_engine_count].interface.deactivate =
-                                               (wakeup_engine_deactivate)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_DEACTIVATE);
-                                       g_wakeup_engine_info[g_engine_count].interface.add_wakeup_word =
-                                               (wakeup_engine_add_wakeup_word)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_ADD_WAKEUP_WORD);
-                                       g_wakeup_engine_info[g_engine_count].interface.add_language =
-                                               (wakeup_engine_add_language)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_ADD_LANGUAGE);
-                                       g_wakeup_engine_info[g_engine_count].interface.set_language =
-                                               (wakeup_engine_set_language)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_SET_LANGUAGE);
-                                       g_wakeup_engine_info[g_engine_count].interface.update_manager_state =
-                                               (wakeup_engine_update_manager_state)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_UPDATE_MANAGER_STATE);
-                                       g_wakeup_engine_info[g_engine_count].interface.set_audio_format =
-                                               (wakeup_engine_set_audio_format)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_FORMAT);
-                                       g_wakeup_engine_info[g_engine_count].interface.get_audio_format =
-                                               (wakeup_engine_get_audio_format)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_GET_AUDIO_FORMAT);
-                                       g_wakeup_engine_info[g_engine_count].interface.feed_audio_data =
-                                               (wakeup_engine_feed_audio_data)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_FEED_AUDIO_DATA);
-                                       g_wakeup_engine_info[g_engine_count].interface.get_utterance_data_count =
-                                               (wakeup_engine_get_utterance_data_count)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA_COUNT);
-                                       g_wakeup_engine_info[g_engine_count].interface.get_utterance_data =
-                                               (wakeup_engine_get_utterance_data)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA);
-                                       g_wakeup_engine_info[g_engine_count].interface.set_assistant_specific_command =
-                                               (wakeup_engine_set_assistant_specific_command)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND);
-                                       g_wakeup_engine_info[g_engine_count].interface.set_wakeup_event_callback =
-                                               (wakeup_engine_set_wakeup_event_callback)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_EVENT_CALLBACK);
-                                       g_wakeup_engine_info[g_engine_count].interface.set_speech_status_callback =
-                                               (wakeup_engine_set_speech_status_callback)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_SET_SPEECH_STATUS_CALLBACK);
-                                       g_wakeup_engine_info[g_engine_count].interface.set_error_callback =
-                                               (wakeup_engine_set_error_callback)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_SET_ERROR_CALLBACK);
-                                       g_wakeup_engine_info[g_engine_count].interface.set_audio_data_require_status_callback =
-                                               (wakeup_engine_set_audio_data_require_status_callback)dlsym(handle,
-                                               MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_DATA_REQUIRE_STATUS_CALLBACK);
-                               }
-                               ++g_engine_count;
+                               wakeup_engine_add_library(name, filepath);
                        }
                } while (NULL != dirp && g_engine_count < MAX_WAKEUP_ENGINE_NUM);
 
@@ -423,33 +466,6 @@ static int wakeup_engine_info_initialize()
                closedir(dp);
        }
 
-       for (int loop = 0;loop < g_engine_count;loop++) {
-               g_wakeup_engine_info[loop].active = false;
-               /* We'll need to check vconf for enabled wakeup engines */
-               g_wakeup_engine_info[loop].enabled = true;
-               g_wakeup_engine_info[loop].audio_data_require_status = false;
-
-               MWR_LOGD("Initializing wakeup engine : %s %p", g_wakeup_engine_info[loop].engine_path, g_wakeup_engine_info[loop].interface.initialize);
-               if (g_wakeup_engine_info[loop].interface.initialize) {
-                       g_wakeup_engine_info[loop].interface.initialize();
-               }
-               if (g_wakeup_engine_info[loop].interface.set_wakeup_event_callback) {
-                       g_wakeup_engine_info[loop].interface.set_wakeup_event_callback(
-                               wakeup_engine_wakeup_event_cb, g_wakeup_engine_info[loop].engine_name);
-               }
-               if (g_wakeup_engine_info[loop].interface.set_speech_status_callback) {
-                       g_wakeup_engine_info[loop].interface.set_speech_status_callback(
-                               wakeup_engine_speech_status_cb, g_wakeup_engine_info[loop].engine_name);
-               }
-               if (g_wakeup_engine_info[loop].interface.set_error_callback) {
-                       g_wakeup_engine_info[loop].interface.set_error_callback(
-                               wakeup_engine_error_cb, g_wakeup_engine_info[loop].engine_name);
-               }
-               if (g_wakeup_engine_info[loop].interface.set_audio_data_require_status_callback) {
-                       g_wakeup_engine_info[loop].interface.set_audio_data_require_status_callback(
-                               wakeup_engine_audio_data_require_status_cb, g_wakeup_engine_info[loop].engine_name);
-               }
-       }
        return 0;
 }
 
@@ -631,10 +647,35 @@ int wakeup_manager_set_assistant_wakeup_engine(const char* appid, const char* en
        }
 
        MWR_LOGD("[DEBUG] appid(%s), wakeup engine(%s)", appid, engine);
+
+       const int NOT_FOUND = -1;
+       int engine_index = NOT_FOUND;
        for (int loop = 0;loop < g_engine_count;loop++) {
                if (0 == strncmp(g_wakeup_engine_info[loop].engine_name, engine, _POSIX_PATH_MAX)) {
-                       g_wakeup_engine_info[loop].assistant_list.push_back(std::string{appid});
+                       engine_index = loop;
+               }
+       }
+       if (NOT_FOUND == engine_index) {
+               pkgmgrinfo_appinfo_h handle;
+               int ret = pkgmgrinfo_appinfo_get_appinfo(engine, &handle);
+               if (ret == PMINFO_R_OK) {
+                       char *root_path = nullptr;
+                       ret = pkgmgrinfo_appinfo_get_root_path(handle, &root_path);
+                       if (ret == PMINFO_R_OK) {
+                               char path[_POSIX_PATH_MAX] = {'\0'};
+                               snprintf(path, _POSIX_PATH_MAX, "%s/%s", root_path, MA_WAKEUP_DEDICATED_ENGINE_PATH);
+                               wakeup_engine_add_library(engine, path);
+                       }
+                       pkgmgrinfo_appinfo_destroy_appinfo(handle);
                }
+               for (int loop = 0;loop < g_engine_count;loop++) {
+                       if (0 == strncmp(g_wakeup_engine_info[loop].engine_name, engine, _POSIX_PATH_MAX)) {
+                               engine_index = loop;
+                       }
+               }
+       }
+       if (engine_index >= 0 && engine_index < MAX_WAKEUP_ENGINE_NUM && engine_index < g_engine_count) {
+               g_wakeup_engine_info[engine_index].assistant_list.push_back(std::string{appid});
        }
 
        MWR_LOGD("[END]");