blob: e779aa6d17f450b6de7188113b79a8ed86ab95eb [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 *
7 * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
8 *
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,
76} st_session_event_id_t;
77
78struct sound_trigger_device;
79struct st_session_ev;
80typedef struct st_session_ev st_session_ev_t;
81
82typedef struct st_session st_session_t;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -070083typedef struct st_proxy_session st_proxy_session_t;
84typedef int (*st_proxy_session_state_fn_t)(st_proxy_session_t*,
85 st_session_ev_t *ev);
86
87struct sound_model_info {
88 unsigned char *sm_data;
89 unsigned int sm_size;
90 sound_trigger_sound_model_type_t sm_type;
91 unsigned int num_keyphrases;
92 unsigned int num_users;
93 char **keyphrases;
94 char **users;
95 char **cf_levels_kw_users;
96 unsigned char *cf_levels;
97 unsigned char *det_cf_levels;
98 unsigned int cf_levels_size;
99 bool sm_merged;
100};
Quinn Male2e883752019-03-22 11:28:54 -0700101
102struct st_session {
103 /* TODO: decouple device below from session */
104 struct listnode list_node;
105 struct listnode transit_list_node;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700106 struct listnode hw_list_node;
Quinn Male2e883752019-03-22 11:28:54 -0700107
108 struct sound_trigger_device *stdev;
109 struct st_vendor_info *vendor_uuid_info;
110
111 pthread_mutex_t lock;
112 st_exec_mode_t exec_mode;
113 st_exec_mode_t ssr_transit_exec_mode;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700114 struct sound_trigger_phrase_sound_model *phrase_sm;
Quinn Male2e883752019-03-22 11:28:54 -0700115 struct sound_trigger_recognition_config *rc_config;
Quinn Male2e883752019-03-22 11:28:54 -0700116 sound_trigger_sound_model_type_t sm_type;
Quinn Male2e883752019-03-22 11:28:54 -0700117 sound_model_handle_t sm_handle;
118 recognition_callback_t callback;
119 void *cookie;
120 audio_io_handle_t capture_handle;
Quinn Male2e883752019-03-22 11:28:54 -0700121 bool capture_requested;
Quinn Male2e883752019-03-22 11:28:54 -0700122
123 unsigned int num_phrases;
124 unsigned int num_users;
125 unsigned int recognition_mode;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700126 enum client_states_t state;
127 bool paused;
128 bool pending_stop;
129 bool pending_load;
130 bool pending_set_device;
Venkatesh Mangalappalib4243f42019-08-19 15:25:39 -0700131 bool detection_sent;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700132 st_det_perf_mode_t client_req_det_mode;
133 unsigned int hist_buf_duration;
134 unsigned int preroll_duration;
135
136 struct listnode second_stage_list;
137 uint32_t conf_levels_intf_version;
138 void *st_conf_levels;
139
140 st_proxy_session_t *hw_proxy_ses;
141 struct sound_model_info sm_info;
142};
143
144struct st_proxy_session {
145 struct listnode clients_list; /* Attached client sessions */
146 struct sound_trigger_device *stdev;
147 struct st_vendor_info *vendor_uuid_info;
148
149 pthread_mutex_t lock;
150 st_exec_mode_t exec_mode;
151 bool enable_trans;
152
153 struct sound_trigger_recognition_config *rc_config;
154 sound_trigger_sound_model_type_t sm_type;
155 sound_model_handle_t sm_handle;
156 bool lab_enabled;
157 unsigned int recognition_mode;
Quinn Male2e883752019-03-22 11:28:54 -0700158
159 st_hw_session_t *hw_ses_cpe; /* cpe hw session */
160 st_hw_session_t *hw_ses_adsp; /* adsp hw session */
161 st_hw_session_t *hw_ses_arm; /* arm hw session */
162 st_hw_session_t *hw_ses_current; /* current hw session, this is set every
163 time there is an exec_mode change and points to one of the above
164 hw sessions */
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700165 st_hw_session_t *hw_ses_prev; /* cached hw_ses_current,
166 used for WDSP<->ADSP transitions */
167 st_session_t *det_stc_ses; /* Current detected client */
Quinn Male2e883752019-03-22 11:28:54 -0700168
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700169 /*
170 * flag gets set if user restarts
171 * session right after detection before we have a chance to stop the
172 * session
173 */
174 bool hw_session_started;
175
176 st_proxy_session_state_fn_t current_state;
Quinn Male2e883752019-03-22 11:28:54 -0700177 bool device_disabled;
Quinn Male2e883752019-03-22 11:28:54 -0700178
179 pthread_t aggregator_thread;
180 pthread_mutex_t ss_detections_lock;
181 pthread_cond_t ss_detections_cond;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700182 bool aggregator_thread_created;
Quinn Male2e883752019-03-22 11:28:54 -0700183 bool exit_aggregator_loop;
Quinn Male2e883752019-03-22 11:28:54 -0700184 bool enable_second_stage;
185 st_session_ev_t *det_session_ev;
186 int rc_config_update_counter;
187 bool detection_requested;
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700188
189 struct sound_model_info sm_info;
Quinn Male3d7d9d42019-05-20 13:35:01 -0700190 FILE *lab_fp;
Quinn Maled0814de2019-05-29 17:33:22 -0700191 uint64_t detection_event_time;
Quinn Male2e883752019-03-22 11:28:54 -0700192};
193
194/*
195 * Initialzies a sound trigger session. Must be called before
196 * any other opertaions.
197 * Parameters:
198 * use_gcs TRUE indicates that GCS should be used for CPE HW
199 * session (WCD9340 and beyond) otherwise use
200 * LSM(WCD9335 and earlier)
201 * exec_mode Indicates initial execution mode for the st
202 * session, whether it is in CPE or ADSP
203 */
204int st_session_init(st_session_t *st_ses, struct sound_trigger_device *stdev,
205 st_exec_mode_t exec_mode, sound_model_handle_t sm_handle);
206int st_session_deinit(st_session_t *);
207int st_session_ss_init(st_session_t *st_ses);
208int st_session_ss_deinit(st_session_t *st_ses);
209
210int st_session_load_sm(st_session_t *st_ses);
211int st_session_start(st_session_t *st_ses);
212int st_session_unload_sm(st_session_t *st_ses);
213int st_session_stop(st_session_t *st_ses);
214int st_session_read_pcm(st_session_t *st_ses, uint8_t *buff,
215 size_t buff_size, /*out*/ size_t *read_size);
216int st_session_stop_lab(st_session_t *st_ses);
217
218int st_session_ssr_offline(st_session_t *st_ses,
219 enum ssr_event_status ssr_type);
220int st_session_ssr_online(st_session_t *st_ses,
221 enum ssr_event_status ssr_type);
222int st_session_pause(st_session_t *st_ses);
223int st_session_resume(st_session_t *st_ses);
Quinn Male2e883752019-03-22 11:28:54 -0700224int st_session_restart(st_session_t *st_ses);
225int st_session_send_custom_chmix_coeff(st_session_t *st_ses, char *str);
226int st_session_get_config(st_session_t *st_ses, struct pcm_config *config);
227int st_session_enable_device(st_session_t *st_ses);
228int st_session_disable_device(st_session_t *st_ses);
229bool st_session_is_detected(st_session_t *st_ses);
230bool st_session_is_active(st_session_t *st_ses);
231bool st_session_is_buffering(st_session_t *st_ses);
232bool st_session_is_ssr_state(st_session_t *st_ses);
233int st_session_set_exec_mode(st_session_t *st_ses, st_exec_mode_t exec);
234int st_session_get_param_data(st_session_t *st_ses, const char *param,
235 void *payload, size_t payload_size, size_t *param_data_size);
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700236int st_session_request_detection(st_session_t *st_ses);
237int st_session_update_recongition_config(st_session_t *st_ses);
238int st_session_get_preroll(st_session_t *st_ses);
239
Quinn Male2e883752019-03-22 11:28:54 -0700240int process_detection_event_keyphrase_v2(
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700241 st_proxy_session_t *st_ses, int detect_status,
Quinn Male2e883752019-03-22 11:28:54 -0700242 void *payload, size_t payload_size,
243 struct sound_trigger_phrase_recognition_event **event);
Quinn Male2e883752019-03-22 11:28:54 -0700244
245#endif /* ST_SESSION_H */