From 1b3cd8e9bb4a2fe2634c370f5df76d1a7126b7f9 Mon Sep 17 00:00:00 2001 From: Ji-hoon Lee Date: Fri, 6 Dec 2019 19:00:44 +0900 Subject: [PATCH] Keep assistants updated about service state changes Change-Id: Ie7e01bc84b61da61da959e03cbebec16bf14a9fa --- inc/multi_assistant_main.h | 1 + inc/multi_assistant_service_client.h | 4 ++ inc/multi_assistant_service_plugin.h | 3 ++ inc/multi_wakeup_recognizer.h | 3 ++ plugins/wakeup-manager/inc/wakeup_manager.h | 1 + .../wakeup-manager/inc/wakeup_manager_wrapper.h | 5 +++ plugins/wakeup-manager/src/wakeup_manager.cpp | 3 ++ .../wakeup-manager/src/wakeup_manager_wrapper.cpp | 30 +++++++++++++ src/multi_assistant_dbus.c | 51 ++++++++++++++++++++++ src/multi_assistant_dbus.h | 8 ++++ src/multi_assistant_service.c | 32 +++++++++++++- src/multi_assistant_service_plugin.c | 36 ++++++++++++++- 12 files changed, 175 insertions(+), 2 deletions(-) diff --git a/inc/multi_assistant_main.h b/inc/multi_assistant_main.h index 3e2b2c1..8adc1c0 100644 --- a/inc/multi_assistant_main.h +++ b/inc/multi_assistant_main.h @@ -82,6 +82,7 @@ #define MAS_METHOD_AUDIO_STREAMING_DATA_SECTION "mas_method_audio_streaming_data_section" #define MAS_METHOD_SEND_PREPROCESSING_RESULT "mas_method_send_preprocessing_result" #define MAS_METHOD_SEND_WAKEUP_ENGINE_COMMAND "mas_method_send_wakeup_engine_command" +#define MAS_METHOD_SERVICE_STATE_CHANGE "mas_method_service_state_change" #define MAS_UI_METHOD_SEND_ASR_RESULT "mas_ui_method_send_asr_result" #define MAS_UI_METHOD_SEND_RESULT "mas_ui_method_send_result" diff --git a/inc/multi_assistant_service_client.h b/inc/multi_assistant_service_client.h index b3eebf1..110d0d1 100644 --- a/inc/multi_assistant_service_client.h +++ b/inc/multi_assistant_service_client.h @@ -83,6 +83,10 @@ int mas_launch_client_by_wakeup_word(const char *wakeup_word); int mas_prelaunch_default_assistant(); +int mas_set_current_service_state(ma_service_state_e state); + +ma_service_state_e mas_get_current_service_state(); + typedef enum { CLIENT_LAUNCH_MODE_ACTIVATION, CLIENT_LAUNCH_MODE_PRELAUNCH, diff --git a/inc/multi_assistant_service_plugin.h b/inc/multi_assistant_service_plugin.h index 7115f95..cae0038 100644 --- a/inc/multi_assistant_service_plugin.h +++ b/inc/multi_assistant_service_plugin.h @@ -173,6 +173,8 @@ typedef int (*wakeup_manager_set_error_callback)(wakeup_service_error_cb callbac typedef int (*wakeup_manager_set_streaming_section_changed_callback)(wakeup_service_streaming_section_changed_cb callback, void* user_data); #define MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_ENGINE_COMMAND_CALLBACK "wakeup_manager_set_wakeup_engine_command_callback" typedef int (*wakeup_manager_set_wakeup_engine_command_callback)(wakeup_service_wakeup_engine_command_cb callback, void* user_data); +#define MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_SERVICE_STATE_CHANGED_CALLBACK "wakeup_manager_set_wakeup_service_state_changed_callback" +typedef int (*wakeup_manager_set_wakeup_service_state_changed_callback)(wakeup_service_wakeup_service_state_changed_cb callback, void* user_data); typedef struct { wakeup_manager_initialize initialize; @@ -210,6 +212,7 @@ typedef struct { wakeup_manager_set_error_callback set_error_callback; wakeup_manager_set_streaming_section_changed_callback set_streaming_section_changed_callback; wakeup_manager_set_wakeup_engine_command_callback set_wakeup_engine_command_callback; + wakeup_manager_set_wakeup_service_state_changed_callback set_wakeup_service_state_changed_callback; } wakeup_manager_interface; #ifdef __cplusplus diff --git a/inc/multi_wakeup_recognizer.h b/inc/multi_wakeup_recognizer.h index 6e1b26a..cdd8140 100644 --- a/inc/multi_wakeup_recognizer.h +++ b/inc/multi_wakeup_recognizer.h @@ -22,6 +22,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -41,6 +42,8 @@ typedef void (*wakeup_service_streaming_section_changed_cb)(ma_audio_streaming_d typedef void (*wakeup_service_wakeup_engine_command_cb)(mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command, void* user_data); +typedef void (*wakeup_service_wakeup_service_state_changed_cb)(ma_service_state_e state, void* user_data); + #ifdef __cplusplus } #endif diff --git a/plugins/wakeup-manager/inc/wakeup_manager.h b/plugins/wakeup-manager/inc/wakeup_manager.h index 8d36921..85ce732 100644 --- a/plugins/wakeup-manager/inc/wakeup_manager.h +++ b/plugins/wakeup-manager/inc/wakeup_manager.h @@ -50,6 +50,7 @@ public: virtual void on_audio_streaming_data_section(ma_audio_streaming_data_section_e section) = 0; virtual void on_wakeup_engine_command( mas_wakeup_engine_command_target_e target, const char* name, const char* command) = 0; + virtual void on_wakeup_service_state_changed(ma_service_state_e state) = 0; }; class ISettingValueObserver { diff --git a/plugins/wakeup-manager/inc/wakeup_manager_wrapper.h b/plugins/wakeup-manager/inc/wakeup_manager_wrapper.h index 92858fb..54fd533 100644 --- a/plugins/wakeup-manager/inc/wakeup_manager_wrapper.h +++ b/plugins/wakeup-manager/inc/wakeup_manager_wrapper.h @@ -24,6 +24,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -55,6 +56,8 @@ typedef void (*wakeup_service_streaming_section_changed_cb)(ma_audio_streaming_d typedef void (*wakeup_service_engine_command_cb)(mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command, void* user_data); +typedef void (*wakeup_service_state_changed_cb)(ma_service_state_e state, void* user_data); + typedef struct { int plugin_version; bool ui_panel_enabled; @@ -130,6 +133,8 @@ EXPORT_API int wakeup_manager_set_streaming_section_changed_callback(wakeup_serv EXPORT_API int wakeup_manager_set_wakeup_engine_command_callback(wakeup_service_engine_command_cb callback, void* user_data); +EXPORT_API int wakeup_manager_set_wakeup_service_state_changed_callback(wakeup_service_state_changed_cb callback, void* user_data); + /* Internal API declarations for dependency modules */ int wakeup_manager_feed_audio_data(mas_speech_streaming_event_e event, void* buffer, int len); diff --git a/plugins/wakeup-manager/src/wakeup_manager.cpp b/plugins/wakeup-manager/src/wakeup_manager.cpp index 352ebbe..7119251 100644 --- a/plugins/wakeup-manager/src/wakeup_manager.cpp +++ b/plugins/wakeup-manager/src/wakeup_manager.cpp @@ -379,6 +379,9 @@ bool CWakeupManager::change_manager_state(wakeup_manager_state_e state) { mWakeupManagerState = state; mWakeupEngineManager.update_manager_state(state); + for (const auto& observer : mWakeupObservers) { + observer->on_wakeup_service_state_changed((ma_service_state_e)state); + } return true; } diff --git a/plugins/wakeup-manager/src/wakeup_manager_wrapper.cpp b/plugins/wakeup-manager/src/wakeup_manager_wrapper.cpp index 2325bcb..5fe6469 100644 --- a/plugins/wakeup-manager/src/wakeup_manager_wrapper.cpp +++ b/plugins/wakeup-manager/src/wakeup_manager_wrapper.cpp @@ -48,6 +48,9 @@ static void* g_streaming_section_changed_user_data; static wakeup_service_engine_command_cb g_wakeup_engine_command_cb; static void* g_wakeup_engine_command_user_data; +static wakeup_service_state_changed_cb g_wakeup_service_state_changed_cb; +static void *g_wakeup_service_state_changed_user_data; + class CWakeupEventObserver : public IWakeupEventObserver { void on_wakeup(mas_wakeup_event_info wakeup_info) override; @@ -56,6 +59,7 @@ class CWakeupEventObserver : public IWakeupEventObserver void on_audio_streaming_data_section(ma_audio_streaming_data_section_e section) override; void on_wakeup_engine_command( mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command) override; + void on_wakeup_service_state_changed(ma_service_state_e state) override; }; class CSettingValueObserver : public ISettingValueObserver @@ -110,6 +114,9 @@ int wakeup_manager_initialize(void) g_wakeup_engine_command_cb = NULL; g_wakeup_engine_command_user_data = NULL; + g_wakeup_service_state_changed_cb = NULL; + g_wakeup_service_state_changed_user_data = NULL; + g_wakeup_manager->initialize(); MWR_LOGD("[END]"); @@ -644,6 +651,22 @@ int wakeup_manager_set_wakeup_engine_command_callback(wakeup_service_engine_comm return 0; } +int wakeup_manager_set_wakeup_service_state_changed_callback(wakeup_service_state_changed_cb callback, void* user_data) +{ + MWR_LOGD("[ENTER]"); + + if (NULL == callback) { + MWR_LOGE("[ERROR] Input parameter is NULL"); + return -1; + } + + g_wakeup_service_state_changed_cb = callback; + g_wakeup_service_state_changed_user_data = user_data; + + MWR_LOGD("[END]"); + return 0; +} + int wakeup_manager_feed_audio_data(mas_speech_streaming_event_e event, void* buffer, int len) { if (nullptr == g_wakeup_manager) return -1; @@ -706,6 +729,13 @@ void CWakeupEventObserver::on_wakeup_engine_command( } } +void CWakeupEventObserver::on_wakeup_service_state_changed(ma_service_state_e state) +{ + if (g_wakeup_service_state_changed_cb) { + g_wakeup_service_state_changed_cb(state, g_wakeup_service_state_changed_user_data); + } +} + void CSettingValueObserver::on_value_changed() { if (g_setting_changed_cb) { diff --git a/src/multi_assistant_dbus.c b/src/multi_assistant_dbus.c index 1fb2ed4..664a111 100644 --- a/src/multi_assistant_dbus.c +++ b/src/multi_assistant_dbus.c @@ -512,6 +512,57 @@ int masc_dbus_send_wakeup_engine_command(int pid, const char* command) return 0; } +int masc_dbus_service_state_change(int pid, int state) +{ + if (0 != __dbus_check()) { + return -1; //MAS_ERROR_OPERATION_FAILED; + } + + DBusMessage* msg; + + DBusError err; + dbus_error_init(&err); + + char service_name[64]; + memset(service_name, '\0', 64); + snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid); + + msg = dbus_message_new_method_call( + service_name, + MA_CLIENT_SERVICE_OBJECT_PATH, + MA_CLIENT_SERVICE_INTERFACE, + MAS_METHOD_SERVICE_STATE_CHANGE); + + static int count = 0; + if (NULL == msg) { + MAS_LOGE(">>>> Request mas send service state message : Fail to make message"); + return -1; // MAS_ERROR_OPERATION_FAILED; + } else { + MAS_LOGD(">>>> Request mas send service state message : %s", service_name); + } + + if (true != dbus_message_append_args(msg, + DBUS_TYPE_INT32, &state, + DBUS_TYPE_INVALID)) { + dbus_message_unref(msg); + MAS_LOGE("[ERROR] Fail to append args"); + return -1; + } + + dbus_message_set_no_reply(msg, TRUE); + + if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) { + MAS_LOGE("[Dbus ERROR] Fail to Send"); + return -1; // MAS_ERROR_OPERATION_FAILED; + } else { + MAS_LOGD("[Dbus DEBUG] Success to Send activate message : %d", state); + dbus_connection_flush(g_conn_sender); + } + + dbus_message_unref(msg); + return 0; +} + int masc_ui_dbus_send_hello(void) { if (0 != __dbus_check()) { diff --git a/src/multi_assistant_dbus.h b/src/multi_assistant_dbus.h index 9a8790c..4c38cd2 100644 --- a/src/multi_assistant_dbus.h +++ b/src/multi_assistant_dbus.h @@ -35,6 +35,14 @@ int masc_dbus_active_state_change(int pid, int state); int masc_dbus_send_preprocessing_information(int pid, const char* app_id); +int masc_dbus_send_streaming_section_changed(int pid, int section); + +int masc_dbus_send_preprocessing_result(int pid, bool result); + +int masc_dbus_send_wakeup_engine_command(int pid, const char* command); + +int masc_dbus_service_state_change(int pid, int state); + int masc_ui_dbus_send_hello(void); int masc_ui_dbus_send_asr_result(int pid, int event, char* asr_result); diff --git a/src/multi_assistant_service.c b/src/multi_assistant_service.c index 0361e22..7faabf5 100644 --- a/src/multi_assistant_service.c +++ b/src/multi_assistant_service.c @@ -75,6 +75,7 @@ static const char *g_wakeup_maclient_appid = NULL; static package_manager_h g_pkgmgr = NULL; static PREPROCESSING_STATE g_current_preprocessing_state = PREPROCESSING_STATE_NONE; +static ma_service_state_e g_current_service_state = MA_SERVICE_STATE_INACTIVE; /* client list */ static GSList* g_client_list = NULL; @@ -259,6 +260,8 @@ int mas_client_initialize(int pid) } else { MAS_LOGD("MA client connected, but its appid does not match with current maclient"); } + + masc_dbus_service_state_change(pid, mas_get_current_service_state()); } else { MAS_LOGE("[ERROR] Fail to retrieve appid"); } @@ -1129,7 +1132,6 @@ int mas_prelaunch_default_assistant() return 0; } - ma_preprocessing_allow_mode_e get_preprocessing_allow_mode(const char* appid) { for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) { @@ -1253,6 +1255,34 @@ int mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event) return 0; } +int mas_set_current_service_state(ma_service_state_e state) +{ + g_current_service_state = state; + + ma_client_s *data = NULL; + + int count = g_slist_length(g_client_list); + int i; + + for (i = 0; i < count; i++) { + data = g_slist_nth_data(g_client_list, i); + + if (NULL != data && -1 != data->pid) { + int ret = masc_dbus_service_state_change(data->pid, state); + if (0 != ret) { + MAS_LOGE("[ERROR] Fail to send wakeup service state change to %d, ret(%d)", + data->pid, ret); + } + } + } + return 0; +} + +ma_service_state_e mas_get_current_service_state() +{ + return g_current_service_state; +} + static int pkg_app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data) { char *appid = NULL; diff --git a/src/multi_assistant_service_plugin.c b/src/multi_assistant_service_plugin.c index aca5001..a486a84 100644 --- a/src/multi_assistant_service_plugin.c +++ b/src/multi_assistant_service_plugin.c @@ -367,6 +367,13 @@ static void __wakeup_engine_command_cb(mas_wakeup_engine_command_target_e target } } +static void __wakeup_service_state_changed_cb(ma_service_state_e state, void* user_data) +{ + MAS_LOGD( "[SUCCESS] __wakeup_service_state_changed_cb is called, state(%d)", state); + + mas_set_current_service_state(state); +} + int multi_assistant_service_plugin_initialize(void) { MAS_LOGD( "[Enter]"); @@ -488,6 +495,9 @@ int multi_assistant_service_plugin_initialize(void) _wakeup_manager_interface.set_wakeup_engine_command_callback = (wakeup_manager_set_wakeup_engine_command_callback)dlsym(g_handle, MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_ENGINE_COMMAND_CALLBACK); + _wakeup_manager_interface.set_wakeup_service_state_changed_callback = + (wakeup_manager_set_wakeup_service_state_changed_callback)dlsym(g_handle, + MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_SERVICE_STATE_CHANGED_CALLBACK); int ret = -1; if (NULL != g_handle) { @@ -1068,6 +1078,12 @@ int multi_assistant_service_plugin_set_callbacks(void) return ret; } + ret = multi_assistant_service_plugin_set_wakeup_service_state_changed_callback(__wakeup_service_state_changed_cb, NULL); + if (0 != ret) { + MAS_LOGE("Fail to set wakeup engine command cb"); + return ret; + } + return 0; } @@ -1230,4 +1246,22 @@ int multi_assistant_service_plugin_set_wakeup_engine_command_callback(wakeup_ser } } return ret; -} \ No newline at end of file +} + +int multi_assistant_service_plugin_set_wakeup_service_state_changed_callback(wakeup_service_wakeup_service_state_changed_cb callback, void* user_data) +{ + int ret = -1; + if (NULL != g_handle) { + wakeup_manager_set_wakeup_service_state_changed_callback func = _wakeup_manager_interface.set_wakeup_service_state_changed_callback; + if (NULL == func) { + MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_SERVICE_STATE_CHANGED_CALLBACK); + } else { + ret = func(callback, user_data); + if (0 != ret) { + MAS_LOGE("[ERROR] Fail to set error callback, ret(%d)", ret); + } + } + } + return ret; +} + -- 2.7.4