1f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov/*
2f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * Copyright (C) 2016 The Android Open Source Project
3f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov *
4f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * Licensed under the Apache License, Version 2.0 (the "License");
5f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * you may not use this file except in compliance with the License.
6f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * You may obtain a copy of the License at
7f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov *
8f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov *      http://www.apache.org/licenses/LICENSE-2.0
9f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov *
10f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * Unless required by applicable law or agreed to in writing, software
11f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * distributed under the License is distributed on an "AS IS" BASIS,
12f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * See the License for the specific language governing permissions and
14f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov * limitations under the License.
15f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov */
16f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
17f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov#define LOG_TAG "EffectsFactoryHalHidl"
18f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov//#define LOG_NDEBUG 0
19f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
20f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov#include <cutils/native_handle.h>
21f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
229f57e3c9da8923649adff62e63c644537f565e90Mikhail Naganov#include "ConversionHelperHidl.h"
23f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov#include "EffectHalHidl.h"
24f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov#include "EffectsFactoryHalHidl.h"
25f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov#include "HidlUtils.h"
26f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
27f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovusing ::android::hardware::audio::common::V2_0::Uuid;
28f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovusing ::android::hardware::audio::effect::V2_0::IEffect;
29f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovusing ::android::hardware::audio::effect::V2_0::Result;
30f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovusing ::android::hardware::Return;
31f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
32f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovnamespace android {
33f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
34f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov// static
35f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovsp<EffectsFactoryHalInterface> EffectsFactoryHalInterface::create() {
36f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    return new EffectsFactoryHalHidl();
37f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
38f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
39f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov// static
40f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovbool EffectsFactoryHalInterface::isNullUuid(const effect_uuid_t *pEffectUuid) {
41159e03c9c1b6993f7649dcfe468fad2753994f02Mikhail Naganov    return memcmp(pEffectUuid, EFFECT_UUID_NULL, sizeof(effect_uuid_t)) == 0;
42f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
43f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
44dfa54faf298f6b617bf1d7684418fc8a2e6e4858Chris PhoenixEffectsFactoryHalHidl::EffectsFactoryHalHidl() : ConversionHelperHidl("EffectsFactory") {
45dfa54faf298f6b617bf1d7684418fc8a2e6e4858Chris Phoenix    mEffectsFactory = IEffectsFactory::getService();
461ba4041b6248aff42c01d27832995ebdf431bbd1Mikhail Naganov    if (mEffectsFactory == 0) {
471ba4041b6248aff42c01d27832995ebdf431bbd1Mikhail Naganov        ALOGE("Failed to obtain IEffectsFactory service, terminating process.");
481ba4041b6248aff42c01d27832995ebdf431bbd1Mikhail Naganov        exit(1);
491ba4041b6248aff42c01d27832995ebdf431bbd1Mikhail Naganov    }
50f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
51f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
52f558e0218d2677a813c9c600886f673894eec927Mikhail NaganovEffectsFactoryHalHidl::~EffectsFactoryHalHidl() {
53f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
54f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
55f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovstatus_t EffectsFactoryHalHidl::queryAllDescriptors() {
56f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    if (mEffectsFactory == 0) return NO_INIT;
57f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Result retval = Result::NOT_INITIALIZED;
58f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Return<void> ret = mEffectsFactory->getAllDescriptors(
59f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov            [&](Result r, const hidl_vec<EffectDescriptor>& result) {
60f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                retval = r;
61f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                if (retval == Result::OK) {
62f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                    mLastDescriptors = result;
63f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                }
64f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov            });
65e83be8af690ef1ac820a63414d522e77ca9d4db6Steven Moreland    if (ret.isOk()) {
66f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        return retval == Result::OK ? OK : NO_INIT;
67f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    }
68f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    mLastDescriptors.resize(0);
69e83be8af690ef1ac820a63414d522e77ca9d4db6Steven Moreland    return processReturn(__FUNCTION__, ret);
70f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
71f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
72f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovstatus_t EffectsFactoryHalHidl::queryNumberEffects(uint32_t *pNumEffects) {
73f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    status_t queryResult = queryAllDescriptors();
74f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    if (queryResult == OK) {
75f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        *pNumEffects = mLastDescriptors.size();
76f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    }
77f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    return queryResult;
78f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
79f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
80f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovstatus_t EffectsFactoryHalHidl::getDescriptor(
81f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        uint32_t index, effect_descriptor_t *pDescriptor) {
82f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    // TODO: We need somehow to track the changes on the server side
83f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    // or figure out how to convert everybody to query all the descriptors at once.
84f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    // TODO: check for nullptr
85f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    if (mLastDescriptors.size() == 0) {
86f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        status_t queryResult = queryAllDescriptors();
87f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        if (queryResult != OK) return queryResult;
88f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    }
89f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    if (index >= mLastDescriptors.size()) return NAME_NOT_FOUND;
90f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    EffectHalHidl::effectDescriptorToHal(mLastDescriptors[index], pDescriptor);
91f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    return OK;
92f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
93f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
94f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovstatus_t EffectsFactoryHalHidl::getDescriptor(
95f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        const effect_uuid_t *pEffectUuid, effect_descriptor_t *pDescriptor) {
96f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    // TODO: check for nullptr
97f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    if (mEffectsFactory == 0) return NO_INIT;
98f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Uuid hidlUuid;
99f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
100f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Result retval = Result::NOT_INITIALIZED;
101f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Return<void> ret = mEffectsFactory->getDescriptor(hidlUuid,
102f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov            [&](Result r, const EffectDescriptor& result) {
103f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                retval = r;
104f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                if (retval == Result::OK) {
105f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                    EffectHalHidl::effectDescriptorToHal(result, pDescriptor);
106f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                }
107f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov            });
108e83be8af690ef1ac820a63414d522e77ca9d4db6Steven Moreland    if (ret.isOk()) {
109f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        if (retval == Result::OK) return OK;
110f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
111f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        else return NO_INIT;
112f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    }
113e83be8af690ef1ac820a63414d522e77ca9d4db6Steven Moreland    return processReturn(__FUNCTION__, ret);
114f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
115f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
116f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovstatus_t EffectsFactoryHalHidl::createEffect(
117f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t ioId,
118f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        sp<EffectHalInterface> *effect) {
119f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    if (mEffectsFactory == 0) return NO_INIT;
120f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Uuid hidlUuid;
121f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
122f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Result retval = Result::NOT_INITIALIZED;
123f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Return<void> ret = mEffectsFactory->createEffect(
124f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov            hidlUuid, sessionId, ioId,
125f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov            [&](Result r, const sp<IEffect>& result, uint64_t effectId) {
126f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                retval = r;
127f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                if (retval == Result::OK) {
128f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                    *effect = new EffectHalHidl(result, effectId);
129f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov                }
130f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov            });
131e83be8af690ef1ac820a63414d522e77ca9d4db6Steven Moreland    if (ret.isOk()) {
132f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        if (retval == Result::OK) return OK;
133f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
134f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov        else return NO_INIT;
135f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    }
136e83be8af690ef1ac820a63414d522e77ca9d4db6Steven Moreland    return processReturn(__FUNCTION__, ret);
137f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
138f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
139f558e0218d2677a813c9c600886f673894eec927Mikhail Naganovstatus_t EffectsFactoryHalHidl::dumpEffects(int fd) {
140f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    if (mEffectsFactory == 0) return NO_INIT;
141f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    native_handle_t* hidlHandle = native_handle_create(1, 0);
142f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    hidlHandle->data[0] = fd;
143f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    Return<void> ret = mEffectsFactory->debugDump(hidlHandle);
144f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov    native_handle_delete(hidlHandle);
145e83be8af690ef1ac820a63414d522e77ca9d4db6Steven Moreland    return processReturn(__FUNCTION__, ret);
146f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov}
147f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov
148f558e0218d2677a813c9c600886f673894eec927Mikhail Naganov} // namespace android
149