1/*
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 "EffectsFactoryHalHidl"
18//#define LOG_NDEBUG 0
19
20#include <cutils/native_handle.h>
21
22#include "ConversionHelperHidl.h"
23#include "EffectBufferHalHidl.h"
24#include "EffectHalHidl.h"
25#include "EffectsFactoryHalHidl.h"
26#include "HidlUtils.h"
27
28using ::android::hardware::audio::common::V2_0::HidlUtils;
29using ::android::hardware::audio::common::V2_0::Uuid;
30using ::android::hardware::audio::effect::V2_0::IEffect;
31using ::android::hardware::audio::effect::V2_0::Result;
32using ::android::hardware::Return;
33
34namespace android {
35
36EffectsFactoryHalHidl::EffectsFactoryHalHidl() : ConversionHelperHidl("EffectsFactory") {
37    mEffectsFactory = IEffectsFactory::getService();
38    if (mEffectsFactory == 0) {
39        ALOGE("Failed to obtain IEffectsFactory service, terminating process.");
40        exit(1);
41    }
42}
43
44EffectsFactoryHalHidl::~EffectsFactoryHalHidl() {
45}
46
47status_t EffectsFactoryHalHidl::queryAllDescriptors() {
48    if (mEffectsFactory == 0) return NO_INIT;
49    Result retval = Result::NOT_INITIALIZED;
50    Return<void> ret = mEffectsFactory->getAllDescriptors(
51            [&](Result r, const hidl_vec<EffectDescriptor>& result) {
52                retval = r;
53                if (retval == Result::OK) {
54                    mLastDescriptors = result;
55                }
56            });
57    if (ret.isOk()) {
58        return retval == Result::OK ? OK : NO_INIT;
59    }
60    mLastDescriptors.resize(0);
61    return processReturn(__FUNCTION__, ret);
62}
63
64status_t EffectsFactoryHalHidl::queryNumberEffects(uint32_t *pNumEffects) {
65    status_t queryResult = queryAllDescriptors();
66    if (queryResult == OK) {
67        *pNumEffects = mLastDescriptors.size();
68    }
69    return queryResult;
70}
71
72status_t EffectsFactoryHalHidl::getDescriptor(
73        uint32_t index, effect_descriptor_t *pDescriptor) {
74    // TODO: We need somehow to track the changes on the server side
75    // or figure out how to convert everybody to query all the descriptors at once.
76    // TODO: check for nullptr
77    if (mLastDescriptors.size() == 0) {
78        status_t queryResult = queryAllDescriptors();
79        if (queryResult != OK) return queryResult;
80    }
81    if (index >= mLastDescriptors.size()) return NAME_NOT_FOUND;
82    EffectHalHidl::effectDescriptorToHal(mLastDescriptors[index], pDescriptor);
83    return OK;
84}
85
86status_t EffectsFactoryHalHidl::getDescriptor(
87        const effect_uuid_t *pEffectUuid, effect_descriptor_t *pDescriptor) {
88    // TODO: check for nullptr
89    if (mEffectsFactory == 0) return NO_INIT;
90    Uuid hidlUuid;
91    HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
92    Result retval = Result::NOT_INITIALIZED;
93    Return<void> ret = mEffectsFactory->getDescriptor(hidlUuid,
94            [&](Result r, const EffectDescriptor& result) {
95                retval = r;
96                if (retval == Result::OK) {
97                    EffectHalHidl::effectDescriptorToHal(result, pDescriptor);
98                }
99            });
100    if (ret.isOk()) {
101        if (retval == Result::OK) return OK;
102        else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
103        else return NO_INIT;
104    }
105    return processReturn(__FUNCTION__, ret);
106}
107
108status_t EffectsFactoryHalHidl::createEffect(
109        const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t ioId,
110        sp<EffectHalInterface> *effect) {
111    if (mEffectsFactory == 0) return NO_INIT;
112    Uuid hidlUuid;
113    HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
114    Result retval = Result::NOT_INITIALIZED;
115    Return<void> ret = mEffectsFactory->createEffect(
116            hidlUuid, sessionId, ioId,
117            [&](Result r, const sp<IEffect>& result, uint64_t effectId) {
118                retval = r;
119                if (retval == Result::OK) {
120                    *effect = new EffectHalHidl(result, effectId);
121                }
122            });
123    if (ret.isOk()) {
124        if (retval == Result::OK) return OK;
125        else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
126        else return NO_INIT;
127    }
128    return processReturn(__FUNCTION__, ret);
129}
130
131status_t EffectsFactoryHalHidl::dumpEffects(int fd) {
132    if (mEffectsFactory == 0) return NO_INIT;
133    native_handle_t* hidlHandle = native_handle_create(1, 0);
134    hidlHandle->data[0] = fd;
135    Return<void> ret = mEffectsFactory->debugDump(hidlHandle);
136    native_handle_delete(hidlHandle);
137    return processReturn(__FUNCTION__, ret);
138}
139
140status_t EffectsFactoryHalHidl::allocateBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) {
141    return EffectBufferHalHidl::allocate(size, buffer);
142}
143
144status_t EffectsFactoryHalHidl::mirrorBuffer(void* external, size_t size,
145                          sp<EffectBufferHalInterface>* buffer) {
146    return EffectBufferHalHidl::mirror(external, size, buffer);
147}
148
149
150} // namespace android
151