1 /*
2  * Copyright (C) 2023 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 <cstdint>
18 #include <cstring>
19 #include <optional>
20 #define LOG_TAG "AidlConversionBassBoost"
21 //#define LOG_NDEBUG 0
22 
23 #include <error/expected_utils.h>
24 #include <media/AidlConversionNdk.h>
25 #include <media/AidlConversionEffect.h>
26 #include <system/audio_effects/aidl_effects_utils.h>
27 #include <system/audio_effects/effect_bassboost.h>
28 
29 #include <utils/Log.h>
30 
31 #include "AidlConversionBassBoost.h"
32 
33 namespace android {
34 namespace effect {
35 
36 using ::aidl::android::convertIntegral;
37 using ::aidl::android::getParameterSpecificField;
38 using ::aidl::android::aidl_utils::statusTFromBinderStatus;
39 using ::aidl::android::hardware::audio::effect::BassBoost;
40 using ::aidl::android::hardware::audio::effect::Parameter;
41 using ::aidl::android::hardware::audio::effect::Range;
42 using ::aidl::android::hardware::audio::effect::VendorExtension;
43 using ::android::status_t;
44 using utils::EffectParamReader;
45 using utils::EffectParamWriter;
46 
setParameter(EffectParamReader & param)47 status_t AidlConversionBassBoost::setParameter(EffectParamReader& param) {
48     uint32_t type = 0;
49     uint16_t value = 0;
50     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint16_t)) ||
51         OK != param.readFromParameter(&type) || OK != param.readFromValue(&value)) {
52         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
53         return BAD_VALUE;
54     }
55     Parameter aidlParam;
56     switch (type) {
57         case BASSBOOST_PARAM_STRENGTH: {
58             aidlParam = VALUE_OR_RETURN_STATUS(
59                     aidl::android::legacy2aidl_uint16_strengthPm_Parameter_BassBoost(value));
60             break;
61         }
62         case BASSBOOST_PARAM_STRENGTH_SUPPORTED: {
63             ALOGW("%s set BASSBOOST_PARAM_STRENGTH_SUPPORTED not supported", __func__);
64             return BAD_VALUE;
65         }
66         default: {
67             // for vendor extension, copy data area to the DefaultExtension, parameter ignored
68             VendorExtension ext = VALUE_OR_RETURN_STATUS(
69                     aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
70             aidlParam = MAKE_SPECIFIC_PARAMETER(BassBoost, bassBoost, vendor, ext);
71             break;
72         }
73     }
74 
75     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
76 }
77 
getParameter(EffectParamWriter & param)78 status_t AidlConversionBassBoost::getParameter(EffectParamWriter& param) {
79     uint32_t type = 0;
80     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint16_t)) ||
81         OK != param.readFromParameter(&type)) {
82         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
83         param.setStatus(BAD_VALUE);
84         return BAD_VALUE;
85     }
86     Parameter aidlParam;
87     switch (type) {
88         case BASSBOOST_PARAM_STRENGTH: {
89             uint16_t value;
90             Parameter::Id id =
91                     MAKE_SPECIFIC_PARAMETER_ID(BassBoost, bassBoostTag, BassBoost::strengthPm);
92             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
93             value = VALUE_OR_RETURN_STATUS(
94                     aidl::android::aidl2legacy_Parameter_BassBoost_uint16_strengthPm(aidlParam));
95             return param.writeToValue(&value);
96         }
97         case BASSBOOST_PARAM_STRENGTH_SUPPORTED: {
98             // an invalid range indicates not setting support for this parameter
99             uint32_t value =
100                     ::aidl::android::hardware::audio::effect::isRangeValid<Range::Tag::bassBoost>(
101                             BassBoost::strengthPm, mDesc.capability);
102             return param.writeToValue(&value);
103         }
104         default: {
105             VENDOR_EXTENSION_GET_AND_RETURN(BassBoost, bassBoost, param);
106         }
107     }
108 }
109 
110 } // namespace effect
111 } // namespace android
112