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 ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_SOUNDTRIGGERHW_H
18 #define ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_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.2/ISoundTriggerHw.h>
23 #include <hardware/sound_trigger.h>
24 #include <hidl/MQDescriptor.h>
25 #include <hidl/Status.h>
26 #include <stdatomic.h>
27 #include <system/sound_trigger.h>
28 #include <utils/KeyedVector.h>
29 #include <utils/threads.h>
30 
31 namespace android {
32 namespace hardware {
33 namespace soundtrigger {
34 namespace V2_2 {
35 namespace implementation {
36 
37 using ::android::sp;
38 using ::android::hardware::hidl_array;
39 using ::android::hardware::hidl_memory;
40 using ::android::hardware::hidl_string;
41 using ::android::hardware::hidl_vec;
42 using ::android::hardware::Return;
43 using ::android::hardware::Void;
44 using ::android::hardware::audio::common::V2_0::Uuid;
45 using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
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_2 implementation copies the V2_0 and V2_1 implementations and
52  * then adds the new V2_2 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     SoundTriggerHw();
88 
89     // Copied from hardware/interfaces/soundtrigger/2.0/default/SoundTriggerHalImpl.h
90     class SoundModelClient : public RefBase {
91        public:
SoundModelClientSoundTriggerHw92         SoundModelClient(uint32_t id, V2_0::ISoundTriggerHwCallback::CallbackCookie cookie)
93             : mId(id), mCookie(cookie) {}
~SoundModelClientSoundTriggerHw94         virtual ~SoundModelClient() {}
95 
getIdSoundTriggerHw96         uint32_t getId() const { return mId; }
getHalHandleSoundTriggerHw97         sound_model_handle_t getHalHandle() const { return mHalHandle; }
setHalHandleSoundTriggerHw98         void setHalHandle(sound_model_handle_t handle) { mHalHandle = handle; }
99 
100         virtual void recognitionCallback(struct sound_trigger_recognition_event* halEvent) = 0;
101         virtual void soundModelCallback(struct sound_trigger_model_event* halEvent) = 0;
102 
103        protected:
104         const uint32_t mId;
105         sound_model_handle_t mHalHandle;
106         V2_0::ISoundTriggerHwCallback::CallbackCookie mCookie;
107     };
108 
109    protected:
110     static void convertPhaseRecognitionEventFromHal(
111         V2_0::ISoundTriggerHwCallback::PhraseRecognitionEvent* event,
112         const struct sound_trigger_phrase_recognition_event* halEvent);
113     static void convertRecognitionEventFromHal(
114         V2_0::ISoundTriggerHwCallback::RecognitionEvent* event,
115         const struct sound_trigger_recognition_event* halEvent);
116     static void convertSoundModelEventFromHal(V2_0::ISoundTriggerHwCallback::ModelEvent* event,
117                                               const struct sound_trigger_model_event* halEvent);
118 
119     virtual ~SoundTriggerHw();
120 
121     uint32_t nextUniqueModelId();
122     int doLoadSoundModel(const V2_0::ISoundTriggerHw::SoundModel& soundModel,
123                          sp<SoundModelClient> client);
124 
125     // RefBase
126     void onFirstRef() override;
127 
128    private:
129     class SoundModelClient_2_0 : public SoundModelClient {
130        public:
SoundModelClient_2_0SoundTriggerHw131         SoundModelClient_2_0(uint32_t id, V2_0::ISoundTriggerHwCallback::CallbackCookie cookie,
132                              sp<V2_0::ISoundTriggerHwCallback> callback)
133             : SoundModelClient(id, cookie), mCallback(callback) {}
134 
135         void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
136         void soundModelCallback(struct sound_trigger_model_event* halEvent) override;
137 
138        private:
139         sp<V2_0::ISoundTriggerHwCallback> mCallback;
140     };
141 
142     void convertUuidFromHal(Uuid* uuid, const sound_trigger_uuid_t* halUuid);
143     void convertUuidToHal(sound_trigger_uuid_t* halUuid, const Uuid* uuid);
144     void convertPropertiesFromHal(V2_0::ISoundTriggerHw::Properties* properties,
145                                   const struct sound_trigger_properties* halProperties);
146     void convertTriggerPhraseToHal(struct sound_trigger_phrase* halTriggerPhrase,
147                                    const V2_0::ISoundTriggerHw::Phrase* triggerPhrase);
148     // returned HAL sound model must be freed by caller
149     struct sound_trigger_sound_model* convertSoundModelToHal(
150         const V2_0::ISoundTriggerHw::SoundModel* soundModel);
151     void convertPhraseRecognitionExtraToHal(struct sound_trigger_phrase_recognition_extra* halExtra,
152                                             const V2_0::PhraseRecognitionExtra* extra);
153     // returned recognition config must be freed by caller
154     struct sound_trigger_recognition_config* convertRecognitionConfigToHal(
155         const V2_0::ISoundTriggerHw::RecognitionConfig* config);
156 
157     static void convertPhraseRecognitionExtraFromHal(
158         V2_0::PhraseRecognitionExtra* extra,
159         const struct sound_trigger_phrase_recognition_extra* halExtra);
160 
161     static void soundModelCallback(struct sound_trigger_model_event* halEvent, void* cookie);
162     static void recognitionCallback(struct sound_trigger_recognition_event* halEvent, void* cookie);
163 
164     const char* mModuleName;
165     struct sound_trigger_hw_device* mHwDevice;
166     volatile atomic_uint_fast32_t mNextModelId;
167     DefaultKeyedVector<int32_t, sp<SoundModelClient> > mClients;
168     Mutex mLock;
169 
170     // Copied from hardware/interfaces/soundtrigger/2.1/default/SoundTriggerHw.h
171     class SoundModelClient_2_1 : public SoundModelClient {
172        public:
SoundModelClient_2_1SoundTriggerHw173         SoundModelClient_2_1(uint32_t id, V2_1::ISoundTriggerHwCallback::CallbackCookie cookie,
174                              sp<V2_1::ISoundTriggerHwCallback> callback)
175             : SoundModelClient(id, cookie), mCallback(callback) {}
176 
177         void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
178         void soundModelCallback(struct sound_trigger_model_event* halEvent) override;
179 
180        private:
181         sp<V2_1::ISoundTriggerHwCallback> mCallback;
182     };
183 };
184 
185 extern "C" ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* name);
186 
187 }  // namespace implementation
188 }  // namespace V2_2
189 }  // namespace soundtrigger
190 }  // namespace hardware
191 }  // namespace android
192 
193 #endif  // ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_SOUNDTRIGGERHW_H
194