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