2 * Copyright 2020 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #ifndef __SERVICE_MAIN_H__
19 #define __SERVICE_MAIN_H__
23 #include <dlog/dlog.h>
24 #include <multi_assistant.h>
25 #include <multi_assistant_internal.h>
26 #include <package_manager.h>
27 #include <pkgmgr-info.h>
29 #include "service_common.h"
30 #include "service_config.h"
31 #include "client_manager.h"
32 #include "service_plugin.h"
33 #include "service_ipc_dbus.h"
34 #include "application_manager.h"
35 #include "preference_manager.h"
42 CLIENT_LAUNCH_MODE_ACTIVATION,
43 CLIENT_LAUNCH_MODE_PRELAUNCH,
47 PREPROCESSING_STATE_NONE,
48 PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED,
49 PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED,
50 PREPROCESSING_STATE_PREPROCESSING_UTTERANCE,
51 PREPROCESSING_STATE_PREPROCESSING_FOLLOW_UP,
52 } PREPROCESSING_STATE;
55 PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED,
56 PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED,
57 PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED,
58 PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED,
59 PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED,
60 PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED,
61 } PREPROCESSING_STATE_EVENT;
63 #define ENABLE_MULTI_ASSISTANT_BY_DEFAULT
65 #define MULTI_ASSISTANT_SETTINGS_ACTIVATED "db/multi-assistant/activated"
66 #define WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID "db/multi-assistant/preprocessing_assistant_appid"
67 #define WAKEUP_SETTINGS_KEY_PRELAUNCH_MODE "db/multi-assistant/prelaunch_mode"
69 #define MAX_MACLIENT_INFO_NUM 16
71 class CServiceMain : public IServiceIpcObserver {
73 CServiceMain(IApplicationManager& applicationManager, IPreferenceManager& preferenceManager) :
74 mApplicationManager{applicationManager},
75 mPreferenceManager{preferenceManager}
77 virtual ~CServiceMain() {}
79 int client_get_audio_format(pid_t pid, int* rate, int* channel, int* audio_type);
80 int client_get_audio_source_type(pid_t pid, char** type);
81 int client_send_preprocessing_information(pid_t pid);
82 int client_send_voice_key_status_change(pid_t pid, ma_voice_key_status_e status);
83 int client_request_speech_data(pid_t pid);
84 int client_send_asr_result(pid_t pid, int event, const char* asr_result);
85 int client_send_result(pid_t pid, const char* display_text,
86 const char* utterance_text, const char* result_json);
87 int client_send_recognition_result(pid_t pid, int result);
88 int client_start_streaming_audio_data(pid_t pid, int type);
89 int client_stop_streaming_audio_data(pid_t pid, int type);
90 int client_update_voice_feedback_state(pid_t pid, int state);
91 int client_set_assistant_specific_command(pid_t pid, const char *command);
92 int client_set_background_volume(pid_t pid, double ratio);
93 int client_set_preprocessing_allow_mode(pid_t pid, ma_preprocessing_allow_mode_e mode, const char* appid);
94 int client_send_preprocessing_result(pid_t pid, bool result);
95 int client_set_wake_word_audio_require_flag(pid_t pid, bool require);
96 int client_set_assistant_language(pid_t pid, const char* language);
97 int client_add_wake_word(pid_t pid, const char* wake_word, const char* language);
98 int client_remove_wake_word(pid_t pid, const char* wake_word, const char* language);
99 int ui_client_initialize(pid_t pid);
100 int ui_client_deinitialize(pid_t pid);
101 int ui_client_change_assistant(const char* appid);
102 pid_t get_current_client_pid();
103 pid_t get_current_preprocessing_client_pid();
104 pid_t get_current_audio_processing_pid();
105 int get_client_pid_by_wakeup_word(const char *wakeup_word);
106 int get_client_pid_by_appid(const char *appid);
107 std::string get_client_appid_by_pid(pid_t pid);
108 bool get_client_custom_ui_option_by_appid(const char *appid);
109 const char* get_client_appid_by_wakeup_word(const char *wakeup_word);
110 int set_current_client_by_wakeup_word(const char *wakeup_word);
111 int set_current_client_by_appid(const char *appid);
112 int launch_client_by_wakeup_word(const char *wakeup_word);
113 int prelaunch_default_assistant();
114 int set_current_service_state(ma_service_state_e state);
115 int bring_client_to_foreground(const char* appid);
116 ma_service_state_e get_current_service_state();
117 int launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode);
118 int process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event);
119 int update_voice_key_support_mode();
121 virtual int on_initialize(pid_t pid) override;
122 virtual int on_deinitialize(pid_t pid) override;
123 virtual int on_get_audio_format(pid_t pid, int& rate, int& channel, int& audio_type) override;
124 virtual int on_get_audio_source_type(pid_t pid, std::string& type) override;
125 virtual int on_send_asr_result(pid_t pid, int event, std::string asr_result) override;
126 virtual int on_send_result(pid_t pid, std::string display_text,
127 std::string utterance_text, std::string result_json) override;
128 virtual int on_send_recognition_result(pid_t pid, int result) override;
129 virtual int on_start_streaming_audio_data(pid_t pid, int type) override;
130 virtual int on_stop_streaming_audio_data(pid_t pid, int type) override;
131 virtual int on_update_voice_feedback_state(pid_t pid, int state) override;
132 virtual int on_send_assistant_specific_command(pid_t pid, std::string command) override;
133 virtual int on_set_background_volume(pid_t pid, double ratio) override;
134 virtual int on_set_preprocessing_allow_mode(pid_t pid, int mode, std::string app_id) override;
135 virtual int on_send_preprocessing_result(pid_t pid, int result) override;
136 virtual int on_set_wake_word_audio_require_flag(pid_t pid, int require) override;
137 virtual int on_set_assistant_language(pid_t pid, std::string language) override;
138 virtual int on_add_wake_word(pid_t pid, std::string wake_word, std::string language) override;
139 virtual int on_remove_wake_word(pid_t pid, std::string wake_word, std::string language) override;
141 virtual int on_ui_initialize(pid_t pid) override;
142 virtual int on_ui_deinitialize(pid_t pid) override;
143 virtual int on_ui_change_assistant(std::string app_id) override;
145 bool app_create(void *data);
146 void app_terminate(void *data);
148 int initialize_service_plugin(void);
149 int deinitialize_service_plugin(void);
151 bool is_valid_wakeup_engine(const char* appid);
152 int process_activated_setting();
154 int add_assistant_info(ma_assistant_info_s* info);
156 CServicePlugin* get_service_plugin() { return &mServicePlugin; }
158 bool is_wakeup_engine(const pkgmgrinfo_appinfo_h handle);
159 bool is_voice_assistant(const pkgmgrinfo_appinfo_h handle);
161 bool check_preprocessing_assistant_exists();
162 ma_preprocessing_allow_mode_e get_preprocessing_allow_mode(const char* appid);
163 bool is_current_preprocessing_assistant(const char* appid);
166 std::string mCurrentLanguage{"en_US"};
170 char appid[MAX_APPID_LEN];
171 char wakeup_word[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN];
172 char wakeup_language[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
173 char wakeup_engine[MAX_APPID_LEN];
174 char supported_language[MAX_SUPPORTED_LANGUAGES_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
175 bool custom_ui_option{false};
176 VOICE_KEY_SUPPORT_MODE voice_key_support_mode{VOICE_KEY_SUPPORT_MODE_PUSH_TO_TALK};
177 float voice_key_tap_duration{0.0f};
179 ma_preprocessing_allow_mode_e preprocessing_allow_mode{MA_PREPROCESSING_ALLOW_NONE};
180 char preprocessing_allow_appid[MAX_APPID_LEN];
182 boost::optional<std::string> audio_processing_appid;
185 ma_client_info mClientInfo[MAX_MACLIENT_INFO_NUM];
187 int mCurrentClientInfo{0};
188 int mCurrentPreprocessingClientInfo{-1};
189 std::string mWakeupClientAppId;
190 package_manager_h mPackageManagerHandle{NULL};
192 PREPROCESSING_STATE mCurrentPreprocessingState{PREPROCESSING_STATE_NONE};
193 ma_service_state_e mCurrentServiceState{MA_SERVICE_STATE_INACTIVE};
195 CServiceConfig mServiceConfig;
196 CServicePlugin mServicePlugin;
197 CServiceIpcDbus mServiceIpc;
199 ma_voice_key_status_e mLastVoiceKeyStatus{MA_VOICE_KEY_STATUS_RELEASED_AFTER_PUSH};
201 CClientManager mClientManager;
203 IApplicationManager& mApplicationManager;
204 IPreferenceManager& mPreferenceManager;
211 #endif /* __SERVICE_MAIN_H__ */