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 "AidlConversionHapticGenerator"
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/effect_hapticgenerator.h>
27
28 #include <utils/Log.h>
29
30 #include "AidlConversionHapticGenerator.h"
31
32 namespace android {
33 namespace effect {
34
35 using ::aidl::android::getParameterSpecificField;
36 using ::aidl::android::aidl_utils::statusTFromBinderStatus;
37 using ::aidl::android::hardware::audio::effect::HapticGenerator;
38 using ::aidl::android::hardware::audio::effect::Parameter;
39 using ::aidl::android::hardware::audio::effect::VendorExtension;
40 using ::android::status_t;
41 using utils::EffectParamReader;
42 using utils::EffectParamWriter;
43
setParameter(EffectParamReader & param)44 status_t AidlConversionHapticGenerator::setParameter(EffectParamReader& param) {
45 uint32_t type = 0;
46 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint16_t)) ||
47 OK != param.readFromParameter(&type)) {
48 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
49 return BAD_VALUE;
50 }
51 Parameter aidlParam;
52 switch (type) {
53 case HG_PARAM_HAPTIC_INTENSITY: {
54 int32_t id = 0, scale;
55 if (OK != param.readFromValue(&id) || OK != param.readFromValue(&scale)) {
56 ALOGE("%s invalid intensity %s", __func__, param.toString().c_str());
57 return BAD_VALUE;
58 }
59 HapticGenerator::HapticScale hpScale(
60 {.id = id, .scale = (HapticGenerator::VibratorScale)(scale)});
61 aidlParam = MAKE_SPECIFIC_PARAMETER(HapticGenerator, hapticGenerator, hapticScales,
62 {hpScale});
63 break;
64 }
65 case HG_PARAM_VIBRATOR_INFO: {
66 float resonantFrequencyHz, qFactor, maxAmplitude;
67 if (OK != param.readFromValue(&resonantFrequencyHz) ||
68 OK != param.readFromValue(&qFactor) || OK != param.readFromValue(&maxAmplitude)) {
69 ALOGE("%s invalid vibrator info %s", __func__, param.toString().c_str());
70 return BAD_VALUE;
71 }
72 HapticGenerator::VibratorInformation info({.resonantFrequencyHz = resonantFrequencyHz,
73 .qFactor = qFactor,
74 .maxAmplitude = maxAmplitude});
75 aidlParam =
76 MAKE_SPECIFIC_PARAMETER(HapticGenerator, hapticGenerator, vibratorInfo, info);
77 break;
78 }
79 default: {
80 // for vendor extension, copy data area to the DefaultExtension, parameter ignored
81 VendorExtension ext = VALUE_OR_RETURN_STATUS(
82 aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
83 aidlParam = MAKE_SPECIFIC_PARAMETER(HapticGenerator, hapticGenerator, vendor, ext);
84 break;
85 }
86 }
87
88 return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
89 }
90
91 // No parameter to get for HapticGenerator
getParameter(EffectParamWriter & param)92 status_t AidlConversionHapticGenerator::getParameter(EffectParamWriter& param) {
93 VENDOR_EXTENSION_GET_AND_RETURN(HapticGenerator, hapticGenerator, param);
94 }
95
96 } // namespace effect
97 } // namespace android
98