Check sender validity on streaming related requests
[platform/core/uifw/multi-assistant-service.git] / inc / service_main.h
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
18 #ifndef __SERVICE_MAIN_H__
19 #define __SERVICE_MAIN_H__
20
21 #include <string>
22
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>
28
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"
36
37 typedef enum {
38         CLIENT_LAUNCH_MODE_ACTIVATION,
39         CLIENT_LAUNCH_MODE_PRELAUNCH,
40 } CLIENT_LAUNCH_MODE;
41
42 typedef enum {
43         PREPROCESSING_STATE_NONE,
44         PREPROCESSING_STATE_WAKEUP_PREPROCESS_ENABLED,
45         PREPROCESSING_STATE_WAKEUP_PREPROCESS_DISABLED,
46         PREPROCESSING_STATE_PREPROCESSING_UTTERANCE,
47         PREPROCESSING_STATE_PREPROCESSING_FOLLOW_UP,
48 } PREPROCESSING_STATE;
49
50 typedef enum {
51         PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED,
52         PREPROCESSING_STATE_EVENT_PREPROCESSING_ALLOW_MODE_CHANGED,
53         PREPROCESSING_STATE_EVENT_UTTERANCE_STREAMING_STARTED,
54         PREPROCESSING_STATE_EVENT_FOLLOW_UP_STREAMING_STARTED,
55         PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED,
56         PREPROCESSING_STATE_EVENT_PREPROCESSING_FAILED,
57 } PREPROCESSING_STATE_EVENT;
58
59 #define ENABLE_MULTI_ASSISTANT_BY_DEFAULT
60
61 #define MULTI_ASSISTANT_SETTINGS_ACTIVATED "db/multi-assistant/activated"
62 #define WAKEUP_SETTINGS_KEY_PREPROCESSING_ASSISTANT_APPID "db/multi-assistant/preprocessing_assistant_appid"
63 #define WAKEUP_SETTINGS_KEY_PRELAUNCH_MODE "db/multi-assistant/prelaunch_mode"
64
65 #define MAX_MACLIENT_INFO_NUM 16
66
67 class CServiceMain : public IServiceIpcObserver {
68 public:
69         CServiceMain(IApplicationManager& applicationManager, IPreferenceManager& preferenceManager) :
70                 mApplicationManager{applicationManager},
71                 mPreferenceManager{preferenceManager}
72                 {}
73         virtual ~CServiceMain() {}
74
75         int client_get_audio_format(pid_t pid, int* rate, int* channel, int* audio_type);
76         int client_get_audio_source_type(pid_t pid, char** type);
77         int client_send_preprocessing_information(pid_t pid);
78         int client_send_voice_key_status_change(pid_t pid, ma_voice_key_status_e status);
79         int client_request_speech_data(pid_t pid);
80         int client_send_asr_result(pid_t pid, int event, const char* asr_result);
81         int client_send_result(pid_t pid, const char* display_text,
82                 const char* utterance_text, const char* result_json);
83         int client_send_recognition_result(pid_t pid, int result);
84         int client_start_streaming_audio_data(pid_t pid, int type);
85         int client_stop_streaming_audio_data(pid_t pid, int type);
86         int client_update_voice_feedback_state(pid_t pid, int state);
87         int client_set_assistant_specific_command(pid_t pid, const char *command);
88         int client_set_background_volume(pid_t pid, double ratio);
89         int client_set_preprocessing_allow_mode(pid_t pid, ma_preprocessing_allow_mode_e mode, const char* appid);
90         int client_send_preprocessing_result(pid_t pid, bool result);
91         int client_set_wake_word_audio_require_flag(pid_t pid, bool require);
92         int client_set_assistant_language(pid_t pid, const char* language);
93         int client_add_wake_word(pid_t pid, const char* wake_word, const char* language);
94         int client_remove_wake_word(pid_t pid, const char* wake_word, const char* language);
95         int ui_client_initialize(pid_t pid);
96         int ui_client_deinitialize(pid_t pid);
97         int ui_client_change_assistant(const char* appid);
98         pid_t get_current_client_pid();
99         pid_t get_current_preprocessing_client_pid();
100         pid_t get_current_audio_processing_pid();
101         int get_client_pid_by_wakeup_word(const char *wakeup_word);
102         int get_client_pid_by_appid(const char *appid);
103         std::string get_client_appid_by_pid(pid_t pid);
104         bool get_client_custom_ui_option_by_appid(const char *appid);
105         const char* get_client_appid_by_wakeup_word(const char *wakeup_word);
106         int set_current_client_by_wakeup_word(const char *wakeup_word);
107         int set_current_client_by_appid(const char *appid);
108         int launch_client_by_wakeup_word(const char *wakeup_word);
109         int prelaunch_default_assistant();
110         int set_current_service_state(ma_service_state_e state);
111         int bring_client_to_foreground(const char* appid);
112         ma_service_state_e get_current_service_state();
113         int launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode);
114         int process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event);
115         int update_voice_key_support_mode();
116
117         int on_initialize(pid_t pid) override;
118         int on_deinitialize(pid_t pid) override;
119         int on_get_audio_format(pid_t pid, int& rate, int& channel, int& audio_type) override;
120         int on_get_audio_source_type(pid_t pid, std::string& type) override;
121         int on_send_asr_result(pid_t pid, int event, std::string asr_result) override;
122         int on_send_result(pid_t pid, std::string display_text,
123                 std::string utterance_text, std::string result_json) override;
124         int on_send_recognition_result(pid_t pid, int result) override;
125         int on_start_streaming_audio_data(pid_t pid, int type) override;
126         int on_stop_streaming_audio_data(pid_t pid, int type) override;
127         int on_update_voice_feedback_state(pid_t pid, int state) override;
128         int on_send_assistant_specific_command(pid_t pid, std::string command) override;
129         int on_set_background_volume(pid_t pid, double ratio) override;
130         int on_set_preprocessing_allow_mode(pid_t pid, int mode, std::string app_id) override;
131         int on_send_preprocessing_result(pid_t pid, int result) override;
132         int on_set_wake_word_audio_require_flag(pid_t pid, int require) override;
133         int on_set_assistant_language(pid_t pid, std::string language) override;
134         int on_add_wake_word(pid_t pid, std::string wake_word, std::string language) override;
135         int on_remove_wake_word(pid_t pid, std::string wake_word, std::string language) override;
136
137         int on_ui_initialize(pid_t pid) override;
138         int on_ui_deinitialize(pid_t pid) override;
139         int on_ui_change_assistant(std::string app_id) override;
140
141         bool app_create(void *data);
142         void app_terminate(void *data);
143
144         int initialize_service_plugin(void);
145         int deinitialize_service_plugin(void);
146
147         bool is_valid_wakeup_engine(const char* appid);
148         int process_activated_setting();
149
150         int add_assistant_info(ma_assistant_info_s* info);
151
152         CServicePlugin* get_service_plugin() { return &mServicePlugin; }
153
154         bool is_wakeup_engine(const pkgmgrinfo_appinfo_h handle);
155         bool is_voice_assistant(const pkgmgrinfo_appinfo_h handle);
156 private:
157         bool check_preprocessing_assistant_exists();
158         ma_preprocessing_allow_mode_e get_preprocessing_allow_mode(const char* appid);
159         bool is_current_preprocessing_assistant(const char* appid);
160         bool is_current_assistant(pid_t pid);
161
162 private:
163         std::string mCurrentLanguage{"en_US"};
164
165         typedef struct {
166                 bool used{false};
167                 char appid[MAX_APPID_LEN];
168                 char wakeup_word[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN];
169                 char wakeup_language[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
170                 char wakeup_engine[MAX_APPID_LEN];
171                 char supported_language[MAX_SUPPORTED_LANGUAGES_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
172                 bool custom_ui_option{false};
173                 VOICE_KEY_SUPPORT_MODE voice_key_support_mode{VOICE_KEY_SUPPORT_MODE_PUSH_TO_TALK};
174                 float voice_key_tap_duration{0.0f};
175
176                 ma_preprocessing_allow_mode_e preprocessing_allow_mode{MA_PREPROCESSING_ALLOW_NONE};
177                 char preprocessing_allow_appid[MAX_APPID_LEN];
178
179                 boost::optional<std::string> audio_processing_appid;
180         } ma_client_info;
181
182         ma_client_info mClientInfo[MAX_MACLIENT_INFO_NUM];
183
184         int mCurrentClientInfo{0};
185         int mCurrentPreprocessingClientInfo{-1};
186         std::string mWakeupClientAppId;
187         package_manager_h mPackageManagerHandle{NULL};
188
189         PREPROCESSING_STATE mCurrentPreprocessingState{PREPROCESSING_STATE_NONE};
190         ma_service_state_e mCurrentServiceState{MA_SERVICE_STATE_INACTIVE};
191
192         CServiceConfig mServiceConfig;
193         CServicePlugin mServicePlugin;
194         CServiceIpcDbus mServiceIpc;
195
196         ma_voice_key_status_e mLastVoiceKeyStatus{MA_VOICE_KEY_STATUS_RELEASED_AFTER_PUSH};
197
198         CClientManager mClientManager;
199
200         IApplicationManager& mApplicationManager;
201         IPreferenceManager& mPreferenceManager;
202 };
203
204 #endif /* __SERVICE_MAIN_H__ */