1 /*
2  * Copyright (C) 2019 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 package com.android.server.soundtrigger_middleware;
18 
19 import android.hardware.soundtrigger3.ISoundTriggerHw;
20 import android.hardware.soundtrigger3.ISoundTriggerHwCallback;
21 import android.hardware.soundtrigger3.ISoundTriggerHwGlobalCallback;
22 import android.media.soundtrigger.ModelParameterRange;
23 import android.media.soundtrigger.PhraseSoundModel;
24 import android.media.soundtrigger.Properties;
25 import android.media.soundtrigger.RecognitionConfig;
26 import android.media.soundtrigger.SoundModel;
27 import android.media.soundtrigger_middleware.PhraseRecognitionEventSys;
28 import android.media.soundtrigger_middleware.RecognitionEventSys;
29 import android.os.IBinder;
30 
31 /**
32  * This interface mimics the soundtrigger HAL interface, with a few key differences:
33  * <ul>
34  * <li>Generally, methods should not throw, except for the following cases:
35  *   <ul>
36  *   <li>Any unexpected HAL behavior is considered an internal HAL malfunction and should be thrown
37  *   as a {@link HalException}, from any method.
38  *   <li>A {@link RuntimeException} with a {@link android.os.DeadObjectException} cause represents
39  *   a dead HAL process and may be thrown by any method.
40  *   <li>Implementations of earlier versions of the interface may throw a
41  *   {@link RecoverableException} with a
42  *   {@link android.media.soundtrigger.Status#OPERATION_NOT_SUPPORTED} for methods that
43  *   have been introduced in later versions of the interface.
44  *   <li>Certain methods are allowed to throw a {@link RecoverableException} with a
45  *   {@link android.media.soundtrigger.Status#RESOURCE_CONTENTION} to indicate transient
46  *   failures.
47  *   </ul>
48  * <li>Some binder-specific details are hidden.
49  * <li>No RemoteExceptions are specified. Some implementations of this interface may rethrow
50  * RemoteExceptions as RuntimeExceptions, some can guarantee handling them somehow and never throw
51  * them.
52  * </ul>
53  * For cases where the client wants to explicitly handle specific versions of the underlying driver
54  * interface, they may call {@link #interfaceDescriptor()}.
55  * <p>
56  * <b>Note to maintainers</b>: This class must always be kept in sync with the latest version,
57  * so that clients have access to the entire functionality without having to burden themselves with
58  * compatibility, as much as possible.
59  */
60 interface ISoundTriggerHal {
61     /**
62      * @see ISoundTriggerHw#getProperties()
63      */
getProperties()64     Properties getProperties();
65 
66     /**
67      * @see ISoundTriggerHw#registerGlobalCallback(ISoundTriggerHwGlobalCallback)
68      */
registerCallback(GlobalCallback callback)69     void registerCallback(GlobalCallback callback);
70 
71     /**
72      * @see ISoundTriggerHw#loadSoundModel(android.media.soundtrigger.SoundModel,
73      * ISoundTriggerHwCallback)
74      */
loadSoundModel(SoundModel soundModel, ModelCallback callback)75     int loadSoundModel(SoundModel soundModel, ModelCallback callback);
76 
77     /**
78      * @see ISoundTriggerHw#loadPhraseSoundModel(android.media.soundtrigger.PhraseSoundModel,
79      * ISoundTriggerHwCallback)
80      */
loadPhraseSoundModel(PhraseSoundModel soundModel, ModelCallback callback)81     int loadPhraseSoundModel(PhraseSoundModel soundModel, ModelCallback callback);
82 
83     /**
84      * @see ISoundTriggerHw#unloadSoundModel(int)
85      */
unloadSoundModel(int modelHandle)86     void unloadSoundModel(int modelHandle);
87 
88     /**
89      * @see ISoundTriggerHw#startRecognition(int, int, int, RecognitionConfig)
90      */
startRecognition(int modelHandle, int deviceHandle, int ioHandle, RecognitionConfig config)91     void startRecognition(int modelHandle, int deviceHandle, int ioHandle,
92             RecognitionConfig config);
93 
94     /**
95      * @see ISoundTriggerHw#stopRecognition(int)
96      */
stopRecognition(int modelHandle)97     void stopRecognition(int modelHandle);
98 
99     /**
100      * @see ISoundTriggerHw#forceRecognitionEvent(int)
101      */
forceRecognitionEvent(int modelHandle)102     void forceRecognitionEvent(int modelHandle);
103 
104     /**
105      * @return null if not supported.
106      * @see ISoundTriggerHw#queryParameter(int, int)
107      */
queryParameter(int modelHandle, int param)108     ModelParameterRange queryParameter(int modelHandle, int param);
109 
110     /**
111      * @see ISoundTriggerHw#getParameter(int, int)
112      */
getModelParameter(int modelHandle, int param)113     int getModelParameter(int modelHandle, int param);
114 
115     /**
116      * @see ISoundTriggerHw#setParameter(int, int, int)
117      */
setModelParameter(int modelHandle, int param, int value)118     void setModelParameter(int modelHandle, int param, int value);
119 
120     /**
121      * @see IBinder#getInterfaceDescriptor()
122      */
interfaceDescriptor()123     String interfaceDescriptor();
124 
125     /**
126      * @see IBinder#linkToDeath(IBinder.DeathRecipient, int)
127      */
linkToDeath(IBinder.DeathRecipient recipient)128     void linkToDeath(IBinder.DeathRecipient recipient);
129 
130     /**
131      * @see IBinder#unlinkToDeath(IBinder.DeathRecipient, int)
132      */
unlinkToDeath(IBinder.DeathRecipient recipient)133     void unlinkToDeath(IBinder.DeathRecipient recipient);
134 
135     /*
136      * This is only useful for testing decorators and doesn't actually do anything with the real
137      * HAL. This method would block until all callbacks that were previously generated have been
138      * invoked. For most decorators, this merely flushes the delegate, but for delegates that may
139      * have additional buffers for callbacks this should flush them.
140      */
flushCallbacks()141     void flushCallbacks();
142 
143     /**
144      * Used only for testing purposes. Called when a client attaches to the framework.
145      * Transmitting this event to the fake STHAL allows observation of this event, which is
146      * normally consumed by the framework, and is not communicated to the STHAL.
147      * @param token - A unique binder token associated with this session.
148      */
clientAttached(IBinder token)149     void clientAttached(IBinder token);
150 
151     /**
152      * Used only for testing purposes. Called when a client detached from the framework.
153      * Transmitting this event to the fake STHAL allows observation of this event, which is
154      * normally consumed by the framework, and is not communicated to the STHAL.
155      * @param token - The same token passed to the corresponding {@link clientAttached(IBinder)}.
156      */
clientDetached(IBinder token)157     void clientDetached(IBinder token);
158 
159     /**
160      * Kill and restart the HAL instance. This is typically a last resort for error recovery and may
161      * result in other related services being killed.
162      */
reboot()163     void reboot();
164 
165     /**
166      * Called when this interface is guaranteed to no longer be used and can free up any resources
167      * used.
168      */
detach()169     void detach();
170 
171     /**
172      * Callback interface for model-related events.
173      */
174     interface ModelCallback {
175         /**
176          * Decorated callback of
177          * {@link ISoundTriggerHwCallback#recognitionCallback(int, RecognitionEvent)} where
178          * {@link RecognitionEventSys} is decorating the returned {@link RecognitionEvent}
179          */
recognitionCallback(int modelHandle, RecognitionEventSys event)180         void recognitionCallback(int modelHandle, RecognitionEventSys event);
181 
182         /**
183          * Decorated callback of
184          * {@link ISoundTriggerHwCallback#phraseRecognitionCallback(int, PhraseRecognitionEvent)}
185          * where {@link PhraseRecognitionEventSys} is decorating the returned
186          * {@link PhraseRecognitionEvent}
187          */
phraseRecognitionCallback(int modelHandle, PhraseRecognitionEventSys event)188         void phraseRecognitionCallback(int modelHandle, PhraseRecognitionEventSys event);
189 
190         /**
191          * @see ISoundTriggerHwCallback#modelUnloaded(int)
192          */
modelUnloaded(int modelHandle)193         void modelUnloaded(int modelHandle);
194     }
195 
196     /**
197      * Callback interface for global events.
198      */
199     interface GlobalCallback {
200         /**
201          * @see ISoundTriggerHwGlobalCallback#onResourcesAvailable()
202          */
onResourcesAvailable()203         void onResourcesAvailable();
204     }
205 }
206