1 /*
2  * Copyright (C) 2022 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 #include <algorithm>
18 #include <cstddef>
19 
20 #define LOG_TAG "AHAL_HapticGeneratorSw"
21 #include <android-base/logging.h>
22 #include <fmq/AidlMessageQueue.h>
23 #include <system/audio_effects/effect_uuid.h>
24 
25 #include "HapticGeneratorSw.h"
26 
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::getEffectImplUuidHapticGeneratorSw;
29 using aidl::android::hardware::audio::effect::getEffectTypeUuidHapticGenerator;
30 using aidl::android::hardware::audio::effect::HapticGeneratorSw;
31 using aidl::android::hardware::audio::effect::IEffect;
32 using aidl::android::hardware::audio::effect::State;
33 using aidl::android::media::audio::common::AudioUuid;
34 
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)35 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
36                                            std::shared_ptr<IEffect>* instanceSpp) {
37     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidHapticGeneratorSw()) {
38         LOG(ERROR) << __func__ << "uuid not supported";
39         return EX_ILLEGAL_ARGUMENT;
40     }
41     if (instanceSpp) {
42         *instanceSpp = ndk::SharedRefBase::make<HapticGeneratorSw>();
43         LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
44         return EX_NONE;
45     } else {
46         LOG(ERROR) << __func__ << " invalid input parameter!";
47         return EX_ILLEGAL_ARGUMENT;
48     }
49 }
50 
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)51 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
52     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidHapticGeneratorSw()) {
53         LOG(ERROR) << __func__ << "uuid not supported";
54         return EX_ILLEGAL_ARGUMENT;
55     }
56     *_aidl_return = HapticGeneratorSw::kDescriptor;
57     return EX_NONE;
58 }
59 
60 namespace aidl::android::hardware::audio::effect {
61 
62 const std::string HapticGeneratorSw::kEffectName = "HapticGeneratorSw";
63 /* Effect descriptor */
64 const Descriptor HapticGeneratorSw::kDescriptor = {
65         .common = {.id = {.type = getEffectTypeUuidHapticGenerator(),
66                           .uuid = getEffectImplUuidHapticGeneratorSw(),
67                           .proxy = std::nullopt},
68                    .flags = {.type = Flags::Type::INSERT,
69                              .insert = Flags::Insert::FIRST,
70                              .volume = Flags::Volume::CTRL},
71                    .name = HapticGeneratorSw::kEffectName,
72                    .implementor = "The Android Open Source Project"}};
73 
getDescriptor(Descriptor * _aidl_return)74 ndk::ScopedAStatus HapticGeneratorSw::getDescriptor(Descriptor* _aidl_return) {
75     LOG(DEBUG) << __func__ << kDescriptor.toString();
76     *_aidl_return = kDescriptor;
77     return ndk::ScopedAStatus::ok();
78 }
79 
setParameterSpecific(const Parameter::Specific & specific)80 ndk::ScopedAStatus HapticGeneratorSw::setParameterSpecific(const Parameter::Specific& specific) {
81     RETURN_IF(Parameter::Specific::hapticGenerator != specific.getTag(), EX_ILLEGAL_ARGUMENT,
82               "EffectNotSupported");
83     RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
84 
85     auto& hgParam = specific.get<Parameter::Specific::hapticGenerator>();
86     auto tag = hgParam.getTag();
87 
88     switch (tag) {
89         case HapticGenerator::hapticScales: {
90             RETURN_IF(mContext->setHgHapticScales(hgParam.get<HapticGenerator::hapticScales>()) !=
91                               RetCode::SUCCESS,
92                       EX_ILLEGAL_ARGUMENT, "HapticScaleNotSupported");
93             return ndk::ScopedAStatus::ok();
94         }
95         case HapticGenerator::vibratorInfo: {
96             RETURN_IF(mContext->setHgVibratorInformation(
97                               hgParam.get<HapticGenerator::vibratorInfo>()) != RetCode::SUCCESS,
98                       EX_ILLEGAL_ARGUMENT, "VibratorInfoNotSupported");
99             return ndk::ScopedAStatus::ok();
100         }
101         default: {
102             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
103             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
104                     EX_ILLEGAL_ARGUMENT, "HapticGeneratorTagNotSupported");
105         }
106     }
107 }
108 
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)109 ndk::ScopedAStatus HapticGeneratorSw::getParameterSpecific(const Parameter::Id& id,
110                                                            Parameter::Specific* specific) {
111     auto tag = id.getTag();
112     RETURN_IF(Parameter::Id::hapticGeneratorTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
113     auto hgId = id.get<Parameter::Id::hapticGeneratorTag>();
114     auto hgIdTag = hgId.getTag();
115     switch (hgIdTag) {
116         case HapticGenerator::Id::commonTag:
117             return getParameterHapticGenerator(hgId.get<HapticGenerator::Id::commonTag>(),
118                                                specific);
119         default:
120             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
121             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
122                     EX_ILLEGAL_ARGUMENT, "HapticGeneratorTagNotSupported");
123     }
124 }
125 
getParameterHapticGenerator(const HapticGenerator::Tag & tag,Parameter::Specific * specific)126 ndk::ScopedAStatus HapticGeneratorSw::getParameterHapticGenerator(const HapticGenerator::Tag& tag,
127                                                                   Parameter::Specific* specific) {
128     RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
129 
130     HapticGenerator hgParam;
131     switch (tag) {
132         case HapticGenerator::hapticScales: {
133             hgParam.set<HapticGenerator::hapticScales>(mContext->getHgHapticScales());
134             break;
135         }
136         case HapticGenerator::vibratorInfo: {
137             hgParam.set<HapticGenerator::vibratorInfo>(mContext->getHgVibratorInformation());
138             break;
139         }
140         default: {
141             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
142             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
143                     EX_ILLEGAL_ARGUMENT, "HapticGeneratorTagNotSupported");
144         }
145     }
146 
147     specific->set<Parameter::Specific::hapticGenerator>(hgParam);
148     return ndk::ScopedAStatus::ok();
149 }
150 
createContext(const Parameter::Common & common)151 std::shared_ptr<EffectContext> HapticGeneratorSw::createContext(const Parameter::Common& common) {
152     if (mContext) {
153         LOG(DEBUG) << __func__ << " context already exist";
154     } else {
155         mContext = std::make_shared<HapticGeneratorSwContext>(1 /* statusFmqDepth */, common);
156     }
157 
158     return mContext;
159 }
160 
releaseContext()161 RetCode HapticGeneratorSw::releaseContext() {
162     if (mContext) {
163         mContext.reset();
164     }
165     return RetCode::SUCCESS;
166 }
167 
168 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)169 IEffect::Status HapticGeneratorSw::effectProcessImpl(float* in, float* out, int samples) {
170     // TODO: get data buffer and process.
171     LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
172     for (int i = 0; i < samples; i++) {
173         *out++ = *in++;
174     }
175     return {STATUS_OK, samples, samples};
176 }
177 
setHgHapticScales(const std::vector<HapticGenerator::HapticScale> & hapticScales)178 RetCode HapticGeneratorSwContext::setHgHapticScales(
179         const std::vector<HapticGenerator::HapticScale>& hapticScales) {
180     // Assume any audio track ID is valid
181     for (auto& it : hapticScales) {
182         mHapticScales[it.id] = it;
183     }
184     return RetCode::SUCCESS;
185 }
186 
getHgHapticScales() const187 std::vector<HapticGenerator::HapticScale> HapticGeneratorSwContext::getHgHapticScales() const {
188     std::vector<HapticGenerator::HapticScale> result;
189     std::transform(mHapticScales.begin(), mHapticScales.end(), std::back_inserter(result),
190                    [](auto& scaleIt) { return scaleIt.second; });
191     return result;
192 }
193 
194 }  // namespace aidl::android::hardware::audio::effect
195