Fix defects detected by static analysis tool
[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         if (0 == mServiceConfig.get_assistant_info(mas_assistant_info_cb, this)) {
587                 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
588                         int inner_loop;
589                         if (0 < strlen(mClientInfo[loop].appid)) {
590                                 mServiceConfig.load_custom_wake_words(mClientInfo[loop].appid,
591                                         mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language);
592                                 if (0 < strlen(mClientInfo[loop].wakeup_engine)) {
593                                         mServicePlugin.set_assistant_wakeup_engine(
594                                                 mClientInfo[loop].appid,
595                                                 mClientInfo[loop].wakeup_engine);
596                                 }
597                                 for (inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
598                                         if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
599                                                 MAS_LOGD("Registering wakeup word %s for app %s",
600                                                         mClientInfo[loop].wakeup_word[inner_loop], mClientInfo[loop].appid);
601                                                 if (0 != mServicePlugin.add_assistant_wakeup_word(
602                                                         mClientInfo[loop].appid,
603                                                         mClientInfo[loop].wakeup_word[inner_loop],
604                                                         mClientInfo[loop].wakeup_language[inner_loop])) {
605                                                         MAS_LOGE("Fail to add assistant's wakeup word");
606                                                 }
607                                         }
608                                 }
609                                 for (inner_loop = 0; inner_loop < MAX_SUPPORTED_LANGUAGES_NUM; inner_loop++) {
610                                         if (0 < strlen(mClientInfo[loop].supported_language[inner_loop])) {
611                                                 MAS_LOGD("Adding language %s for app %s",
612                                                         mClientInfo[loop].supported_language[inner_loop], mClientInfo[loop].appid);
613                                                 if (0 != mServicePlugin.add_assistant_language(
614                                                         mClientInfo[loop].appid,
615                                                         mClientInfo[loop].supported_language[inner_loop])) {
616                                                         MAS_LOGE("Fail to add assistant's language");
617                                                 }
618                                         }
619                                 }
620                         }
621                 }
622         } else {
623                 MAS_LOGE("Fail to load assistant info");
624         }
625
626         if (0 != mServicePlugin.set_callbacks()) {
627                 MAS_LOGE("Fail to set callbacks");
628                 return -1;
629         }
630
631         return 0;
632 }
633
634 int CServiceMain::deinitialize_service_plugin(void)
635 {
636         MAS_LOGI("[ENTER]");
637         if (0 != mServicePlugin.deactivate()) {
638                 MAS_LOGE("Fail to deactivate");
639         }
640         if (0 != mServicePlugin.deinitialize()) {
641                 MAS_LOGE("Fail to deinitialize");
642         }
643         MAS_LOGI("[END]");
644         return 0;
645 }
646
647 int CServiceMain::process_activated_setting()
648 {
649         if (mPreferenceManager.register_changed_callback(
650                 MULTI_ASSISTANT_SETTINGS_ACTIVATED, active_state_changed_cb, &mPreferenceManager)) {
651                 /* Activate / deactivate according to the vconf key setting */
652                 active_state_changed_cb(std::string{}, &mPreferenceManager);
653         } else {
654 #ifdef ENABLE_MULTI_ASSISTANT_BY_DEFAULT
655                 /* Multi-assistant needs to be enabled by default, unless disabled explicitly */
656                 mServicePlugin.activate();
657                 const char *default_assistant = NULL;
658                 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
659                         if (NULL == default_assistant) {
660                                 if (mClientInfo[0].used) {
661                                         default_assistant = mClientInfo[0].appid;
662                                         MAS_LOGW("No default assistant, setting %s as default", default_assistant);
663                                         mServicePlugin.set_default_assistant(default_assistant);
664                                 } else {
665                                         MAS_LOGE("No default assistant, and no assistant installed");
666                                 }
667                         }
668                 }
669 #endif
670         }
671         return 0;
672 }
673
674 int CServiceMain::mas_get_current_client_pid()
675 {
676         int ret = -1;
677         if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
678                 const char *appid = mClientInfo[mCurrentClientInfo].appid;
679                 if (appid) {
680                         ret = mClientManager.find_client_pid_by_appid(std::string{appid});
681                 }
682         }
683         return ret;
684 }
685
686 int CServiceMain::mas_get_current_preprocessing_client_pid()
687 {
688         int ret = -1;
689         if (mCurrentPreprocessingClientInfo >= 0 && mCurrentPreprocessingClientInfo < MAX_MACLIENT_INFO_NUM) {
690                 const char *appid = mClientInfo[mCurrentPreprocessingClientInfo].appid;
691                 if (appid) {
692                         ret = mClientManager.find_client_pid_by_appid(std::string{appid});
693                 }
694         }
695         return ret;
696 }
697
698 int CServiceMain::mas_get_client_pid_by_appid(const char *appid)
699 {
700         int ret = -1;
701
702         if (appid) {
703                 ret = mClientManager.find_client_pid_by_appid(std::string{appid});
704         }
705
706         if (-1 != ret && !mApplicationManager.is_application_running(ret)) {
707                 MAS_LOGE("The PID for %s was %d, but it seems to be terminated", appid, ret);
708                 on_deinitialize(ret);
709                 ret = -1;
710         }
711
712         return ret;
713 }
714
715 bool CServiceMain::mas_get_client_custom_ui_option_by_appid(const char *appid)
716 {
717         bool ret = false;
718         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
719                 if (mClientInfo[loop].used &&
720                         0 < strlen(mClientInfo[loop].appid) &&
721                         0 < strlen(mClientInfo[loop].wakeup_word[0])) {
722                         if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
723                                 ret = mClientInfo[loop].custom_ui_option;
724                         }
725                 }
726         }
727         return ret;
728 }
729
730 int CServiceMain::mas_get_client_pid_by_wakeup_word(const char *wakeup_word)
731 {
732         const char *appid = mas_get_client_appid_by_wakeup_word(wakeup_word);
733         return mas_get_client_pid_by_appid(appid);
734 }
735
736 const char* CServiceMain::mas_get_client_appid_by_wakeup_word(const char *wakeup_word)
737 {
738         int loop;
739         const char *appid = NULL;
740
741         if (NULL == wakeup_word) return NULL;
742
743         for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
744                 if (mClientInfo[loop].used &&
745                         0 < strlen(mClientInfo[loop].appid)) {
746                         for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
747                                 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
748                                         if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
749                                                 appid = mClientInfo[loop].appid;
750                                         }
751                                 }
752                         }
753                 }
754         }
755
756         /* Perform extended search, by eliminating blank characters */
757         if (NULL == appid) {
758                 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && NULL == appid; loop++) {
759                         if (mClientInfo[loop].used &&
760                                 0 < strlen(mClientInfo[loop].appid)) {
761                                 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
762                                         if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
763                                                 char comparand[MAX_WAKEUP_WORD_LEN];
764                                                 int comparand_index = 0;
765                                                 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
766                                                         if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
767                                                                 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
768                                                         }
769                                                 }
770                                                 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
771                                                         appid = mClientInfo[loop].appid;
772                                                 }
773                                         }
774                                 }
775                         }
776                 }
777         }
778
779         return appid;
780 }
781
782 int CServiceMain::mas_set_current_client_by_wakeup_word(const char *wakeup_word)
783 {
784         int loop;
785         int ret = -1;
786         int prev_selection = mCurrentClientInfo;
787
788         for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
789                 if (mClientInfo[loop].used &&
790                         0 < strlen(mClientInfo[loop].appid)) {
791                         for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
792                                 if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
793                                         if (0 == strncmp(wakeup_word, mClientInfo[loop].wakeup_word[inner_loop], MAX_WAKEUP_WORD_LEN)) {
794                                                 mCurrentClientInfo = loop;
795                                                 ret = 0;
796                                         }
797                                 }
798                         }
799                 }
800         }
801         /* Perform extended search, by eliminating blank characters */
802         if (ret == -1) {
803                 for (loop = 0; loop < MAX_MACLIENT_INFO_NUM && -1 == ret; loop++) {
804                         if (mClientInfo[loop].used &&
805                                 0 < strlen(mClientInfo[loop].appid)) {
806                                 for (int inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
807                                         if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
808                                                 char comparand[MAX_WAKEUP_WORD_LEN];
809                                                 int comparand_index = 0;
810                                                 for (int index = 0; index < MAX_WAKEUP_WORD_LEN; index++) {
811                                                         if (' ' != mClientInfo[loop].wakeup_word[inner_loop][index]) {
812                                                                 comparand[comparand_index++] = mClientInfo[loop].wakeup_word[inner_loop][index];
813                                                         }
814                                                 }
815                                                 if (0 == strncmp(wakeup_word, comparand, MAX_WAKEUP_WORD_LEN)) {
816                                                         mCurrentClientInfo = loop;
817                                                         ret = 0;
818                                                 }
819                                         }
820                                 }
821                         }
822                 }
823         }
824
825         if (mCurrentClientInfo != prev_selection) {
826                 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
827                         int pid = mas_get_client_pid_by_appid(mClientInfo[prev_selection].appid);
828                         mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
829                 }
830         }
831
832         return ret;
833 }
834
835 int CServiceMain::mas_set_current_client_by_appid(const char *appid)
836 {
837         int ret = -1;
838         int prev_selection = mCurrentClientInfo;
839
840         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
841                 if (mClientInfo[loop].used &&
842                         0 < strlen(mClientInfo[loop].appid) &&
843                         0 < strlen(mClientInfo[loop].wakeup_word[0])) {
844                         if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
845                                 mCurrentClientInfo = loop;
846                                 ret = 0;
847                         }
848                 }
849         }
850
851         if (mCurrentClientInfo != prev_selection) {
852                 if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
853                         int pid = mas_get_client_pid_by_appid(mClientInfo[prev_selection].appid);
854                         mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
855                 }
856         }
857
858         return ret;
859 }
860
861 int CServiceMain::mas_launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode)
862 {
863         int result = 0;
864
865         if (NULL == appid || 0 == strlen(appid)) {
866                 MAS_LOGE("appid invalid, failed launching MA Client");
867                 return -1;
868         }
869
870         if (CLIENT_LAUNCH_MODE_PRELAUNCH == launch_mode) {
871                 if (mApplicationManager.is_application_running(appid)) {
872                         MAS_LOGE("appid %s is already running, no need for a prelaunch", appid);
873                         return -1;
874                 }
875
876                 result = mApplicationManager.launch_app_async(appid, true);
877         } else {
878                 result = mApplicationManager.launch_app_async(appid, false);
879         }
880
881         if (CLIENT_LAUNCH_MODE_ACTIVATION == launch_mode) {
882                 bool found = false;
883                 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
884                         if (mClientInfo[loop].used &&
885                                 0 < strlen(mClientInfo[loop].appid)) {
886                                 if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
887                                         mWakeupClientAppId = mClientInfo[loop].appid;
888                                         found = true;
889                                 }
890                         }
891                 }
892                 MAS_LOGD("mWakeupClientAppId : %s, %d", mWakeupClientAppId.c_str(), found);
893         }
894
895         return result;
896 }
897
898 int CServiceMain::mas_bring_client_to_foreground(const char* appid)
899 {
900         int ret = 0;
901
902         if (NULL == appid || 0 == strlen(appid)) {
903                 MAS_LOGE("appid invalid, failed launching MA Client");
904                 return -1;
905         }
906
907         if (!mApplicationManager.bring_app_to_foreground(appid)) {
908                 ret = -1;
909         }
910
911         return ret;
912 }
913
914 int CServiceMain::mas_launch_client_by_wakeup_word(const char *wakeup_word)
915 {
916         const char *appid = mas_get_client_appid_by_wakeup_word(wakeup_word);
917         return mas_launch_client_by_appid(appid, CLIENT_LAUNCH_MODE_ACTIVATION);
918 }
919
920 int CServiceMain::mas_prelaunch_default_assistant()
921 {
922         /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
923         boost::optional<bool> prelaunch_mode =
924                 mPreferenceManager.get_bool(WAKEUP_SETTINGS_KEY_PRELAUNCH_MODE);
925         if (prelaunch_mode && *prelaunch_mode) {
926                 const char *default_assistant = NULL;
927                 if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
928                         if (!(mApplicationManager.is_application_running(default_assistant))) {
929                                 MAS_LOGD("prelaunching default_assistant_appid : %s", default_assistant);
930                                 mas_launch_client_by_appid(default_assistant, CLIENT_LAUNCH_MODE_PRELAUNCH);
931                         }
932                 }
933         }
934         return 0;
935 }
936
937 int CServiceMain::mas_update_voice_key_support_mode()
938 {
939         /* CHECK NEEDED : should the code segment below and activation logic above be moved to wakeup manger? */
940         bool successful = false;
941         const char *default_assistant = NULL;
942         if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
943                 for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
944                         if (mClientInfo[loop].used) {
945                                 if (default_assistant &&
946                                         strncmp(default_assistant, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
947                                         float duration = mClientInfo[loop].voice_key_tap_duration;
948                                         if (0.0f < duration) {
949                                                 mServicePlugin.set_voice_key_tap_duration(duration);
950                                         } else {
951                                                 mServicePlugin.unset_voice_key_tap_duration();
952                                         }
953                                         mServicePlugin.set_voice_key_support_mode(
954                                                 mClientInfo[loop].voice_key_support_mode);
955                                         successful = true;
956                                 }
957                         }
958                 }
959         }
960
961         if (!successful) {
962                 mServicePlugin.unset_voice_key_tap_duration();
963                 mServicePlugin.set_voice_key_support_mode(VOICE_KEY_SUPPORT_MODE_NONE);
964         }
965         return 0;
966 }
967
968 ma_preprocessing_allow_mode_e CServiceMain::get_preprocessing_allow_mode(const char* appid)
969 {
970         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
971                 if (appid && mClientInfo[loop].used) {
972                         if (strncmp(appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) {
973                                 return mClientInfo[loop].preprocessing_allow_mode;
974                         }
975                 }
976         }
977         return MA_PREPROCESSING_ALLOW_NONE;
978 }
979
980 /* This might need to be read from settings in the future, but using macro for now */
981 //#define BRING_PREPROCESSING_ASSISTANT_TO_FRONT
982
983 int CServiceMain::mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event)
984 {
985         const char* current_maclient_appid = NULL;
986         const char* preprocessing_allow_appid = NULL;
987         if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
988                 current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
989                 preprocessing_allow_appid = mClientInfo[mCurrentClientInfo].preprocessing_allow_appid;
990         }
991         ma_preprocessing_allow_mode_e mode = get_preprocessing_allow_mode(current_maclient_appid);
992
993         switch (event) {
994                 case PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED:
995                 {
996 #ifndef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
997                         /* If there is no need to bring preprocessing assistant to front,
998                                 current_maclient should always be brought to front */
999                         mas_bring_client_to_foreground(current_maclient_appid);
1000 #endif
1001                         mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1002                         if (check_preprocessing_assistant_exists()) {
1003                                 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1004                                         MA_PREPROCESSING_ALLOW_ALL == mode) {
1005                                         if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1006                                                 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1007                                         }
1008                                 }
1009                         } else {
1010 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1011                                 /* If preprocessing assistant does not exist, there is no way to enable
1012                                         preprocessing assistant, so bring current maclient to front right away */
1013                                 mas_bring_client_to_foreground(current_maclient_appid);
1014 #endif
1015                         }
1016                 }
1017                 break;
1018                 case PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED:
1019                 {
1020                         mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED;
1021                         /* Enable preprocessing mode only if the preprocessing assistant exists */
1022                         if (check_preprocessing_assistant_exists()) {
1023                                 if (MA_PREPROCESSING_ALLOW_UTTERANCE == mode ||
1024                                         MA_PREPROCESSING_ALLOW_ALL == mode) {
1025                                         if (is_current_preprocessing_assistant(preprocessing_allow_appid)) {
1026                                                 mCurrentPreprocessingState = PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED;
1027                                         }
1028                                 }
1029                         }
1030                 }
1031                 break;
1032                 case PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED:
1033                 {
1034                         if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED == mCurrentPreprocessingState) {
1035                                 mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_UTTERANCE;
1036                         } else if (PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED == mCurrentPreprocessingState) {
1037                                 /* If preprocessing assistant does not exist, the current_maclient
1038                                         would have been brought to front already on wakeup event */
1039 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1040                                 if (check_preprocessing_assistant_exists()) {
1041                                         mas_bring_client_to_foreground(current_maclient_appid);
1042                                 }
1043 #endif
1044                                 mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1045                         }
1046                 }
1047                 break;
1048                 case PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED:
1049                 {
1050                         mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1051                         if (check_preprocessing_assistant_exists()) {
1052                                 if (MA_PREPROCESSING_ALLOW_FOLLOW_UP == mode ||
1053                                         MA_PREPROCESSING_ALLOW_ALL == mode) {
1054                                         mCurrentPreprocessingState = PREPROCESSING_STATE_PREPROCESSING_FOLLOW_UP;
1055                                 }
1056                         }
1057                 }
1058                 break;
1059                 case PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED:
1060                 {
1061 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1062                         if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1063                                 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1064                                 boost::optional<std::string> preprocessing_assistant =
1065                                         mPreferenceManager.get_bool(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1066                                 if (preprocessing_assistant) {
1067                                         MAS_LOGD("preprocessing_assistant_appid : %s", (*preprocessing_assistant).c_str());
1068                                         mas_bring_client_to_foreground((*preprocessing_assistant).c_str());
1069                                 }
1070                         }
1071 #endif
1072                         mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1073                 }
1074                 break;
1075                 case PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED:
1076                 {
1077 #ifdef BRING_PREPROCESSING_ASSISTANT_TO_FRONT
1078                         if (PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED == mCurrentPreprocessingState ||
1079                                 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED == mCurrentPreprocessingState) {
1080                                 mas_bring_client_to_foreground(current_maclient_appid);
1081                         }
1082 #endif
1083                         mCurrentPreprocessingState = PREPROCESSING_STATE_NONE;
1084                 }
1085                 break;
1086         }
1087         return 0;
1088 }
1089
1090 int CServiceMain::mas_set_current_service_state(ma_service_state_e state)
1091 {
1092         mCurrentServiceState = state;
1093
1094         int count = mClientManager.get_client_num();
1095         int i;
1096
1097         for (i = 0; i < count; i++) {
1098                 int pid = mClientManager.find_client_pid_by_index(i);
1099
1100                 if (-1 != pid) {
1101                         int ret = mServiceIpc.change_service_state(pid, state);
1102                         if (0 != ret) {
1103                                 MAS_LOGE("[ERROR] Fail to set service state change to %d, ret(%d)", pid, ret);
1104                         }
1105                 }
1106         }
1107         return 0;
1108 }
1109
1110 ma_service_state_e CServiceMain::mas_get_current_service_state()
1111 {
1112         return mCurrentServiceState;
1113 }
1114
1115 bool CServiceMain::is_valid_wakeup_engine(const char* appid)
1116 {
1117         for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM;loop++) {
1118                 if (mClientInfo[loop].used) {
1119                         LOGD("comparing appid : %s %s", mClientInfo[loop].wakeup_engine, appid);
1120                         if (0 == strncmp(mClientInfo[loop].wakeup_engine, appid, MAX_APPID_LEN)) {
1121                                 return true;
1122                         }
1123                 }
1124         }
1125         return false;
1126 }
1127
1128 int pkg_app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
1129 {
1130         if (nullptr == g_service_main) return -1;
1131
1132         char *appid = NULL;
1133
1134         int ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
1135         if (PMINFO_R_OK == ret && NULL != appid) {
1136                 int *result = (int*)user_data;
1137                 if (result) {
1138                         bool exists = g_service_main->is_valid_wakeup_engine(appid);
1139                         if (exists) {
1140                                 *result = 1;
1141                         }
1142                 }
1143         } else {
1144                 LOGE("pkgmgrinfo_appinfo_get_appid failed! error code=%d", ret);
1145         }
1146
1147         return 0;
1148 }
1149
1150 /*
1151 INFO: Package install/update/uninstall scenario
1152 Install and Uninstall are obviously simple.
1153    Install: just INSTALL
1154    Uninstall: just UNINSTALL
1155 Update package (change the source codes and Run As again), there are four scenarios:
1156 1. UPDATE
1157    Source code change
1158 2. UNINSTALL -> INSTALL
1159    This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Check "Enable Project specific settings"
1160    and change Application ID in tizen-manifest.xml file and Run As.
1161 3. UPDATE -> INSTALL
1162    This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Uncheck "Enable Project specific settings"
1163    and change Application ID in tizen-manifest.xml file and Run As.
1164    At UPDATE event, pkgid (package parameter) is invalid...
1165 4. UPDATE
1166    Exceptionally, only UPDATE can be called when Application ID in tizen-manifest.xml file is changed.
1167    At UPDATE event, pkgid (package parameter) is valid, and only appid is changed; the previous appid is invalid.
1168 */
1169 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)
1170 {
1171         CServiceMain* service_main = static_cast<CServiceMain*>(user_data);
1172
1173         int ret = 0;
1174         uid_t uid = getuid();
1175         pkgmgrinfo_pkginfo_h handle = NULL;
1176         static bool in_progress = false;
1177         bool should_exit = false;
1178
1179         if (!package || !type)
1180                 return;
1181
1182         if (PACKAGE_MANAGER_EVENT_TYPE_UPDATE != event_type &&
1183                 PACKAGE_MANAGER_EVENT_TYPE_INSTALL != event_type &&
1184                 PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL != event_type)
1185                 return;
1186
1187         if (PACKAGE_MANAGER_EVENT_STATE_STARTED != event_state &&
1188                 PACKAGE_MANAGER_EVENT_STATE_COMPLETED != event_state)
1189                 return;
1190
1191         bool user = false;
1192         MAS_LOGD("type=%s package=%s event_type=%d event_state=%d progress=%d error=%d",
1193                 type, package, event_type, event_state, progress, error);
1194         ret = pkgmgrinfo_pkginfo_get_pkginfo(package, &handle);
1195         if (ret != PMINFO_R_OK || NULL == handle) {
1196                 MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid());
1197                 /* Try to get in user packages */
1198                 user = true;
1199                 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package, uid, &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                         return;
1203                 }
1204         }
1205
1206         if (user) {
1207                 /* Try to get in user packages */
1208                 pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret, uid);
1209         }  else {
1210                 /* Try to get in global packages */
1211                 pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret);
1212         }
1213         if (1 == ret) {
1214                 if (PACKAGE_MANAGER_EVENT_STATE_STARTED == event_state) {
1215                         MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_STARTED event : %d", event_type);
1216                         if (false == in_progress) {
1217                                 in_progress = true;
1218                                 if (service_main) {
1219                                         service_main->deinitialize_service_plugin();
1220                                 } else {
1221                                         MAS_LOGE("service_main is NULL");
1222                                 }
1223                         }
1224                 } else if (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state) {
1225                         MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_COMPLETED event : %d", event_type);
1226                         if (false == in_progress) {
1227                                 if (service_main) {
1228                                         service_main->deinitialize_service_plugin();
1229                                 } else {
1230                                         MAS_LOGE("service_main is NULL");
1231                                 }
1232                         }
1233                         /*
1234                         if (service_main) {
1235                                 service_main->initialize_service_plugin();
1236                                 service_main->process_activated_setting();
1237                         } else {
1238                                 MAS_LOGE("service_main is NULL");
1239                         }
1240                         */
1241                         should_exit = true;
1242                         in_progress = false;
1243                 }
1244         }
1245
1246         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1247
1248         if (should_exit) {
1249                 LOGI("Now restarting multi-assistant-service for reloading updated modules");
1250                 service_app_exit();
1251         }
1252
1253         return;
1254 }
1255
1256 bool CServiceMain::app_create(void *data)
1257 {
1258         // Todo: add your code here.
1259         MAS_LOGD("[Enter] Service app create");
1260
1261         g_service_main = this;
1262
1263         mClientManager.set_application_manager(&mApplicationManager);
1264
1265         mServiceIpc.set_client_manager(&mClientManager);
1266         mServiceIpc.set_service_ipc_observer(this);
1267
1268         mServicePlugin.set_service_ipc(&mServiceIpc);
1269         mServicePlugin.set_service_main(this);
1270
1271         int ret = mServiceIpc.open_connection();
1272         if (0 != ret) {
1273                 MAS_LOGE("[ERROR] Fail to open connection");
1274         }
1275
1276         initialize_service_plugin();
1277
1278         process_activated_setting();
1279
1280         mas_prelaunch_default_assistant();
1281         mas_update_voice_key_support_mode();
1282
1283         /* For the case of preprocessing assistant, it always have to be launched beforehand */
1284         boost::optional<std::string> preprocessing_assistant =
1285                 mPreferenceManager.get_string(WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID);
1286         if (preprocessing_assistant) {
1287                 MAS_LOGD("prelaunching preprocessing_assistant_appid : %s", (*preprocessing_assistant).c_str());
1288                 mas_launch_client_by_appid((*preprocessing_assistant).c_str(), CLIENT_LAUNCH_MODE_PRELAUNCH);
1289         }
1290
1291         if (!mPackageManagerHandle) {
1292                 int ret = package_manager_create(&mPackageManagerHandle);
1293                 if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1294                         ret = package_manager_set_event_cb(mPackageManagerHandle, _package_manager_event_cb, this);
1295                         if (ret == PACKAGE_MANAGER_ERROR_NONE) {
1296                                 LOGD("package_manager_set_event_cb succeeded.");
1297                         } else {
1298                                 LOGE("package_manager_set_event_cb failed(%d)", ret);
1299                         }
1300                 } else {
1301                         LOGE("package_manager_create failed(%d)", ret);
1302                 }
1303         }
1304
1305         return true;
1306 }
1307
1308 void CServiceMain::app_terminate(void *data)
1309 {
1310         MAS_LOGI("[ENTER]");
1311         if (mPackageManagerHandle) {
1312                 package_manager_unset_event_cb(mPackageManagerHandle);
1313                 package_manager_destroy(mPackageManagerHandle);
1314                 mPackageManagerHandle = NULL;
1315         }
1316
1317         deinitialize_service_plugin();
1318
1319         mPreferenceManager.unregister_changed_callback(
1320                 MULTI_ASSISTANT_SETTINGS_ACTIVATED, active_state_changed_cb);
1321
1322         int ret = mServiceIpc.close_connection();
1323         if (0 != ret) {
1324                 MAS_LOGE("[ERROR] Fail to close connection");
1325         }
1326
1327         g_service_main = nullptr;
1328
1329         MAS_LOGI("[END]");
1330         return;
1331 }
1332
1333 int CServiceMain::on_initialize(int pid) {
1334         MAS_LOGD("[Enter] pid(%d)", pid);
1335
1336         std::string pid_appid;
1337         boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
1338         if (appid_by_pid) {
1339                 pid_appid = *appid_by_pid;
1340                 MAS_LOGD("appid for pid %d is : %s", pid, pid_appid.c_str());
1341
1342                 /* Remove existing client that has same appid, if there's any */
1343                 mClientManager.destroy_client_by_appid(pid_appid.c_str());
1344
1345                 /* And remove a client that has same pid also */
1346                 mClientManager.destroy_client_by_pid(pid);
1347
1348                 mClientManager.create_client(pid, pid_appid.c_str());
1349
1350                 const char *current_maclient_appid = NULL;
1351                 if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) {
1352                         current_maclient_appid = mClientInfo[mCurrentClientInfo].appid;
1353                 }
1354
1355                 mas_client_send_preprocessing_information(pid);
1356                 if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) {
1357                         mas_client_send_voice_key_status_change(pid, mLastVoiceKeyStatus);
1358                 }
1359                 if (current_maclient_appid && 0 == pid_appid.compare(current_maclient_appid)) {
1360                         MAS_LOGD("MA client with current maclient appid connected!");
1361
1362                         if (0 == mWakeupClientAppId.compare(pid_appid)) {
1363                                 mWakeupClientAppId.clear();
1364                                 mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_ACTIVE);
1365                                 mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED);
1366                         } else {
1367                                 MAS_LOGE("[ERROR] mWakeupClientAppId and appid differ : %s %s",
1368                                         mWakeupClientAppId.c_str(), pid_appid.c_str());
1369                         }
1370                 } else {
1371                         MAS_LOGD("MA client connected, but its appid does not match with current maclient");
1372                 }
1373
1374                 mServiceIpc.change_service_state(pid, mas_get_current_service_state());
1375         } else {
1376                 MAS_LOGE("[ERROR] Fail to retrieve appid");
1377         }
1378
1379         return 0;
1380 }
1381
1382 int CServiceMain::on_deinitialize(int pid) {
1383         MAS_LOGD("[Enter] pid(%d)", pid);
1384         mClientManager.destroy_client_by_pid(pid);
1385         return 0;
1386 }
1387
1388 int CServiceMain::on_get_audio_format(int pid, int& rate, int& channel, int& audio_type) {
1389         int main_rate, main_channel, main_audio_type;
1390         int ret = mas_client_get_audio_format(pid,
1391                 &main_rate, &main_channel, &main_audio_type);
1392         rate = main_rate;
1393         channel = main_channel;
1394         audio_type = main_audio_type;
1395         return ret;
1396 }
1397
1398 int CServiceMain::on_get_audio_source_type(int pid, std::string& type) {
1399         char *main_type = nullptr;
1400         int ret = mas_client_get_audio_source_type(pid, &main_type);
1401         if (0 == ret && main_type) {
1402                 type = std::string{main_type};
1403         }
1404         return ret;
1405 }
1406
1407 int CServiceMain::on_send_asr_result(int pid, int event, std::string asr_result) {
1408         return mas_client_send_asr_result(pid, event, asr_result.c_str());
1409 }
1410
1411 int CServiceMain::on_send_result(int pid, std::string display_text,
1412         std::string utterance_text, std::string result_json) {
1413         return mas_client_send_result(pid,
1414                 display_text.c_str(), utterance_text.c_str(), result_json.c_str());
1415 }
1416
1417 int CServiceMain::on_send_recognition_result(int pid, int result) {
1418         return mas_client_send_recognition_result(pid, result);
1419 }
1420
1421 int CServiceMain::on_start_streaming_audio_data(int pid, int type) {
1422         return mas_client_start_streaming_audio_data(pid, type);
1423 }
1424
1425 int CServiceMain::on_stop_streaming_audio_data(int pid, int type) {
1426         return mas_client_stop_streaming_audio_data(pid, type);
1427 }
1428
1429 int CServiceMain::on_update_voice_feedback_state(int pid, int state) {
1430         return mas_client_update_voice_feedback_state(pid, state);
1431 }
1432
1433 int CServiceMain::on_send_assistant_specific_command(int pid, std::string command) {
1434         return mas_client_set_assistant_specific_command(pid, command.c_str());
1435 }
1436
1437 int CServiceMain::on_set_background_volume(int pid, double ratio) {
1438         return mas_client_set_background_volume(pid, ratio);
1439 }
1440
1441 int CServiceMain::on_set_preprocessing_allow_mode(int pid, int mode, std::string app_id) {
1442         return mas_client_set_preprocessing_allow_mode(pid,
1443                 static_cast<ma_preprocessing_allow_mode_e>(mode), app_id.c_str());
1444 }
1445
1446 int CServiceMain::on_send_preprocessing_result(int pid, int result) {
1447         return mas_client_send_preprocessing_result(pid, result);
1448 }
1449
1450 int CServiceMain::on_set_wake_word_audio_require_flag(int pid, int require) {
1451         return mas_client_set_wake_word_audio_require_flag(pid, require);
1452 }
1453
1454 int CServiceMain::on_set_assistant_language(int pid, std::string language) {
1455         return mas_client_set_assistant_language(pid, language.c_str());
1456 }
1457
1458 int CServiceMain::on_add_wake_word(int pid, std::string wake_word, std::string language) {
1459         return mas_client_add_wake_word(pid, wake_word.c_str(), language.c_str());
1460 }
1461
1462 int CServiceMain::on_remove_wake_word(int pid, std::string wake_word, std::string language) {
1463         return mas_client_remove_wake_word(pid, wake_word.c_str(), language.c_str());
1464 }
1465
1466 int CServiceMain::on_ui_initialize(int pid)
1467 {
1468         return mas_ui_client_initialize(pid);
1469 }
1470
1471 int CServiceMain::on_ui_deinitialize(int pid)
1472 {
1473         return mas_ui_client_deinitialize(pid);
1474 }
1475
1476 int CServiceMain::on_ui_change_assistant(std::string app_id)
1477 {
1478         return mas_ui_client_change_assistant(app_id.c_str());
1479 }