Adding getModelState to soundtrigger with build fix.
original cl was ag/4879522
Bug: 70206501
Test: Built android
Change-Id: I11f164d8ade66cd7e56f1fa4e46dafa1d56db8cd
diff --git a/soundtrigger/2.2/default/Android.mk b/soundtrigger/2.2/default/Android.mk
new file mode 100644
index 0000000..8c0f653
--- /dev/null
+++ b/soundtrigger/2.2/default/Android.mk
@@ -0,0 +1,50 @@
+#
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.soundtrigger@2.2-impl
+LOCAL_VENDOR_MODULE := true
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_SRC_FILES := \
+ SoundTriggerHw.cpp
+
+LOCAL_CFLAGS := -Wall -Werror
+
+LOCAL_SHARED_LIBRARIES := \
+ libhardware \
+ libhidlbase \
+ libhidlmemory \
+ libhidltransport \
+ liblog \
+ libutils \
+ android.hardware.soundtrigger@2.2 \
+ android.hardware.soundtrigger@2.1 \
+ android.hardware.soundtrigger@2.0 \
+ android.hardware.soundtrigger@2.0-core \
+ android.hidl.allocator@1.0 \
+ android.hidl.memory@1.0
+
+LOCAL_C_INCLUDE_DIRS := $(LOCAL_PATH)
+
+ifeq ($(strip $(AUDIOSERVER_MULTILIB)),)
+LOCAL_MULTILIB := 32
+else
+LOCAL_MULTILIB := $(AUDIOSERVER_MULTILIB)
+endif
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/soundtrigger/2.2/default/SoundTriggerHw.cpp b/soundtrigger/2.2/default/SoundTriggerHw.cpp
new file mode 100644
index 0000000..90faf33
--- /dev/null
+++ b/soundtrigger/2.2/default/SoundTriggerHw.cpp
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "SoundTriggerHw"
+
+#include "SoundTriggerHw.h"
+
+#include <android/log.h>
+#include <utility>
+
+namespace android {
+namespace hardware {
+namespace soundtrigger {
+namespace V2_2 {
+namespace implementation {
+
+Return<void> SoundTriggerHw::getModelState_2_2(int32_t modelHandle, getModelState_cb hidl_cb) {
+ sp<SoundModelClient> client;
+ if (mHwDevice == NULL) {
+ hidl_cb(-ENODEV, NULL);
+ return Void();
+ }
+
+ {
+ AutoMutex lock(mLock);
+ client = mClients.valueFor(modelHandle);
+ if (client == 0) {
+ hidl_cb(-ENOSYS, NULL);
+ return Void();
+ }
+ }
+
+ if (mHwDevice->get_model_state == NULL) {
+ ALOGE("Failed to get model state from device, no such method");
+ hidl_cb(-ENODEV, NULL);
+ return Void();
+ }
+
+ // Get the state from the device (as a recognition event)
+ struct sound_trigger_recognition_event* event =
+ mHwDevice->get_model_state(mHwDevice, client->getHalHandle());
+ if (event == NULL) {
+ ALOGE("Failed to get model state from device");
+ hidl_cb(-ENODEV, NULL);
+ return Void();
+ }
+
+ // Allocate shared memory to return to the client
+ sp<IAllocator> alloc = IAllocator::getService("ashmem");
+ if (alloc == 0) {
+ ALOGE("Failed to retrieve ashmem allocator service");
+ free(event);
+ hidl_cb(-ENOMEM, NULL);
+ return Void();
+ }
+ // Note: Only generic recognition events are currently supported
+ int n_bytes = sizeof(struct sound_trigger_generic_recognition_event);
+ bool success = false;
+ const hidl_memory& mem;
+ Return<void> r = ashmem->allocate(n_bytes, [&](bool s, const hidl_memory& m) {
+ success = s;
+ if (success) mem = m;
+ });
+ if (r.isOk() && success) {
+ // Copy the event data to the shared memory
+ sp<IMemory> memory = mapMemory(mem);
+ if (memory != 0) {
+ struct sound_trigger_generic_recognition_event* data =
+ (struct sound_trigger_generic_recognition_event*)memory->getPointer();
+ memory->update();
+ *data = *event;
+ memory->commit();
+
+ // Return the event memory via this callback
+ hidl_cb(0, memory);
+ } else {
+ ALOGE("Failed to map memory for recognition event");
+ hidl_cb(-ENOMEM, NULL);
+ }
+ } else {
+ ALOGE("Failed to allocate %d bytes from ashmem allocator service", n_bytes);
+ hidl_cb(-ENOMEM, NULL);
+ }
+
+ free(event);
+ return Void();
+}
+
+ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* /* name */) {
+ return (new SoundTriggerHw())->getInterface();
+}
+
+} // namespace implementation
+} // namespace V2_2
+} // namespace soundtrigger
+} // namespace hardware
+} // namespace android
diff --git a/soundtrigger/2.2/default/SoundTriggerHw.h b/soundtrigger/2.2/default/SoundTriggerHw.h
new file mode 100644
index 0000000..7db54db
--- /dev/null
+++ b/soundtrigger/2.2/default/SoundTriggerHw.h
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_SOUNDTRIGGERHW_H
+#define ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_SOUNDTRIGGERHW_H
+
+#include <SoundTriggerHalImpl.h>
+#include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h>
+#include <android/hardware/soundtrigger/2.2/ISoundTriggerHw.h>
+#include <hidl/Status.h>
+
+namespace android {
+namespace hardware {
+namespace soundtrigger {
+namespace V2_2 {
+namespace implementation {
+
+using ::android::sp;
+using ::android::hardware::Return;
+
+struct SoundTriggerHw : public V2_1::implementation::SoundTriggerHw {
+ SoundTriggerHw() = default;
+ ISoundTriggerHw* getInterface() { return new TrampolineSoundTriggerHw_2_2(this); }
+
+ protected:
+ virtual ~SoundTriggerHw() = default;
+
+ Return<sp<struct sound_trigger_recognition_event>> getModelState_2_2(
+ V2_0::SoundModelHandle modelHandle modelHandle);
+
+ private:
+ struct TrampolineSoundTriggerHw_2_2 : public ISoundTriggerHw {
+ explicit TrampolineSoundTriggerHw_2_2(sp<SoundTriggerHw> impl) : mImpl(impl) {}
+
+ // Methods from ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw follow.
+ Return<void> getProperties(getProperties_cb _hidl_cb) override {
+ return mImpl->getProperties(_hidl_cb);
+ }
+ Return<void> loadSoundModel(const V2_0::ISoundTriggerHw::SoundModel& soundModel,
+ const sp<V2_0::ISoundTriggerHwCallback>& callback,
+ int32_t cookie, loadSoundModel_cb _hidl_cb) override {
+ return mImpl->loadSoundModel(soundModel, callback, cookie, _hidl_cb);
+ }
+ Return<void> loadPhraseSoundModel(const V2_0::ISoundTriggerHw::PhraseSoundModel& soundModel,
+ const sp<V2_0::ISoundTriggerHwCallback>& callback,
+ int32_t cookie,
+ loadPhraseSoundModel_cb _hidl_cb) override {
+ return mImpl->loadPhraseSoundModel(soundModel, callback, cookie, _hidl_cb);
+ }
+ Return<int32_t> unloadSoundModel(V2_0::SoundModelHandle modelHandle) override {
+ return mImpl->unloadSoundModel(modelHandle);
+ }
+ Return<int32_t> startRecognition(int32_t modelHandle,
+ const V2_0::ISoundTriggerHw::RecognitionConfig& config,
+ const sp<V2_0::ISoundTriggerHwCallback>& /*callback*/,
+ int32_t /*cookie*/) override {
+ return mImpl->startRecognition(modelHandle, config);
+ }
+ Return<int32_t> stopRecognition(V2_0::SoundModelHandle modelHandle) override {
+ return mImpl->stopRecognition(modelHandle);
+ }
+ Return<int32_t> stopAllRecognitions() override { return mImpl->stopAllRecognitions(); }
+
+ // Methods from V2_1::ISoundTriggerHw follow.
+ Return<void> loadSoundModel_2_1(const V2_1::ISoundTriggerHw::SoundModel& soundModel,
+ const sp<V2_1::ISoundTriggerHwCallback>& callback,
+ int32_t cookie, loadSoundModel_2_1_cb _hidl_cb) override {
+ return mImpl->loadSoundModel_2_1(soundModel, callback, cookie, _hidl_cb);
+ }
+ Return<void> loadPhraseSoundModel_2_1(
+ const V2_1::ISoundTriggerHw::PhraseSoundModel& soundModel,
+ const sp<V2_1::ISoundTriggerHwCallback>& callback, int32_t cookie,
+ loadPhraseSoundModel_2_1_cb _hidl_cb) override {
+ return mImpl->loadPhraseSoundModel_2_1(soundModel, callback, cookie, _hidl_cb);
+ }
+ Return<int32_t> startRecognition_2_1(int32_t modelHandle,
+ const V2_1::ISoundTriggerHw::RecognitionConfig& config,
+ const sp<V2_1::ISoundTriggerHwCallback>& /*callback*/,
+ int32_t /*cookie*/) override {
+ return mImpl->startRecognition_2_1(modelHandle, config);
+ }
+
+ // Methods from V2_2::ISoundTriggerHw follow.
+ Return<void> getModelState(int32_t modelHandle, getModelState_cb hidl_cb) override {
+ return mImpl->getModelState_2_2(modelHandle, hidl_cb);
+ }
+
+ private:
+ sp<SoundTriggerHw> mImpl;
+ };
+};
+
+extern "C" ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* name);
+
+} // namespace implementation
+} // namespace V2_2
+} // namespace soundtrigger
+} // namespace hardware
+} // namespace android
+
+#endif // ANDROID_HARDWARE_SOUNDTRIGGER_V2_2_SOUNDTRIGGERHW_H