2 * Copyright 2018-2019 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>
26 #include <pkgmgr-info.h>
33 #include "service_common.h"
34 #include "service_main.h"
35 #include "service_plugin.h"
36 #include "service_ipc_dbus.h"
37 #include "service_config.h"
39 static CServiceMain* g_service_main = nullptr;
41 bool CServiceMain::check_preprocessing_assistant_exists()
45 char* vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
47 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
48 if (mClientInfo[loop].used &&
49 strncmp(vconf_str, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
50 if (mClientManager.check_client_validity_by_appid(std::string{vconf_str})) {
59 MAS_LOGD("result : %d", ret);
64 bool CServiceMain::is_current_preprocessing_assistant(const char* appid)
66 if (NULL == appid) return false;
70 char* vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
72 if (strncmp(vconf_str, appid, MAX_APPID_LEN) == 0) {
82 int CServiceMain::mas_client_get_audio_format(int pid, int* rate, int* channel, int* audio_type)
84 MAS_LOGD("[Enter] pid(%d)", pid);
86 int ret = mServicePlugin.get_recording_audio_format(rate, channel, audio_type);
88 MAS_LOGE("[ERROR] Fail to get recording audio format, ret(%d)", ret);
94 #define MAX_LOCAL_VARIABLE_STRING_LEN 256
95 int CServiceMain::mas_client_get_audio_source_type(int pid, char** type)
97 MAS_LOGD("[Enter] pid(%d)", pid);
99 if (NULL == type) return -1;
101 static char cached[MAX_LOCAL_VARIABLE_STRING_LEN] = {'\0'};
102 int ret = mServicePlugin.get_recording_audio_source_type(type);
104 MAS_LOGE("[ERROR] Fail to get recording audio source type, ret(%d)", ret);
107 strncpy(cached, *type, MAX_LOCAL_VARIABLE_STRING_LEN - 1);
108 cached[MAX_LOCAL_VARIABLE_STRING_LEN - 1] = '\0';
114 int CServiceMain::mas_client_send_preprocessing_information(int pid)
117 MAS_LOGD("[Enter] pid(%d)", pid);
119 char* vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
120 MAS_LOGD("preprocessing_assistant_appid : %s", vconf_str);
122 ret = mServiceIpc.send_preprocessing_information(pid, vconf_str);
130 int CServiceMain::mas_client_send_voice_key_status_change(int pid, ma_voice_key_status_e status)
133 MAS_LOGD("[Enter] pid(%d)", pid);
135 ret = mServiceIpc.voice_key_status_change(pid, status);
137 MAS_LOGE("[ERROR] Fail to send voice key status changed information, ret(%d)", ret);
139 mLastVoiceKeyStatus = status;
144 int CServiceMain::mas_client_send_asr_result(int pid, int event, const char* asr_result)
146 MAS_LOGD("[Enter] pid(%d), event(%d), asr_result(%s)", pid, event, asr_result);
147 int ret = mServiceIpc.masc_ui_dbus_send_asr_result(pid, event, asr_result);
149 MAS_LOGE("[ERROR] Fail to send asr result, ret(%d)", ret);
152 // if final event is , launch assistant app which is invoked with wakeup word.
157 int CServiceMain::mas_client_send_result(int pid, const char* display_text,
158 const char* utterance_text, const char* result_json)
160 MAS_LOGD("[Enter] pid(%d), display_text(%s), utterance_text(%s), result_json(%s)", pid, display_text, utterance_text, result_json);
161 int ret = mServiceIpc.masc_ui_dbus_send_result(pid, display_text, utterance_text, result_json);
163 MAS_LOGE("[ERROR] Fail to send result, ret(%d)", ret);
166 const char* pid_appid = NULL;
167 char buf[MAX_APPID_LEN] = {'\0', };
168 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
169 buf[MAX_APPID_LEN - 1] = '\0';
172 mServicePlugin.update_recognition_result(pid_appid, MA_RECOGNITION_RESULT_EVENT_SUCCESS);
177 int CServiceMain::mas_client_send_recognition_result(int pid, int result)
179 MAS_LOGD("[Enter] pid(%d), result(%d)", pid, result);
180 int ret = mServiceIpc.masc_ui_dbus_send_recognition_result(pid, result);
182 MAS_LOGE("[ERROR] Fail to send recognition result, ret(%d)", ret);
185 const char* pid_appid = NULL;
186 char buf[MAX_APPID_LEN] = {'\0', };
187 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
188 buf[MAX_APPID_LEN - 1] = '\0';
191 mServicePlugin.update_recognition_result(pid_appid, result);
196 int CServiceMain::mas_client_start_streaming_audio_data(int pid, int type)
200 case MA_AUDIO_STREAMING_DATA_TYPE_CURRENT_UTTERANCE:
201 ret = mServicePlugin.start_streaming_utterance_data();
202 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED);
204 case MA_AUDIO_STREAMING_DATA_TYPE_PREVIOUS_UTTERANCE:
205 ret = mServicePlugin.start_streaming_previous_utterance_data();
206 /* Preprocessing is not required for previous utterance streaming */
208 case MA_AUDIO_STREAMING_DATA_TYPE_FOLLOW_UP_SPEECH:
209 ret = mServicePlugin.start_streaming_follow_up_data();
210 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED);
216 int CServiceMain::mas_client_stop_streaming_audio_data(int pid, int type)
220 case MA_AUDIO_STREAMING_DATA_TYPE_CURRENT_UTTERANCE:
221 ret = mServicePlugin.stop_streaming_utterance_data();
223 case MA_AUDIO_STREAMING_DATA_TYPE_PREVIOUS_UTTERANCE:
224 ret = mServicePlugin.stop_streaming_previous_utterance_data();
226 case MA_AUDIO_STREAMING_DATA_TYPE_FOLLOW_UP_SPEECH:
227 ret = mServicePlugin.stop_streaming_follow_up_data();
233 int CServiceMain::mas_client_update_voice_feedback_state(int pid, int state)
235 const char* pid_appid = NULL;
236 char buf[MAX_APPID_LEN] = {'\0', };
237 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
238 buf[MAX_APPID_LEN - 1] = '\0';
241 mServicePlugin.update_voice_feedback_state(pid_appid, state);
245 int CServiceMain::mas_client_set_assistant_specific_command(int pid, const char *command)
247 const char* pid_appid = NULL;
248 char buf[MAX_APPID_LEN] = {'\0', };
249 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
250 buf[MAX_APPID_LEN - 1] = '\0';
253 mServicePlugin.set_assistant_specific_command(pid_appid, command);
257 int CServiceMain::mas_client_set_background_volume(int pid, double ratio)
259 const char* pid_appid = NULL;
260 char buf[MAX_APPID_LEN] = {'\0', };
261 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
262 buf[MAX_APPID_LEN - 1] = '\0';
265 mServicePlugin.set_background_volume(pid_appid, ratio);
269 int CServiceMain::mas_client_set_preprocessing_allow_mode(int pid, ma_preprocessing_allow_mode_e mode, const char* appid)
271 const char* pid_appid = NULL;
272 char buf[MAX_APPID_LEN] = {'\0', };
273 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
274 buf[MAX_APPID_LEN - 1] = '\0';
278 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
279 if (mClientInfo[loop].used) {
280 if (pid_appid && strncmp(pid_appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
281 mClientInfo[loop].preprocessing_allow_mode = mode;
283 strncpy(mClientInfo[loop].preprocessing_allow_appid, appid, MAX_APPID_LEN);
284 mClientInfo[loop].preprocessing_allow_appid[MAX_APPID_LEN - 1] = '\0';
286 mClientInfo[loop].preprocessing_allow_appid[0] = '\0';
292 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED);
297 int CServiceMain::mas_client_send_preprocessing_result(int pid, bool result)
299 const char* pid_appid = NULL;
300 char buf[MAX_APPID_LEN] = {'\0', };
301 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
302 buf[MAX_APPID_LEN - 1] = '\0';
305 if (!is_current_preprocessing_assistant(pid_appid)) return -1;
307 const char *current_maclient_appid = NULL;
308 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
309 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
313 MAS_LOGD("Preprocessing succeeded, bring (%s) to foreground", pid_appid);
314 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED);
316 MAS_LOGD("Preprocessing failed, bring (%s) to foreground", current_maclient_appid);
317 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED);
320 if (current_maclient_appid) {
321 int pid = mClientManager.find_client_pid_by_appid(
322 std::string{current_maclient_appid});
323 mServiceIpc.send_preprocessing_result(pid, result);
329 int CServiceMain::mas_client_set_wake_word_audio_require_flag(int pid, bool require)
331 const char* pid_appid = NULL;
332 char buf[MAX_APPID_LEN] = {'\0', };
333 if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) {
334 buf[MAX_APPID_LEN - 1] = '\0';
338 mServicePlugin.set_wake_word_audio_require_flag(pid_appid, require);
342 int CServiceMain::mas_client_set_assistant_language(int pid, const char* language)
344 const char* pid_appid = NULL;
345 char buf[MAX_APPID_LEN] = {'\0', };
346 int ret = aul_app_get_appid_bypid(pid, buf, sizeof(buf));
347 if (AUL_R_OK == ret) {
348 buf[MAX_APPID_LEN - 1] = '\0';
352 mServicePlugin.set_assistant_language(pid_appid, language);
356 int CServiceMain::mas_client_add_wake_word(int pid, const char* wake_word, const char* language)
358 const char* pid_appid = NULL;
359 char buf[MAX_APPID_LEN] = {'\0', };
360 int ret = aul_app_get_appid_bypid(pid, buf, sizeof(buf));
361 if (AUL_R_OK == ret) {
362 buf[MAX_APPID_LEN - 1] = '\0';
366 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
367 if (mClientInfo[loop].used &&
368 0 == strncmp(buf, mClientInfo[loop].appid, MAX_APPID_LEN)) {
369 ret = mServiceConfig.add_custom_wake_word(wake_word, language,
370 mClientInfo[loop].wakeup_word,
371 mClientInfo[loop].wakeup_language);
373 mServiceConfig.save_custom_wake_words(pid_appid,
374 mClientInfo[loop].wakeup_word,
375 mClientInfo[loop].wakeup_language);
377 LOGE("add new wake word failed!");
383 mServicePlugin.add_assistant_wakeup_word(pid_appid, wake_word, language);
387 int CServiceMain::mas_client_remove_wake_word(int pid, const char* wake_word, const char* language)
389 const char* pid_appid = NULL;
390 char buf[MAX_APPID_LEN] = {'\0', };
391 int ret = aul_app_get_appid_bypid(pid, buf, sizeof(buf));
392 if (AUL_R_OK == ret) {
393 buf[MAX_APPID_LEN - 1] = '\0';
397 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
398 if (mClientInfo[loop].used &&
399 0 == strncmp(buf, mClientInfo[loop].appid, MAX_APPID_LEN)) {
400 ret = mServiceConfig.remove_custom_wake_word(wake_word, language,
401 mClientInfo[loop].wakeup_word,
402 mClientInfo[loop].wakeup_language);
404 mServiceConfig.save_custom_wake_words(pid_appid,
405 mClientInfo[loop].wakeup_word,
406 mClientInfo[loop].wakeup_language);
411 mServicePlugin.remove_assistant_wakeup_word(pid_appid, wake_word, language);
415 int CServiceMain::mas_ui_client_initialize(int pid)
417 MAS_LOGD("[Enter] pid(%d)", pid);
422 int CServiceMain::mas_ui_client_deinitialize(int pid)
424 MAS_LOGD("[Enter] pid(%d)", pid);
429 int CServiceMain::mas_ui_client_change_assistant(const char* appid)
434 MAS_LOGE("NULL parameter");
438 bool use_custom_ui = mas_get_client_custom_ui_option_by_appid(appid);
439 mServiceIpc.masc_ui_dbus_enable_common_ui(!use_custom_ui);
441 mas_set_current_client_by_appid(appid);
442 int pid = mas_get_client_pid_by_appid(appid);
444 mas_bring_client_to_foreground(appid);
445 mas_client_send_preprocessing_information(pid);
446 if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) {
447 mas_client_send_voice_key_status_change(pid, mLastVoiceKeyStatus);
450 mServiceIpc.active_state_change(pid, MA_ACTIVE_STATE_ACTIVE);
451 MAS_LOGD("MA Client with appid %s exists, requesting speech data", (appid ? appid : "NULL"));
453 int ret = mServicePlugin.start_streaming_utterance_data();
455 MAS_LOGE("[ERROR] Fail to start streaming utterance data(%d)", ret);
459 // Appropriate MA Client not available, trying to launch new one
460 MAS_LOGD("MA Client with appid %s does not exist, launching client", (appid ? appid : "NULL"));
462 /* The appid parameter might not exist after this function call, so we use appid string in our mClientInfo */
463 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
464 if (mClientInfo[loop].used &&
465 0 < strlen(mClientInfo[loop].appid) &&
466 0 < strlen(mClientInfo[loop].wakeup_word[0])) {
467 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
468 mas_launch_client_by_appid(mClientInfo[loop].appid, CLIENT_LAUNCH_MODE_ACTIVATION);
477 static int mas_assistant_info_cb(ma_assistant_info_s* info, void* user_data) {
479 CServiceMain* service_main = static_cast<CServiceMain*>(user_data);
481 ret = service_main->add_assistant_info(info);
486 int CServiceMain::add_assistant_info(ma_assistant_info_s* info) {
487 MAS_LOGD("__mas_assistant_info_cb called");
490 MAS_LOGE("info NULL, returning");
493 if (NULL == info->app_id) {
494 MAS_LOGE("app_id NULL, returning");
500 while(-1 == index && loop < MAX_MACLIENT_INFO_NUM) {
501 if (false == mClientInfo[loop].used) {
507 mClientInfo[index].used = true;
508 mClientInfo[index].preprocessing_allow_mode = MA_PREPROCESSING_ALLOW_NONE;
509 mClientInfo[index].preprocessing_allow_appid[0] = '\0';
510 MAS_LOGD("app_id(%s)", info->app_id);
511 strncpy(mClientInfo[index].appid, info->app_id, MAX_APPID_LEN);
512 mClientInfo[index].appid[MAX_APPID_LEN - 1] = '\0';
514 if (is_current_preprocessing_assistant(mClientInfo[index].appid)) {
515 mCurrentPreprocessingClientInfo = index;
518 for (loop = 0;loop < MAX_WAKEUP_WORDS_NUM;loop++) {
519 if (loop < info->cnt_wakeup && info->wakeup_list[loop]) {
520 MAS_LOGD("wakeup_list(%d)(%s)(%s)", loop, info->wakeup_list[loop], info->wakeup_language[loop]);
521 strncpy(mClientInfo[index].wakeup_word[loop], info->wakeup_list[loop], MAX_WAKEUP_WORD_LEN);
522 mClientInfo[index].wakeup_word[loop][MAX_WAKEUP_WORD_LEN - 1] = '\0';
523 if (info->wakeup_language[loop]) {
524 strncpy(mClientInfo[index].wakeup_language[loop], info->wakeup_language[loop], MAX_SUPPORTED_LANGUAGE_LEN);
525 mClientInfo[index].wakeup_language[loop][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
527 strncpy(mClientInfo[index].wakeup_language[loop], "", MAX_SUPPORTED_LANGUAGE_LEN);
530 strncpy(mClientInfo[index].wakeup_word[loop], "", MAX_WAKEUP_WORD_LEN);
534 for (loop = 0;loop < MAX_SUPPORTED_LANGUAGES_NUM;loop++) {
535 if (loop < info->cnt_lang && info->supported_lang[loop]) {
536 MAS_LOGD("supported_lang(%d)(%s)", loop, info->supported_lang[loop]);
537 strncpy(mClientInfo[index].supported_language[loop], info->supported_lang[loop], MAX_SUPPORTED_LANGUAGE_LEN);
538 mClientInfo[index].supported_language[loop][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
540 strncpy(mClientInfo[index].supported_language[loop], "", MAX_SUPPORTED_LANGUAGE_LEN);
544 MAS_LOGD("wakeup_engine(%s)", info->wakeup_engine);
545 if (info->wakeup_engine) {
546 strncpy(mClientInfo[index].wakeup_engine, info->wakeup_engine, MAX_APPID_LEN);
547 mClientInfo[index].wakeup_engine[MAX_APPID_LEN - 1] = '\0';
549 mClientInfo[index].wakeup_engine[0] = '\0';
550 MAS_LOGW("Wakeup engine information not provided for : %s", info->app_id);
552 mClientInfo[index].custom_ui_option = info->custom_ui_option;
554 MAS_LOGD("voice_key_support_mode(%d)", info->voice_key_support_mode);
555 mClientInfo[index].voice_key_support_mode = info->voice_key_support_mode;
556 MAS_LOGD("voice_key_tap_duration(%f)", info->voice_key_tap_duration);
557 mClientInfo[index].voice_key_tap_duration = info->voice_key_tap_duration;
559 MAS_LOGD("Couldn't find an empty slot for storing assistant info");
562 MAS_LOGD("__mas_assistant_info_cb end");
567 static void active_state_changed_cb(keynode_t* key, void* data)
570 if (vconf_get_bool(MULTI_ASSISTANT_SETTINGS_ACTIVATED, &vconf_value) == 0) {
571 MAS_LOGD("multi-assistant active state : %d\n", vconf_value);
573 CServicePlugin *plugin = nullptr;
574 if (g_service_main) {
575 plugin = g_service_main->get_service_plugin();
581 plugin->deactivate();
584 MAS_LOGE("Could not change plugin state : %p %p", g_service_main, plugin);
589 int CServiceMain::initialize_service_plugin(void)
591 if (0 != mServicePlugin.initialize()) {
592 MAS_LOGE("Fail to ws intialize");
596 if (0 != mServicePlugin.set_language(mCurrentLanguage.c_str())) {
597 MAS_LOGE("Fail to ws set language");
601 if (0 == mServiceConfig.get_assistant_info(mas_assistant_info_cb, this)) {
602 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
604 if (mClientInfo[loop].used &&
605 0 < strlen(mClientInfo[loop].appid)) {
606 mServiceConfig.load_custom_wake_words(mClientInfo[loop].appid,
607 mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language);
608 if (0 < strlen(mClientInfo[loop].wakeup_engine)) {
609 mServicePlugin.set_assistant_wakeup_engine(
610 mClientInfo[loop].appid,
611 mClientInfo[loop].wakeup_engine);
613 for (inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
614 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
615 MAS_LOGD("Registering wakeup word %s for app %s",
616 mClientInfo[loop].wakeup_word[inner_loop], mClientInfo[loop].appid);
617 if (0 != mServicePlugin.add_assistant_wakeup_word(
618 mClientInfo[loop].appid,
619 mClientInfo[loop].wakeup_word[inner_loop],
620 mClientInfo[loop].wakeup_language[inner_loop])) {
621 MAS_LOGE("Fail to add assistant's wakeup word");
625 for (inner_loop = 0; inner_loop < MAX_SUPPORTED_LANGUAGE_NUM; inner_loop++) {
626 if (0 < strlen(mClientInfo[loop].supported_language[inner_loop])) {
627 MAS_LOGD("Adding language %s for app %s",
628 mClientInfo[loop].supported_language[inner_loop], mClientInfo[loop].appid);
629 if (0 != mServicePlugin.add_assistant_language(
630 mClientInfo[loop].appid,
631 mClientInfo[loop].supported_language[inner_loop])) {
632 MAS_LOGE("Fail to add assistant's language");
639 MAS_LOGE("Fail to load assistant info");
642 if (0 != mServicePlugin.set_callbacks()) {
643 MAS_LOGE("Fail to set callbacks");
650 int CServiceMain::deinitialize_service_plugin(void)
653 if (0 != mServicePlugin.deactivate()) {
654 MAS_LOGE("Fail to deactivate");
656 if (0 != mServicePlugin.deinitialize()) {
657 MAS_LOGE("Fail to deinitialize");
663 int CServiceMain::process_activated_setting()
665 if (0 == vconf_notify_key_changed(MULTI_ASSISTANT_SETTINGS_ACTIVATED, active_state_changed_cb, NULL)) {
666 /* Activate / deactivate according to the vconf key setting */
667 active_state_changed_cb(NULL, NULL);
669 #ifdef ENABLE_MULTI_ASSISTANT_BY_DEFAULT
670 /* Multi-assistant needs to be enabled by default, unless disabled explicitly */
671 mServicePlugin.activate();
672 const char *default_assistant = NULL;
673 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
674 if (NULL == default_assistant) {
675 if (mClientInfo[0].used) {
676 default_assistant = mClientInfo[0].appid;
677 MAS_LOGW("No default assistant, setting %s as default", default_assistant);
678 mServicePlugin.set_default_assistant(default_assistant);
680 MAS_LOGE("No default assistant, and no assistant installed");
689 int CServiceMain::mas_get_current_client_pid()
692 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
693 const char *appid = mClientInfo[mCurrentClientInfo].appid;
695 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
701 int CServiceMain::mas_get_current_preprocessing_client_pid()
704 if (mCurrentPreprocessingClientInfo >= 0 && mCurrentPreprocessingClientInfo < MAX_MACLIENT_INFO_NUM) {
705 const char *appid = mClientInfo[mCurrentPreprocessingClientInfo].appid;
707 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
713 int CServiceMain::mas_get_client_pid_by_appid(const char *appid)
718 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
721 int status = aul_app_get_status_bypid(ret);
722 if (-1 != ret && 0 > status) {
723 MAS_LOGE("The PID for %s was %d, but it seems to be terminated : %d",
724 (appid ? appid : "NULL"), ret, status);
725 on_deinitialize(ret);
732 bool CServiceMain::mas_get_client_custom_ui_option_by_appid(const char *appid)
735 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
736 if (mClientInfo[loop].used &&
737 0 < strlen(mClientInfo[loop].appid) &&
738 0 < strlen(mClientInfo[loop].wakeup_word[0])) {
739 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
740 ret = mClientInfo[loop].custom_ui_option;
747 int CServiceMain::mas_get_client_pid_by_wakeup_word(const char *wakeup_word)
749 const char *appid = mas_get_client_appid_by_wakeup_word(wakeup_word);
750 return mas_get_client_pid_by_appid(appid);
753 const char* CServiceMain::mas_get_client_appid_by_wakeup_word(const char *wakeup_word)
756 const char *appid = NULL;
758 if (NULL == wakeup_word) return NULL;
760 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
761 if (mClientInfo[loop].used &&
762 0 < strlen(mClientInfo[loop].appid)) {
763 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
764 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
765 if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
766 appid = mClientInfo[loop].appid;
773 /* Perform extended search, by eliminating blank characters */
775 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
776 if (mClientInfo[loop].used &&
777 0 < strlen(mClientInfo[loop].appid)) {
778 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
779 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
780 char comparand[MAX_WAKEUP_WORD_LEN];
781 int comparand_index = 0;
782 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
783 if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
784 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
787 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
788 appid = mClientInfo[loop].appid;
799 int CServiceMain::mas_set_current_client_by_wakeup_word(const char *wakeup_word)
803 int prev_selection = mCurrentClientInfo;
805 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
806 if (mClientInfo[loop].used &&
807 0 < strlen(mClientInfo[loop].appid)) {
808 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
809 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
810 if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
811 mCurrentClientInfo = loop;
818 /* Perform extended search, by eliminating blank characters */
820 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
821 if (mClientInfo[loop].used &&
822 0 < strlen(mClientInfo[loop].appid)) {
823 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
824 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
825 char comparand[MAX_WAKEUP_WORD_LEN];
826 int comparand_index = 0;
827 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
828 if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
829 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
832 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
833 mCurrentClientInfo = loop;
842 if (mCurrentClientInfo != prev_selection) {
843 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
844 int pid = mas_get_client_pid_by_appid(mClientInfo[prev_selection].appid);
845 mServiceIpc.active_state_change(pid, MA_ACTIVE_STATE_INACTIVE);
852 int CServiceMain::mas_set_current_client_by_appid(const char *appid)
855 int prev_selection = mCurrentClientInfo;
857 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
858 if (mClientInfo[loop].used &&
859 0 < strlen(mClientInfo[loop].appid) &&
860 0 < strlen(mClientInfo[loop].wakeup_word[0])) {
861 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
862 mCurrentClientInfo = loop;
868 if (mCurrentClientInfo != prev_selection) {
869 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
870 int pid = mas_get_client_pid_by_appid(mClientInfo[prev_selection].appid);
871 mServiceIpc.active_state_change(pid, MA_ACTIVE_STATE_INACTIVE);
878 int CServiceMain::mas_launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode)
880 if (NULL == appid || 0 == strlen(appid)) {
881 MAS_LOGE("appid invalid, failed launching MA Client");
885 if (CLIENT_LAUNCH_MODE_PRELAUNCH == launch_mode) {
886 if (1 == aul_app_is_running(appid)) {
887 MAS_LOGE("appid %s is already running, no need for a prelaunch", appid);
895 MAS_LOGE("Failed creating bundle for aul operation");
899 int result = aul_svc_set_background_launch(b,
900 (CLIENT_LAUNCH_MODE_PRELAUNCH == launch_mode ? TRUE : FALSE));
901 if (result < AUL_R_OK) {
902 MAS_LOGE("ERROR : aul_svc_set_background_launch failed. app_id [%s] bundle[%p] result[%d : %s]",
903 appid, b, result, get_error_message(result));
906 result = aul_launch_app_async(appid, b);
907 if (result < AUL_R_OK) {
908 MAS_LOGE("ERROR : aul_launch_app_async failed. app_id [%s] bundle[%p] result[%d : %s]",
909 appid, b, result, get_error_message(result));
912 if (CLIENT_LAUNCH_MODE_ACTIVATION == launch_mode) {
914 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
915 if (mClientInfo[loop].used &&
916 0 < strlen(mClientInfo[loop].appid)) {
917 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
918 mWakeupClientAppId = mClientInfo[loop].appid;
923 MAS_LOGD("mWakeupClientAppId : %s, %d", mWakeupClientAppId.c_str(), found);
926 if (b) bundle_free(b);
932 int CServiceMain::mas_bring_client_to_foreground(const char* appid)
934 /* Bring MA client to foreground - is there a better way other than launching? */
935 if (NULL == appid || 0 == strlen(appid)) {
936 MAS_LOGE("appid invalid, failed launching MA Client");
943 MAS_LOGE("Failed creating bundle for aul operation");
947 int result = aul_launch_app(appid, b);
948 if (result < AUL_R_OK) {
949 MAS_LOGE("ERROR : aul_launch_app failed. app_id [%s] bundle[%p] result[%d : %s]",
950 appid, b, result, get_error_message(result));
953 if (b) bundle_free(b);
959 int CServiceMain::mas_launch_client_by_wakeup_word(const char *wakeup_word)
961 const char *appid = mas_get_client_appid_by_wakeup_word(wakeup_word);
962 return mas_launch_client_by_appid(appid, CLIENT_LAUNCH_MODE_ACTIVATION);
965 int CServiceMain::mas_prelaunch_default_assistant()
967 /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
969 int res = vconf_get_bool(WAKEUP_SETTINGS_KEY_PRELAUNCH_MODE, &prelaunch_mode);
970 if (0 == res && 0 != prelaunch_mode) {
971 const char *default_assistant = NULL;
972 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
973 if (0 == aul_app_is_running(default_assistant)) {
974 MAS_LOGD("prelaunching default_assistant_appid : %s", default_assistant);
975 mas_launch_client_by_appid(default_assistant, CLIENT_LAUNCH_MODE_PRELAUNCH);
982 int CServiceMain::mas_update_voice_key_support_mode()
984 /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
985 bool successful = false;
986 const char *default_assistant = NULL;
987 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
988 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
989 if (mClientInfo[loop].used) {
990 if (default_assistant &&
991 strncmp(default_assistant, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
992 float duration = mClientInfo[loop].voice_key_tap_duration;
993 if (0.0f < duration) {
994 mServicePlugin.set_voice_key_tap_duration(duration);
996 mServicePlugin.unset_voice_key_tap_duration();
998 mServicePlugin.set_voice_key_support_mode(
999 mClientInfo[loop].voice_key_support_mode);
1007 mServicePlugin.unset_voice_key_tap_duration();
1008 mServicePlugin.set_voice_key_support_mode(VOICE_KEY_SUPPORT_MODE_NONE);
1013 ma_preprocessing_allow_mode_e CServiceMain::get_preprocessing_allow_mode(const char* appid)
1015 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
1016 if (appid && mClientInfo[loop].used) {
1017 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
1018 return mClientInfo[loop].preprocessing_allow_mode;
1022 return MA_PREPROCESSING_ALLOW_NONE;
1025 /* This might need to be read from settings in the future, but using macro for now */
1026 //#define BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1028 int CServiceMain::mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event)
1030 const char* current_maclient_appid = NULL;
1031 const char* preprocessing_allow_appid = NULL;
1032 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
1033 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
1034 preprocessing_allow_appid = mClientInfo[mCurrentClientInfo].preprocessing_allow_appid;
1036 ma_preprocessing_allow_mode_e mode = get_preprocessing_allow_mode(current_maclient_appid);
1039 case PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED:
1041 #ifndef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1042 /* If there is no need to bring preprocessing assistant to front,
1043 current_maclient should always be brought to front */
1044 mas_bring_client_to_foreground(current_maclient_appid);
1046 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1047 if (check_preprocessing_assistant_exists()) {
1048 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1049 MA_PREPROCESSING_ALLOW_ALL == mode) {
1050 if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1051 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1055 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1056 /* If preprocessing assistant does not exist, there is no way to enable
1057 preprocessing assistant, so bring current maclient to front right away */
1058 mas_bring_client_to_foreground(current_maclient_appid);
1063 case PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED:
1065 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1066 /* Enable preprocessing mode only if the preprocessing assistant exists */
1067 if (check_preprocessing_assistant_exists()) {
1068 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1069 MA_PREPROCESSING_ALLOW_ALL == mode) {
1070 if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1071 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1077 case PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED:
1079 if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED == mCurrentPreprocessingState) {
1080 mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_UTTERANCE;
1081 } else if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED == mCurrentPreprocessingState) {
1082 /* If preprocessing assistant does not exist, the current_maclient
1083 would have been brought to front already on wakeup event */
1084 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1085 if (check_preprocessing_assistant_exists()) {
1086 mas_bring_client_to_foreground(current_maclient_appid);
1089 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1093 case PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED:
1095 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1096 if (check_preprocessing_assistant_exists()) {
1097 if (MA_PREPROCESSING_ALLOW_FOLLOW_UP == mode ||
1098 MA_PREPROCESSING_ALLOW_ALL == mode) {
1099 mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_FOLLOW_UP;
1104 case PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED:
1106 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1107 if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1108 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1109 char* vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1110 MAS_LOGD("preprocessing_assistant_appid : %s", vconf_str);
1112 mas_bring_client_to_foreground(vconf_str);
1118 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1121 case PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED:
1123 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1124 if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1125 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1126 mas_bring_client_to_foreground(current_maclient_appid);
1129 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1136 int CServiceMain::mas_set_current_service_state(ma_service_state_e state)
1138 mCurrentServiceState = state;
1140 ma_client_s *data = NULL;
1142 int count = mClientManager.get_client_num();
1145 for (i = 0; i < count; i++) {
1146 int pid = mClientManager.find_client_pid_by_index(i);
1149 int ret = mServiceIpc.service_state_change(pid, state);
1151 MAS_LOGE("[ERROR] Fail to set service state change to %d, ret(%d)", pid, ret);
1158 ma_service_state_e CServiceMain::mas_get_current_service_state()
1160 return mCurrentServiceState;
1163 bool CServiceMain::is_valid_wakeup_engine(const char* appid)
1165 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM;loop++) {
1166 if (mClientInfo[loop].used) {
1167 LOGD("comparing appid : %s %s", mClientInfo[loop].wakeup_engine, appid);
1168 if (0 == strncmp(mClientInfo[loop].wakeup_engine, appid, MAX_APPID_LEN)) {
1176 int pkg_app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
1178 if (nullptr == g_service_main) return -1;
1182 int ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
1183 if (PMINFO_R_OK == ret && NULL != appid) {
1184 int *result = (int*)user_data;
1186 bool exists = g_service_main->is_valid_wakeup_engine(appid);
1192 LOGE("pkgmgrinfo_appinfo_get_appid failed! error code=%d", ret);
1199 INFO: Package install/update/uninstall scenario
1200 Install and Uninstall are obviously simple.
1201 Install: just INSTALL
1202 Uninstall: just UNINSTALL
1203 Update package (change the source codes and Run As again), there are four scenarios:
1206 2. UNINSTALL -> INSTALL
1207 This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Check "Enable Project specific settings"
1208 and change Application ID in tizen-manifest.xml file and Run As.
1209 3. UPDATE -> INSTALL
1210 This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Uncheck "Enable Project specific settings"
1211 and change Application ID in tizen-manifest.xml file and Run As.
1212 At UPDATE event, pkgid (package parameter) is invalid...
1214 Exceptionally, only UPDATE can be called when Application ID in tizen-manifest.xml file is changed.
1215 At UPDATE event, pkgid (package parameter) is valid, and only appid is changed; the previous appid is invalid.
1217 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)
1219 CServiceMain* service_main = static_cast<CServiceMain*>(user_data);
1222 uid_t uid = getuid();
1223 pkgmgrinfo_pkginfo_h handle = NULL;
1224 static bool in_progress = false;
1225 bool should_exit = false;
1227 if (!package || !type)
1230 if (PACKAGE_MANAGER_EVENT_TYPE_UPDATE != event_type &&
1231 PACKAGE_MANAGER_EVENT_TYPE_INSTALL != event_type &&
1232 PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL != event_type)
1235 if (PACKAGE_MANAGER_EVENT_STATE_STARTED != event_state &&
1236 PACKAGE_MANAGER_EVENT_STATE_COMPLETED != event_state)
1240 MAS_LOGD("type=%s package=%s event_type=%d event_state=%d progress=%d error=%d",
1241 type, package, event_type, event_state, progress, error);
1242 ret = pkgmgrinfo_pkginfo_get_pkginfo(package, &handle);
1243 if (ret != PMINFO_R_OK || NULL == handle) {
1244 MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid());
1245 /* Try to get in user packages */
1247 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package, uid, &handle);
1248 if (ret != PMINFO_R_OK || NULL == handle) {
1249 MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid());
1255 /* Try to get in user packages */
1256 pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret, uid);
1258 /* Try to get in global packages */
1259 pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret);
1262 if (PACKAGE_MANAGER_EVENT_STATE_STARTED == event_state) {
1263 MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_STARTED event : %d", event_type);
1264 if (false == in_progress) {
1267 service_main->deinitialize_service_plugin();
1269 MAS_LOGE("service_main is NULL");
1272 } else if (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state) {
1273 MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_COMPLETED event : %d", event_type);
1274 if (false == in_progress) {
1276 service_main->deinitialize_service_plugin();
1278 MAS_LOGE("service_main is NULL");
1283 service_main->initialize_service_plugin();
1284 service_main->process_activated_setting();
1286 MAS_LOGE("service_main is NULL");
1290 in_progress = false;
1294 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1297 LOGI("Now restarting multi-assistant-service for reloading updated modules");
1304 bool CServiceMain::app_create(void *data)
1306 // Todo: add your code here.
1307 MAS_LOGD("[Enter] Service app create");
1309 g_service_main = this;
1311 mClientManager.set_application_manager(&mApplicationManager);
1313 mServiceIpc.set_client_manager(&mClientManager);
1314 mServiceIpc.set_service_ipc_observer(this);
1316 mServicePlugin.set_service_ipc(&mServiceIpc);
1317 mServicePlugin.set_service_main(this);
1319 int ret = mServiceIpc.open_connection();
1321 MAS_LOGE("[ERROR] Fail to open connection");
1324 initialize_service_plugin();
1326 process_activated_setting();
1328 mas_prelaunch_default_assistant();
1329 mas_update_voice_key_support_mode();
1331 /* For the case of preprocessing assistant, it always have to be launched beforehand */
1333 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1335 MAS_LOGD("prelaunching preprocessing_assistant_appid : %s", vconf_str);
1336 mas_launch_client_by_appid(vconf_str, CLIENT_LAUNCH_MODE_PRELAUNCH);
1341 if (!mPackageManagerHandle) {
1342 int ret = package_manager_create(&mPackageManagerHandle);
1343 if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1344 ret = package_manager_set_event_cb(mPackageManagerHandle, _package_manager_event_cb, this);
1345 if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1346 LOGD("package_manager_set_event_cb succeeded.");
1348 LOGE("package_manager_set_event_cb failed(%d)", ret);
1351 LOGE("package_manager_create failed(%d)", ret);
1358 void CServiceMain::app_terminate(void *data)
1360 MAS_LOGI("[ENTER]");
1361 if (mPackageManagerHandle) {
1362 package_manager_unset_event_cb(mPackageManagerHandle);
1363 package_manager_destroy(mPackageManagerHandle);
1364 mPackageManagerHandle = NULL;
1367 deinitialize_service_plugin();
1369 vconf_ignore_key_changed(MULTI_ASSISTANT_SETTINGS_ACTIVATED, active_state_changed_cb);
1371 int ret = mServiceIpc.close_connection();
1373 MAS_LOGE("[ERROR] Fail to close connection");
1376 g_service_main = nullptr;
1382 int CServiceMain::on_initialize(int pid) {
1383 MAS_LOGD("[Enter] pid(%d)", pid);
1385 char appid[MAX_APPID_LEN] = {'\0', };
1386 if (AUL_R_OK == aul_app_get_appid_bypid(pid, appid, sizeof(appid))) {
1387 appid[MAX_APPID_LEN - 1] = '\0';
1389 MAS_LOGD("appid for pid %d is : %s", pid, (appid ? appid : "Unknown"));
1391 /* Remove existing client that has same appid, if there's any */
1392 mClientManager.destroy_client_by_appid(appid);
1394 /* And remove a client that has same pid also */
1395 mClientManager.destroy_client_by_pid(pid);
1397 mClientManager.create_client(pid, appid);
1399 const char *current_maclient_appid = NULL;
1400 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
1401 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
1404 mas_client_send_preprocessing_information(pid);
1405 if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) {
1406 mas_client_send_voice_key_status_change(pid, mLastVoiceKeyStatus);
1408 if (current_maclient_appid && 0 == strncmp(current_maclient_appid, appid, MAX_APPID_LEN)) {
1409 MAS_LOGD("MA client with current maclient appid connected!");
1411 if (0 == mWakeupClientAppId.compare(appid)) {
1412 mWakeupClientAppId.clear();
1413 mServiceIpc.active_state_change(pid, MA_ACTIVE_STATE_ACTIVE);
1414 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED);
1416 MAS_LOGE("[ERROR] mWakeupClientAppId and appid differ : %s %s",
1417 mWakeupClientAppId.c_str(), appid);
1420 MAS_LOGD("MA client connected, but its appid does not match with current maclient");
1423 mServiceIpc.service_state_change(pid, mas_get_current_service_state());
1425 MAS_LOGE("[ERROR] Fail to retrieve appid");
1431 int CServiceMain::on_deinitialize(int pid) {
1432 MAS_LOGD("[Enter] pid(%d)", pid);
1433 mClientManager.destroy_client_by_pid(pid);
1437 int CServiceMain::on_get_audio_format(int pid, int& rate, int& channel, int& audio_type) {
1438 int main_rate, main_channel, main_audio_type;
1439 int ret = mas_client_get_audio_format(pid,
1440 &main_rate, &main_channel, &main_audio_type);
1442 channel = main_channel;
1443 audio_type = main_audio_type;
1447 int CServiceMain::on_get_audio_source_type(int pid, std::string& type) {
1448 char *main_type = nullptr;
1449 int ret = mas_client_get_audio_source_type(pid, &main_type);
1450 if (0 == ret && main_type) {
1451 type = std::string{main_type};
1456 int CServiceMain::on_send_asr_result(int pid, int event, std::string asr_result) {
1457 return mas_client_send_asr_result(pid, event, asr_result.c_str());
1460 int CServiceMain::on_send_result(int pid, std::string display_text,
1461 std::string utterance_text, std::string result_json) {
1462 return mas_client_send_result(pid,
1463 display_text.c_str(), utterance_text.c_str(), result_json.c_str());
1466 int CServiceMain::on_send_recognition_result(int pid, int result) {
1467 return mas_client_send_recognition_result(pid, result);
1470 int CServiceMain::on_start_streaming_audio_data(int pid, int type) {
1471 return mas_client_start_streaming_audio_data(pid, type);
1474 int CServiceMain::on_stop_streaming_audio_data(int pid, int type) {
1475 return mas_client_stop_streaming_audio_data(pid, type);
1478 int CServiceMain::on_update_voice_feedback_state(int pid, int state) {
1479 return mas_client_update_voice_feedback_state(pid, state);
1482 int CServiceMain::on_send_assistant_specific_command(int pid, std::string command) {
1483 return mas_client_set_assistant_specific_command(pid, command.c_str());
1486 int CServiceMain::on_set_background_volume(int pid, double ratio) {
1487 return mas_client_set_background_volume(pid, ratio);
1490 int CServiceMain::on_set_preprocessing_allow_mode(int pid, int mode, std::string app_id) {
1491 return mas_client_set_preprocessing_allow_mode(pid,
1492 static_cast<ma_preprocessing_allow_mode_e>(mode), app_id.c_str());
1495 int CServiceMain::on_send_preprocessing_result(int pid, int result) {
1496 return mas_client_send_preprocessing_result(pid, result);
1499 int CServiceMain::on_set_wake_word_audio_require_flag(int pid, int require) {
1500 return mas_client_set_wake_word_audio_require_flag(pid, require);
1503 int CServiceMain::on_set_assistant_language(int pid, std::string language) {
1504 return mas_client_set_assistant_language(pid, language.c_str());
1507 int CServiceMain::on_add_wake_word(int pid, std::string wake_word, std::string language) {
1508 return mas_client_add_wake_word(pid, wake_word.c_str(), language.c_str());
1511 int CServiceMain::on_remove_wake_word(int pid, std::string wake_word, std::string language) {
1512 return mas_client_remove_wake_word(pid, wake_word.c_str(), language.c_str());
1515 int CServiceMain::on_ui_initialize(int pid)
1517 return mas_ui_client_initialize(pid);
1520 int CServiceMain::on_ui_deinitialize(int pid)
1522 return mas_ui_client_deinitialize(pid);
1525 int CServiceMain::on_ui_change_assistant(std::string app_id)
1527 return mas_ui_client_change_assistant(app_id.c_str());