Fix defects detected by static analysis tool
[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 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 typedef enum {
42         CLIENT_LAUNCH_MODE_ACTIVATION,
43         CLIENT_LAUNCH_MODE_PRELAUNCH,
44 } CLIENT_LAUNCH_MODE;
45
46 typedef enum {
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;
53
54 typedef enum {
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;
62
63 #define ENABLE_MULTI_ASSISTANT_BY_DEFAULT
64
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"
68
69 #define MAX_MACLIENT_INFO_NUM 16
70
71 class CServiceMain : public IServiceIpcObserver {
72 public:
73         CServiceMain(IApplicationManager& applicationManager, IPreferenceManager& preferenceManager) :
74                 mApplicationManager{applicationManager},
75                 mPreferenceManager{preferenceManager}
76                 {}
77         virtual ~CServiceMain() {}
78
79         int mas_client_get_audio_format(int pid, int* rate, int* channel, int* audio_type);
80         int mas_client_get_audio_source_type(int pid, char** type);
81         int mas_client_send_preprocessing_information(int pid);
82         int mas_client_send_voice_key_status_change(int pid, ma_voice_key_status_e status);
83         int mas_client_request_speech_data(int pid);
84         int mas_client_send_asr_result(int pid, int event, const char* asr_result);
85         int mas_client_send_result(int pid, const char* display_text,
86                 const char* utterance_text, const char* result_json);
87         int mas_client_send_recognition_result(int pid, int result);
88         int mas_client_start_streaming_audio_data(int pid, int type);
89         int mas_client_stop_streaming_audio_data(int pid, int type);
90         int mas_client_update_voice_feedback_state(int pid, int state);
91         int mas_client_set_assistant_specific_command(int pid, const char *command);
92         int mas_client_set_background_volume(int pid, double ratio);
93         int mas_client_set_preprocessing_allow_mode(int pid, ma_preprocessing_allow_mode_e mode, const char* appid);
94         int mas_client_send_preprocessing_result(int pid, bool result);
95         int mas_client_set_wake_word_audio_require_flag(int pid, bool require);
96         int mas_client_set_assistant_language(int pid, const char* language);
97         int mas_client_add_wake_word(int pid, const char* wake_word, const char* language);
98         int mas_client_remove_wake_word(int pid, const char* wake_word, const char* language);
99         int mas_ui_client_initialize(int pid);
100         int mas_ui_client_deinitialize(int pid);
101         int mas_ui_client_change_assistant(const char* appid);
102         int mas_get_current_client_pid();
103         int mas_get_current_preprocessing_client_pid();
104         int mas_get_client_pid_by_wakeup_word(const char *wakeup_word);
105         int mas_get_client_pid_by_appid(const char *appid);
106         std::string mas_get_client_appid_by_pid(int pid);
107         bool mas_get_client_custom_ui_option_by_appid(const char *appid);
108         const char* mas_get_client_appid_by_wakeup_word(const char *wakeup_word);
109         int mas_set_current_client_by_wakeup_word(const char *wakeup_word);
110         int mas_set_current_client_by_appid(const char *appid);
111         int mas_launch_client_by_wakeup_word(const char *wakeup_word);
112         int mas_prelaunch_default_assistant();
113         int mas_set_current_service_state(ma_service_state_e state);
114         int mas_bring_client_to_foreground(const char* appid);
115         ma_service_state_e mas_get_current_service_state();
116         int mas_launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode);
117         int mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event);
118         int mas_update_voice_key_support_mode();
119
120         virtual int on_initialize(int pid) override;
121         virtual int on_deinitialize(int pid) override;
122         virtual int on_get_audio_format(int pid, int& rate, int& channel, int& audio_type) override;
123         virtual int on_get_audio_source_type(int pid, std::string& type) override;
124         virtual int on_send_asr_result(int pid, int event, std::string asr_result) override;
125         virtual int on_send_result(int pid, std::string display_text,
126                 std::string utterance_text, std::string result_json) override;
127         virtual int on_send_recognition_result(int pid, int result) override;
128         virtual int on_start_streaming_audio_data(int pid, int type) override;
129         virtual int on_stop_streaming_audio_data(int pid, int type) override;
130         virtual int on_update_voice_feedback_state(int pid, int state) override;
131         virtual int on_send_assistant_specific_command(int pid, std::string command) override;
132         virtual int on_set_background_volume(int pid, double ratio) override;
133         virtual int on_set_preprocessing_allow_mode(int pid, int mode, std::string app_id) override;
134         virtual int on_send_preprocessing_result(int pid, int result) override;
135         virtual int on_set_wake_word_audio_require_flag(int pid, int require) override;
136         virtual int on_set_assistant_language(int pid, std::string language) override;
137         virtual int on_add_wake_word(int pid, std::string wake_word, std::string language) override;
138         virtual int on_remove_wake_word(int pid, std::string wake_word, std::string language) override;
139
140         virtual int on_ui_initialize(int pid) override;
141         virtual int on_ui_deinitialize(int pid) override;
142         virtual int on_ui_change_assistant(std::string app_id) override;
143
144         bool app_create(void *data);
145         void app_terminate(void *data);
146
147         int initialize_service_plugin(void);
148         int deinitialize_service_plugin(void);
149
150         bool is_valid_wakeup_engine(const char* appid);
151         int process_activated_setting();
152
153         int add_assistant_info(ma_assistant_info_s* info);
154
155         CServicePlugin* get_service_plugin() { return &mServicePlugin; }
156
157         bool is_wakeup_engine(const pkgmgrinfo_appinfo_h handle);
158         bool is_voice_assistant(const pkgmgrinfo_appinfo_h handle);
159 private:
160         bool check_preprocessing_assistant_exists();
161         ma_preprocessing_allow_mode_e get_preprocessing_allow_mode(const char* appid);
162         bool is_current_preprocessing_assistant(const char* appid);
163
164 private:
165         std::string mCurrentLanguage{"en_US"};
166
167         typedef struct {
168                 bool used{false};
169                 char appid[MAX_APPID_LEN];
170                 char wakeup_word[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN];
171                 char wakeup_language[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
172                 char wakeup_engine[MAX_APPID_LEN];
173                 char supported_language[MAX_SUPPORTED_LANGUAGES_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
174                 bool custom_ui_option{false};
175                 VOICE_KEY_SUPPORT_MODE voice_key_support_mode{VOICE_KEY_SUPPORT_MODE_PUSH_TO_TALK};
176                 float voice_key_tap_duration{0.0f};
177
178                 ma_preprocessing_allow_mode_e preprocessing_allow_mode{MA_PREPROCESSING_ALLOW_NONE};
179                 char preprocessing_allow_appid[MAX_APPID_LEN];
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 #ifdef __cplusplus
205 }
206 #endif
207
208 #endif /* __SERVICE_MAIN_H__ */