blob: 52f4caa401b0a9f4d85c4b6930912c026fe2e848 [file] [log] [blame]
Quinn Male2e883752019-03-22 11:28:54 -07001/* st_session.h
2 *
3 * This file contains a sound trigger user session abstraction. This
4 * abstraction represents a single st session from the application/framework
5 * point of view.
6 *
Quinn Male2bfe13b2020-08-27 16:53:51 -07007 * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
Quinn Male2e883752019-03-22 11:28:54 -07008 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of The Linux Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#ifndef ST_SESSION_H
36#define ST_SESSION_H
37
38#include <stdint.h>
39#include <pthread.h>
40#include "st_hw_session.h"
41#include "sound_trigger_platform.h"
42#include "st_common_defs.h"
43
Quinn Male2e883752019-03-22 11:28:54 -070044/* Below are the states that can be requested from the client */
45enum client_states_t {
46 ST_STATE_IDLE,
47 ST_STATE_LOADED,
48 ST_STATE_ACTIVE
49};
50
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -070051typedef enum {
52 ST_DET_LOW_POWER_MODE,
53 ST_DET_HIGH_PERF_MODE,
54 ST_DET_UNKNOWN_MODE = 0xFF,
55} st_det_perf_mode_t;
56
Quinn Male2e883752019-03-22 11:28:54 -070057typedef enum st_session_event_id {
58 ST_SES_EV_LOAD_SM,
59 ST_SES_EV_UNLOAD_SM,
60 ST_SES_EV_START,
61 ST_SES_EV_RESTART,
62 ST_SES_EV_STOP,
63 ST_SES_EV_DETECTED,
64 ST_SES_EV_READ_PCM,
65 ST_SES_EV_END_BUFFERING,
66 ST_SES_EV_SET_EXEC_MODE,
67 ST_SES_EV_SSR_OFFLINE,
68 ST_SES_EV_SSR_ONLINE,
69 ST_SES_EV_PAUSE,
70 ST_SES_EV_RESUME,
71 ST_SES_EV_SEND_CHMIX_COEFF,
72 ST_SES_EV_SET_DEVICE,
73 ST_SES_EV_GET_PARAM_DATA,
74 ST_SES_EV_DEFERRED_STOP,
75 ST_SES_EV_REQUEST_DET,
Harshal Ahire89337992020-07-13 02:38:14 +053076 ST_SES_EV_GET_MODULE_VERSION,
Quinn Male2e883752019-03-22 11:28:54 -070077} st_session_event_id_t;
78
79struct sound_trigger_device;
80struct st_session_ev;
81typedef struct st_session_ev st_session_ev_t;
82
83typedef struct st_session st_session_t;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -070084typedef struct st_proxy_session st_proxy_session_t;
85typedef int (*st_proxy_session_state_fn_t)(st_proxy_session_t*,
86 st_session_ev_t *ev);
Quinn Male58749452020-03-26 17:14:56 -070087typedef struct multi_model_result_info multi_model_result_info_t;
88
89struct multi_model_result_info {
90 uint32_t minor_version;
91 uint32_t num_detected_models;
92 uint32_t detected_model_id;
93 uint32_t detected_keyword_id;
94 uint32_t best_channel_idx;
95 int32_t best_confidence_level;
96 int32_t keyword_start_idx_bytes;
97 int32_t keyword_end_idx_bytes;
98 uint32_t timestamp_lsw_us;
99 uint32_t timestamp_msw_us;
100};
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700101
102struct sound_model_info {
Quinn Male58749452020-03-26 17:14:56 -0700103 unsigned int model_id;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700104 unsigned char *sm_data;
105 unsigned int sm_size;
106 sound_trigger_sound_model_type_t sm_type;
107 unsigned int num_keyphrases;
108 unsigned int num_users;
109 char **keyphrases;
110 char **users;
111 char **cf_levels_kw_users;
112 unsigned char *cf_levels;
113 unsigned char *det_cf_levels;
114 unsigned int cf_levels_size;
Quinn Male58749452020-03-26 17:14:56 -0700115};
116
117struct st_proxy_ses_sm_info_wrapper {
118 struct listnode sm_list_node;
119 struct sound_model_info sm_info;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700120};
Quinn Male2e883752019-03-22 11:28:54 -0700121
122struct st_session {
123 /* TODO: decouple device below from session */
124 struct listnode list_node;
125 struct listnode transit_list_node;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700126 struct listnode hw_list_node;
Quinn Male2e883752019-03-22 11:28:54 -0700127
128 struct sound_trigger_device *stdev;
129 struct st_vendor_info *vendor_uuid_info;
130
131 pthread_mutex_t lock;
132 st_exec_mode_t exec_mode;
133 st_exec_mode_t ssr_transit_exec_mode;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700134 struct sound_trigger_phrase_sound_model *phrase_sm;
Quinn Male2e883752019-03-22 11:28:54 -0700135 struct sound_trigger_recognition_config *rc_config;
Quinn Male2e883752019-03-22 11:28:54 -0700136 sound_trigger_sound_model_type_t sm_type;
Quinn Male2e883752019-03-22 11:28:54 -0700137 sound_model_handle_t sm_handle;
138 recognition_callback_t callback;
139 void *cookie;
140 audio_io_handle_t capture_handle;
Quinn Male2e883752019-03-22 11:28:54 -0700141 bool capture_requested;
Quinn Male2e883752019-03-22 11:28:54 -0700142
143 unsigned int num_phrases;
144 unsigned int num_users;
145 unsigned int recognition_mode;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700146 enum client_states_t state;
147 bool paused;
148 bool pending_stop;
149 bool pending_load;
150 bool pending_set_device;
Venkatesh Mangalappalib4243f42019-08-19 15:25:39 -0700151 bool detection_sent;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700152 st_det_perf_mode_t client_req_det_mode;
153 unsigned int hist_buf_duration;
154 unsigned int preroll_duration;
155
156 struct listnode second_stage_list;
157 uint32_t conf_levels_intf_version;
158 void *st_conf_levels;
159
160 st_proxy_session_t *hw_proxy_ses;
161 struct sound_model_info sm_info;
Quinn Male58749452020-03-26 17:14:56 -0700162
163 st_module_type_t f_stage_version;
Quinn Male2bfe13b2020-08-27 16:53:51 -0700164
165 uint32_t fs_det_count;
166 uint32_t ss_det_count;
167 uint32_t ss_rej_count;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700168};
169
170struct st_proxy_session {
171 struct listnode clients_list; /* Attached client sessions */
172 struct sound_trigger_device *stdev;
173 struct st_vendor_info *vendor_uuid_info;
174
175 pthread_mutex_t lock;
176 st_exec_mode_t exec_mode;
177 bool enable_trans;
178
179 struct sound_trigger_recognition_config *rc_config;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700180 sound_model_handle_t sm_handle;
181 bool lab_enabled;
182 unsigned int recognition_mode;
Quinn Male2e883752019-03-22 11:28:54 -0700183
184 st_hw_session_t *hw_ses_cpe; /* cpe hw session */
185 st_hw_session_t *hw_ses_adsp; /* adsp hw session */
186 st_hw_session_t *hw_ses_arm; /* arm hw session */
187 st_hw_session_t *hw_ses_current; /* current hw session, this is set every
188 time there is an exec_mode change and points to one of the above
189 hw sessions */
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700190 st_hw_session_t *hw_ses_prev; /* cached hw_ses_current,
191 used for WDSP<->ADSP transitions */
192 st_session_t *det_stc_ses; /* Current detected client */
Quinn Male2e883752019-03-22 11:28:54 -0700193
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700194 /*
195 * flag gets set if user restarts
196 * session right after detection before we have a chance to stop the
197 * session
198 */
199 bool hw_session_started;
200
201 st_proxy_session_state_fn_t current_state;
Quinn Male2e883752019-03-22 11:28:54 -0700202 bool device_disabled;
Quinn Male2e883752019-03-22 11:28:54 -0700203
204 pthread_t aggregator_thread;
205 pthread_mutex_t ss_detections_lock;
206 pthread_cond_t ss_detections_cond;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700207 bool aggregator_thread_created;
Quinn Male2e883752019-03-22 11:28:54 -0700208 bool exit_aggregator_loop;
Quinn Male2e883752019-03-22 11:28:54 -0700209 bool enable_second_stage;
210 st_session_ev_t *det_session_ev;
211 int rc_config_update_counter;
212 bool detection_requested;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700213
Quinn Male58749452020-03-26 17:14:56 -0700214 struct listnode sm_info_list;
215 bool sm_merged;
Quinn Male3d7d9d42019-05-20 13:35:01 -0700216 FILE *lab_fp;
Quinn Maled0814de2019-05-29 17:33:22 -0700217 uint64_t detection_event_time;
Quinn Male58749452020-03-26 17:14:56 -0700218
219 st_module_type_t f_stage_version;
Quinn Male2e883752019-03-22 11:28:54 -0700220};
221
222/*
223 * Initialzies a sound trigger session. Must be called before
224 * any other opertaions.
225 * Parameters:
226 * use_gcs TRUE indicates that GCS should be used for CPE HW
227 * session (WCD9340 and beyond) otherwise use
228 * LSM(WCD9335 and earlier)
229 * exec_mode Indicates initial execution mode for the st
230 * session, whether it is in CPE or ADSP
231 */
232int st_session_init(st_session_t *st_ses, struct sound_trigger_device *stdev,
233 st_exec_mode_t exec_mode, sound_model_handle_t sm_handle);
234int st_session_deinit(st_session_t *);
235int st_session_ss_init(st_session_t *st_ses);
236int st_session_ss_deinit(st_session_t *st_ses);
237
238int st_session_load_sm(st_session_t *st_ses);
239int st_session_start(st_session_t *st_ses);
240int st_session_unload_sm(st_session_t *st_ses);
241int st_session_stop(st_session_t *st_ses);
242int st_session_read_pcm(st_session_t *st_ses, uint8_t *buff,
243 size_t buff_size, /*out*/ size_t *read_size);
244int st_session_stop_lab(st_session_t *st_ses);
245
246int st_session_ssr_offline(st_session_t *st_ses,
247 enum ssr_event_status ssr_type);
248int st_session_ssr_online(st_session_t *st_ses,
249 enum ssr_event_status ssr_type);
250int st_session_pause(st_session_t *st_ses);
251int st_session_resume(st_session_t *st_ses);
Quinn Male2e883752019-03-22 11:28:54 -0700252int st_session_restart(st_session_t *st_ses);
253int st_session_send_custom_chmix_coeff(st_session_t *st_ses, char *str);
254int st_session_get_config(st_session_t *st_ses, struct pcm_config *config);
255int st_session_enable_device(st_session_t *st_ses);
256int st_session_disable_device(st_session_t *st_ses);
257bool st_session_is_detected(st_session_t *st_ses);
258bool st_session_is_active(st_session_t *st_ses);
259bool st_session_is_buffering(st_session_t *st_ses);
260bool st_session_is_ssr_state(st_session_t *st_ses);
261int st_session_set_exec_mode(st_session_t *st_ses, st_exec_mode_t exec);
262int st_session_get_param_data(st_session_t *st_ses, const char *param,
263 void *payload, size_t payload_size, size_t *param_data_size);
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700264int st_session_request_detection(st_session_t *st_ses);
265int st_session_update_recongition_config(st_session_t *st_ses);
266int st_session_get_preroll(st_session_t *st_ses);
Harshal Ahire89337992020-07-13 02:38:14 +0530267int st_session_get_module_version(st_session_t *st_ses, char *version);
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700268
Quinn Male2e883752019-03-22 11:28:54 -0700269int process_detection_event_keyphrase_v2(
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700270 st_proxy_session_t *st_ses, int detect_status,
Quinn Male2e883752019-03-22 11:28:54 -0700271 void *payload, size_t payload_size,
272 struct sound_trigger_phrase_recognition_event **event);
Quinn Male2e883752019-03-22 11:28:54 -0700273
274#endif /* ST_SESSION_H */