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