blob: 0f9aa1589f9337bf6e1c70086f84e4740e4b2d42 [file] [log] [blame]
Eric Laurent7a544b42016-08-05 19:01:13 -07001/*
2 * Copyright (C) 2016 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#define LOG_TAG "SoundTriggerHalHidl"
18//#define LOG_NDEBUG 0
19
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080020#include <android/hidl/allocator/1.0/IAllocator.h>
Mikhail Naganovd621ac82017-01-12 17:17:45 -080021#include <media/audiohal/hidl/HalDeathHandler.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070022#include <utils/Log.h>
23#include "SoundTriggerHalHidl.h"
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080024#include <hidlmemory/mapping.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070025#include <hwbinder/IPCThreadState.h>
26#include <hwbinder/ProcessState.h>
27
28namespace android {
29
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080030using ::android::hardware::ProcessState;
31using ::android::hardware::Return;
32using ::android::hardware::Status;
33using ::android::hardware::Void;
34using ::android::hardware::audio::common::V2_0::AudioDevice;
35using ::android::hardware::hidl_memory;
36using ::android::hidl::allocator::V1_0::IAllocator;
37using ::android::hidl::memory::V1_0::IMemory;
38
39namespace {
40
41// Backs up by the vector with the contents of shared memory.
42// It is assumed that the passed hidl_vector is empty, so it's
43// not cleared if the memory is a null object.
44// The caller needs to keep the returned sp<IMemory> as long as
45// the data is needed.
46std::pair<bool, sp<IMemory>> memoryAsVector(const hidl_memory& m, hidl_vec<uint8_t>* vec) {
47 sp<IMemory> memory;
48 if (m.size() == 0) {
49 return std::make_pair(true, memory);
50 }
51 memory = mapMemory(m);
52 if (memory != nullptr) {
53 memory->read();
54 vec->setToExternal(static_cast<uint8_t*>(static_cast<void*>(memory->getPointer())),
55 memory->getSize());
56 return std::make_pair(true, memory);
57 }
58 ALOGE("%s: Could not map HIDL memory to IMemory", __func__);
59 return std::make_pair(false, memory);
60}
61
62// Moves the data from the vector into allocated shared memory,
63// emptying the vector.
64// It is assumed that the passed hidl_memory is a null object, so it's
65// not reset if the vector is empty.
66// The caller needs to keep the returned sp<IMemory> as long as
67// the data is needed.
68std::pair<bool, sp<IMemory>> moveVectorToMemory(hidl_vec<uint8_t>* v, hidl_memory* mem) {
69 sp<IMemory> memory;
70 if (v->size() == 0) {
71 return std::make_pair(true, memory);
72 }
73 sp<IAllocator> ashmem = IAllocator::getService("ashmem");
74 if (ashmem == 0) {
75 ALOGE("Failed to retrieve ashmem allocator service");
76 return std::make_pair(false, memory);
77 }
78 bool success = false;
79 Return<void> r = ashmem->allocate(v->size(), [&](bool s, const hidl_memory& m) {
80 success = s;
81 if (success) *mem = m;
82 });
83 if (r.isOk() && success) {
84 memory = hardware::mapMemory(*mem);
85 if (memory != 0) {
86 memory->update();
87 memcpy(memory->getPointer(), v->data(), v->size());
88 memory->commit();
89 v->resize(0);
90 return std::make_pair(true, memory);
91 } else {
92 ALOGE("Failed to map allocated ashmem");
93 }
94 } else {
95 ALOGE("Failed to allocate %llu bytes from ashmem", (unsigned long long)v->size());
96 }
97 return std::make_pair(false, memory);
98}
99
100} // namespace
Eric Laurent7a544b42016-08-05 19:01:13 -0700101
Eric Laurent7a544b42016-08-05 19:01:13 -0700102/* static */
103sp<SoundTriggerHalInterface> SoundTriggerHalInterface::connectModule(const char *moduleName)
104{
105 return new SoundTriggerHalHidl(moduleName);
106}
107
108int SoundTriggerHalHidl::getProperties(struct sound_trigger_properties *properties)
109{
110 sp<ISoundTriggerHw> soundtrigger = getService();
111 if (soundtrigger == 0) {
112 return -ENODEV;
113 }
114
115 ISoundTriggerHw::Properties halProperties;
116 Return<void> hidlReturn;
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800117 int ret;
Eric Laurent7a544b42016-08-05 19:01:13 -0700118 {
119 AutoMutex lock(mHalLock);
120 hidlReturn = soundtrigger->getProperties([&](int rc, auto res) {
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800121 ret = rc;
Eric Laurent7a544b42016-08-05 19:01:13 -0700122 halProperties = res;
123 ALOGI("getProperties res implementor %s", res.implementor.c_str());
124 });
125 }
126
Steven Morelande83be8a2017-01-06 11:06:33 -0800127 if (hidlReturn.isOk()) {
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800128 if (ret == 0) {
129 convertPropertiesFromHal(properties, &halProperties);
130 }
Eric Laurent7a544b42016-08-05 19:01:13 -0700131 } else {
Steven Morelande83be8a2017-01-06 11:06:33 -0800132 ALOGE("getProperties error %s", hidlReturn.description().c_str());
Mikhail Naganovd621ac82017-01-12 17:17:45 -0800133 return FAILED_TRANSACTION;
Eric Laurent7a544b42016-08-05 19:01:13 -0700134 }
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800135 ALOGI("getProperties ret %d", ret);
Eric Laurent7a544b42016-08-05 19:01:13 -0700136 return ret;
137}
138
139int SoundTriggerHalHidl::loadSoundModel(struct sound_trigger_sound_model *sound_model,
140 sound_model_callback_t callback,
141 void *cookie,
142 sound_model_handle_t *handle)
143{
144 if (handle == NULL) {
145 return -EINVAL;
146 }
147
148 sp<ISoundTriggerHw> soundtrigger = getService();
149 if (soundtrigger == 0) {
150 return -ENODEV;
151 }
152
153 uint32_t modelId;
154 {
155 AutoMutex lock(mLock);
156 do {
157 modelId = nextUniqueId();
158 ALOGI("loadSoundModel modelId %u", modelId);
159 sp<SoundModel> model = mSoundModels.valueFor(modelId);
160 ALOGI("loadSoundModel model %p", model.get());
161 } while (mSoundModels.valueFor(modelId) != 0 && modelId != 0);
162 }
163 LOG_ALWAYS_FATAL_IF(modelId == 0,
164 "loadSoundModel(): wrap around in sound model IDs, num loaded models %zd",
165 mSoundModels.size());
166
Eric Laurent7a544b42016-08-05 19:01:13 -0700167 Return<void> hidlReturn;
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800168 int ret;
Eric Laurent7a544b42016-08-05 19:01:13 -0700169 SoundModelHandle halHandle;
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800170 sp<V2_1_ISoundTriggerHw> soundtrigger_2_1 = toService2_1(soundtrigger);
171 if (sound_model->type == SOUND_MODEL_TYPE_KEYPHRASE) {
172 if (!soundtrigger_2_1) {
173 ISoundTriggerHw::PhraseSoundModel halSoundModel;
174 convertPhraseSoundModelToHal(&halSoundModel, sound_model);
175 AutoMutex lock(mHalLock);
Eric Laurent7a544b42016-08-05 19:01:13 -0700176 hidlReturn = soundtrigger->loadPhraseSoundModel(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800177 halSoundModel,
Eric Laurent7a544b42016-08-05 19:01:13 -0700178 this, modelId, [&](int32_t retval, auto res) {
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800179 ret = retval;
180 halHandle = res;
181 });
Eric Laurent7a544b42016-08-05 19:01:13 -0700182 } else {
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800183 V2_1_ISoundTriggerHw::PhraseSoundModel halSoundModel;
184 auto result = convertPhraseSoundModelToHal(&halSoundModel, sound_model);
185 if (result.first) {
186 AutoMutex lock(mHalLock);
187 hidlReturn = soundtrigger_2_1->loadPhraseSoundModel_2_1(
188 halSoundModel,
189 this, modelId, [&](int32_t retval, auto res) {
190 ret = retval;
191 halHandle = res;
192 });
193 } else {
194 return NO_MEMORY;
195 }
196 }
197 } else {
198 if (!soundtrigger_2_1) {
199 ISoundTriggerHw::SoundModel halSoundModel;
200 convertSoundModelToHal(&halSoundModel, sound_model);
201 AutoMutex lock(mHalLock);
202 hidlReturn = soundtrigger->loadSoundModel(halSoundModel,
Eric Laurent7a544b42016-08-05 19:01:13 -0700203 this, modelId, [&](int32_t retval, auto res) {
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800204 ret = retval;
205 halHandle = res;
206 });
207 } else {
208 V2_1_ISoundTriggerHw::SoundModel halSoundModel;
209 auto result = convertSoundModelToHal(&halSoundModel, sound_model);
210 if (result.first) {
211 AutoMutex lock(mHalLock);
212 hidlReturn = soundtrigger_2_1->loadSoundModel_2_1(halSoundModel,
213 this, modelId, [&](int32_t retval, auto res) {
214 ret = retval;
215 halHandle = res;
216 });
217 } else {
218 return NO_MEMORY;
219 }
Eric Laurent7a544b42016-08-05 19:01:13 -0700220 }
221 }
222
Steven Morelande83be8a2017-01-06 11:06:33 -0800223 if (hidlReturn.isOk()) {
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800224 if (ret == 0) {
225 AutoMutex lock(mLock);
226 *handle = (sound_model_handle_t)modelId;
227 sp<SoundModel> model = new SoundModel(*handle, callback, cookie, halHandle);
228 mSoundModels.add(*handle, model);
229 }
Eric Laurent7a544b42016-08-05 19:01:13 -0700230 } else {
Steven Morelande83be8a2017-01-06 11:06:33 -0800231 ALOGE("loadSoundModel error %s", hidlReturn.description().c_str());
Mikhail Naganovd621ac82017-01-12 17:17:45 -0800232 return FAILED_TRANSACTION;
Eric Laurent7a544b42016-08-05 19:01:13 -0700233 }
234
Eric Laurent7a544b42016-08-05 19:01:13 -0700235 return ret;
236}
237
238int SoundTriggerHalHidl::unloadSoundModel(sound_model_handle_t handle)
239{
240 sp<ISoundTriggerHw> soundtrigger = getService();
241 if (soundtrigger == 0) {
242 return -ENODEV;
243 }
244
245 sp<SoundModel> model = removeModel(handle);
246 if (model == 0) {
247 ALOGE("unloadSoundModel model not found for handle %u", handle);
248 return -EINVAL;
249 }
250
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800251 Return<int32_t> hidlReturn(0);
Eric Laurent7a544b42016-08-05 19:01:13 -0700252 {
253 AutoMutex lock(mHalLock);
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800254 hidlReturn = soundtrigger->unloadSoundModel(model->mHalHandle);
Eric Laurent7a544b42016-08-05 19:01:13 -0700255 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800256
257 if (!hidlReturn.isOk()) {
258 ALOGE("unloadSoundModel error %s", hidlReturn.description().c_str());
Mikhail Naganovd621ac82017-01-12 17:17:45 -0800259 return FAILED_TRANSACTION;
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800260 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800261
262 return hidlReturn;
Eric Laurent7a544b42016-08-05 19:01:13 -0700263}
264
265int SoundTriggerHalHidl::startRecognition(sound_model_handle_t handle,
266 const struct sound_trigger_recognition_config *config,
267 recognition_callback_t callback,
268 void *cookie)
269{
270 sp<ISoundTriggerHw> soundtrigger = getService();
271 if (soundtrigger == 0) {
272 return -ENODEV;
273 }
274
275 sp<SoundModel> model = getModel(handle);
276 if (model == 0) {
277 ALOGE("startRecognition model not found for handle %u", handle);
278 return -EINVAL;
279 }
280
281 model->mRecognitionCallback = callback;
282 model->mRecognitionCookie = cookie;
283
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800284 sp<V2_1_ISoundTriggerHw> soundtrigger_2_1 = toService2_1(soundtrigger);
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800285 Return<int32_t> hidlReturn(0);
Eric Laurent7a544b42016-08-05 19:01:13 -0700286
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800287 if (!soundtrigger_2_1) {
288 ISoundTriggerHw::RecognitionConfig halConfig;
289 convertRecognitionConfigToHal(&halConfig, config);
290 {
291 AutoMutex lock(mHalLock);
292 hidlReturn = soundtrigger->startRecognition(model->mHalHandle, halConfig, this, handle);
293 }
294 } else {
295 V2_1_ISoundTriggerHw::RecognitionConfig halConfig;
296 auto result = convertRecognitionConfigToHal(&halConfig, config);
297 if (result.first) {
298 AutoMutex lock(mHalLock);
299 hidlReturn = soundtrigger_2_1->startRecognition_2_1(
300 model->mHalHandle, halConfig, this, handle);
301 } else {
302 return NO_MEMORY;
303 }
304 }
Eric Laurent7a544b42016-08-05 19:01:13 -0700305
Steven Morelande83be8a2017-01-06 11:06:33 -0800306 if (!hidlReturn.isOk()) {
307 ALOGE("startRecognition error %s", hidlReturn.description().c_str());
Mikhail Naganovd621ac82017-01-12 17:17:45 -0800308 return FAILED_TRANSACTION;
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800309 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800310 return hidlReturn;
Eric Laurent7a544b42016-08-05 19:01:13 -0700311}
312
313int SoundTriggerHalHidl::stopRecognition(sound_model_handle_t handle)
314{
315 sp<ISoundTriggerHw> soundtrigger = getService();
316 if (soundtrigger == 0) {
317 return -ENODEV;
318 }
319
320 sp<SoundModel> model = getModel(handle);
321 if (model == 0) {
322 ALOGE("stopRecognition model not found for handle %u", handle);
323 return -EINVAL;
324 }
325
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800326 Return<int32_t> hidlReturn(0);
Eric Laurent7a544b42016-08-05 19:01:13 -0700327 {
328 AutoMutex lock(mHalLock);
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800329 hidlReturn = soundtrigger->stopRecognition(model->mHalHandle);
Eric Laurent7a544b42016-08-05 19:01:13 -0700330 }
331
Steven Morelande83be8a2017-01-06 11:06:33 -0800332 if (!hidlReturn.isOk()) {
333 ALOGE("stopRecognition error %s", hidlReturn.description().c_str());
Mikhail Naganovd621ac82017-01-12 17:17:45 -0800334 return FAILED_TRANSACTION;
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800335 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800336 return hidlReturn;
Eric Laurent7a544b42016-08-05 19:01:13 -0700337}
338
339int SoundTriggerHalHidl::stopAllRecognitions()
340{
341 sp<ISoundTriggerHw> soundtrigger = getService();
342 if (soundtrigger == 0) {
343 return -ENODEV;
344 }
345
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800346 Return<int32_t> hidlReturn(0);
Eric Laurent7a544b42016-08-05 19:01:13 -0700347 {
348 AutoMutex lock(mHalLock);
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800349 hidlReturn = soundtrigger->stopAllRecognitions();
Eric Laurent7a544b42016-08-05 19:01:13 -0700350 }
351
Steven Morelande83be8a2017-01-06 11:06:33 -0800352 if (!hidlReturn.isOk()) {
353 ALOGE("stopAllRecognitions error %s", hidlReturn.description().c_str());
Mikhail Naganovd621ac82017-01-12 17:17:45 -0800354 return FAILED_TRANSACTION;
Eric Laurent4b38e7a2016-11-11 13:28:53 -0800355 }
Steven Morelande83be8a2017-01-06 11:06:33 -0800356 return hidlReturn;
Eric Laurent7a544b42016-08-05 19:01:13 -0700357}
358
Michael Dooley67e3d412018-10-16 19:51:16 +0000359int SoundTriggerHalHidl::getModelState(sound_model_handle_t handle,
360 struct sound_trigger_recognition_event** event)
361{
362 sp<ISoundTriggerHw> soundtrigger = getService();
363 if (soundtrigger == 0) {
364 return -ENODEV;
365 }
366
367 sp<V2_2_ISoundTriggerHw> soundtrigger_2_2 = toService2_2(soundtrigger);
368 if (soundtrigger_2_2 == 0) {
369 ALOGE("getModelState not supported");
370 return -ENODEV;
371 }
372
373 sp<SoundModel> model = getModel(handle);
374 if (model == 0) {
375 ALOGE("getModelState model not found for handle %u", handle);
376 return -EINVAL;
377 }
378
379 int ret = NO_ERROR;
380 Return<void> hidlReturn;
381 {
382 AutoMutex lock(mHalLock);
383 hidlReturn = soundtrigger_2_2->getModelState(
384 model->mHalHandle,
385 [&](int r, const V2_0_ISoundTriggerHwCallback::RecognitionEvent& halEvent) {
386 ret = r;
387 if (ret != 0) {
388 ALOGE("getModelState returned error code %d", ret);
389 } else {
390 *event = convertRecognitionEventFromHal(&halEvent);
391 }
392 });
393 }
394 if (!hidlReturn.isOk()) {
395 ALOGE("getModelState error %s", hidlReturn.description().c_str());
396 free(*event);
397 *event = nullptr;
398 ret = FAILED_TRANSACTION;
399 }
400 return ret;
401}
402
Eric Laurent7a544b42016-08-05 19:01:13 -0700403SoundTriggerHalHidl::SoundTriggerHalHidl(const char *moduleName)
404 : mModuleName(moduleName), mNextUniqueId(1)
405{
Mikhail Naganov49ad5522017-04-13 11:00:06 -0700406 LOG_ALWAYS_FATAL_IF(strcmp(mModuleName, "primary") != 0,
407 "Treble soundtrigger only supports primary module");
Eric Laurent7a544b42016-08-05 19:01:13 -0700408}
409
Eric Laurent7a544b42016-08-05 19:01:13 -0700410SoundTriggerHalHidl::~SoundTriggerHalHidl()
411{
412}
413
414sp<ISoundTriggerHw> SoundTriggerHalHidl::getService()
415{
416 AutoMutex lock(mLock);
417 if (mISoundTrigger == 0) {
418 if (mModuleName == NULL) {
419 mModuleName = "primary";
420 }
Mikhail Naganov49ad5522017-04-13 11:00:06 -0700421 mISoundTrigger = ISoundTriggerHw::getService();
Mikhail Naganovd621ac82017-01-12 17:17:45 -0800422 if (mISoundTrigger != 0) {
423 mISoundTrigger->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
424 }
Eric Laurent7a544b42016-08-05 19:01:13 -0700425 }
426 return mISoundTrigger;
427}
428
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800429sp<V2_1_ISoundTriggerHw> SoundTriggerHalHidl::toService2_1(const sp<ISoundTriggerHw>& s)
430{
431 auto castResult_2_1 = V2_1_ISoundTriggerHw::castFrom(s);
432 return castResult_2_1.isOk() ? static_cast<sp<V2_1_ISoundTriggerHw>>(castResult_2_1) : nullptr;
433}
434
Michael Dooley67e3d412018-10-16 19:51:16 +0000435sp<V2_2_ISoundTriggerHw> SoundTriggerHalHidl::toService2_2(const sp<ISoundTriggerHw>& s)
436{
437 auto castResult_2_2 = V2_2_ISoundTriggerHw::castFrom(s);
438 return castResult_2_2.isOk() ? static_cast<sp<V2_2_ISoundTriggerHw>>(castResult_2_2) : nullptr;
439}
440
Eric Laurent7a544b42016-08-05 19:01:13 -0700441sp<SoundTriggerHalHidl::SoundModel> SoundTriggerHalHidl::getModel(sound_model_handle_t handle)
442{
443 AutoMutex lock(mLock);
444 return mSoundModels.valueFor(handle);
445}
446
447sp<SoundTriggerHalHidl::SoundModel> SoundTriggerHalHidl::removeModel(sound_model_handle_t handle)
448{
449 AutoMutex lock(mLock);
450 sp<SoundModel> model = mSoundModels.valueFor(handle);
451 mSoundModels.removeItem(handle);
452 return model;
453}
454
455uint32_t SoundTriggerHalHidl::nextUniqueId()
456{
457 return (uint32_t) atomic_fetch_add_explicit(&mNextUniqueId,
458 (uint_fast32_t) 1, memory_order_acq_rel);
459}
460
461void SoundTriggerHalHidl::convertUuidToHal(Uuid *halUuid,
Eric Laurentf7854d42016-10-14 15:57:18 -0700462 const sound_trigger_uuid_t *uuid)
Eric Laurent7a544b42016-08-05 19:01:13 -0700463{
464 halUuid->timeLow = uuid->timeLow;
465 halUuid->timeMid = uuid->timeMid;
466 halUuid->versionAndTimeHigh = uuid->timeHiAndVersion;
467 halUuid->variantAndClockSeqHigh = uuid->clockSeq;
468 memcpy(halUuid->node.data(), &uuid->node[0], sizeof(uuid->node));
469}
470
Eric Laurentf7854d42016-10-14 15:57:18 -0700471void SoundTriggerHalHidl::convertUuidFromHal(sound_trigger_uuid_t *uuid,
Eric Laurent7a544b42016-08-05 19:01:13 -0700472 const Uuid *halUuid)
473{
474 uuid->timeLow = halUuid->timeLow;
475 uuid->timeMid = halUuid->timeMid;
476 uuid->timeHiAndVersion = halUuid->versionAndTimeHigh;
477 uuid->clockSeq = halUuid->variantAndClockSeqHigh;
478 memcpy(&uuid->node[0], halUuid->node.data(), sizeof(uuid->node));
479}
480
481void SoundTriggerHalHidl::convertPropertiesFromHal(
482 struct sound_trigger_properties *properties,
483 const ISoundTriggerHw::Properties *halProperties)
484{
485 strlcpy(properties->implementor,
486 halProperties->implementor.c_str(), SOUND_TRIGGER_MAX_STRING_LEN);
487 strlcpy(properties->description,
488 halProperties->description.c_str(), SOUND_TRIGGER_MAX_STRING_LEN);
489 properties->version = halProperties->version;
490 convertUuidFromHal(&properties->uuid, &halProperties->uuid);
491 properties->max_sound_models = halProperties->maxSoundModels;
492 properties->max_key_phrases = halProperties->maxKeyPhrases;
493 properties->max_users = halProperties->maxUsers;
494 properties->recognition_modes = halProperties->recognitionModes;
495 properties->capture_transition = (bool)halProperties->captureTransition;
496 properties->max_buffer_ms = halProperties->maxBufferMs;
497 properties->concurrent_capture = (bool)halProperties->concurrentCapture;
498 properties->trigger_in_event = (bool)halProperties->triggerInEvent;
499 properties->power_consumption_mw = halProperties->powerConsumptionMw;
500}
501
502void SoundTriggerHalHidl::convertTriggerPhraseToHal(
503 ISoundTriggerHw::Phrase *halTriggerPhrase,
504 const struct sound_trigger_phrase *triggerPhrase)
505{
506 halTriggerPhrase->id = triggerPhrase->id;
507 halTriggerPhrase->recognitionModes = triggerPhrase->recognition_mode;
508 halTriggerPhrase->users.setToExternal((uint32_t *)&triggerPhrase->users[0], triggerPhrase->num_users);
509 halTriggerPhrase->locale = triggerPhrase->locale;
510 halTriggerPhrase->text = triggerPhrase->text;
511}
512
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800513
514void SoundTriggerHalHidl::convertTriggerPhrasesToHal(
515 hidl_vec<ISoundTriggerHw::Phrase> *halTriggerPhrases,
516 struct sound_trigger_phrase_sound_model *keyPhraseModel)
517{
518 halTriggerPhrases->resize(keyPhraseModel->num_phrases);
519 for (unsigned int i = 0; i < keyPhraseModel->num_phrases; i++) {
520 convertTriggerPhraseToHal(&(*halTriggerPhrases)[i], &keyPhraseModel->phrases[i]);
521 }
522}
523
524void SoundTriggerHalHidl::convertSoundModelToHal(ISoundTriggerHw::SoundModel *halModel,
Eric Laurent7a544b42016-08-05 19:01:13 -0700525 const struct sound_trigger_sound_model *soundModel)
526{
Eric Laurent7a544b42016-08-05 19:01:13 -0700527 halModel->type = (SoundModelType)soundModel->type;
528 convertUuidToHal(&halModel->uuid, &soundModel->uuid);
529 convertUuidToHal(&halModel->vendorUuid, &soundModel->vendor_uuid);
530 halModel->data.setToExternal((uint8_t *)soundModel + soundModel->data_offset, soundModel->data_size);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800531}
Eric Laurent7a544b42016-08-05 19:01:13 -0700532
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800533std::pair<bool, sp<IMemory>> SoundTriggerHalHidl::convertSoundModelToHal(
534 V2_1_ISoundTriggerHw::SoundModel *halModel,
535 const struct sound_trigger_sound_model *soundModel)
536{
537 convertSoundModelToHal(&halModel->header, soundModel);
538 return moveVectorToMemory(&halModel->header.data, &halModel->data);
539}
540
541void SoundTriggerHalHidl::convertPhraseSoundModelToHal(
542 ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel,
543 const struct sound_trigger_sound_model *soundModel)
544{
545 struct sound_trigger_phrase_sound_model *keyPhraseModel =
546 (struct sound_trigger_phrase_sound_model *)soundModel;
547 convertTriggerPhrasesToHal(&halKeyPhraseModel->phrases, keyPhraseModel);
548 convertSoundModelToHal(&halKeyPhraseModel->common, soundModel);
549}
550
551std::pair<bool, sp<IMemory>> SoundTriggerHalHidl::convertPhraseSoundModelToHal(
552 V2_1_ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel,
553 const struct sound_trigger_sound_model *soundModel)
554{
555 struct sound_trigger_phrase_sound_model *keyPhraseModel =
556 (struct sound_trigger_phrase_sound_model *)soundModel;
557 convertTriggerPhrasesToHal(&halKeyPhraseModel->phrases, keyPhraseModel);
558 return convertSoundModelToHal(&halKeyPhraseModel->common, soundModel);
Eric Laurent7a544b42016-08-05 19:01:13 -0700559}
560
561void SoundTriggerHalHidl::convertPhraseRecognitionExtraToHal(
562 PhraseRecognitionExtra *halExtra,
563 const struct sound_trigger_phrase_recognition_extra *extra)
564{
565 halExtra->id = extra->id;
566 halExtra->recognitionModes = extra->recognition_modes;
567 halExtra->confidenceLevel = extra->confidence_level;
Eric Laurent7a544b42016-08-05 19:01:13 -0700568 halExtra->levels.resize(extra->num_levels);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800569 for (unsigned int i = 0; i < extra->num_levels; i++) {
570 halExtra->levels[i].userId = extra->levels[i].user_id;
571 halExtra->levels[i].levelPercent = extra->levels[i].level;
572 }
Eric Laurent7a544b42016-08-05 19:01:13 -0700573}
574
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800575void SoundTriggerHalHidl::convertRecognitionConfigToHal(
576 ISoundTriggerHw::RecognitionConfig *halConfig,
Eric Laurent7a544b42016-08-05 19:01:13 -0700577 const struct sound_trigger_recognition_config *config)
578{
Eric Laurent7a544b42016-08-05 19:01:13 -0700579 halConfig->captureHandle = config->capture_handle;
580 halConfig->captureDevice = (AudioDevice)config->capture_device;
581 halConfig->captureRequested = (uint32_t)config->capture_requested;
582
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800583 halConfig->phrases.resize(config->num_phrases);
Eric Laurent7a544b42016-08-05 19:01:13 -0700584 for (unsigned int i = 0; i < config->num_phrases; i++) {
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800585 convertPhraseRecognitionExtraToHal(&halConfig->phrases[i],
Eric Laurent7a544b42016-08-05 19:01:13 -0700586 &config->phrases[i]);
587 }
Eric Laurent7a544b42016-08-05 19:01:13 -0700588
589 halConfig->data.setToExternal((uint8_t *)config + config->data_offset, config->data_size);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800590}
Eric Laurent7a544b42016-08-05 19:01:13 -0700591
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800592std::pair<bool, sp<IMemory>> SoundTriggerHalHidl::convertRecognitionConfigToHal(
593 V2_1_ISoundTriggerHw::RecognitionConfig *halConfig,
594 const struct sound_trigger_recognition_config *config)
595{
596 convertRecognitionConfigToHal(&halConfig->header, config);
597 return moveVectorToMemory(&halConfig->header.data, &halConfig->data);
Eric Laurent7a544b42016-08-05 19:01:13 -0700598}
599
600
601// ISoundTriggerHwCallback
602::android::hardware::Return<void> SoundTriggerHalHidl::recognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800603 const V2_0_ISoundTriggerHwCallback::RecognitionEvent& halEvent,
Eric Laurent7a544b42016-08-05 19:01:13 -0700604 CallbackCookie cookie)
605{
606 sp<SoundModel> model;
607 {
608 AutoMutex lock(mLock);
609 model = mSoundModels.valueFor((SoundModelHandle)cookie);
610 if (model == 0) {
611 return Return<void>();
612 }
613 }
614 struct sound_trigger_recognition_event *event = convertRecognitionEventFromHal(&halEvent);
615 if (event == NULL) {
616 return Return<void>();
617 }
618 event->model = model->mHandle;
619 model->mRecognitionCallback(event, model->mRecognitionCookie);
620
621 free(event);
622
623 return Return<void>();
624}
625
626::android::hardware::Return<void> SoundTriggerHalHidl::phraseRecognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800627 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& halEvent,
Eric Laurent7a544b42016-08-05 19:01:13 -0700628 CallbackCookie cookie)
629{
630 sp<SoundModel> model;
631 {
632 AutoMutex lock(mLock);
633 model = mSoundModels.valueFor((SoundModelHandle)cookie);
634 if (model == 0) {
635 return Return<void>();
636 }
637 }
638
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800639 struct sound_trigger_phrase_recognition_event *event =
640 convertPhraseRecognitionEventFromHal(&halEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700641 if (event == NULL) {
642 return Return<void>();
643 }
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800644 event->common.model = model->mHandle;
645 model->mRecognitionCallback(&event->common, model->mRecognitionCookie);
Eric Laurent7a544b42016-08-05 19:01:13 -0700646
647 free(event);
648
649 return Return<void>();
650}
651
652::android::hardware::Return<void> SoundTriggerHalHidl::soundModelCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800653 const V2_0_ISoundTriggerHwCallback::ModelEvent& halEvent,
Eric Laurent7a544b42016-08-05 19:01:13 -0700654 CallbackCookie cookie)
655{
656 sp<SoundModel> model;
657 {
658 AutoMutex lock(mLock);
659 model = mSoundModels.valueFor((SoundModelHandle)cookie);
660 if (model == 0) {
661 return Return<void>();
662 }
663 }
664
665 struct sound_trigger_model_event *event = convertSoundModelEventFromHal(&halEvent);
666 if (event == NULL) {
667 return Return<void>();
668 }
669
670 event->model = model->mHandle;
671 model->mSoundModelCallback(event, model->mSoundModelCookie);
672
673 free(event);
674
675 return Return<void>();
676}
677
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800678::android::hardware::Return<void> SoundTriggerHalHidl::recognitionCallback_2_1(
679 const ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie) {
680 // The data vector in the 'header' part of V2.1 structure is empty, thus copying is cheap.
681 V2_0_ISoundTriggerHwCallback::RecognitionEvent event_2_0 = event.header;
682 auto result = memoryAsVector(event.data, &event_2_0.data);
683 return result.first ? recognitionCallback(event_2_0, cookie) : Void();
684}
685
686::android::hardware::Return<void> SoundTriggerHalHidl::phraseRecognitionCallback_2_1(
687 const ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie) {
688 V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent event_2_0;
689 // The data vector in the 'header' part of V2.1 structure is empty, thus copying is cheap.
690 event_2_0.common = event.common.header;
691 event_2_0.phraseExtras.setToExternal(
692 const_cast<PhraseRecognitionExtra*>(event.phraseExtras.data()),
693 event.phraseExtras.size());
694 auto result = memoryAsVector(event.common.data, &event_2_0.common.data);
695 return result.first ? phraseRecognitionCallback(event_2_0, cookie) : Void();
696}
697
698::android::hardware::Return<void> SoundTriggerHalHidl::soundModelCallback_2_1(
699 const ISoundTriggerHwCallback::ModelEvent& event, CallbackCookie cookie) {
700 // The data vector in the 'header' part of V2.1 structure is empty, thus copying is cheap.
701 V2_0_ISoundTriggerHwCallback::ModelEvent event_2_0 = event.header;
702 auto result = memoryAsVector(event.data, &event_2_0.data);
703 return result.first ? soundModelCallback(event_2_0, cookie) : Void();
704}
705
Eric Laurent7a544b42016-08-05 19:01:13 -0700706
707struct sound_trigger_model_event *SoundTriggerHalHidl::convertSoundModelEventFromHal(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800708 const V2_0_ISoundTriggerHwCallback::ModelEvent *halEvent)
Eric Laurent7a544b42016-08-05 19:01:13 -0700709{
710 struct sound_trigger_model_event *event = (struct sound_trigger_model_event *)malloc(
711 sizeof(struct sound_trigger_model_event) +
712 halEvent->data.size());
713 if (event == NULL) {
714 return NULL;
715 }
716
717 event->status = (int)halEvent->status;
718 // event->model to be set by caller
719 event->data_offset = sizeof(struct sound_trigger_model_event);
720 event->data_size = halEvent->data.size();
721 uint8_t *dst = (uint8_t *)event + event->data_offset;
722 uint8_t *src = (uint8_t *)&halEvent->data[0];
723 memcpy(dst, src, halEvent->data.size());
724
725 return event;
726}
727
728void SoundTriggerHalHidl::convertPhraseRecognitionExtraFromHal(
729 struct sound_trigger_phrase_recognition_extra *extra,
730 const PhraseRecognitionExtra *halExtra)
731{
732 extra->id = halExtra->id;
733 extra->recognition_modes = halExtra->recognitionModes;
734 extra->confidence_level = halExtra->confidenceLevel;
735
736 size_t i;
737 for (i = 0; i < halExtra->levels.size() && i < SOUND_TRIGGER_MAX_USERS; i++) {
738 extra->levels[i].user_id = halExtra->levels[i].userId;
739 extra->levels[i].level = halExtra->levels[i].levelPercent;
740 }
741 extra->num_levels = (unsigned int)i;
742}
743
744
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800745struct sound_trigger_phrase_recognition_event* SoundTriggerHalHidl::convertPhraseRecognitionEventFromHal(
746 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent *halPhraseEvent)
Eric Laurent7a544b42016-08-05 19:01:13 -0700747{
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800748 if (halPhraseEvent->common.type != SoundModelType::KEYPHRASE) {
749 ALOGE("Received non-keyphrase event type as PhraseRecognitionEvent");
750 return NULL;
Eric Laurent7a544b42016-08-05 19:01:13 -0700751 }
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800752 struct sound_trigger_phrase_recognition_event *phraseEvent =
753 (struct sound_trigger_phrase_recognition_event *)malloc(
754 sizeof(struct sound_trigger_phrase_recognition_event) +
755 halPhraseEvent->common.data.size());
756 if (phraseEvent == NULL) {
757 return NULL;
758 }
759 phraseEvent->common.data_offset = sizeof(sound_trigger_phrase_recognition_event);
760
761 for (unsigned int i = 0; i < halPhraseEvent->phraseExtras.size(); i++) {
762 convertPhraseRecognitionExtraFromHal(&phraseEvent->phrase_extras[i],
763 &halPhraseEvent->phraseExtras[i]);
764 }
765 phraseEvent->num_phrases = halPhraseEvent->phraseExtras.size();
766
767 fillRecognitionEventFromHal(&phraseEvent->common, &halPhraseEvent->common);
768 return phraseEvent;
769}
770
771struct sound_trigger_recognition_event *SoundTriggerHalHidl::convertRecognitionEventFromHal(
772 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent)
773{
774 if (halEvent->type == SoundModelType::KEYPHRASE) {
775 ALOGE("Received keyphrase event type as RecognitionEvent");
776 return NULL;
777 }
778 struct sound_trigger_recognition_event *event;
779 event = (struct sound_trigger_recognition_event *)malloc(
780 sizeof(struct sound_trigger_recognition_event) + halEvent->data.size());
781 if (event == NULL) {
782 return NULL;
783 }
784 event->data_offset = sizeof(sound_trigger_recognition_event);
785
786 fillRecognitionEventFromHal(event, halEvent);
787 return event;
788}
789
790void SoundTriggerHalHidl::fillRecognitionEventFromHal(
791 struct sound_trigger_recognition_event *event,
792 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent)
793{
Eric Laurent7a544b42016-08-05 19:01:13 -0700794 event->status = (int)halEvent->status;
795 event->type = (sound_trigger_sound_model_type_t)halEvent->type;
796 // event->model to be set by caller
797 event->capture_available = (bool)halEvent->captureAvailable;
798 event->capture_session = halEvent->captureSession;
799 event->capture_delay_ms = halEvent->captureDelayMs;
800 event->capture_preamble_ms = halEvent->capturePreambleMs;
801 event->trigger_in_data = (bool)halEvent->triggerInData;
802 event->audio_config.sample_rate = halEvent->audioConfig.sampleRateHz;
803 event->audio_config.channel_mask = (audio_channel_mask_t)halEvent->audioConfig.channelMask;
804 event->audio_config.format = (audio_format_t)halEvent->audioConfig.format;
805
806 event->data_size = halEvent->data.size();
807 uint8_t *dst = (uint8_t *)event + event->data_offset;
808 uint8_t *src = (uint8_t *)&halEvent->data[0];
809 memcpy(dst, src, halEvent->data.size());
Eric Laurent7a544b42016-08-05 19:01:13 -0700810}
811
812} // namespace android