Divide wakeup manager into a class and its wrapper
[platform/core/uifw/multi-assistant-service.git] / plugins / wakeup-manager / inc / wakeup_engine_manager.h
1 /*
2  * Copyright 2018  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 #ifndef _WAKEUP_ENGINE_MANAGER_H_
18 #define _WAKEUP_ENGINE_MANAGER_H_
19
20 #include "wakeup_manager_wrapper.h"
21
22 #include <atomic>
23 #include <string>
24 #include <thread>
25 #include <vector>
26
27 namespace multiassistant
28 {
29 namespace wakeup
30 {
31
32 using namespace std;
33
34 /**************************************************************************************
35  *** Definitions for wakeup engine interface
36  *************************************************************************************/
37 #define MA_WAKEUP_ENGINE_PATH                                   tzplatform_mkpath(tzplatform_getid("TZ_SYS_RO_SHARE"), "multiassistant/engines")
38 /* Need to check whether this dedicated engine path also needs to be configurable */
39 #define MA_WAKEUP_DEDICATED_ENGINE_PATH                 "shared/lib/libwakeup-engine.so"
40
41 #define MA_WAKEUP_ENGINE_FUNC_INITIALIZE "wakeup_engine_initialize"
42 typedef int (*wakeup_engine_initialize)(void);
43 #define MA_WAKEUP_ENGINE_FUNC_DEINITIALIZE "wakeup_engine_deinitialize"
44 typedef int (*wakeup_engine_deinitialize)(void);
45 #define MA_WAKEUP_ENGINE_FUNC_ACTIVATE "wakeup_engine_activate"
46 typedef int (*wakeup_engine_activate)(void);
47 #define MA_WAKEUP_ENGINE_FUNC_DEACTIVATE "wakeup_engine_deactivate"
48 typedef int (*wakeup_engine_deactivate)(void);
49 #define MA_WAKEUP_ENGINE_FUNC_ADD_WAKEUP_WORD "wakeup_engine_add_wakeup_word"
50 typedef int (*wakeup_engine_add_wakeup_word)(const char* appid, const char* wakeup_word, const char* language);
51 #define MA_WAKEUP_ENGINE_FUNC_ADD_LANGUAGE "wakeup_engine_add_language"
52 typedef int (*wakeup_engine_add_language)(const char* appid, const char* language);
53 #define MA_WAKEUP_ENGINE_FUNC_SET_LANGUAGE "wakeup_engine_set_language"
54 typedef int (*wakeup_engine_set_language)(const char* language);
55 #define MA_WAKEUP_ENGINE_FUNC_UPDATE_MANAGER_STATE "wakeup_engine_update_manager_state"
56 typedef int (*wakeup_engine_update_manager_state)(wakeup_manager_state_e state);
57 #define MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_FORMAT "wakeup_engine_set_audio_format"
58 typedef int (*wakeup_engine_set_audio_format)(int rate, int channel, int audio_type);
59 #define MA_WAKEUP_ENGINE_FUNC_GET_AUDIO_FORMAT "wakeup_engine_get_audio_format"
60 typedef int (*wakeup_engine_get_audio_format)(int* rate, int* channel, int* audio_type);
61 #define MA_WAKEUP_ENGINE_FUNC_FEED_AUDIO_DATA "wakeup_engine_feed_audio_data"
62 typedef int (*wakeup_engine_feed_audio_data)(long time, void* data, int len);
63 #define MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA_COUNT "wakeup_engine_get_utterance_data_count"
64 typedef int (*wakeup_engine_get_utterance_data_count)(void);
65 #define MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA "wakeup_engine_get_utterance_data"
66 typedef int (*wakeup_engine_get_utterance_data)(int index, wakeup_speech_data *data);
67 #define MA_WAKEUP_ENGINE_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND "wakeup_engine_set_assistant_specific_command"
68 typedef int (*wakeup_engine_set_assistant_specific_command)(const char* appid, const char* command);
69 #define MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_EVENT_CALLBACK "wakeup_engine_set_wakeup_event_callback"
70 typedef int (*wakeup_engine_set_wakeup_event_callback)(wakeup_service_wakeup_event_cb callback, void* user_data);
71 #define MA_WAKEUP_ENGINE_FUNC_SET_SPEECH_STATUS_CALLBACK "wakeup_engine_set_speech_status_callback"
72 typedef int (*wakeup_engine_set_speech_status_callback)(wakeup_service_speech_status_cb callback, void* user_data);
73 #define MA_WAKEUP_ENGINE_FUNC_SET_ERROR_CALLBACK "wakeup_engine_set_error_callback"
74 typedef int (*wakeup_engine_set_error_callback)(wakeup_service_error_cb callback, void* user_data);
75 #define MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_DATA_REQUIRE_STATUS_CALLBACK "wakeup_engine_set_audio_data_require_status_callback"
76 typedef int (*wakeup_engine_set_audio_data_require_status_callback)(wakeup_service_audio_data_require_status_cb callback, void* user_data);
77
78 typedef struct {
79         wakeup_engine_initialize                                                                initialize;
80         wakeup_engine_deinitialize                                                              deinitialize;
81         wakeup_engine_activate                                                                  activate;
82         wakeup_engine_deactivate                                                                deactivate;
83         wakeup_engine_add_wakeup_word                                                   add_wakeup_word;
84         wakeup_engine_add_language                                                              add_language;
85         wakeup_engine_set_language                                                              set_language;
86         wakeup_engine_update_manager_state                                              update_manager_state;
87         wakeup_engine_set_audio_format                                                  set_audio_format;
88         wakeup_engine_get_audio_format                                                  get_audio_format;
89         wakeup_engine_feed_audio_data                                                   feed_audio_data;
90         wakeup_engine_get_utterance_data_count                                  get_utterance_data_count;
91         wakeup_engine_get_utterance_data                                                get_utterance_data;
92         wakeup_engine_set_assistant_specific_command                    set_assistant_specific_command;
93         wakeup_engine_set_wakeup_event_callback                                 set_wakeup_event_callback;
94         wakeup_engine_set_speech_status_callback                                set_speech_status_callback;
95         wakeup_engine_set_error_callback                                                set_error_callback;
96         wakeup_engine_set_audio_data_require_status_callback    set_audio_data_require_status_callback;
97 } wakeup_engine_interface;
98
99 class IEngineEventObserver
100 {
101 public:
102         virtual ~IEngineEventObserver() = default;
103         virtual bool on_wakeup_event(string engine_name, wakeup_event_info info) = 0;
104         virtual bool on_speech_status(string engine_name, wakeup_service_speech_status_e status) = 0;
105         virtual bool on_error(string engine_name, int error_code, string error_message) = 0;
106         virtual bool on_audio_data_require_status(string engine_name, bool require) = 0;
107
108         virtual bool on_streaming_audio_data(
109                 wakeup_speech_streaming_event_e event, void* buffer, unsigned int len) = 0;
110 };
111
112 class CWakeupEngineManager
113 {
114 public:
115         CWakeupEngineManager();
116         CWakeupEngineManager(IEngineEventObserver *observer);
117         virtual ~CWakeupEngineManager();
118
119         CWakeupEngineManager(const CWakeupEngineManager&) = delete;
120         CWakeupEngineManager& operator=(const CWakeupEngineManager&) = delete;
121
122         void initialize();
123         void deinitialize();
124
125         void subscribe(IEngineEventObserver *observer);
126         void unsubscribe(IEngineEventObserver *observer);
127
128         bool get_audio_data_required();
129         void set_selected_wakeup_info(wakeup_event_info info);
130
131         void start_streaming_current_utterance_data();
132         void stop_streaming_current_utterance_data();
133
134         void update_manager_state(wakeup_manager_state_e state);
135
136         void engine_add_target_assistant(string engine_name, string appid);
137         void engine_add_wakeup_word(string appid, string wakeup_word, string language);
138         void engine_set_assistant_specific_command(string appid, string command);
139         void engine_feed_audio_data(long time, void* data, int len);
140
141         bool on_wakeup_event(string engine_name, wakeup_event_info info);
142         bool on_speech_status(string engine_name, wakeup_service_speech_status_e status);
143         bool on_error(string engine_name, int error_code, string error_message);
144         bool on_audio_data_require_status(string engine_name, bool require);
145 private:
146         typedef struct {
147                 string engine_name;
148                 bool active{false};
149                 bool enabled{false};
150                 bool audio_data_require_status{false};
151                 string engine_path;
152                 wakeup_engine_interface interface{nullptr, };
153                 vector<string> assistant_list;
154                 void *engine_handle{nullptr};
155         } EngineInfo;
156
157         void add_engine_directory(string name, string path);
158         void add_engine(string name, string path);
159
160         vector<IEngineEventObserver*> mObservers;
161
162         void streaming_speech_data_thread_func();
163
164         vector<EngineInfo> mEngineInfo;
165         const EngineInfo* mSelectedEngine{nullptr};
166         bool mAudioDataRequired{false};
167
168         thread mStreamingThread;
169         atomic_bool mStopStreamingThread{false};
170 };
171
172 } // wakeup
173 } // multiassistant
174
175 #endif /* _WAKEUP_ENGINE_MANAGER_H_ */