1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 SOUND_TRIGGER_INTF_H
18 #define SOUND_TRIGGER_INTF_H
19 
20 #include <cutils/list.h>
21 #include <hardware/sound_trigger.h>
22 #include "tinyalsa/asoundlib.h"
23 
24 /*-------------------- Begin: AHAL-STHAL Interface ---------------------------*/
25 /*
26  * Maintain the proprietary interface between AHAL and STHAL locally to avoid
27  * the compilation dependency of interface header file from STHAL.
28  */
29 
30 #define MAKE_HAL_VERSION(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
31 #define MAJOR_VERSION(ver) (((ver) & 0xff00) >> 8)
32 #define MINOR_VERSION(ver) ((ver) & 0x00ff)
33 
34 /* Proprietary interface version used for compatibility with STHAL */
35 #define STHAL_PROP_API_VERSION_2_0 MAKE_HAL_VERSION(2, 0)
36 #define STHAL_PROP_API_CURRENT_VERSION STHAL_PROP_API_VERSION_2_0
37 
38 #define ST_EVENT_CONFIG_MAX_STR_VALUE 32
39 
40 #define AUDIO_HAL_NAME_PREFIX "audio.primary"
41 #define SOUND_TRIGGER_PLATFORM "msmnile"
42 
43 #ifdef __LP64__
44     #define AUDIO_HAL_LIBRARY_PATH "/vendor/lib64/hw"
45 #else
46     #define AUDIO_HAL_LIBRARY_PATH "/vendor/lib/hw"
47 #endif
48 
49 const unsigned int sthal_prop_api_version = STHAL_PROP_API_CURRENT_VERSION;
50 
51 #define SOUND_TRIGGER_SAMPLING_RATE     16000
52 #define SOUND_TRIGGER_CHANNEL               1
53 #define SOUND_TRIGGER_PERIOD_COUNT          2
54 #define SOUND_TRIGGER_PERIOD_DURATION_MSEC 20
55 #define SOUND_TRIGGER_PERIOD_SIZE   (SOUND_TRIGGER_SAMPLING_RATE * \
56                                     SOUND_TRIGGER_CHANNEL * \
57                                     SOUND_TRIGGER_PERIOD_DURATION_MSEC) / 1000
58 
59 static struct pcm_config stdev_hotword_pcm_config = {
60     .channels = SOUND_TRIGGER_CHANNEL,
61     .rate = SOUND_TRIGGER_SAMPLING_RATE,
62     .period_size = SOUND_TRIGGER_PERIOD_SIZE,
63     .period_count = SOUND_TRIGGER_PERIOD_COUNT,
64     .format = PCM_FORMAT_S16_LE,
65 };
66 
67 typedef enum {
68     ST_EVENT_SESSION_REGISTER,
69     ST_EVENT_SESSION_DEREGISTER
70 } sound_trigger_event_type_t;
71 
72 typedef enum {
73     AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE,
74     AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE,
75     AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE,
76     AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE,
77     AUDIO_EVENT_STOP_LAB,
78     AUDIO_EVENT_SSR,
79     AUDIO_EVENT_NUM_ST_SESSIONS,
80     AUDIO_EVENT_READ_SAMPLES,
81     AUDIO_EVENT_DEVICE_CONNECT,
82     AUDIO_EVENT_DEVICE_DISCONNECT,
83     AUDIO_EVENT_SVA_EXEC_MODE,
84     AUDIO_EVENT_SVA_EXEC_MODE_STATUS,
85     AUDIO_EVENT_CAPTURE_STREAM_INACTIVE,
86     AUDIO_EVENT_CAPTURE_STREAM_ACTIVE,
87 } audio_event_type_t;
88 
89 typedef enum {
90     USECASE_TYPE_PCM_PLAYBACK,
91     USECASE_TYPE_PCM_CAPTURE,
92     USECASE_TYPE_VOICE_CALL,
93     USECASE_TYPE_VOIP_CALL,
94 } audio_stream_usecase_type_t;
95 
96 enum ssr_event_status {
97     SND_CARD_STATUS_OFFLINE,
98     SND_CARD_STATUS_ONLINE,
99     CPE_STATUS_OFFLINE,
100     CPE_STATUS_ONLINE,
101     SLPI_STATUS_OFFLINE,
102     SLPI_STATUS_ONLINE
103 };
104 
105 struct sound_trigger_session_info {
106     void* p_ses; /* opaque pointer to st_session obj */
107     int capture_handle;
108     struct pcm *pcm;
109     struct pcm_config config;
110 };
111 
112 struct audio_read_samples_info {
113     struct sound_trigger_session_info *ses_info;
114     void *buf;
115     size_t num_bytes;
116 };
117 
118 struct audio_hal_usecase {
119     audio_stream_usecase_type_t type;
120 };
121 
122 struct sound_trigger_event_info {
123     struct sound_trigger_session_info st_ses;
124 };
125 
126 struct sound_trigger_get_param_data {
127     char *param;
128     int sm_handle;
129     struct str_parms *reply;
130 };
131 
132 struct audio_device_info {
133     struct listnode list;
134     audio_devices_t type;
135     char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
136 };
137 
138 struct sound_trigger_device_info {
139     struct listnode devices;
140 };
141 
142 struct audio_event_info {
143     union {
144         enum ssr_event_status status;
145         int value;
146         struct sound_trigger_session_info ses_info;
147         struct audio_read_samples_info aud_info;
148         char str_value[ST_EVENT_CONFIG_MAX_STR_VALUE];
149         struct audio_hal_usecase usecase;
150         bool audio_ec_ref_enabled;
151         struct sound_trigger_get_param_data st_get_param_data;
152     } u;
153     struct sound_trigger_device_info device_info;
154 };
155 
156 /* STHAL callback which is called by AHAL */
157 typedef int (*sound_trigger_hw_call_back_t)(audio_event_type_t,
158                                 struct audio_event_info*);
159 
160 /* AHAL callback which is called by STHAL */
161 typedef void (*audio_hw_call_back_t)(sound_trigger_event_type_t,
162                         struct sound_trigger_event_info*);
163 
164 /*---------------- End: AHAL-STHAL Interface ----------------------------------*/
165 #endif /* SOUND_TRIGGER_INTF_H */
166