Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 1 | /* 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 44 | /* Below are the states that can be requested from the client */ |
| 45 | enum client_states_t { |
| 46 | ST_STATE_IDLE, |
| 47 | ST_STATE_LOADED, |
| 48 | ST_STATE_ACTIVE |
| 49 | }; |
| 50 | |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 51 | typedef 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 57 | typedef 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 | |
| 78 | struct sound_trigger_device; |
| 79 | struct st_session_ev; |
| 80 | typedef struct st_session_ev st_session_ev_t; |
| 81 | |
| 82 | typedef struct st_session st_session_t; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 83 | typedef struct st_proxy_session st_proxy_session_t; |
| 84 | typedef int (*st_proxy_session_state_fn_t)(st_proxy_session_t*, |
| 85 | st_session_ev_t *ev); |
| 86 | |
| 87 | struct 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 101 | |
| 102 | struct st_session { |
| 103 | /* TODO: decouple device below from session */ |
| 104 | struct listnode list_node; |
| 105 | struct listnode transit_list_node; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 106 | struct listnode hw_list_node; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 107 | |
| 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 Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 114 | struct sound_trigger_phrase_sound_model *phrase_sm; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 115 | struct sound_trigger_recognition_config *rc_config; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 116 | sound_trigger_sound_model_type_t sm_type; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 117 | sound_model_handle_t sm_handle; |
| 118 | recognition_callback_t callback; |
| 119 | void *cookie; |
| 120 | audio_io_handle_t capture_handle; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 121 | bool capture_requested; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 122 | |
| 123 | unsigned int num_phrases; |
| 124 | unsigned int num_users; |
| 125 | unsigned int recognition_mode; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 126 | enum client_states_t state; |
| 127 | bool paused; |
| 128 | bool pending_stop; |
| 129 | bool pending_load; |
| 130 | bool pending_set_device; |
Venkatesh Mangalappali | b4243f4 | 2019-08-19 15:25:39 -0700 | [diff] [blame] | 131 | bool detection_sent; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 132 | 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 | |
| 144 | struct 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 158 | |
| 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 Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 165 | 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 168 | |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 169 | /* |
| 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 177 | bool device_disabled; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 178 | |
| 179 | pthread_t aggregator_thread; |
| 180 | pthread_mutex_t ss_detections_lock; |
| 181 | pthread_cond_t ss_detections_cond; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 182 | bool aggregator_thread_created; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 183 | bool exit_aggregator_loop; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 184 | bool enable_second_stage; |
| 185 | st_session_ev_t *det_session_ev; |
| 186 | int rc_config_update_counter; |
| 187 | bool detection_requested; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 188 | |
| 189 | struct sound_model_info sm_info; |
Quinn Male | 3d7d9d4 | 2019-05-20 13:35:01 -0700 | [diff] [blame] | 190 | FILE *lab_fp; |
Quinn Male | d0814de | 2019-05-29 17:33:22 -0700 | [diff] [blame] | 191 | uint64_t detection_event_time; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 192 | }; |
| 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 | */ |
| 204 | int 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); |
| 206 | int st_session_deinit(st_session_t *); |
| 207 | int st_session_ss_init(st_session_t *st_ses); |
| 208 | int st_session_ss_deinit(st_session_t *st_ses); |
| 209 | |
| 210 | int st_session_load_sm(st_session_t *st_ses); |
| 211 | int st_session_start(st_session_t *st_ses); |
| 212 | int st_session_unload_sm(st_session_t *st_ses); |
| 213 | int st_session_stop(st_session_t *st_ses); |
| 214 | int st_session_read_pcm(st_session_t *st_ses, uint8_t *buff, |
| 215 | size_t buff_size, /*out*/ size_t *read_size); |
| 216 | int st_session_stop_lab(st_session_t *st_ses); |
| 217 | |
| 218 | int st_session_ssr_offline(st_session_t *st_ses, |
| 219 | enum ssr_event_status ssr_type); |
| 220 | int st_session_ssr_online(st_session_t *st_ses, |
| 221 | enum ssr_event_status ssr_type); |
| 222 | int st_session_pause(st_session_t *st_ses); |
| 223 | int st_session_resume(st_session_t *st_ses); |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 224 | int st_session_restart(st_session_t *st_ses); |
| 225 | int st_session_send_custom_chmix_coeff(st_session_t *st_ses, char *str); |
| 226 | int st_session_get_config(st_session_t *st_ses, struct pcm_config *config); |
| 227 | int st_session_enable_device(st_session_t *st_ses); |
| 228 | int st_session_disable_device(st_session_t *st_ses); |
| 229 | bool st_session_is_detected(st_session_t *st_ses); |
| 230 | bool st_session_is_active(st_session_t *st_ses); |
| 231 | bool st_session_is_buffering(st_session_t *st_ses); |
| 232 | bool st_session_is_ssr_state(st_session_t *st_ses); |
| 233 | int st_session_set_exec_mode(st_session_t *st_ses, st_exec_mode_t exec); |
| 234 | int 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 Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 236 | int st_session_request_detection(st_session_t *st_ses); |
| 237 | int st_session_update_recongition_config(st_session_t *st_ses); |
| 238 | int st_session_get_preroll(st_session_t *st_ses); |
| 239 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 240 | int process_detection_event_keyphrase_v2( |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 241 | st_proxy_session_t *st_ses, int detect_status, |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 242 | void *payload, size_t payload_size, |
| 243 | struct sound_trigger_phrase_recognition_event **event); |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 244 | |
| 245 | #endif /* ST_SESSION_H */ |