2 * Copyright 2020 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
18 #include <service_app.h>
19 #include <app_manager.h>
30 #include "service_common.h"
31 #include "service_main.h"
32 #include "service_plugin.h"
33 #include "service_ipc_dbus.h"
34 #include "service_config.h"
36 static CServiceMain* g_service_main = nullptr;
38 bool CServiceMain::check_preprocessing_assistant_exists()
42 boost::optional<std::string> preprocessing_appid =
43 mPreferenceManager.get_string(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
44 if (preprocessing_appid) {
45 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
46 if (mClientInfo[loop].used &&
47 strncmp((*preprocessing_appid).c_str(), mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
48 if (mClientManager.check_client_validity_by_appid(*preprocessing_appid)) {
55 MAS_LOGD("result : %d", ret);
60 bool CServiceMain::is_current_preprocessing_assistant(const char* appid)
62 if (NULL == appid) return false;
66 boost::optional<std::string> preprocessing_appid =
67 mPreferenceManager.get_string(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
68 if (preprocessing_appid) {
69 if (strncmp((*preprocessing_appid).c_str(), appid, MAX_APPID_LEN) == 0) {
77 int CServiceMain::mas_client_get_audio_format(int pid, int* rate, int* channel, int* audio_type)
79 MAS_LOGD("[Enter] pid(%d)", pid);
81 int ret = mServicePlugin.get_recording_audio_format(rate, channel, audio_type);
83 MAS_LOGE("[ERROR] Fail to get recording audio format, ret(%d)", ret);
89 #define MAX_LOCAL_VARIABLE_STRING_LEN 256
90 int CServiceMain::mas_client_get_audio_source_type(int pid, char** type)
92 MAS_LOGD("[Enter] pid(%d)", pid);
94 if (NULL == type) return -1;
96 static char cached[MAX_LOCAL_VARIABLE_STRING_LEN] = {'\0'};
97 int ret = mServicePlugin.get_recording_audio_source_type(type);
99 MAS_LOGE("[ERROR] Fail to get recording audio source type, ret(%d)", ret);
102 strncpy(cached, *type, MAX_LOCAL_VARIABLE_STRING_LEN - 1);
103 cached[MAX_LOCAL_VARIABLE_STRING_LEN - 1] = '\0';
109 int CServiceMain::mas_client_send_preprocessing_information(int pid)
112 MAS_LOGD("[Enter] pid(%d)", pid);
114 boost::optional<std::string> preprocessing_appid =
115 mPreferenceManager.get_string(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
116 if (preprocessing_appid) {
117 MAS_LOGD("preprocessing_assistant_appid : %s", (*preprocessing_appid).c_str());
118 ret = mServiceIpc.send_preprocessing_information(pid, (*preprocessing_appid).c_str());
124 int CServiceMain::mas_client_send_voice_key_status_change(int pid, ma_voice_key_status_e status)
127 MAS_LOGD("[Enter] pid(%d)", pid);
129 ret = mServiceIpc.change_voice_key_status(pid, status);
131 MAS_LOGE("[ERROR] Fail to send voice key status changed information, ret(%d)", ret);
133 mLastVoiceKeyStatus = status;
138 int CServiceMain::mas_client_send_asr_result(int pid, int event, const char* asr_result)
140 MAS_LOGD("[Enter] pid(%d), event(%d), asr_result(%s)", pid, event, asr_result);
141 int ret = mServiceIpc.masc_ui_dbus_send_asr_result(pid, event, asr_result);
143 MAS_LOGE("[ERROR] Fail to send asr result, ret(%d)", ret);
146 // if final event is , launch assistant app which is invoked with wakeup word.
151 int CServiceMain::mas_client_send_result(int pid, const char* display_text,
152 const char* utterance_text, const char* result_json)
154 MAS_LOGD("[Enter] pid(%d), display_text(%s), utterance_text(%s), result_json(%s)", pid, display_text, utterance_text, result_json);
155 int ret = mServiceIpc.masc_ui_dbus_send_result(pid, display_text, utterance_text, result_json);
157 MAS_LOGE("[ERROR] Fail to send result, ret(%d)", ret);
160 std::string pid_appid;
161 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
163 pid_appid = *appid_by_pid;
165 mServicePlugin.update_recognition_result(pid_appid.c_str(), MA_RECOGNITION_RESULT_EVENT_SUCCESS);
170 int CServiceMain::mas_client_send_recognition_result(int pid, int result)
172 MAS_LOGD("[Enter] pid(%d), result(%d)", pid, result);
173 int ret = mServiceIpc.masc_ui_dbus_send_recognition_result(pid, result);
175 MAS_LOGE("[ERROR] Fail to send recognition result, ret(%d)", ret);
178 std::string pid_appid;
179 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
181 pid_appid = *appid_by_pid;
183 mServicePlugin.update_recognition_result(pid_appid.c_str(), result);
188 int CServiceMain::mas_client_start_streaming_audio_data(int pid, int type)
192 case MA_AUDIO_STREAMING_DATA_TYPE_CURRENT_UTTERANCE:
193 ret = mServicePlugin.start_streaming_utterance_data();
194 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED);
196 case MA_AUDIO_STREAMING_DATA_TYPE_PREVIOUS_UTTERANCE:
197 ret = mServicePlugin.start_streaming_previous_utterance_data();
198 /* Preprocessing is not required for previous utterance streaming */
200 case MA_AUDIO_STREAMING_DATA_TYPE_FOLLOW_UP_SPEECH:
201 ret = mServicePlugin.start_streaming_follow_up_data();
202 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED);
208 int CServiceMain::mas_client_stop_streaming_audio_data(int pid, int type)
212 case MA_AUDIO_STREAMING_DATA_TYPE_CURRENT_UTTERANCE:
213 ret = mServicePlugin.stop_streaming_utterance_data();
215 case MA_AUDIO_STREAMING_DATA_TYPE_PREVIOUS_UTTERANCE:
216 ret = mServicePlugin.stop_streaming_previous_utterance_data();
218 case MA_AUDIO_STREAMING_DATA_TYPE_FOLLOW_UP_SPEECH:
219 ret = mServicePlugin.stop_streaming_follow_up_data();
225 int CServiceMain::mas_client_update_voice_feedback_state(int pid, int state)
227 std::string pid_appid;
228 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
230 pid_appid = *appid_by_pid;
232 mServicePlugin.update_voice_feedback_state(pid_appid.c_str(), state);
236 int CServiceMain::mas_client_set_assistant_specific_command(int pid, const char *command)
238 std::string pid_appid;
239 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
241 pid_appid = *appid_by_pid;
243 mServicePlugin.set_assistant_specific_command(pid_appid.c_str(), command);
247 int CServiceMain::mas_client_set_background_volume(int pid, double ratio)
249 std::string pid_appid;
250 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
252 pid_appid = *appid_by_pid;
254 mServicePlugin.set_background_volume(pid_appid.c_str(), ratio);
258 int CServiceMain::mas_client_set_preprocessing_allow_mode(int pid, ma_preprocessing_allow_mode_e mode, const char* appid)
260 std::string pid_appid;
261 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
263 pid_appid = *appid_by_pid;
266 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
267 if (mClientInfo[loop].used) {
268 if (0 == pid_appid.compare(mClientInfo[loop].appid)) {
269 mClientInfo[loop].preprocessing_allow_mode = mode;
271 strncpy(mClientInfo[loop].preprocessing_allow_appid, appid, MAX_APPID_LEN);
272 mClientInfo[loop].preprocessing_allow_appid[MAX_APPID_LEN - 1] = '\0';
274 mClientInfo[loop].preprocessing_allow_appid[0] = '\0';
280 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED);
285 int CServiceMain::mas_client_send_preprocessing_result(int pid, bool result)
287 std::string pid_appid;
288 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
290 pid_appid = *appid_by_pid;
292 if (!is_current_preprocessing_assistant(pid_appid.c_str())) return -1;
294 const char *current_maclient_appid = NULL;
295 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
296 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
300 MAS_LOGD("Preprocessing succeeded, bring (%s) to foreground", pid_appid.c_str());
301 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED);
303 MAS_LOGD("Preprocessing failed, bring (%s) to foreground", current_maclient_appid);
304 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED);
307 if (current_maclient_appid) {
308 int pid_by_appid = mClientManager.find_client_pid_by_appid(
309 std::string{current_maclient_appid});
310 mServiceIpc.send_preprocessing_result(pid_by_appid, result);
316 int CServiceMain::mas_client_set_wake_word_audio_require_flag(int pid, bool require)
318 std::string pid_appid;
319 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
321 pid_appid = *appid_by_pid;
324 mServicePlugin.set_wake_word_audio_require_flag(pid_appid.c_str(), require);
328 int CServiceMain::mas_client_set_assistant_language(int pid, const char* language)
330 std::string pid_appid;
331 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
333 pid_appid = *appid_by_pid;
336 mServicePlugin.set_assistant_language(pid_appid.c_str(), language);
340 int CServiceMain::mas_client_add_wake_word(int pid, const char* wake_word, const char* language)
342 std::string pid_appid;
343 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
345 pid_appid = *appid_by_pid;
348 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
349 if (mClientInfo[loop].used &&
350 0 == pid_appid.compare(mClientInfo[loop].appid)) {
351 int ret = mServiceConfig.add_custom_wake_word(wake_word, language,
352 mClientInfo[loop].wakeup_word,
353 mClientInfo[loop].wakeup_language);
355 mServiceConfig.save_custom_wake_words(pid_appid.c_str(),
356 mClientInfo[loop].wakeup_word,
357 mClientInfo[loop].wakeup_language);
359 LOGE("add new wake word failed!");
365 mServicePlugin.add_assistant_wakeup_word(pid_appid.c_str(), wake_word, language);
369 int CServiceMain::mas_client_remove_wake_word(int pid, const char* wake_word, const char* language)
371 std::string pid_appid;
372 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
374 pid_appid = *appid_by_pid;
377 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
378 if (mClientInfo[loop].used &&
379 0 == pid_appid.compare(mClientInfo[loop].appid)) {
380 int ret = mServiceConfig.remove_custom_wake_word(wake_word, language,
381 mClientInfo[loop].wakeup_word,
382 mClientInfo[loop].wakeup_language);
384 mServiceConfig.save_custom_wake_words(pid_appid.c_str(),
385 mClientInfo[loop].wakeup_word,
386 mClientInfo[loop].wakeup_language);
391 mServicePlugin.remove_assistant_wakeup_word(pid_appid.c_str(), wake_word, language);
395 int CServiceMain::mas_ui_client_initialize(int pid)
397 MAS_LOGD("[Enter] pid(%d)", pid);
402 int CServiceMain::mas_ui_client_deinitialize(int pid)
404 MAS_LOGD("[Enter] pid(%d)", pid);
409 int CServiceMain::mas_ui_client_change_assistant(const char* appid)
414 MAS_LOGE("NULL parameter");
418 bool use_custom_ui = mas_get_client_custom_ui_option_by_appid(appid);
419 mServiceIpc.masc_ui_dbus_enable_common_ui(!use_custom_ui);
421 mas_set_current_client_by_appid(appid);
422 int pid = mas_get_client_pid_by_appid(appid);
424 mas_bring_client_to_foreground(appid);
425 mas_client_send_preprocessing_information(pid);
426 if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) {
427 mas_client_send_voice_key_status_change(pid, mLastVoiceKeyStatus);
430 mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_ACTIVE);
431 MAS_LOGD("MA Client with appid %s exists, requesting speech data", (appid ? appid : "NULL"));
433 int ret = mServicePlugin.start_streaming_utterance_data();
435 MAS_LOGE("[ERROR] Fail to start streaming utterance data(%d)", ret);
439 // Appropriate MA Client not available, trying to launch new one
440 MAS_LOGD("MA Client with appid %s does not exist, launching client", (appid ? appid : "NULL"));
442 /* The appid parameter might not exist after this function call, so we use appid string in our mClientInfo */
443 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
444 if (mClientInfo[loop].used &&
445 0 < strlen(mClientInfo[loop].appid) &&
446 0 < strlen(mClientInfo[loop].wakeup_word[0])) {
447 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
448 mas_launch_client_by_appid(mClientInfo[loop].appid, CLIENT_LAUNCH_MODE_ACTIVATION);
457 static int mas_assistant_info_cb(ma_assistant_info_s* info, void* user_data) {
459 CServiceMain* service_main = static_cast<CServiceMain*>(user_data);
461 ret = service_main->add_assistant_info(info);
466 int CServiceMain::add_assistant_info(ma_assistant_info_s* info) {
467 MAS_LOGD("__mas_assistant_info_cb called");
470 MAS_LOGE("info NULL, returning");
473 if (NULL == info->app_id) {
474 MAS_LOGE("app_id NULL, returning");
480 while(-1 == index && loop < MAX_MACLIENT_INFO_NUM) {
481 if (false == mClientInfo[loop].used) {
487 mClientInfo[index].used = true;
488 mClientInfo[index].preprocessing_allow_mode = MA_PREPROCESSING_ALLOW_NONE;
489 mClientInfo[index].preprocessing_allow_appid[0] = '\0';
490 MAS_LOGD("app_id(%s)", info->app_id);
491 strncpy(mClientInfo[index].appid, info->app_id, MAX_APPID_LEN);
492 mClientInfo[index].appid[MAX_APPID_LEN - 1] = '\0';
494 if (is_current_preprocessing_assistant(mClientInfo[index].appid)) {
495 mCurrentPreprocessingClientInfo = index;
498 for (loop = 0;loop < MAX_WAKEUP_WORDS_NUM;loop++) {
499 if (loop < info->cnt_wakeup && info->wakeup_list[loop]) {
500 MAS_LOGD("wakeup_list(%d)(%s)(%s)", loop, info->wakeup_list[loop], info->wakeup_language[loop]);
501 strncpy(mClientInfo[index].wakeup_word[loop], info->wakeup_list[loop], MAX_WAKEUP_WORD_LEN);
502 mClientInfo[index].wakeup_word[loop][MAX_WAKEUP_WORD_LEN - 1] = '\0';
503 if (info->wakeup_language[loop]) {
504 strncpy(mClientInfo[index].wakeup_language[loop], info->wakeup_language[loop], MAX_SUPPORTED_LANGUAGE_LEN);
505 mClientInfo[index].wakeup_language[loop][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
507 strncpy(mClientInfo[index].wakeup_language[loop], "", MAX_SUPPORTED_LANGUAGE_LEN);
510 strncpy(mClientInfo[index].wakeup_word[loop], "", MAX_WAKEUP_WORD_LEN);
514 for (loop = 0;loop < MAX_SUPPORTED_LANGUAGES_NUM;loop++) {
515 if (loop < info->cnt_lang && info->supported_lang[loop]) {
516 MAS_LOGD("supported_lang(%d)(%s)", loop, info->supported_lang[loop]);
517 strncpy(mClientInfo[index].supported_language[loop], info->supported_lang[loop], MAX_SUPPORTED_LANGUAGE_LEN);
518 mClientInfo[index].supported_language[loop][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
520 strncpy(mClientInfo[index].supported_language[loop], "", MAX_SUPPORTED_LANGUAGE_LEN);
524 MAS_LOGD("wakeup_engine(%s)", info->wakeup_engine);
525 if (info->wakeup_engine) {
526 strncpy(mClientInfo[index].wakeup_engine, info->wakeup_engine, MAX_APPID_LEN);
527 mClientInfo[index].wakeup_engine[MAX_APPID_LEN - 1] = '\0';
529 mClientInfo[index].wakeup_engine[0] = '\0';
530 MAS_LOGW("Wakeup engine information not provided for : %s", info->app_id);
532 mClientInfo[index].custom_ui_option = info->custom_ui_option;
534 MAS_LOGD("voice_key_support_mode(%d)", info->voice_key_support_mode);
535 mClientInfo[index].voice_key_support_mode = info->voice_key_support_mode;
536 MAS_LOGD("voice_key_tap_duration(%f)", info->voice_key_tap_duration);
537 mClientInfo[index].voice_key_tap_duration = info->voice_key_tap_duration;
539 MAS_LOGD("Couldn't find an empty slot for storing assistant info");
542 MAS_LOGD("__mas_assistant_info_cb end");
547 static void active_state_changed_cb(std::string key, void* user_data)
549 IPreferenceManager* manager = static_cast<IPreferenceManager*>(user_data);
550 if (nullptr == manager) return;
552 boost::optional<bool> activated =
553 manager->get_bool(MULTI_ASSISTANT_SETTINGS_ACTIVATED);
555 MAS_LOGD("multi-assistant active state : %d\n", *activated);
557 CServicePlugin *plugin = nullptr;
558 if (g_service_main) {
559 plugin = g_service_main->get_service_plugin();
565 plugin->deactivate();
568 MAS_LOGE("Could not change plugin state : %p %p", g_service_main, plugin);
573 int CServiceMain::initialize_service_plugin(void)
576 if (0 != mServicePlugin.initialize()) {
577 MAS_LOGE("Fail to ws intialize");
581 if (0 != mServicePlugin.set_language(mCurrentLanguage.c_str())) {
582 MAS_LOGE("Fail to ws set language");
586 memset(&mClientInfo, 0x00, sizeof(mClientInfo));
587 mCurrentClientInfo = -1;
588 mCurrentPreprocessingClientInfo = -1;
589 mWakeupClientAppId.clear();
591 if (0 == mServiceConfig.get_assistant_info(mas_assistant_info_cb, this)) {
592 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
594 if (0 < strlen(mClientInfo[loop].appid)) {
595 mServiceConfig.load_custom_wake_words(mClientInfo[loop].appid,
596 mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language);
597 if (0 < strlen(mClientInfo[loop].wakeup_engine)) {
598 mServicePlugin.set_assistant_wakeup_engine(
599 mClientInfo[loop].appid,
600 mClientInfo[loop].wakeup_engine);
602 for (inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
603 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
604 MAS_LOGD("Registering wakeup word %s for app %s",
605 mClientInfo[loop].wakeup_word[inner_loop], mClientInfo[loop].appid);
606 if (0 != mServicePlugin.add_assistant_wakeup_word(
607 mClientInfo[loop].appid,
608 mClientInfo[loop].wakeup_word[inner_loop],
609 mClientInfo[loop].wakeup_language[inner_loop])) {
610 MAS_LOGE("Fail to add assistant's wakeup word");
614 for (inner_loop = 0; inner_loop < MAX_SUPPORTED_LANGUAGES_NUM; inner_loop++) {
615 if (0 < strlen(mClientInfo[loop].supported_language[inner_loop])) {
616 MAS_LOGD("Adding language %s for app %s",
617 mClientInfo[loop].supported_language[inner_loop], mClientInfo[loop].appid);
618 if (0 != mServicePlugin.add_assistant_language(
619 mClientInfo[loop].appid,
620 mClientInfo[loop].supported_language[inner_loop])) {
621 MAS_LOGE("Fail to add assistant's language");
628 MAS_LOGE("Fail to load assistant info");
631 if (0 != mServicePlugin.set_callbacks()) {
632 MAS_LOGE("Fail to set callbacks");
639 int CServiceMain::deinitialize_service_plugin(void)
642 if (0 != mServicePlugin.deactivate()) {
643 MAS_LOGE("Fail to deactivate");
645 if (0 != mServicePlugin.deinitialize()) {
646 MAS_LOGE("Fail to deinitialize");
652 int CServiceMain::process_activated_setting()
654 if (mPreferenceManager.register_changed_callback(
655 MULTI_ASSISTANT_SETTINGS_ACTIVATED, active_state_changed_cb, &mPreferenceManager)) {
656 /* Activate / deactivate according to the vconf key setting */
657 active_state_changed_cb(std::string{}, &mPreferenceManager);
659 #ifdef ENABLE_MULTI_ASSISTANT_BY_DEFAULT
660 /* Multi-assistant needs to be enabled by default, unless disabled explicitly */
661 mServicePlugin.activate();
662 const char *default_assistant = NULL;
663 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
664 if (NULL == default_assistant) {
665 if (mClientInfo[0].used) {
666 default_assistant = mClientInfo[0].appid;
667 MAS_LOGW("No default assistant, setting %s as default", default_assistant);
668 mServicePlugin.set_default_assistant(default_assistant);
670 MAS_LOGE("No default assistant, and no assistant installed");
679 int CServiceMain::mas_get_current_client_pid()
682 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
683 const char *appid = mClientInfo[mCurrentClientInfo].appid;
685 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
691 int CServiceMain::mas_get_current_preprocessing_client_pid()
694 if (mCurrentPreprocessingClientInfo >= 0 && mCurrentPreprocessingClientInfo < MAX_MACLIENT_INFO_NUM) {
695 const char *appid = mClientInfo[mCurrentPreprocessingClientInfo].appid;
697 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
703 int CServiceMain::mas_get_client_pid_by_appid(const char *appid)
708 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
711 if (-1 != ret && !mApplicationManager.is_application_running(ret)) {
712 MAS_LOGE("The PID for %s was %d, but it seems to be terminated", appid, ret);
713 on_deinitialize(ret);
720 bool CServiceMain::mas_get_client_custom_ui_option_by_appid(const char *appid)
723 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
724 if (mClientInfo[loop].used &&
725 0 < strlen(mClientInfo[loop].appid) &&
726 0 < strlen(mClientInfo[loop].wakeup_word[0])) {
727 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
728 ret = mClientInfo[loop].custom_ui_option;
735 int CServiceMain::mas_get_client_pid_by_wakeup_word(const char *wakeup_word)
737 const char *appid = mas_get_client_appid_by_wakeup_word(wakeup_word);
738 return mas_get_client_pid_by_appid(appid);
741 const char* CServiceMain::mas_get_client_appid_by_wakeup_word(const char *wakeup_word)
744 const char *appid = NULL;
746 if (NULL == wakeup_word) return NULL;
748 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
749 if (mClientInfo[loop].used &&
750 0 < strlen(mClientInfo[loop].appid)) {
751 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
752 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
753 if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
754 appid = mClientInfo[loop].appid;
761 /* Perform extended search, by eliminating blank characters */
763 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
764 if (mClientInfo[loop].used &&
765 0 < strlen(mClientInfo[loop].appid)) {
766 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
767 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
768 char comparand[MAX_WAKEUP_WORD_LEN];
769 int comparand_index = 0;
770 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
771 if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
772 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
775 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
776 appid = mClientInfo[loop].appid;
787 int CServiceMain::mas_set_current_client_by_wakeup_word(const char *wakeup_word)
791 int prev_selection = mCurrentClientInfo;
793 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
794 if (mClientInfo[loop].used &&
795 0 < strlen(mClientInfo[loop].appid)) {
796 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
797 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
798 if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
799 mCurrentClientInfo = loop;
806 /* Perform extended search, by eliminating blank characters */
808 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
809 if (mClientInfo[loop].used &&
810 0 < strlen(mClientInfo[loop].appid)) {
811 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
812 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
813 char comparand[MAX_WAKEUP_WORD_LEN];
814 int comparand_index = 0;
815 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
816 if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
817 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
820 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
821 mCurrentClientInfo = loop;
830 if (mCurrentClientInfo != prev_selection) {
831 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
832 int pid = mas_get_client_pid_by_appid(mClientInfo[prev_selection].appid);
833 mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
840 int CServiceMain::mas_set_current_client_by_appid(const char *appid)
843 int prev_selection = mCurrentClientInfo;
845 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
846 if (mClientInfo[loop].used &&
847 0 < strlen(mClientInfo[loop].appid) &&
848 0 < strlen(mClientInfo[loop].wakeup_word[0])) {
849 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
850 mCurrentClientInfo = loop;
856 if (mCurrentClientInfo != prev_selection) {
857 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
858 int pid = mas_get_client_pid_by_appid(mClientInfo[prev_selection].appid);
859 mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
866 int CServiceMain::mas_launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode)
870 if (NULL == appid || 0 == strlen(appid)) {
871 MAS_LOGE("appid invalid, failed launching MA Client");
875 if (CLIENT_LAUNCH_MODE_PRELAUNCH == launch_mode) {
876 if (mApplicationManager.is_application_running(appid)) {
877 MAS_LOGE("appid %s is already running, no need for a prelaunch", appid);
881 result = mApplicationManager.launch_app_async(appid, true);
883 result = mApplicationManager.launch_app_async(appid, false);
886 if (CLIENT_LAUNCH_MODE_ACTIVATION == launch_mode) {
888 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
889 if (mClientInfo[loop].used &&
890 0 < strlen(mClientInfo[loop].appid)) {
891 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
892 mWakeupClientAppId = mClientInfo[loop].appid;
897 MAS_LOGD("mWakeupClientAppId : %s, %d", mWakeupClientAppId.c_str(), found);
903 int CServiceMain::mas_bring_client_to_foreground(const char* appid)
907 if (NULL == appid || 0 == strlen(appid)) {
908 MAS_LOGE("appid invalid, failed launching MA Client");
912 if (!mApplicationManager.bring_app_to_foreground(appid)) {
919 int CServiceMain::mas_launch_client_by_wakeup_word(const char *wakeup_word)
921 const char *appid = mas_get_client_appid_by_wakeup_word(wakeup_word);
922 return mas_launch_client_by_appid(appid, CLIENT_LAUNCH_MODE_ACTIVATION);
925 int CServiceMain::mas_prelaunch_default_assistant()
927 /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
928 boost::optional<bool> prelaunch_mode =
929 mPreferenceManager.get_bool(WAKEUP_SETTINGS_KEY_PRELAUNCH_MODE);
930 if (prelaunch_mode && *prelaunch_mode) {
931 const char *default_assistant = NULL;
932 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
933 if (!(mApplicationManager.is_application_running(default_assistant))) {
934 MAS_LOGD("prelaunching default_assistant_appid : %s", default_assistant);
935 mas_launch_client_by_appid(default_assistant, CLIENT_LAUNCH_MODE_PRELAUNCH);
942 int CServiceMain::mas_update_voice_key_support_mode()
944 /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
945 bool successful = false;
946 const char *default_assistant = NULL;
947 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
948 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
949 if (mClientInfo[loop].used) {
950 if (default_assistant &&
951 strncmp(default_assistant, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
952 float duration = mClientInfo[loop].voice_key_tap_duration;
953 if (0.0f < duration) {
954 mServicePlugin.set_voice_key_tap_duration(duration);
956 mServicePlugin.unset_voice_key_tap_duration();
958 mServicePlugin.set_voice_key_support_mode(
959 mClientInfo[loop].voice_key_support_mode);
967 mServicePlugin.unset_voice_key_tap_duration();
968 mServicePlugin.set_voice_key_support_mode(VOICE_KEY_SUPPORT_MODE_NONE);
973 ma_preprocessing_allow_mode_e CServiceMain::get_preprocessing_allow_mode(const char* appid)
975 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
976 if (appid && mClientInfo[loop].used) {
977 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
978 return mClientInfo[loop].preprocessing_allow_mode;
982 return MA_PREPROCESSING_ALLOW_NONE;
985 /* This might need to be read from settings in the future, but using macro for now */
986 //#define BRING_PREPROCESSING_ASSISTANT_TO_FRONT
988 int CServiceMain::mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event)
990 const char* current_maclient_appid = NULL;
991 const char* preprocessing_allow_appid = NULL;
992 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
993 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
994 preprocessing_allow_appid = mClientInfo[mCurrentClientInfo].preprocessing_allow_appid;
996 ma_preprocessing_allow_mode_e mode = get_preprocessing_allow_mode(current_maclient_appid);
999 case PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED:
1001 #ifndef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1002 /* If there is no need to bring preprocessing assistant to front,
1003 current_maclient should always be brought to front */
1004 mas_bring_client_to_foreground(current_maclient_appid);
1006 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1007 if (check_preprocessing_assistant_exists()) {
1008 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1009 MA_PREPROCESSING_ALLOW_ALL == mode) {
1010 if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1011 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1015 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1016 /* If preprocessing assistant does not exist, there is no way to enable
1017 preprocessing assistant, so bring current maclient to front right away */
1018 mas_bring_client_to_foreground(current_maclient_appid);
1023 case PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED:
1025 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1026 /* Enable preprocessing mode only if the preprocessing assistant exists */
1027 if (check_preprocessing_assistant_exists()) {
1028 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1029 MA_PREPROCESSING_ALLOW_ALL == mode) {
1030 if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1031 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1037 case PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED:
1039 if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED == mCurrentPreprocessingState) {
1040 mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_UTTERANCE;
1041 } else if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED == mCurrentPreprocessingState) {
1042 /* If preprocessing assistant does not exist, the current_maclient
1043 would have been brought to front already on wakeup event */
1044 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1045 if (check_preprocessing_assistant_exists()) {
1046 mas_bring_client_to_foreground(current_maclient_appid);
1049 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1053 case PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED:
1055 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1056 if (check_preprocessing_assistant_exists()) {
1057 if (MA_PREPROCESSING_ALLOW_FOLLOW_UP == mode ||
1058 MA_PREPROCESSING_ALLOW_ALL == mode) {
1059 mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_FOLLOW_UP;
1064 case PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED:
1066 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1067 if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1068 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1069 boost::optional<std::string> preprocessing_assistant =
1070 mPreferenceManager.get_bool(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1071 if (preprocessing_assistant) {
1072 MAS_LOGD("preprocessing_assistant_appid : %s", (*preprocessing_assistant).c_str());
1073 mas_bring_client_to_foreground((*preprocessing_assistant).c_str());
1077 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1080 case PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED:
1082 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1083 if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1084 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1085 mas_bring_client_to_foreground(current_maclient_appid);
1088 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1095 int CServiceMain::mas_set_current_service_state(ma_service_state_e state)
1097 mCurrentServiceState = state;
1099 int count = mClientManager.get_client_num();
1102 for (i = 0; i < count; i++) {
1103 int pid = mClientManager.find_client_pid_by_index(i);
1106 int ret = mServiceIpc.change_service_state(pid, state);
1108 MAS_LOGE("[ERROR] Fail to set service state change to %d, ret(%d)", pid, ret);
1115 ma_service_state_e CServiceMain::mas_get_current_service_state()
1117 return mCurrentServiceState;
1120 bool CServiceMain::is_valid_wakeup_engine(const char* appid)
1122 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM;loop++) {
1123 if (mClientInfo[loop].used) {
1124 LOGD("comparing appid : %s %s", mClientInfo[loop].wakeup_engine, appid);
1125 if (0 == strncmp(mClientInfo[loop].wakeup_engine, appid, MAX_APPID_LEN)) {
1133 bool CServiceMain::is_wakeup_engine(const pkgmgrinfo_appinfo_h handle)
1135 if (nullptr == g_service_main) return false;
1136 bool is_wakeup_engine = false;
1140 int ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
1141 if (PMINFO_R_OK == ret && NULL != appid) {
1142 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM;loop++) {
1143 if (mClientInfo[loop].used) {
1144 LOGD("comparing appid : %s %s", mClientInfo[loop].wakeup_engine, appid);
1145 if (0 == strncmp(mClientInfo[loop].wakeup_engine, appid, MAX_APPID_LEN)) {
1146 is_wakeup_engine = true;
1151 LOGE("pkgmgrinfo_appinfo_get_appid failed! error code=%d", ret);
1154 return is_wakeup_engine;
1157 bool CServiceMain::is_voice_assistant(const pkgmgrinfo_appinfo_h handle)
1159 bool is_voice_assistant = false;
1160 char* metadata_value = NULL;
1161 const char* voice_assistant_metadata = "http://tizen.org/metadata/multi-assistant/name";
1162 int ret = pkgmgrinfo_appinfo_get_metadata_value(handle, voice_assistant_metadata, &metadata_value);
1163 if (PMINFO_R_OK == ret && NULL != metadata_value) {
1164 is_voice_assistant = true;
1166 LOGE("pkgmgrinfo_appinfo_get_metadata_value failed! error code=%d", ret);
1168 return is_voice_assistant;
1171 static int pkg_app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
1173 if (!g_service_main) return 0;
1175 int *result = (int*)user_data;
1177 if (g_service_main->is_voice_assistant(handle)) {
1179 } else if (g_service_main->is_wakeup_engine(handle)) {
1187 INFO: Package install/update/uninstall scenario
1188 Install and Uninstall are obviously simple.
1189 Install: just INSTALL
1190 Uninstall: just UNINSTALL
1191 Update package (change the source codes and Run As again), there are four scenarios:
1194 2. UNINSTALL -> INSTALL
1195 This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Check "Enable Project specific settings"
1196 and change Application ID in tizen-manifest.xml file and Run As.
1197 3. UPDATE -> INSTALL
1198 This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Uncheck "Enable Project specific settings"
1199 and change Application ID in tizen-manifest.xml file and Run As.
1200 At UPDATE event, pkgid (package parameter) is invalid...
1202 Exceptionally, only UPDATE can be called when Application ID in tizen-manifest.xml file is changed.
1203 At UPDATE event, pkgid (package parameter) is valid, and only appid is changed; the previous appid is invalid.
1205 static void _package_manager_event_cb(const char *type, const char *package, package_manager_event_type_e event_type, package_manager_event_state_e event_state, int progress, package_manager_error_e error, void *user_data)
1207 CServiceMain* service_main = static_cast<CServiceMain*>(user_data);
1210 uid_t uid = getuid();
1211 pkgmgrinfo_pkginfo_h handle = NULL;
1212 static bool in_progress = false;
1213 bool should_exit = false;
1215 if (!package || !type)
1218 if (PACKAGE_MANAGER_EVENT_TYPE_UPDATE != event_type &&
1219 PACKAGE_MANAGER_EVENT_TYPE_INSTALL != event_type &&
1220 PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL != event_type)
1223 if (PACKAGE_MANAGER_EVENT_STATE_STARTED != event_state &&
1224 PACKAGE_MANAGER_EVENT_STATE_COMPLETED != event_state)
1228 MAS_LOGD("type=%s package=%s event_type=%d event_state=%d progress=%d error=%d",
1229 type, package, event_type, event_state, progress, error);
1230 ret = pkgmgrinfo_pkginfo_get_pkginfo(package, &handle);
1231 if (ret != PMINFO_R_OK || NULL == handle) {
1232 MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo(\"%s\",~) returned %d", package, ret);
1233 /* Try to get in user packages */
1235 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package, uid, &handle);
1236 if (ret != PMINFO_R_OK || NULL == handle) {
1237 MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid());
1243 /* Try to get in user packages */
1244 pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret, uid);
1246 /* Try to get in global packages */
1247 pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret);
1250 if (PACKAGE_MANAGER_EVENT_STATE_STARTED == event_state) {
1251 MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_STARTED event : %d", event_type);
1252 if (false == in_progress) {
1255 service_main->deinitialize_service_plugin();
1257 MAS_LOGE("service_main is NULL");
1260 } else if (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state) {
1261 MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_COMPLETED event : %d", event_type);
1262 if (false == in_progress) {
1264 service_main->deinitialize_service_plugin();
1266 MAS_LOGE("service_main is NULL");
1271 service_main->initialize_service_plugin();
1272 service_main->process_activated_setting();
1274 MAS_LOGE("service_main is NULL");
1278 in_progress = false;
1282 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1285 LOGI("Now restarting multi-assistant-service for reloading updated modules");
1292 bool CServiceMain::app_create(void *data)
1294 // Todo: add your code here.
1295 MAS_LOGD("[Enter] Service app create");
1297 g_service_main = this;
1299 mClientManager.set_application_manager(&mApplicationManager);
1301 mServiceIpc.set_client_manager(&mClientManager);
1302 mServiceIpc.set_service_ipc_observer(this);
1304 mServicePlugin.set_service_ipc(&mServiceIpc);
1305 mServicePlugin.set_service_main(this);
1307 int ret = mServiceIpc.open_connection();
1309 MAS_LOGE("[ERROR] Fail to open connection");
1312 initialize_service_plugin();
1314 process_activated_setting();
1316 mas_prelaunch_default_assistant();
1317 mas_update_voice_key_support_mode();
1319 /* For the case of preprocessing assistant, it always have to be launched beforehand */
1320 boost::optional<std::string> preprocessing_assistant =
1321 mPreferenceManager.get_string(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1322 if (preprocessing_assistant) {
1323 MAS_LOGD("prelaunching preprocessing_assistant_appid : %s", (*preprocessing_assistant).c_str());
1324 mas_launch_client_by_appid((*preprocessing_assistant).c_str(), CLIENT_LAUNCH_MODE_PRELAUNCH);
1327 if (!mPackageManagerHandle) {
1328 int ret = package_manager_create(&mPackageManagerHandle);
1329 if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1330 ret = package_manager_set_event_cb(mPackageManagerHandle, _package_manager_event_cb, this);
1331 if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1332 LOGD("package_manager_set_event_cb succeeded.");
1334 LOGE("package_manager_set_event_cb failed(%d)", ret);
1337 LOGE("package_manager_create failed(%d)", ret);
1344 void CServiceMain::app_terminate(void *data)
1346 MAS_LOGI("[ENTER]");
1347 if (mPackageManagerHandle) {
1348 package_manager_unset_event_cb(mPackageManagerHandle);
1349 package_manager_destroy(mPackageManagerHandle);
1350 mPackageManagerHandle = NULL;
1353 deinitialize_service_plugin();
1355 mPreferenceManager.unregister_changed_callback(
1356 MULTI_ASSISTANT_SETTINGS_ACTIVATED, active_state_changed_cb);
1358 int ret = mServiceIpc.close_connection();
1360 MAS_LOGE("[ERROR] Fail to close connection");
1363 g_service_main = nullptr;
1369 int CServiceMain::on_initialize(int pid) {
1370 MAS_LOGD("[Enter] pid(%d)", pid);
1372 std::string pid_appid;
1373 boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
1375 pid_appid = *appid_by_pid;
1376 MAS_LOGD("appid for pid %d is : %s", pid, pid_appid.c_str());
1378 /* Remove existing client that has same appid, if there's any */
1379 mClientManager.destroy_client_by_appid(pid_appid.c_str());
1381 /* And remove a client that has same pid also */
1382 mClientManager.destroy_client_by_pid(pid);
1384 mClientManager.create_client(pid, pid_appid.c_str());
1386 const char *current_maclient_appid = NULL;
1387 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
1388 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
1391 mas_client_send_preprocessing_information(pid);
1392 if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) {
1393 mas_client_send_voice_key_status_change(pid, mLastVoiceKeyStatus);
1395 if (current_maclient_appid && 0 == pid_appid.compare(current_maclient_appid)) {
1396 MAS_LOGD("MA client with current maclient appid connected!");
1398 if (0 == mWakeupClientAppId.compare(pid_appid)) {
1399 mWakeupClientAppId.clear();
1400 mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_ACTIVE);
1401 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED);
1403 MAS_LOGE("[ERROR] mWakeupClientAppId and appid differ : %s %s",
1404 mWakeupClientAppId.c_str(), pid_appid.c_str());
1407 MAS_LOGD("MA client connected, but its appid does not match with current maclient");
1410 mServiceIpc.change_service_state(pid, mas_get_current_service_state());
1412 MAS_LOGE("[ERROR] Fail to retrieve appid");
1418 int CServiceMain::on_deinitialize(int pid) {
1419 MAS_LOGD("[Enter] pid(%d)", pid);
1420 mClientManager.destroy_client_by_pid(pid);
1424 int CServiceMain::on_get_audio_format(int pid, int& rate, int& channel, int& audio_type) {
1425 int main_rate, main_channel, main_audio_type;
1426 int ret = mas_client_get_audio_format(pid,
1427 &main_rate, &main_channel, &main_audio_type);
1429 channel = main_channel;
1430 audio_type = main_audio_type;
1434 int CServiceMain::on_get_audio_source_type(int pid, std::string& type) {
1435 char *main_type = nullptr;
1436 int ret = mas_client_get_audio_source_type(pid, &main_type);
1437 if (0 == ret && main_type) {
1438 type = std::string{main_type};
1443 int CServiceMain::on_send_asr_result(int pid, int event, std::string asr_result) {
1444 return mas_client_send_asr_result(pid, event, asr_result.c_str());
1447 int CServiceMain::on_send_result(int pid, std::string display_text,
1448 std::string utterance_text, std::string result_json) {
1449 return mas_client_send_result(pid,
1450 display_text.c_str(), utterance_text.c_str(), result_json.c_str());
1453 int CServiceMain::on_send_recognition_result(int pid, int result) {
1454 return mas_client_send_recognition_result(pid, result);
1457 int CServiceMain::on_start_streaming_audio_data(int pid, int type) {
1458 return mas_client_start_streaming_audio_data(pid, type);
1461 int CServiceMain::on_stop_streaming_audio_data(int pid, int type) {
1462 return mas_client_stop_streaming_audio_data(pid, type);
1465 int CServiceMain::on_update_voice_feedback_state(int pid, int state) {
1466 return mas_client_update_voice_feedback_state(pid, state);
1469 int CServiceMain::on_send_assistant_specific_command(int pid, std::string command) {
1470 return mas_client_set_assistant_specific_command(pid, command.c_str());
1473 int CServiceMain::on_set_background_volume(int pid, double ratio) {
1474 return mas_client_set_background_volume(pid, ratio);
1477 int CServiceMain::on_set_preprocessing_allow_mode(int pid, int mode, std::string app_id) {
1478 return mas_client_set_preprocessing_allow_mode(pid,
1479 static_cast<ma_preprocessing_allow_mode_e>(mode), app_id.c_str());
1482 int CServiceMain::on_send_preprocessing_result(int pid, int result) {
1483 return mas_client_send_preprocessing_result(pid, result);
1486 int CServiceMain::on_set_wake_word_audio_require_flag(int pid, int require) {
1487 return mas_client_set_wake_word_audio_require_flag(pid, require);
1490 int CServiceMain::on_set_assistant_language(int pid, std::string language) {
1491 return mas_client_set_assistant_language(pid, language.c_str());
1494 int CServiceMain::on_add_wake_word(int pid, std::string wake_word, std::string language) {
1495 return mas_client_add_wake_word(pid, wake_word.c_str(), language.c_str());
1498 int CServiceMain::on_remove_wake_word(int pid, std::string wake_word, std::string language) {
1499 return mas_client_remove_wake_word(pid, wake_word.c_str(), language.c_str());
1502 int CServiceMain::on_ui_initialize(int pid)
1504 return mas_ui_client_initialize(pid);
1507 int CServiceMain::on_ui_deinitialize(int pid)
1509 return mas_ui_client_deinitialize(pid);
1512 int CServiceMain::on_ui_change_assistant(std::string app_id)
1514 return mas_ui_client_change_assistant(app_id.c_str());