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 #ifndef ANDROID_HARDWARE_SOUNDTRIGGER_V2_3_SOUNDTRIGGERHW_H
18 #define ANDROID_HARDWARE_SOUNDTRIGGER_V2_3_SOUNDTRIGGERHW_H
19 
20 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHw.h>
21 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
22 #include <android/hardware/soundtrigger/2.3/ISoundTriggerHw.h>
23 #include <android/hardware/soundtrigger/2.3/types.h>
24 #include <hardware/sound_trigger.h>
25 #include <hidl/MQDescriptor.h>
26 #include <hidl/Status.h>
27 #include <stdatomic.h>
28 #include <system/sound_trigger.h>
29 #include <utils/KeyedVector.h>
30 #include <utils/threads.h>
31 
32 namespace android {
33 namespace hardware {
34 namespace soundtrigger {
35 namespace V2_3 {
36 namespace implementation {
37 
38 using ::android::sp;
39 using ::android::hardware::hidl_array;
40 using ::android::hardware::hidl_memory;
41 using ::android::hardware::hidl_string;
42 using ::android::hardware::hidl_vec;
43 using ::android::hardware::Return;
44 using ::android::hardware::Void;
45 using ::android::hardware::audio::common::V2_0::Uuid;
46 
47 /**
48  * According to the HIDL C++ Users Guide: client and server implementations
49  * should never directly refer to anything other than the interface header
50  * generated from the HIDL definition file (ie. ISoundTriggerHw.hal), so
51  * this V2_3 implementation copies the previous implementations and
52  * then adds the new implementation.
53  */
54 struct SoundTriggerHw : public ISoundTriggerHw {
55     // Methods from V2_0::ISoundTriggerHw follow.
56     Return<void> getProperties(getProperties_cb _hidl_cb) override;
57     Return<void> loadSoundModel(const V2_0::ISoundTriggerHw::SoundModel& soundModel,
58                                 const sp<V2_0::ISoundTriggerHwCallback>& callback, int32_t cookie,
59                                 loadSoundModel_cb _hidl_cb) override;
60     Return<void> loadPhraseSoundModel(const V2_0::ISoundTriggerHw::PhraseSoundModel& soundModel,
61                                       const sp<V2_0::ISoundTriggerHwCallback>& callback,
62                                       int32_t cookie, loadPhraseSoundModel_cb _hidl_cb) override;
63     Return<int32_t> unloadSoundModel(int32_t modelHandle) override;
64     Return<int32_t> startRecognition(int32_t modelHandle,
65                                      const V2_0::ISoundTriggerHw::RecognitionConfig& config,
66                                      const sp<V2_0::ISoundTriggerHwCallback>& callback,
67                                      int32_t cookie) override;
68     Return<int32_t> stopRecognition(int32_t modelHandle) override;
69     Return<int32_t> stopAllRecognitions() override;
70 
71     // Methods from V2_1::ISoundTriggerHw follow.
72     Return<void> loadSoundModel_2_1(const V2_1::ISoundTriggerHw::SoundModel& soundModel,
73                                     const sp<V2_1::ISoundTriggerHwCallback>& callback,
74                                     int32_t cookie, loadSoundModel_2_1_cb _hidl_cb) override;
75     Return<void> loadPhraseSoundModel_2_1(const V2_1::ISoundTriggerHw::PhraseSoundModel& soundModel,
76                                           const sp<V2_1::ISoundTriggerHwCallback>& callback,
77                                           int32_t cookie,
78                                           loadPhraseSoundModel_2_1_cb _hidl_cb) override;
79     Return<int32_t> startRecognition_2_1(int32_t modelHandle,
80                                          const V2_1::ISoundTriggerHw::RecognitionConfig& config,
81                                          const sp<V2_1::ISoundTriggerHwCallback>& callback,
82                                          int32_t cookie) override;
83 
84     // Methods from V2_2::ISoundTriggerHw follow.
85     Return<int32_t> getModelState(int32_t modelHandle) override;
86 
87     // Methods from V2_3::ISoundTriggerHw follow.
88     Return<void> getProperties_2_3(getProperties_2_3_cb _hidl_cb) override;
89     Return<int32_t> startRecognition_2_3(int32_t modelHandle,
90                                          const V2_3::RecognitionConfig& config) override;
91     Return<int32_t> setParameter(V2_0::SoundModelHandle modelHandle, ModelParameter modelParam,
92                                  int32_t value) override;
93     Return<void> getParameter(V2_0::SoundModelHandle modelHandle, ModelParameter modelParam,
94                               ISoundTriggerHw::getParameter_cb _hidl_cb) override;
95     Return<void> queryParameter(V2_0::SoundModelHandle modelHandle, ModelParameter modelParam,
96                                 ISoundTriggerHw::queryParameter_cb _hidl_cb) override;
97 
98     SoundTriggerHw();
99 
100     // Copied from hardware/interfaces/soundtrigger/2.0/default/SoundTriggerHalImpl.h
101 
102     /**
103      * Client object holding active handles and callback sctructures. Used for referencing
104      * which models map to which client of the HAL. SoundModelClients are stored in the
105      * mClients object while the model is active.
106      */
107     class SoundModelClient : public RefBase {
108       public:
SoundModelClientSoundTriggerHw109         SoundModelClient(uint32_t id, V2_0::ISoundTriggerHwCallback::CallbackCookie cookie)
110             : mId(id), mCookie(cookie) {}
~SoundModelClientSoundTriggerHw111         virtual ~SoundModelClient() {}
112 
getIdSoundTriggerHw113         uint32_t getId() const { return mId; }
getHalHandleSoundTriggerHw114         sound_model_handle_t getHalHandle() const { return mHalHandle; }
setHalHandleSoundTriggerHw115         void setHalHandle(sound_model_handle_t handle) { mHalHandle = handle; }
116 
117         virtual void recognitionCallback(struct sound_trigger_recognition_event* halEvent) = 0;
118         virtual void soundModelCallback(struct sound_trigger_model_event* halEvent) = 0;
119 
120       protected:
121         const uint32_t mId;
122         sound_model_handle_t mHalHandle;
123         V2_0::ISoundTriggerHwCallback::CallbackCookie mCookie;
124     };
125 
126   private:
127     static void convertPhaseRecognitionEventFromHal(
128             V2_0::ISoundTriggerHwCallback::PhraseRecognitionEvent* event,
129             const struct sound_trigger_phrase_recognition_event* halEvent);
130     static void convertRecognitionEventFromHal(
131             V2_0::ISoundTriggerHwCallback::RecognitionEvent* event,
132             const struct sound_trigger_recognition_event* halEvent);
133     static void convertSoundModelEventFromHal(V2_0::ISoundTriggerHwCallback::ModelEvent* event,
134                                               const struct sound_trigger_model_event* halEvent);
135 
136     virtual ~SoundTriggerHw();
137 
138     uint32_t nextUniqueModelId();
139     int doLoadSoundModel(const V2_0::ISoundTriggerHw::SoundModel& soundModel,
140                          sp<SoundModelClient> client);
141 
142     // RefBase
143     void onFirstRef() override;
144 
145     class SoundModelClient_2_0 : public SoundModelClient {
146       public:
SoundModelClient_2_0SoundTriggerHw147         SoundModelClient_2_0(uint32_t id, V2_0::ISoundTriggerHwCallback::CallbackCookie cookie,
148                              sp<V2_0::ISoundTriggerHwCallback> callback)
149             : SoundModelClient(id, cookie), mCallback(callback) {}
150 
151         void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
152         void soundModelCallback(struct sound_trigger_model_event* halEvent) override;
153 
154       private:
155         sp<V2_0::ISoundTriggerHwCallback> mCallback;
156     };
157 
158     void convertUuidFromHal(Uuid* uuid, const sound_trigger_uuid_t* halUuid);
159     void convertUuidToHal(sound_trigger_uuid_t* halUuid, const Uuid* uuid);
160     void convertPropertiesFromHal(V2_0::ISoundTriggerHw::Properties* properties,
161                                   const struct sound_trigger_properties* halProperties);
162     void convertPropertiesFromHal(V2_3::Properties* properties,
163                                   const struct sound_trigger_properties_header* header);
164     static sound_trigger_model_parameter_t convertModelParameterToHal(ModelParameter param);
165     void convertTriggerPhraseToHal(struct sound_trigger_phrase* halTriggerPhrase,
166                                    const V2_0::ISoundTriggerHw::Phrase* triggerPhrase);
167     // returned HAL sound model must be freed by caller
168     struct sound_trigger_sound_model* convertSoundModelToHal(
169             const V2_0::ISoundTriggerHw::SoundModel* soundModel);
170     void convertPhraseRecognitionExtraToHal(struct sound_trigger_phrase_recognition_extra* halExtra,
171                                             const V2_0::PhraseRecognitionExtra* extra);
172     // returned recognition config must be freed by caller
173     struct sound_trigger_recognition_config* convertRecognitionConfigToHal(
174             const V2_0::ISoundTriggerHw::RecognitionConfig* config);
175     struct sound_trigger_recognition_config_header* convertRecognitionConfigToHalHeader(
176             const V2_3::RecognitionConfig* config);
177 
178     static void convertPhraseRecognitionExtraFromHal(
179             V2_0::PhraseRecognitionExtra* extra,
180             const struct sound_trigger_phrase_recognition_extra* halExtra);
181 
182     static void soundModelCallback(struct sound_trigger_model_event* halEvent, void* cookie);
183     static void recognitionCallback(struct sound_trigger_recognition_event* halEvent, void* cookie);
184 
185     const char* mModuleName;
186     struct sound_trigger_hw_device* mHwDevice;
187     volatile atomic_uint_fast32_t mNextModelId;
188     DefaultKeyedVector<int32_t, sp<SoundModelClient>> mClients;
189     Mutex mLock;
190 
191     // Copied from hardware/interfaces/soundtrigger/2.1/default/SoundTriggerHw.h
192     class SoundModelClient_2_1 : public SoundModelClient {
193       public:
SoundModelClient_2_1SoundTriggerHw194         SoundModelClient_2_1(uint32_t id, V2_1::ISoundTriggerHwCallback::CallbackCookie cookie,
195                              sp<V2_1::ISoundTriggerHwCallback> callback)
196             : SoundModelClient(id, cookie), mCallback(callback) {}
197 
198         void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
199         void soundModelCallback(struct sound_trigger_model_event* halEvent) override;
200 
201       private:
202         sp<V2_1::ISoundTriggerHwCallback> mCallback;
203     };
204 };
205 
206 extern "C" ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* name);
207 
208 }  // namespace implementation
209 }  // namespace V2_3
210 }  // namespace soundtrigger
211 }  // namespace hardware
212 }  // namespace android
213 
214 #endif  // ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_SOUNDTRIGGERHW_H
215