Add wakeup policy support in dependency module 13/219313/2
authorJi-hoon Lee <dalton.lee@samsung.com>
Tue, 3 Dec 2019 09:02:24 +0000 (18:02 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Fri, 6 Dec 2019 09:59:50 +0000 (18:59 +0900)
Change-Id: I54d5e54b7a1a45a4eef03a80679b0705925dcc32

plugins/wakeup-manager/CMakeLists.txt
plugins/wakeup-manager/inc/dependency_resolver.h
plugins/wakeup-manager/inc/wakeup_manager_wrapper.h
plugins/wakeup-manager/inc/wakeup_policy.h
plugins/wakeup-manager/inc/wakeup_policy_default.h
plugins/wakeup-manager/inc/wakeup_policy_external.h [new file with mode: 0644]
plugins/wakeup-manager/src/dependency_resolver.cpp
plugins/wakeup-manager/src/wakeup_manager.cpp
plugins/wakeup-manager/src/wakeup_manager_wrapper.cpp
plugins/wakeup-manager/src/wakeup_policy_default.cpp
plugins/wakeup-manager/src/wakeup_policy_external.cpp [new file with mode: 0644]

index 1f804c1..55a8770 100644 (file)
@@ -42,6 +42,7 @@ SET(SRCS
        src/wakeup_settings.cpp
        src/wakeup_policy.cpp
        src/wakeup_policy_default.cpp
+       src/wakeup_policy_external.cpp
        src/wakeup_audio_manager.cpp
        src/wakeup_engine_manager.cpp
        src/wakeup_manager_wrapper.cpp
index d0ab5b7..75d882e 100644 (file)
 extern "C" {
 #endif
 
-int dependency_resolver_initialize(mas_dependency_plugin_proxy_interface interface);
-int dependency_resolver_deinitialize(void);
-int dependency_resolver_set_error_callback(mas_error_cb callback, void* user_data);
-int dependency_resolver_start_recording();
-int dependency_resolver_stop_recording();
-int dependency_resolver_set_recording_session(unsigned int);
-int dependency_resolver_set_background_volume(double ratio);
-int dependency_resolver_get_audio_format(int* rate, int* channel, int* audio_type);
-int dependency_resolver_get_audio_source_type(char** type);
-int dependency_resolver_process_wakeup_engine_command(const char* engine, const char* command);
-
 /**************************************************************************************
  *** Definitions for dependencies
  *************************************************************************************/
@@ -63,6 +52,8 @@ typedef int (*mas_dependency_get_audio_format)(int* rate, int* channel, int* aud
 typedef int (*mas_dependency_get_audio_source_type)(char** type);
 #define MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND "mas_dependency_process_wakeup_engine_command"
 typedef int (*mas_dependency_process_wakeup_engine_command)(const char* engine_name, const char* command);
+#define MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_CANDIDATE "mas_dependency_process_wakeup_candidate"
+typedef int (*mas_dependency_process_wakeup_candidate)(const mas_wakeup_event_info* info);
 
 typedef struct {
        mas_dependency_initialize                                                               initialize;
@@ -75,8 +66,23 @@ typedef struct {
        mas_dependency_get_audio_format                                                 get_audio_format;
        mas_dependency_get_audio_source_type                                    get_audio_source_type;
        mas_dependency_process_wakeup_engine_command                    process_wakeup_engine_command;
+       mas_dependency_process_wakeup_candidate                                 process_wakeup_candidate;
 } mas_dependency_module_interface;
 
+int dependency_resolver_initialize(mas_dependency_plugin_proxy_interface interface);
+int dependency_resolver_deinitialize(void);
+int dependency_resolver_set_error_callback(mas_error_cb callback, void* user_data);
+int dependency_resolver_start_recording();
+int dependency_resolver_stop_recording();
+int dependency_resolver_set_recording_session(unsigned int);
+int dependency_resolver_set_background_volume(double ratio);
+int dependency_resolver_get_audio_format(int* rate, int* channel, int* audio_type);
+int dependency_resolver_get_audio_source_type(char** type);
+int dependency_resolver_process_wakeup_engine_command(const char* engine, const char* command);
+int dependency_resolver_process_wakeup_candidate(mas_wakeup_event_info* info);
+
+mas_dependency_module_interface* dependency_resolver_get_interface();
+
 #ifdef __cplusplus
 }
 #endif
index 533c1bd..92858fb 100644 (file)
@@ -136,6 +136,8 @@ int wakeup_manager_feed_audio_data(mas_speech_streaming_event_e event, void* buf
 
 int wakeup_manager_send_dependency_module_command(const char* engine_name, const char* command);
 
+int wakeup_manager_wakeup_assistant(const mas_wakeup_event_info* wakeup_info);
+
 #ifdef __cplusplus
 }
 #endif
index 09bbd8f..238a27b 100644 (file)
@@ -48,10 +48,13 @@ public:
        CWakeupPolicy(const CWakeupPolicy&) = delete;
        CWakeupPolicy& operator=(const CWakeupPolicy&) = delete;
 
+       virtual bool valid() = 0;
+
        void subscribe(IPolicyEventObserver *observer);
        void unsubscribe(IPolicyEventObserver *observer);
 
        virtual void wakeup_candidate(mas_wakeup_event_info wakeup_info) = 0;
+       virtual void select_candidate(mas_wakeup_event_info wakeup_info) = 0;
 protected:
        unique_ptr<CWakeupPolicyImpl> mImpl;
 };
index f2a3081..d4b36b7 100644 (file)
@@ -34,14 +34,17 @@ using namespace std;
 class CWakeupPolicyDefault : public CWakeupPolicy
 {
 public:
-       CWakeupPolicyDefault();
        CWakeupPolicyDefault(IPolicyEventObserver *observer);
        ~CWakeupPolicyDefault();
 
+       bool valid() override;
+
        void set_assistant_priority(string appid, int priority);
        void set_delay(float seconds);
 
-       void wakeup_candidate(mas_wakeup_event_info wakeup_info);
+       void wakeup_candidate(mas_wakeup_event_info wakeup_info) override;
+       void select_candidate(mas_wakeup_event_info wakeup_info) override;
+
        void timer_expired();
 private:
        typedef struct {
diff --git a/plugins/wakeup-manager/inc/wakeup_policy_external.h b/plugins/wakeup-manager/inc/wakeup_policy_external.h
new file mode 100644 (file)
index 0000000..846330f
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2018  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _WAKEUP_POLICY_EXTERNAL_H_
+#define _WAKEUP_POLICY_EXTERNAL_H_
+
+#include "wakeup_policy.h"
+
+namespace multiassistant
+{
+namespace wakeup
+{
+
+using namespace std;
+
+class CWakeupPolicyExternal : public CWakeupPolicy
+{
+public:
+       CWakeupPolicyExternal(IPolicyEventObserver *observer);
+       ~CWakeupPolicyExternal();
+
+       bool valid() override;
+
+       void wakeup_candidate(mas_wakeup_event_info wakeup_info) override;
+       void select_candidate(mas_wakeup_event_info wakeup_info) override;
+};
+
+} // wakeup
+} // multiassistant
+
+#endif /* _WAKEUP_POLICY_EXTERNAL_H_ */
index e329bda..c2537d6 100644 (file)
 static void *g_handle = NULL;
 static mas_dependency_module_interface g_mas_dependency = { NULL, };
 
+mas_dependency_module_interface* dependency_resolver_get_interface()
+{
+       return &g_mas_dependency;
+}
+
 int dependency_resolver_initialize(mas_dependency_plugin_proxy_interface interface)
 {
        MAS_LOGD("[Enter]");
@@ -89,6 +94,9 @@ int dependency_resolver_initialize(mas_dependency_plugin_proxy_interface interfa
        g_mas_dependency.process_wakeup_engine_command =
                (mas_dependency_process_wakeup_engine_command)dlsym(g_handle,
                MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_ENGINE_COMMAND);
+       g_mas_dependency.process_wakeup_candidate =
+               (mas_dependency_process_wakeup_candidate)dlsym(g_handle,
+               MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_CANDIDATE);
 
        int ret = -1;
        int dependency_version = 0;
@@ -344,3 +352,28 @@ int dependency_resolver_process_wakeup_engine_command(const char* engine, const
 
        return ret;
 }
+
+int dependency_resolver_process_wakeup_candidate(mas_wakeup_event_info* info)
+{
+       int ret = -1;
+       if (NULL != g_handle) {
+               mas_dependency_process_wakeup_candidate func = g_mas_dependency.process_wakeup_candidate;
+               if (NULL == func) {
+                       MAS_LOGE("[ERROR] symbol lookup failed : %s", MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_CANDIDATE);
+               } else {
+                       try {
+                               ret = func(info);
+                       } catch (const std::exception& e) {
+                               MAS_LOGE("[ERROR] %s of dependency module threw exception : %s",
+                                       MAS_DEPENDENCY_FUNC_PROCESS_WAKEUP_CANDIDATE, e.what());
+                       }
+                       if (0 != ret) {
+                               MAS_LOGE("[ERROR] Fail to process wakeup candidate, ret(%d)", ret);
+                       }
+               }
+       } else {
+               MAS_LOGE("[ERROR] g_handle is not valid");
+       }
+
+       return ret;
+}
index a18eef8..352ebbe 100644 (file)
@@ -17,6 +17,7 @@
 #include "wakeup_manager.h"
 #include "wakeup_manager_main.h"
 #include "wakeup_policy_default.h"
+#include "wakeup_policy_external.h"
 #include "dependency_resolver.h"
 
 #include <algorithm>
@@ -73,19 +74,22 @@ CWakeupManager::~CWakeupManager()
 
 void CWakeupManager::initialize_wakeup_policy()
 {
-       mWakeupPolicy.reset(new CWakeupPolicyDefault{&mPolicyEventObserver});
-
-       /* Default Policy specific initialization */
-       CWakeupPolicyDefault *policy =
-               dynamic_cast<CWakeupPolicyDefault*>(mWakeupPolicy.get());
-       if (policy) {
-               int priority = 0;
-
-               policy->set_delay(mWakeupSettings.get_wakeup_policy_delay());
-               MWR_LOGD("Setting Delay : %f", mWakeupSettings.get_wakeup_policy_delay());
-               for (const auto& assistant : mWakeupSettings.get_wakeup_policy_priority()) {
-                       policy->set_assistant_priority(assistant, ++priority);
-                       MWR_LOGD("Setting Priority : %d %s", priority, assistant.c_str());
+       mWakeupPolicy.reset(new CWakeupPolicyExternal{&mPolicyEventObserver});
+       if (nullptr == mWakeupPolicy || !(mWakeupPolicy->valid())) {
+               mWakeupPolicy.reset(new CWakeupPolicyDefault{&mPolicyEventObserver});
+
+               /* Default Policy specific initialization */
+               CWakeupPolicyDefault *policy =
+                       dynamic_cast<CWakeupPolicyDefault*>(mWakeupPolicy.get());
+               if (policy) {
+                       int priority = 0;
+
+                       policy->set_delay(mWakeupSettings.get_wakeup_policy_delay());
+                       MWR_LOGD("Setting Delay : %f", mWakeupSettings.get_wakeup_policy_delay());
+                       for (const auto& assistant : mWakeupSettings.get_wakeup_policy_priority()) {
+                               policy->set_assistant_priority(assistant, ++priority);
+                               MWR_LOGD("Setting Priority : %d %s", priority, assistant.c_str());
+                       }
                }
        }
 }
@@ -106,8 +110,6 @@ bool CWakeupManager::initialize()
        mAudioManager.subscribe(&mAudioEventObserver);
        mAudioManager.initialize();
 
-       initialize_wakeup_policy();
-
        mWakeupEngineManager.subscribe(&mEngineEventObserver);
        mWakeupEngineManager.initialize();
 
@@ -115,9 +117,12 @@ bool CWakeupManager::initialize()
        interface.process_event = wakeup_manager_process_event;
        interface.feed_audio_data = wakeup_manager_feed_audio_data;
        //interface.send_dependency_module_command = wakeup_manager_send_dependency_module_command;
+       interface.wakeup_assistant = wakeup_manager_wakeup_assistant;
 
        dependency_resolver_initialize(interface);
 
+       initialize_wakeup_policy();
+
        MWR_LOGD("[END]");
        return true;
 }
index a5cdc59..2325bcb 100644 (file)
@@ -660,6 +660,13 @@ int wakeup_manager_send_dependency_module_command(const char* engine_name, const
        return 0;
 }
 
+int wakeup_manager_wakeup_assistant(const mas_wakeup_event_info* wakeup_info)
+{
+       if (nullptr == g_wakeup_manager) return -1;
+       CWakeupPolicy* policy = g_wakeup_manager->get_wakeup_policy();
+       if (policy) policy->select_candidate(*wakeup_info);
+}
+
 void CWakeupEventObserver::on_wakeup(mas_wakeup_event_info wakeup_info)
 {
        if (NULL != g_wakeup_event_cb) {
index 1715ddf..7a471fd 100644 (file)
@@ -6,19 +6,11 @@
 #include <Eina.h>
 #include <dlog.h>
 
-#ifndef LOG_TAG
-#define LOG_TAG "WakeupPolicyDefault"
-#endif
-
 namespace multiassistant
 {
 namespace wakeup
 {
 
-CWakeupPolicyDefault::CWakeupPolicyDefault()
-{
-}
-
 CWakeupPolicyDefault::CWakeupPolicyDefault(IPolicyEventObserver *observer)
        : CWakeupPolicy(observer)
 {
@@ -32,6 +24,11 @@ CWakeupPolicyDefault::~CWakeupPolicyDefault()
        }
 }
 
+bool CWakeupPolicyDefault::valid()
+{
+       return true;
+}
+
 void CWakeupPolicyDefault::set_assistant_priority(string appid, int priority)
 {
        PRIORITY_INFO info;
@@ -68,6 +65,11 @@ void CWakeupPolicyDefault::wakeup_candidate(mas_wakeup_event_info wakeup_info)
        }
 }
 
+void CWakeupPolicyDefault::select_candidate(mas_wakeup_event_info wakeup_info)
+{
+       LOGE("Default wakeup policy does not support selecting candidate");
+}
+
 void CWakeupPolicyDefault::timer_expired()
 {
        LOGD("[ENTER]");
diff --git a/plugins/wakeup-manager/src/wakeup_policy_external.cpp b/plugins/wakeup-manager/src/wakeup_policy_external.cpp
new file mode 100644 (file)
index 0000000..2c94a2a
--- /dev/null
@@ -0,0 +1,49 @@
+#include "wakeup_policy_external.h"
+#include "wakeup_policy_impl.h"
+
+#include "dependency_resolver.h"
+
+#include <dlog.h>
+
+#ifndef LOG_TAG
+#define LOG_TAG "WakeupPolicyExternal"
+#endif
+
+namespace multiassistant
+{
+namespace wakeup
+{
+
+CWakeupPolicyExternal::CWakeupPolicyExternal(IPolicyEventObserver *observer)
+       : CWakeupPolicy(observer)
+{
+}
+
+CWakeupPolicyExternal::~CWakeupPolicyExternal()
+{
+}
+
+bool CWakeupPolicyExternal::valid()
+{
+       bool ret = false;
+       mas_dependency_module_interface* interface = dependency_resolver_get_interface();
+       if (interface) {
+               if (interface->process_wakeup_candidate) {
+                       ret = true;
+               }
+       }
+       return ret;
+}
+
+void CWakeupPolicyExternal::wakeup_candidate(mas_wakeup_event_info wakeup_info)
+{
+       dependency_resolver_process_wakeup_candidate(&wakeup_info);
+}
+
+void CWakeupPolicyExternal::select_candidate(mas_wakeup_event_info wakeup_info)
+{
+       if (mImpl) mImpl->wakeup(wakeup_info);
+}
+
+} // wakeup
+} // multiassistant