blob: c82c9ea0e252f31d1edb1045acce014809c95d8c [file] [log] [blame]
Nicholas Ambur1e900192019-10-01 13:11:26 -07001/*
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
32namespace android {
33namespace hardware {
34namespace soundtrigger {
35namespace V2_3 {
36namespace implementation {
37
38using ::android::sp;
39using ::android::hardware::hidl_array;
40using ::android::hardware::hidl_memory;
41using ::android::hardware::hidl_string;
42using ::android::hardware::hidl_vec;
43using ::android::hardware::Return;
44using ::android::hardware::Void;
45using ::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 */
54struct 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<int32_t> setParameter(V2_0::SoundModelHandle modelHandle, ModelParameter modelParam,
89 int32_t value) override;
90 Return<void> getParameter(V2_0::SoundModelHandle modelHandle, ModelParameter modelParam,
91 ISoundTriggerHw::getParameter_cb _hidl_cb) override;
92 Return<void> queryParameter(V2_0::SoundModelHandle modelHandle, ModelParameter modelParam,
93 ISoundTriggerHw::queryParameter_cb _hidl_cb) override;
94
95 SoundTriggerHw();
96
97 // Copied from hardware/interfaces/soundtrigger/2.0/default/SoundTriggerHalImpl.h
98
99 /**
100 * Client object holding active handles and callback sctructures. Used for referencing
101 * which models map to which client of the HAL. SoundModelClients are stored in the
102 * mClients object while the model is active.
103 */
104 class SoundModelClient : public RefBase {
105 public:
106 SoundModelClient(uint32_t id, V2_0::ISoundTriggerHwCallback::CallbackCookie cookie)
107 : mId(id), mCookie(cookie) {}
108 virtual ~SoundModelClient() {}
109
110 uint32_t getId() const { return mId; }
111 sound_model_handle_t getHalHandle() const { return mHalHandle; }
112 void setHalHandle(sound_model_handle_t handle) { mHalHandle = handle; }
113
114 virtual void recognitionCallback(struct sound_trigger_recognition_event* halEvent) = 0;
115 virtual void soundModelCallback(struct sound_trigger_model_event* halEvent) = 0;
116
117 protected:
118 const uint32_t mId;
119 sound_model_handle_t mHalHandle;
120 V2_0::ISoundTriggerHwCallback::CallbackCookie mCookie;
121 };
122
123 private:
124 static void convertPhaseRecognitionEventFromHal(
125 V2_0::ISoundTriggerHwCallback::PhraseRecognitionEvent* event,
126 const struct sound_trigger_phrase_recognition_event* halEvent);
127 static void convertRecognitionEventFromHal(
128 V2_0::ISoundTriggerHwCallback::RecognitionEvent* event,
129 const struct sound_trigger_recognition_event* halEvent);
130 static void convertSoundModelEventFromHal(V2_0::ISoundTriggerHwCallback::ModelEvent* event,
131 const struct sound_trigger_model_event* halEvent);
132
133 virtual ~SoundTriggerHw();
134
135 uint32_t nextUniqueModelId();
136 int doLoadSoundModel(const V2_0::ISoundTriggerHw::SoundModel& soundModel,
137 sp<SoundModelClient> client);
138
139 // RefBase
140 void onFirstRef() override;
141
142 class SoundModelClient_2_0 : public SoundModelClient {
143 public:
144 SoundModelClient_2_0(uint32_t id, V2_0::ISoundTriggerHwCallback::CallbackCookie cookie,
145 sp<V2_0::ISoundTriggerHwCallback> callback)
146 : SoundModelClient(id, cookie), mCallback(callback) {}
147
148 void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
149 void soundModelCallback(struct sound_trigger_model_event* halEvent) override;
150
151 private:
152 sp<V2_0::ISoundTriggerHwCallback> mCallback;
153 };
154
155 void convertUuidFromHal(Uuid* uuid, const sound_trigger_uuid_t* halUuid);
156 void convertUuidToHal(sound_trigger_uuid_t* halUuid, const Uuid* uuid);
157 void convertPropertiesFromHal(V2_0::ISoundTriggerHw::Properties* properties,
158 const struct sound_trigger_properties* halProperties);
159 static sound_trigger_model_parameter_t convertModelParameterToHal(ModelParameter param);
160 void convertTriggerPhraseToHal(struct sound_trigger_phrase* halTriggerPhrase,
161 const V2_0::ISoundTriggerHw::Phrase* triggerPhrase);
162 // returned HAL sound model must be freed by caller
163 struct sound_trigger_sound_model* convertSoundModelToHal(
164 const V2_0::ISoundTriggerHw::SoundModel* soundModel);
165 void convertPhraseRecognitionExtraToHal(struct sound_trigger_phrase_recognition_extra* halExtra,
166 const V2_0::PhraseRecognitionExtra* extra);
167 // returned recognition config must be freed by caller
168 struct sound_trigger_recognition_config* convertRecognitionConfigToHal(
169 const V2_0::ISoundTriggerHw::RecognitionConfig* config);
170
171 static void convertPhraseRecognitionExtraFromHal(
172 V2_0::PhraseRecognitionExtra* extra,
173 const struct sound_trigger_phrase_recognition_extra* halExtra);
174
175 static void soundModelCallback(struct sound_trigger_model_event* halEvent, void* cookie);
176 static void recognitionCallback(struct sound_trigger_recognition_event* halEvent, void* cookie);
177
178 const char* mModuleName;
179 struct sound_trigger_hw_device* mHwDevice;
180 volatile atomic_uint_fast32_t mNextModelId;
181 DefaultKeyedVector<int32_t, sp<SoundModelClient>> mClients;
182 Mutex mLock;
183
184 // Copied from hardware/interfaces/soundtrigger/2.1/default/SoundTriggerHw.h
185 class SoundModelClient_2_1 : public SoundModelClient {
186 public:
187 SoundModelClient_2_1(uint32_t id, V2_1::ISoundTriggerHwCallback::CallbackCookie cookie,
188 sp<V2_1::ISoundTriggerHwCallback> callback)
189 : SoundModelClient(id, cookie), mCallback(callback) {}
190
191 void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
192 void soundModelCallback(struct sound_trigger_model_event* halEvent) override;
193
194 private:
195 sp<V2_1::ISoundTriggerHwCallback> mCallback;
196 };
197};
198
199extern "C" ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* name);
200
201} // namespace implementation
202} // namespace V2_3
203} // namespace soundtrigger
204} // namespace hardware
205} // namespace android
206
207#endif // ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_SOUNDTRIGGERHW_H