Merge changes I94bee8d4,I9dc8f8fb,Id361946a,I1b8aef64 into tizen
[platform/core/uifw/multi-assistant-service.git] / src / service_main.cpp
1 /*
2  * Copyright 2020 Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <tizen.h>
18 #include <service_app.h>
19 #include <app_manager.h>
20 #include <app.h>
21 #include <malloc.h>
22 #include <Ecore.h>
23 #include <vconf.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib.h>
29
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"
35
36 static CServiceMain* g_service_main = nullptr;
37
38 bool CServiceMain::check_preprocessing_assistant_exists()
39 {
40         bool ret = false;
41
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)) {
49                                         ret = true;
50                                 }
51                         }
52                 }
53         }
54
55         MAS_LOGD("result : %d", ret);
56
57         return ret;
58 }
59
60 bool CServiceMain::is_current_preprocessing_assistant(const char* appid)
61 {
62         if (NULL == appid) return false;
63
64         bool ret = false;
65
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) {
70                         ret = true;
71                 }
72         }
73
74         return ret;
75 }
76
77 int CServiceMain::client_get_audio_format(pid_t pid, int* rate, int* channel, int* audio_type)
78 {
79         MAS_LOGD("[Enter] pid(%d)", pid);
80
81         int ret = mServicePlugin.get_recording_audio_format(rate, channel, audio_type);
82         if (0 != ret){
83                 MAS_LOGE("[ERROR] Fail to get recording audio format, ret(%d)", ret);
84         }
85
86         return ret;
87 }
88
89 #define MAX_LOCAL_VARIABLE_STRING_LEN 256
90 int CServiceMain::client_get_audio_source_type(pid_t pid, char** type)
91 {
92         MAS_LOGD("[Enter] pid(%d)", pid);
93
94         if (NULL == type) return -1;
95
96         static char cached[MAX_LOCAL_VARIABLE_STRING_LEN] = {'\0'};
97         int ret = mServicePlugin.get_recording_audio_source_type(type);
98         if (0 != ret){
99                 MAS_LOGE("[ERROR] Fail to get recording audio source type, ret(%d)", ret);
100                 *type = cached;
101         } else if (*type) {
102                 strncpy(cached, *type, MAX_LOCAL_VARIABLE_STRING_LEN - 1);
103                 cached[MAX_LOCAL_VARIABLE_STRING_LEN - 1] = '\0';
104         }
105
106         return ret;
107 }
108
109 int CServiceMain::client_send_preprocessing_information(pid_t pid)
110 {
111         int ret = -1;
112         MAS_LOGD("[Enter] pid(%d)", pid);
113
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());
119         }
120
121         return ret;
122 }
123
124 int CServiceMain::client_send_voice_key_status_change(pid_t pid, ma_voice_key_status_e status)
125 {
126         int ret = -1;
127         MAS_LOGD("[Enter] pid(%d)", pid);
128
129         ret = mServiceIpc.change_voice_key_status(pid, status);
130         if (0 != ret) {
131                 MAS_LOGE("[ERROR] Fail to send voice key status changed information, ret(%d)", ret);
132         }
133         mLastVoiceKeyStatus = status;
134
135         return ret;
136 }
137
138 int CServiceMain::client_send_asr_result(pid_t pid, int event, const char* asr_result)
139 {
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);
142         if (0 != ret){
143                 MAS_LOGE("[ERROR] Fail to send asr result, ret(%d)", ret);
144         }
145
146         // if final event is , launch assistant app which is invoked with wakeup word.
147         /* TO_DO */
148         return ret;
149 }
150
151 int CServiceMain::client_send_result(pid_t pid, const char* display_text,
152         const char* utterance_text, const char* result_json)
153 {
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);
156         if (0 != ret){
157                 MAS_LOGE("[ERROR] Fail to send result, ret(%d)", ret);
158         }
159
160         std::string pid_appid;
161         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
162         if (appid_by_pid) {
163                 pid_appid = *appid_by_pid;
164         }
165         mServicePlugin.update_recognition_result(pid_appid.c_str(), MA_RECOGNITION_RESULT_EVENT_SUCCESS);
166
167         return ret;
168 }
169
170 int CServiceMain::client_send_recognition_result(pid_t pid, int result)
171 {
172         MAS_LOGD("[Enter] pid(%d), result(%d)", pid, result);
173         int ret = mServiceIpc.masc_ui_dbus_send_recognition_result(pid, result);
174         if (0 != ret){
175                 MAS_LOGE("[ERROR] Fail to send recognition result, ret(%d)", ret);
176         }
177
178         std::string pid_appid;
179         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
180         if (appid_by_pid) {
181                 pid_appid = *appid_by_pid;
182         }
183         mServicePlugin.update_recognition_result(pid_appid.c_str(), result);
184
185         return ret;
186 }
187
188 int CServiceMain::client_start_streaming_audio_data(pid_t pid, int type)
189 {
190         int ret = -1;
191         switch(type) {
192                 case MA_AUDIO_STREAMING_DATA_TYPE_CURRENT_UTTERANCE:
193                         ret = mServicePlugin.start_streaming_utterance_data();
194                         process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED);
195                         break;
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 */
199                         break;
200                 case MA_AUDIO_STREAMING_DATA_TYPE_FOLLOW_UP_SPEECH:
201                         ret = mServicePlugin.start_streaming_follow_up_data();
202                         process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED);
203                         break;
204         }
205         return ret;
206 }
207
208 int CServiceMain::client_stop_streaming_audio_data(pid_t pid, int type)
209 {
210         int ret = -1;
211         switch(type) {
212                 case MA_AUDIO_STREAMING_DATA_TYPE_CURRENT_UTTERANCE:
213                         ret = mServicePlugin.stop_streaming_utterance_data();
214                         break;
215                 case MA_AUDIO_STREAMING_DATA_TYPE_PREVIOUS_UTTERANCE:
216                         ret = mServicePlugin.stop_streaming_previous_utterance_data();
217                         break;
218                 case MA_AUDIO_STREAMING_DATA_TYPE_FOLLOW_UP_SPEECH:
219                         ret = mServicePlugin.stop_streaming_follow_up_data();
220                         break;
221         }
222         return ret;
223 }
224
225 int CServiceMain::client_update_voice_feedback_state(pid_t pid, int state)
226 {
227         std::string pid_appid;
228         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
229         if (appid_by_pid) {
230                 pid_appid = *appid_by_pid;
231         }
232         mServicePlugin.update_voice_feedback_state(pid_appid.c_str(), state);
233         return 0;
234 }
235
236 int CServiceMain::client_set_assistant_specific_command(pid_t pid, const char *command)
237 {
238         std::string pid_appid;
239         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
240         if (appid_by_pid) {
241                 pid_appid = *appid_by_pid;
242         }
243         mServicePlugin.set_assistant_specific_command(pid_appid.c_str(), command);
244         return 0;
245 }
246
247 int CServiceMain::client_set_background_volume(pid_t pid, double ratio)
248 {
249         std::string pid_appid;
250         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
251         if (appid_by_pid) {
252                 pid_appid = *appid_by_pid;
253         }
254         mServicePlugin.set_background_volume(pid_appid.c_str(), ratio);
255         return 0;
256 }
257
258 int CServiceMain::client_set_preprocessing_allow_mode(pid_t pid, ma_preprocessing_allow_mode_e mode, const char* appid)
259 {
260         std::string pid_appid;
261         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
262         if (appid_by_pid) {
263                 pid_appid = *appid_by_pid;
264         }
265
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;
270                                 if (appid) {
271                                         strncpy(mClientInfo[loop].preprocessing_allow_appid, appid, MAX_APPID_LEN);
272                                         mClientInfo[loop].preprocessing_allow_appid[MAX_APPID_LEN - 1] = '\0';
273                                 } else {
274                                         mClientInfo[loop].preprocessing_allow_appid[0] = '\0';
275                                 }
276                         }
277                 }
278         }
279
280         process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED);
281
282         return 0;
283 }
284
285 int CServiceMain::client_send_preprocessing_result(pid_t pid, bool result)
286 {
287         std::string pid_appid;
288         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
289         if (appid_by_pid) {
290                 pid_appid = *appid_by_pid;
291         }
292         if (!is_current_preprocessing_assistant(pid_appid.c_str())) return -1;
293
294         const char *current_maclient_appid = NULL;
295         if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
296                 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
297         }
298
299         if (result) {
300                 MAS_LOGD("Preprocessing succeeded, bring (%s) to foreground", pid_appid.c_str());
301                 process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED);
302         } else {
303                 MAS_LOGD("Preprocessing failed, bring (%s) to foreground", current_maclient_appid);
304                 process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED);
305         }
306
307         if (current_maclient_appid) {
308                 pid_t pid_by_appid = mClientManager.find_client_pid_by_appid(
309                         std::string{current_maclient_appid});
310                 mServiceIpc.send_preprocessing_result(pid_by_appid, result);
311         }
312
313         return 0;
314 }
315
316 int CServiceMain::client_set_wake_word_audio_require_flag(pid_t pid, bool require)
317 {
318         std::string pid_appid;
319         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
320         if (appid_by_pid) {
321                 pid_appid = *appid_by_pid;
322         }
323
324         mServicePlugin.set_wake_word_audio_require_flag(pid_appid.c_str(), require);
325         return 0;
326 }
327
328 int CServiceMain::client_set_assistant_language(pid_t pid, const char* language)
329 {
330         std::string pid_appid;
331         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
332         if (appid_by_pid) {
333                 pid_appid = *appid_by_pid;
334         }
335
336         mServicePlugin.set_assistant_language(pid_appid.c_str(), language);
337         return 0;
338 }
339
340 int CServiceMain::client_add_wake_word(pid_t pid, const char* wake_word, const char* language)
341 {
342         std::string pid_appid;
343         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
344         if (appid_by_pid) {
345                 pid_appid = *appid_by_pid;
346         }
347
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);
354                         if (0 == ret) {
355                                 mServiceConfig.save_custom_wake_words(pid_appid.c_str(),
356                                         mClientInfo[loop].wakeup_word,
357                                         mClientInfo[loop].wakeup_language);
358                         } else {
359                                 LOGE("add new wake word failed!");
360                                 return -1;
361                         }
362                 }
363         }
364
365         mServicePlugin.add_assistant_wakeup_word(pid_appid.c_str(), wake_word, language);
366         return 0;
367 }
368
369 int CServiceMain::client_remove_wake_word(pid_t pid, const char* wake_word, const char* language)
370 {
371         std::string pid_appid;
372         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
373         if (appid_by_pid) {
374                 pid_appid = *appid_by_pid;
375         }
376
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);
383                         if (0 == ret) {
384                                 mServiceConfig.save_custom_wake_words(pid_appid.c_str(),
385                                         mClientInfo[loop].wakeup_word,
386                                         mClientInfo[loop].wakeup_language);
387                         }
388                 }
389         }
390
391         mServicePlugin.remove_assistant_wakeup_word(pid_appid.c_str(), wake_word, language);
392         return 0;
393 }
394
395 int CServiceMain::ui_client_initialize(pid_t pid)
396 {
397         MAS_LOGD("[Enter] pid(%d)", pid);
398
399         return 0;
400 }
401
402 int CServiceMain::ui_client_deinitialize(pid_t pid)
403 {
404         MAS_LOGD("[Enter] pid(%d)", pid);
405
406         return 0;
407 }
408
409 int CServiceMain::ui_client_change_assistant(const char* appid)
410 {
411         MAS_LOGD("[Enter]");
412
413         if (NULL == appid) {
414                 MAS_LOGE("NULL parameter");
415                 return -1;
416         }
417
418         bool use_custom_ui = get_client_custom_ui_option_by_appid(appid);
419         mServiceIpc.masc_ui_dbus_enable_common_ui(!use_custom_ui);
420
421         set_current_client_by_appid(appid);
422         pid_t pid = get_client_pid_by_appid(appid);
423         if (pid != -1) {
424                 bring_client_to_foreground(appid);
425                 client_send_preprocessing_information(pid);
426                 if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) {
427                         client_send_voice_key_status_change(pid, mLastVoiceKeyStatus);
428                 }
429
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"));
432                 /*
433                 int ret = mServicePlugin.start_streaming_utterance_data();
434                 if (0 != ret) {
435                         MAS_LOGE("[ERROR] Fail to start streaming utterance data(%d)", ret);
436                 }
437                 */
438         } else {
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"));
441
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                                         launch_client_by_appid(mClientInfo[loop].appid, CLIENT_LAUNCH_MODE_ACTIVATION);
449                                 }
450                         }
451                 }
452         }
453
454         return 0;
455 }
456
457 static int assistant_info_cb(ma_assistant_info_s* info, void* user_data) {
458         int ret = -1;
459         CServiceMain* service_main = static_cast<CServiceMain*>(user_data);
460         if (service_main) {
461                 ret = service_main->add_assistant_info(info);
462         }
463         return ret;
464 }
465
466 int CServiceMain::add_assistant_info(ma_assistant_info_s* info) {
467         MAS_LOGD("__assistant_info_cb called");
468
469         if (NULL == info) {
470                 MAS_LOGE("info NULL, returning");
471                 return -1;
472         }
473         if (NULL == info->app_id) {
474                 MAS_LOGE("app_id NULL, returning");
475                 return -1;
476         }
477
478         int index = -1;
479         int loop = 0;
480         while(-1 == index && loop < MAX_MACLIENT_INFO_NUM) {
481                 if (false == mClientInfo[loop].used) {
482                         index = loop;
483                 }
484                 loop++;
485         }
486         if (-1 != index) {
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';
493
494                 if (is_current_preprocessing_assistant(mClientInfo[index].appid)) {
495                         mCurrentPreprocessingClientInfo = index;
496                 }
497
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';
506                                 } else {
507                                         strncpy(mClientInfo[index].wakeup_language[loop], "", MAX_SUPPORTED_LANGUAGE_LEN);
508                                 }
509                         } else {
510                                 strncpy(mClientInfo[index].wakeup_word[loop], "", MAX_WAKEUP_WORD_LEN);
511                         }
512                 }
513
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';
519                         } else {
520                                 strncpy(mClientInfo[index].supported_language[loop], "", MAX_SUPPORTED_LANGUAGE_LEN);
521                         }
522                 }
523
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';
528                 } else {
529                         mClientInfo[index].wakeup_engine[0] = '\0';
530                         MAS_LOGW("Wakeup engine information not provided for : %s", info->app_id);
531                 }
532                 mClientInfo[index].custom_ui_option = info->custom_ui_option;
533
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;
538         } else {
539                 MAS_LOGD("Couldn't find an empty slot for storing assistant info");
540         }
541
542         MAS_LOGD("__assistant_info_cb end");
543
544         return 0;
545 }
546
547 static void active_state_changed_cb(std::string key, void* user_data)
548 {
549         IPreferenceManager* manager = static_cast<IPreferenceManager*>(user_data);
550         if (nullptr == manager) return;
551
552         boost::optional<bool> activated =
553                 manager->get_bool(MULTI_ASSISTANT_SETTINGS_ACTIVATED);
554         if (activated) {
555                 MAS_LOGD("multi-assistant active state : %d\n", *activated);
556
557                 CServicePlugin *plugin = nullptr;
558                 if (g_service_main) {
559                         plugin = g_service_main->get_service_plugin();
560                 }
561                 if (plugin) {
562                         if (*activated) {
563                                 plugin->activate();
564                         } else {
565                                 plugin->deactivate();
566                         }
567                 } else {
568                         MAS_LOGE("Could not change plugin state : %p %p", g_service_main, plugin);
569                 }
570         }
571 }
572
573 int CServiceMain::initialize_service_plugin(void)
574 {
575         MAS_LOGI("[ENTER]");
576         if (0 != mServicePlugin.initialize()) {
577                 MAS_LOGE("Fail to ws intialize");
578                 return -1;
579         }
580
581         if (0 != mServicePlugin.set_language(mCurrentLanguage.c_str())) {
582                 MAS_LOGE("Fail to ws set language");
583                 return -1;
584         }
585
586         memset(&mClientInfo, 0x00, sizeof(mClientInfo));
587         mCurrentClientInfo = -1;
588         mCurrentPreprocessingClientInfo = -1;
589         mWakeupClientAppId.clear();
590
591         if (0 == mServiceConfig.get_assistant_info(assistant_info_cb, this)) {
592                 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
593                         int inner_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);
601                                 }
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");
611                                                 }
612                                         }
613                                 }
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");
622                                                 }
623                                         }
624                                 }
625                         }
626                 }
627         } else {
628                 MAS_LOGE("Fail to load assistant info");
629         }
630
631         if (0 != mServicePlugin.set_callbacks()) {
632                 MAS_LOGE("Fail to set callbacks");
633                 return -1;
634         }
635
636         return 0;
637 }
638
639 int CServiceMain::deinitialize_service_plugin(void)
640 {
641         MAS_LOGI("[ENTER]");
642         if (0 != mServicePlugin.deactivate()) {
643                 MAS_LOGE("Fail to deactivate");
644         }
645         if (0 != mServicePlugin.deinitialize()) {
646                 MAS_LOGE("Fail to deinitialize");
647         }
648         MAS_LOGI("[END]");
649         return 0;
650 }
651
652 int CServiceMain::process_activated_setting()
653 {
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);
658         } else {
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);
669                                 } else {
670                                         MAS_LOGE("No default assistant, and no assistant installed");
671                                 }
672                         }
673                 }
674 #endif
675         }
676         return 0;
677 }
678
679 int CServiceMain::get_current_client_pid()
680 {
681         int ret = -1;
682         if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
683                 const char *appid = mClientInfo[mCurrentClientInfo].appid;
684                 if (appid) {
685                         ret = mClientManager.find_client_pid_by_appid(std::string{appid});
686                 }
687         }
688         return ret;
689 }
690
691 pid_t CServiceMain::get_current_preprocessing_client_pid()
692 {
693         pid_t ret = -1;
694         if (mCurrentPreprocessingClientInfo >= 0 && mCurrentPreprocessingClientInfo < MAX_MACLIENT_INFO_NUM) {
695                 const char *appid = mClientInfo[mCurrentPreprocessingClientInfo].appid;
696                 if (appid) {
697                         ret = mClientManager.find_client_pid_by_appid(std::string{appid});
698                 }
699         }
700         return ret;
701 }
702
703 pid_t CServiceMain::get_current_audio_processing_pid()
704 {
705         pid_t ret = -1;
706         if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
707                 boost::optional<std::string> audio_processing_appid =
708                         mClientInfo[mCurrentClientInfo].audio_processing_appid;
709                 if (audio_processing_appid) {
710                         boost::optional<pid_t> optional_pid_t;
711                         optional_pid_t = mApplicationManager.get_pid_by_appid((*audio_processing_appid).c_str());
712                         if (optional_pid_t) {
713                                 ret = *optional_pid_t;
714                         }
715                 }
716         }
717         return ret;
718 }
719
720 pid_t CServiceMain::get_client_pid_by_appid(const char *appid)
721 {
722         pid_t ret = -1;
723
724         if (appid) {
725                 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
726         }
727
728         if (-1 != ret && !mApplicationManager.is_application_running(ret)) {
729                 MAS_LOGE("The PID for %s was %d, but it seems to be terminated", appid, ret);
730                 on_deinitialize(ret);
731                 ret = -1;
732         }
733
734         return ret;
735 }
736
737 bool CServiceMain::get_client_custom_ui_option_by_appid(const char *appid)
738 {
739         bool ret = false;
740         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
741                 if (mClientInfo[loop].used &&
742                         0 < strlen(mClientInfo[loop].appid) &&
743                         0 < strlen(mClientInfo[loop].wakeup_word[0])) {
744                         if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
745                                 ret = mClientInfo[loop].custom_ui_option;
746                         }
747                 }
748         }
749         return ret;
750 }
751
752 int CServiceMain::get_client_pid_by_wakeup_word(const char *wakeup_word)
753 {
754         const char *appid = get_client_appid_by_wakeup_word(wakeup_word);
755         return get_client_pid_by_appid(appid);
756 }
757
758 const char* CServiceMain::get_client_appid_by_wakeup_word(const char *wakeup_word)
759 {
760         int loop;
761         const char *appid = NULL;
762
763         if (NULL == wakeup_word) return NULL;
764
765         for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
766                 if (mClientInfo[loop].used &&
767                         0 < strlen(mClientInfo[loop].appid)) {
768                         for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
769                                 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
770                                         if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
771                                                 appid = mClientInfo[loop].appid;
772                                         }
773                                 }
774                         }
775                 }
776         }
777
778         /* Perform extended search, by eliminating blank characters */
779         if (NULL == appid) {
780                 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
781                         if (mClientInfo[loop].used &&
782                                 0 < strlen(mClientInfo[loop].appid)) {
783                                 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
784                                         if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
785                                                 char comparand[MAX_WAKEUP_WORD_LEN];
786                                                 int comparand_index = 0;
787                                                 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
788                                                         if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
789                                                                 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
790                                                         }
791                                                 }
792                                                 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
793                                                         appid = mClientInfo[loop].appid;
794                                                 }
795                                         }
796                                 }
797                         }
798                 }
799         }
800
801         return appid;
802 }
803
804 int CServiceMain::set_current_client_by_wakeup_word(const char *wakeup_word)
805 {
806         int loop;
807         int ret = -1;
808         int prev_selection = mCurrentClientInfo;
809
810         for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
811                 if (mClientInfo[loop].used &&
812                         0 < strlen(mClientInfo[loop].appid)) {
813                         for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
814                                 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
815                                         if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
816                                                 mCurrentClientInfo = loop;
817                                                 ret = 0;
818                                         }
819                                 }
820                         }
821                 }
822         }
823         /* Perform extended search, by eliminating blank characters */
824         if (ret == -1) {
825                 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
826                         if (mClientInfo[loop].used &&
827                                 0 < strlen(mClientInfo[loop].appid)) {
828                                 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
829                                         if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
830                                                 char comparand[MAX_WAKEUP_WORD_LEN];
831                                                 int comparand_index = 0;
832                                                 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
833                                                         if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
834                                                                 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
835                                                         }
836                                                 }
837                                                 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
838                                                         mCurrentClientInfo = loop;
839                                                         ret = 0;
840                                                 }
841                                         }
842                                 }
843                         }
844                 }
845         }
846
847         if (mCurrentClientInfo != prev_selection) {
848                 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
849                         pid_t pid = get_client_pid_by_appid(mClientInfo[prev_selection].appid);
850                         mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
851                 }
852         }
853
854         return ret;
855 }
856
857 int CServiceMain::set_current_client_by_appid(const char *appid)
858 {
859         int ret = -1;
860         int prev_selection = mCurrentClientInfo;
861
862         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
863                 if (mClientInfo[loop].used &&
864                         0 < strlen(mClientInfo[loop].appid) &&
865                         0 < strlen(mClientInfo[loop].wakeup_word[0])) {
866                         if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
867                                 mCurrentClientInfo = loop;
868                                 ret = 0;
869                         }
870                 }
871         }
872
873         if (mCurrentClientInfo != prev_selection) {
874                 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
875                         pid_t pid = get_client_pid_by_appid(mClientInfo[prev_selection].appid);
876                         mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
877                 }
878         }
879
880         return ret;
881 }
882
883 int CServiceMain::launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode)
884 {
885         int result = 0;
886
887         if (NULL == appid || 0 == strlen(appid)) {
888                 MAS_LOGE("appid invalid, failed launching MA Client");
889                 return -1;
890         }
891
892         if (CLIENT_LAUNCH_MODE_PRELAUNCH == launch_mode) {
893                 if (mApplicationManager.is_application_running(appid)) {
894                         MAS_LOGE("appid %s is already running, no need for a prelaunch", appid);
895                         return -1;
896                 }
897
898                 result = mApplicationManager.launch_app_async(appid, true);
899         } else {
900                 result = mApplicationManager.launch_app_async(appid, false);
901         }
902
903         if (CLIENT_LAUNCH_MODE_ACTIVATION == launch_mode) {
904                 bool found = false;
905                 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
906                         if (mClientInfo[loop].used &&
907                                 0 < strlen(mClientInfo[loop].appid)) {
908                                 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
909                                         mWakeupClientAppId = mClientInfo[loop].appid;
910                                         found = true;
911                                 }
912                         }
913                 }
914                 MAS_LOGD("mWakeupClientAppId : %s, %d", mWakeupClientAppId.c_str(), found);
915         }
916
917         return result;
918 }
919
920 int CServiceMain::bring_client_to_foreground(const char* appid)
921 {
922         int ret = 0;
923
924         if (NULL == appid || 0 == strlen(appid)) {
925                 MAS_LOGE("appid invalid, failed launching MA Client");
926                 return -1;
927         }
928
929         if (!mApplicationManager.bring_app_to_foreground(appid)) {
930                 ret = -1;
931         }
932
933         return ret;
934 }
935
936 int CServiceMain::launch_client_by_wakeup_word(const char *wakeup_word)
937 {
938         const char *appid = get_client_appid_by_wakeup_word(wakeup_word);
939         return launch_client_by_appid(appid, CLIENT_LAUNCH_MODE_ACTIVATION);
940 }
941
942 int CServiceMain::prelaunch_default_assistant()
943 {
944         /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
945         boost::optional<bool> prelaunch_mode =
946                 mPreferenceManager.get_bool(WAKEUP_SETTINGS_KEY_PRELAUNCH_MODE);
947         if (prelaunch_mode && *prelaunch_mode) {
948                 const char *default_assistant = NULL;
949                 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
950                         if (!(mApplicationManager.is_application_running(default_assistant))) {
951                                 MAS_LOGD("prelaunching default_assistant_appid : %s", default_assistant);
952                                 launch_client_by_appid(default_assistant, CLIENT_LAUNCH_MODE_PRELAUNCH);
953                         }
954                 }
955         }
956         return 0;
957 }
958
959 int CServiceMain::update_voice_key_support_mode()
960 {
961         /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
962         bool successful = false;
963         const char *default_assistant = NULL;
964         if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
965                 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
966                         if (mClientInfo[loop].used) {
967                                 if (default_assistant &&
968                                         strncmp(default_assistant, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
969                                         float duration = mClientInfo[loop].voice_key_tap_duration;
970                                         if (0.0f < duration) {
971                                                 mServicePlugin.set_voice_key_tap_duration(duration);
972                                         } else {
973                                                 mServicePlugin.unset_voice_key_tap_duration();
974                                         }
975                                         mServicePlugin.set_voice_key_support_mode(
976                                                 mClientInfo[loop].voice_key_support_mode);
977                                         successful = true;
978                                 }
979                         }
980                 }
981         }
982
983         if (!successful) {
984                 mServicePlugin.unset_voice_key_tap_duration();
985                 mServicePlugin.set_voice_key_support_mode(VOICE_KEY_SUPPORT_MODE_NONE);
986         }
987         return 0;
988 }
989
990 ma_preprocessing_allow_mode_e CServiceMain::get_preprocessing_allow_mode(const char* appid)
991 {
992         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
993                 if (appid && mClientInfo[loop].used) {
994                         if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
995                                 return mClientInfo[loop].preprocessing_allow_mode;
996                         }
997                 }
998         }
999         return MA_PREPROCESSING_ALLOW_NONE;
1000 }
1001
1002 /* This might need to be read from settings in the future, but using macro for now */
1003 //#define BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1004
1005 int CServiceMain::process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event)
1006 {
1007         const char* current_maclient_appid = NULL;
1008         const char* preprocessing_allow_appid = NULL;
1009         if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
1010                 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
1011                 preprocessing_allow_appid = mClientInfo[mCurrentClientInfo].preprocessing_allow_appid;
1012         }
1013         ma_preprocessing_allow_mode_e mode = get_preprocessing_allow_mode(current_maclient_appid);
1014
1015         switch (event) {
1016                 case PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED:
1017                 {
1018 #ifndef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1019                         /* If there is no need to bring preprocessing assistant to front,
1020                                 current_maclient should always be brought to front */
1021                         bring_client_to_foreground(current_maclient_appid);
1022 #endif
1023                         mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1024                         if (check_preprocessing_assistant_exists()) {
1025                                 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1026                                         MA_PREPROCESSING_ALLOW_ALL == mode) {
1027                                         if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1028                                                 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1029                                         }
1030                                 }
1031                         } else {
1032 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1033                                 /* If preprocessing assistant does not exist, there is no way to enable
1034                                         preprocessing assistant, so bring current maclient to front right away */
1035                                 bring_client_to_foreground(current_maclient_appid);
1036 #endif
1037                         }
1038                 }
1039                 break;
1040                 case PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED:
1041                 {
1042                         mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1043                         /* Enable preprocessing mode only if the preprocessing assistant exists */
1044                         if (check_preprocessing_assistant_exists()) {
1045                                 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1046                                         MA_PREPROCESSING_ALLOW_ALL == mode) {
1047                                         if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1048                                                 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1049                                         }
1050                                 }
1051                         }
1052                 }
1053                 break;
1054                 case PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED:
1055                 {
1056                         if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED == mCurrentPreprocessingState) {
1057                                 mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_UTTERANCE;
1058                         } else if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED == mCurrentPreprocessingState) {
1059                                 /* If preprocessing assistant does not exist, the current_maclient
1060                                         would have been brought to front already on wakeup event */
1061 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1062                                 if (check_preprocessing_assistant_exists()) {
1063                                         bring_client_to_foreground(current_maclient_appid);
1064                                 }
1065 #endif
1066                                 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1067                         }
1068                 }
1069                 break;
1070                 case PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED:
1071                 {
1072                         mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1073                         if (check_preprocessing_assistant_exists()) {
1074                                 if (MA_PREPROCESSING_ALLOW_FOLLOW_UP == mode ||
1075                                         MA_PREPROCESSING_ALLOW_ALL == mode) {
1076                                         mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_FOLLOW_UP;
1077                                 }
1078                         }
1079                 }
1080                 break;
1081                 case PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED:
1082                 {
1083 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1084                         if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1085                                 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1086                                 boost::optional<std::string> preprocessing_assistant =
1087                                         mPreferenceManager.get_bool(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1088                                 if (preprocessing_assistant) {
1089                                         MAS_LOGD("preprocessing_assistant_appid : %s", (*preprocessing_assistant).c_str());
1090                                         bring_client_to_foreground((*preprocessing_assistant).c_str());
1091                                 }
1092                         }
1093 #endif
1094                         mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1095                 }
1096                 break;
1097                 case PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED:
1098                 {
1099 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1100                         if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1101                                 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1102                                 bring_client_to_foreground(current_maclient_appid);
1103                         }
1104 #endif
1105                         mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1106                 }
1107                 break;
1108         }
1109         return 0;
1110 }
1111
1112 int CServiceMain::set_current_service_state(ma_service_state_e state)
1113 {
1114         mCurrentServiceState = state;
1115
1116         int count = mClientManager.get_client_num();
1117         int i;
1118
1119         for (i = 0; i < count; i++) {
1120                 pid_t pid = mClientManager.find_client_pid_by_index(i);
1121
1122                 if (-1 != pid) {
1123                         int ret = mServiceIpc.change_service_state(pid, state);
1124                         if (0 != ret) {
1125                                 MAS_LOGE("[ERROR] Fail to set service state change to %d, ret(%d)", pid, ret);
1126                         }
1127                 }
1128         }
1129         return 0;
1130 }
1131
1132 ma_service_state_e CServiceMain::get_current_service_state()
1133 {
1134         return mCurrentServiceState;
1135 }
1136
1137 bool CServiceMain::is_valid_wakeup_engine(const char* appid)
1138 {
1139         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM;loop++) {
1140                 if (mClientInfo[loop].used) {
1141                         LOGD("comparing appid : %s %s", mClientInfo[loop].wakeup_engine, appid);
1142                         if (0 == strncmp(mClientInfo[loop].wakeup_engine, appid, MAX_APPID_LEN)) {
1143                                 return true;
1144                         }
1145                 }
1146         }
1147         return false;
1148 }
1149
1150 bool CServiceMain::is_wakeup_engine(const pkgmgrinfo_appinfo_h handle)
1151 {
1152         if (nullptr == g_service_main) return false;
1153         bool is_wakeup_engine = false;
1154
1155         char *appid = NULL;
1156
1157         int ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
1158         if (PMINFO_R_OK == ret && NULL != appid) {
1159                 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM;loop++) {
1160                         if (mClientInfo[loop].used) {
1161                                 LOGD("comparing appid : %s %s", mClientInfo[loop].wakeup_engine, appid);
1162                                 if (0 == strncmp(mClientInfo[loop].wakeup_engine, appid, MAX_APPID_LEN)) {
1163                                         is_wakeup_engine = true;
1164                                 }
1165                         }
1166                 }
1167         } else {
1168                 LOGE("pkgmgrinfo_appinfo_get_appid failed! error code=%d", ret);
1169         }
1170
1171         return is_wakeup_engine;
1172 }
1173
1174 bool CServiceMain::is_voice_assistant(const pkgmgrinfo_appinfo_h handle)
1175 {
1176         bool is_voice_assistant = false;
1177         char* metadata_value = NULL;
1178         const char* voice_assistant_metadata = "http://tizen.org/metadata/multi-assistant/name";
1179         int ret = pkgmgrinfo_appinfo_get_metadata_value(handle, voice_assistant_metadata, &metadata_value);
1180         if (PMINFO_R_OK == ret && NULL != metadata_value) {
1181                 is_voice_assistant = true;
1182         } else {
1183                 LOGE("pkgmgrinfo_appinfo_get_metadata_value failed! error code=%d", ret);
1184         }
1185         return is_voice_assistant;
1186 }
1187
1188 static int pkg_app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
1189 {
1190         if (!g_service_main) return 0;
1191
1192         int *result = (int*)user_data;
1193         if (result) {
1194                 if (g_service_main->is_voice_assistant(handle)) {
1195                         *result = 1;
1196                 } else if (g_service_main->is_wakeup_engine(handle)) {
1197                         *result = 1;
1198                 }
1199         }
1200         return 0;
1201 }
1202
1203 /*
1204 INFO: Package install/update/uninstall scenario
1205 Install and Uninstall are obviously simple.
1206    Install: just INSTALL
1207    Uninstall: just UNINSTALL
1208 Update package (change the source codes and Run As again), there are four scenarios:
1209 1. UPDATE
1210    Source code change
1211 2. UNINSTALL -> INSTALL
1212    This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Check "Enable Project specific settings"
1213    and change Application ID in tizen-manifest.xml file and Run As.
1214 3. UPDATE -> INSTALL
1215    This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Uncheck "Enable Project specific settings"
1216    and change Application ID in tizen-manifest.xml file and Run As.
1217    At UPDATE event, pkgid (package parameter) is invalid...
1218 4. UPDATE
1219    Exceptionally, only UPDATE can be called when Application ID in tizen-manifest.xml file is changed.
1220    At UPDATE event, pkgid (package parameter) is valid, and only appid is changed; the previous appid is invalid.
1221 */
1222 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)
1223 {
1224         CServiceMain* service_main = static_cast<CServiceMain*>(user_data);
1225
1226         int ret = 0;
1227         uid_t uid = getuid();
1228         pkgmgrinfo_pkginfo_h handle = NULL;
1229         static bool in_progress = false;
1230         bool should_exit = false;
1231
1232         if (!package || !type)
1233                 return;
1234
1235         if (PACKAGE_MANAGER_EVENT_TYPE_UPDATE != event_type &&
1236                 PACKAGE_MANAGER_EVENT_TYPE_INSTALL != event_type &&
1237                 PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL != event_type)
1238                 return;
1239
1240         if (PACKAGE_MANAGER_EVENT_STATE_STARTED != event_state &&
1241                 PACKAGE_MANAGER_EVENT_STATE_COMPLETED != event_state)
1242                 return;
1243
1244         bool user = false;
1245         MAS_LOGD("type=%s package=%s event_type=%d event_state=%d progress=%d error=%d",
1246                 type, package, event_type, event_state, progress, error);
1247         ret = pkgmgrinfo_pkginfo_get_pkginfo(package, &handle);
1248         if (ret != PMINFO_R_OK || NULL == handle) {
1249                 MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo(\"%s\",~) returned %d", package, ret);
1250                 /* Try to get in user packages */
1251                 user = true;
1252                 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package, uid, &handle);
1253                 if (ret != PMINFO_R_OK || NULL == handle) {
1254                         MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid());
1255                         return;
1256                 }
1257         }
1258
1259         if (user) {
1260                 /* Try to get in user packages */
1261                 pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret, uid);
1262         }  else {
1263                 /* Try to get in global packages */
1264                 pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret);
1265         }
1266         if (1 == ret) {
1267                 if (PACKAGE_MANAGER_EVENT_STATE_STARTED == event_state) {
1268                         MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_STARTED event : %d", event_type);
1269                         if (false == in_progress) {
1270                                 in_progress = true;
1271                                 if (service_main) {
1272                                         service_main->deinitialize_service_plugin();
1273                                 } else {
1274                                         MAS_LOGE("service_main is NULL");
1275                                 }
1276                         }
1277                 } else if (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state) {
1278                         MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_COMPLETED event : %d", event_type);
1279                         if (false == in_progress) {
1280                                 if (service_main) {
1281                                         service_main->deinitialize_service_plugin();
1282                                 } else {
1283                                         MAS_LOGE("service_main is NULL");
1284                                 }
1285                         }
1286                         /*
1287                         if (service_main) {
1288                                 service_main->initialize_service_plugin();
1289                                 service_main->process_activated_setting();
1290                         } else {
1291                                 MAS_LOGE("service_main is NULL");
1292                         }
1293                         */
1294                         should_exit = true;
1295                         in_progress = false;
1296                 }
1297         }
1298
1299         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1300
1301         if (should_exit) {
1302                 LOGI("Now restarting multi-assistant-service for reloading updated modules");
1303                 service_app_exit();
1304         }
1305
1306         return;
1307 }
1308
1309 bool CServiceMain::app_create(void *data)
1310 {
1311         // Todo: add your code here.
1312         MAS_LOGD("[Enter] Service app create");
1313
1314         g_service_main = this;
1315
1316         mClientManager.set_application_manager(&mApplicationManager);
1317
1318         mServiceIpc.set_client_manager(&mClientManager);
1319         mServiceIpc.set_application_manager(&mApplicationManager);
1320         mServiceIpc.set_service_ipc_observer(this);
1321
1322         mServicePlugin.set_service_ipc(&mServiceIpc);
1323         mServicePlugin.set_service_main(this);
1324
1325         int ret = mServiceIpc.open_connection();
1326         if (0 != ret) {
1327                 MAS_LOGE("[ERROR] Fail to open connection");
1328         }
1329
1330         initialize_service_plugin();
1331
1332         process_activated_setting();
1333
1334         prelaunch_default_assistant();
1335         update_voice_key_support_mode();
1336
1337         /* For the case of preprocessing assistant, it always have to be launched beforehand */
1338         boost::optional<std::string> preprocessing_assistant =
1339                 mPreferenceManager.get_string(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1340         if (preprocessing_assistant) {
1341                 MAS_LOGD("prelaunching preprocessing_assistant_appid : %s", (*preprocessing_assistant).c_str());
1342                 launch_client_by_appid((*preprocessing_assistant).c_str(), CLIENT_LAUNCH_MODE_PRELAUNCH);
1343         }
1344
1345         if (!mPackageManagerHandle) {
1346                 int ret = package_manager_create(&mPackageManagerHandle);
1347                 if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1348                         ret = package_manager_set_event_cb(mPackageManagerHandle, _package_manager_event_cb, this);
1349                         if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1350                                 LOGD("package_manager_set_event_cb succeeded.");
1351                         } else {
1352                                 LOGE("package_manager_set_event_cb failed(%d)", ret);
1353                         }
1354                 } else {
1355                         LOGE("package_manager_create failed(%d)", ret);
1356                 }
1357         }
1358
1359         return true;
1360 }
1361
1362 void CServiceMain::app_terminate(void *data)
1363 {
1364         MAS_LOGI("[ENTER]");
1365         if (mPackageManagerHandle) {
1366                 package_manager_unset_event_cb(mPackageManagerHandle);
1367                 package_manager_destroy(mPackageManagerHandle);
1368                 mPackageManagerHandle = NULL;
1369         }
1370
1371         deinitialize_service_plugin();
1372
1373         mPreferenceManager.unregister_changed_callback(
1374                 MULTI_ASSISTANT_SETTINGS_ACTIVATED, active_state_changed_cb);
1375
1376         int ret = mServiceIpc.close_connection();
1377         if (0 != ret) {
1378                 MAS_LOGE("[ERROR] Fail to close connection");
1379         }
1380
1381         g_service_main = nullptr;
1382
1383         MAS_LOGI("[END]");
1384         return;
1385 }
1386
1387 int CServiceMain::on_initialize(pid_t pid) {
1388         MAS_LOGD("[Enter] pid(%d)", pid);
1389
1390         std::string pid_appid;
1391         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
1392         if (appid_by_pid) {
1393                 pid_appid = *appid_by_pid;
1394                 MAS_LOGD("appid for pid %d is : %s", pid, pid_appid.c_str());
1395
1396                 /* Remove existing client that has same appid, if there's any */
1397                 mClientManager.destroy_client_by_appid(pid_appid.c_str());
1398
1399                 /* And remove a client that has same pid also */
1400                 mClientManager.destroy_client_by_pid(pid);
1401
1402                 mClientManager.create_client(pid, pid_appid.c_str());
1403
1404                 const char *current_maclient_appid = NULL;
1405                 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
1406                         current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
1407                 }
1408
1409                 client_send_preprocessing_information(pid);
1410                 if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) {
1411                         client_send_voice_key_status_change(pid, mLastVoiceKeyStatus);
1412                 }
1413                 if (current_maclient_appid && 0 == pid_appid.compare(current_maclient_appid)) {
1414                         MAS_LOGD("MA client with current maclient appid connected!");
1415
1416                         if (0 == mWakeupClientAppId.compare(pid_appid)) {
1417                                 mWakeupClientAppId.clear();
1418                                 mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_ACTIVE);
1419                                 process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED);
1420                         } else {
1421                                 MAS_LOGE("[ERROR] mWakeupClientAppId and appid differ : %s %s",
1422                                         mWakeupClientAppId.c_str(), pid_appid.c_str());
1423                         }
1424                 } else {
1425                         MAS_LOGD("MA client connected, but its appid does not match with current maclient");
1426                 }
1427
1428                 mServiceIpc.change_service_state(pid, get_current_service_state());
1429         } else {
1430                 MAS_LOGE("[ERROR] Fail to retrieve appid");
1431         }
1432
1433         return 0;
1434 }
1435
1436 int CServiceMain::on_deinitialize(pid_t pid) {
1437         MAS_LOGD("[Enter] pid(%d)", pid);
1438         mClientManager.destroy_client_by_pid(pid);
1439         return 0;
1440 }
1441
1442 int CServiceMain::on_get_audio_format(pid_t pid, int& rate, int& channel, int& audio_type) {
1443         int main_rate, main_channel, main_audio_type;
1444         int ret = client_get_audio_format(pid,
1445                 &main_rate, &main_channel, &main_audio_type);
1446         rate = main_rate;
1447         channel = main_channel;
1448         audio_type = main_audio_type;
1449         return ret;
1450 }
1451
1452 int CServiceMain::on_get_audio_source_type(pid_t pid, std::string& type) {
1453         char *main_type = nullptr;
1454         int ret = client_get_audio_source_type(pid, &main_type);
1455         if (0 == ret && main_type) {
1456                 type = std::string{main_type};
1457         }
1458         return ret;
1459 }
1460
1461 int CServiceMain::on_send_asr_result(pid_t pid, int event, std::string asr_result) {
1462         return client_send_asr_result(pid, event, asr_result.c_str());
1463 }
1464
1465 int CServiceMain::on_send_result(pid_t pid, std::string display_text,
1466         std::string utterance_text, std::string result_json) {
1467         return client_send_result(pid,
1468                 display_text.c_str(), utterance_text.c_str(), result_json.c_str());
1469 }
1470
1471 int CServiceMain::on_send_recognition_result(pid_t pid, int result) {
1472         return client_send_recognition_result(pid, result);
1473 }
1474
1475 int CServiceMain::on_start_streaming_audio_data(pid_t pid, int type) {
1476         return client_start_streaming_audio_data(pid, type);
1477 }
1478
1479 int CServiceMain::on_stop_streaming_audio_data(pid_t pid, int type) {
1480         return client_stop_streaming_audio_data(pid, type);
1481 }
1482
1483 int CServiceMain::on_update_voice_feedback_state(pid_t pid, int state) {
1484         return client_update_voice_feedback_state(pid, state);
1485 }
1486
1487 int CServiceMain::on_send_assistant_specific_command(pid_t pid, std::string command) {
1488         return client_set_assistant_specific_command(pid, command.c_str());
1489 }
1490
1491 int CServiceMain::on_set_background_volume(pid_t pid, double ratio) {
1492         return client_set_background_volume(pid, ratio);
1493 }
1494
1495 int CServiceMain::on_set_preprocessing_allow_mode(pid_t pid, int mode, std::string app_id) {
1496         return client_set_preprocessing_allow_mode(pid,
1497                 static_cast<ma_preprocessing_allow_mode_e>(mode), app_id.c_str());
1498 }
1499
1500 int CServiceMain::on_send_preprocessing_result(pid_t pid, int result) {
1501         return client_send_preprocessing_result(pid, result);
1502 }
1503
1504 int CServiceMain::on_set_wake_word_audio_require_flag(pid_t pid, int require) {
1505         return client_set_wake_word_audio_require_flag(pid, require);
1506 }
1507
1508 int CServiceMain::on_set_assistant_language(pid_t pid, std::string language) {
1509         return client_set_assistant_language(pid, language.c_str());
1510 }
1511
1512 int CServiceMain::on_add_wake_word(pid_t pid, std::string wake_word, std::string language) {
1513         return client_add_wake_word(pid, wake_word.c_str(), language.c_str());
1514 }
1515
1516 int CServiceMain::on_remove_wake_word(pid_t pid, std::string wake_word, std::string language) {
1517         return client_remove_wake_word(pid, wake_word.c_str(), language.c_str());
1518 }
1519
1520 int CServiceMain::on_ui_initialize(pid_t pid)
1521 {
1522         return ui_client_initialize(pid);
1523 }
1524
1525 int CServiceMain::on_ui_deinitialize(pid_t pid)
1526 {
1527         return ui_client_deinitialize(pid);
1528 }
1529
1530 int CServiceMain::on_ui_change_assistant(std::string app_id)
1531 {
1532         return ui_client_change_assistant(app_id.c_str());
1533 }