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