1 /* 2 * Copyright (C) 2017 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 #pragma once 17 18 #include <aidl/android/hardware/vibrator/BnVibrator.h> 19 #include <tinyalsa/asoundlib.h> 20 21 #include <array> 22 #include <fstream> 23 #include <future> 24 25 namespace aidl { 26 namespace android { 27 namespace hardware { 28 namespace vibrator { 29 30 class Vibrator : public BnVibrator { 31 public: 32 // APIs for interfacing with the kernel driver. 33 class HwApi { 34 public: 35 virtual ~HwApi() = default; 36 // Stores the LRA resonant frequency to be used for PWLE playback 37 // and click compensation. 38 virtual bool setF0(uint32_t value) = 0; 39 // Stores the frequency offset for long vibrations. 40 virtual bool setF0Offset(uint32_t value) = 0; 41 // Stores the LRA series resistance to be used for click 42 // compensation. 43 virtual bool setRedc(uint32_t value) = 0; 44 // Stores the LRA Q factor to be used for Q-dependent waveform 45 // selection. 46 virtual bool setQ(uint32_t value) = 0; 47 // Activates/deactivates the vibrator for durations specified by 48 // setDuration(). 49 virtual bool setActivate(bool value) = 0; 50 // Specifies the vibration duration in milliseconds. 51 virtual bool setDuration(uint32_t value) = 0; 52 // Reports the number of effect waveforms loaded in firmware. 53 virtual bool getEffectCount(uint32_t *value) = 0; 54 // Reports the duration of the waveform selected by 55 // setEffectIndex(), measured in 48-kHz periods. 56 virtual bool getEffectDuration(uint32_t *value) = 0; 57 // Selects the waveform associated with vibration calls from 58 // the Android vibrator HAL. 59 virtual bool setEffectIndex(uint32_t value) = 0; 60 // Specifies an array of waveforms, delays, and repetition markers to 61 // generate complex waveforms. 62 virtual bool setEffectQueue(std::string value) = 0; 63 // Reports whether setEffectScale() is supported. 64 virtual bool hasEffectScale() = 0; 65 // Indicates the number of 0.125-dB steps of attenuation to apply to 66 // waveforms triggered in response to vibration calls from the 67 // Android vibrator HAL. 68 virtual bool setEffectScale(uint32_t value) = 0; 69 // Indicates the number of 0.125-dB steps of attenuation to apply to 70 // any output waveform (additive to all other set*Scale() 71 // controls). 72 virtual bool setGlobalScale(uint32_t value) = 0; 73 // Specifies the active state of the vibrator 74 // (true = enabled, false = disabled). 75 virtual bool setState(bool value) = 0; 76 // Reports whether getAspEnable()/setAspEnable() is supported. 77 virtual bool hasAspEnable() = 0; 78 // Enables/disables ASP playback. 79 virtual bool getAspEnable(bool *value) = 0; 80 // Reports enabled/disabled state of ASP playback. 81 virtual bool setAspEnable(bool value) = 0; 82 // Selects the waveform associated with a GPIO1 falling edge. 83 virtual bool setGpioFallIndex(uint32_t value) = 0; 84 // Indicates the number of 0.125-dB steps of attenuation to apply to 85 // waveforms triggered in response to a GPIO1 falling edge. 86 virtual bool setGpioFallScale(uint32_t value) = 0; 87 // Selects the waveform associated with a GPIO1 rising edge. 88 virtual bool setGpioRiseIndex(uint32_t value) = 0; 89 // Indicates the number of 0.125-dB steps of attenuation to apply to 90 // waveforms triggered in response to a GPIO1 rising edge. 91 virtual bool setGpioRiseScale(uint32_t value) = 0; 92 // Blocks until vibrator reaches desired state 93 // (true = enabled, false = disabled). 94 virtual bool pollVibeState(bool value) = 0; 95 // Enables/disables closed-loop active braking. 96 virtual bool setClabEnable(bool value) = 0; 97 // Reports the number of available PWLE segments. 98 virtual bool getAvailablePwleSegments(uint32_t *value) = 0; 99 // Reports whether piecewise-linear envelope for waveforms is supported. 100 virtual bool hasPwle() = 0; 101 // Specifies piecewise-linear specifications to generate complex 102 // waveforms. 103 virtual bool setPwle(std::string value) = 0; 104 // Emit diagnostic information to the given file. 105 virtual void debug(int fd) = 0; 106 }; 107 108 // APIs for obtaining calibration/configuration data from persistent memory. 109 class HwCal { 110 public: 111 virtual ~HwCal() = default; 112 // Obtain the calibration version 113 virtual bool getVersion(uint32_t *value) = 0; 114 // Obtains the LRA resonant frequency to be used for PWLE playback 115 // and click compensation. 116 virtual bool getF0(uint32_t *value) = 0; 117 // Obtains the LRA series resistance to be used for click 118 // compensation. 119 virtual bool getRedc(uint32_t *value) = 0; 120 // Obtains the LRA Q factor to be used for Q-dependent waveform 121 // selection. 122 virtual bool getQ(uint32_t *value) = 0; 123 // Obtains frequency shift for long vibrations. 124 virtual bool getLongFrequencyShift(int32_t *value) = 0; 125 // Obtains the discreet voltage levels to be applied for the various 126 // waveforms, in units of 1%. 127 virtual bool getVolLevels(std::array<uint32_t, 6> *value) = 0; 128 // Obtains the v0/v1(min/max) voltage levels to be applied for 129 // tick/click/long in units of 1%. 130 virtual bool getTickVolLevels(std::array<uint32_t, 2> *value) = 0; 131 virtual bool getClickVolLevels(std::array<uint32_t, 2> *value) = 0; 132 virtual bool getLongVolLevels(std::array<uint32_t, 2> *value) = 0; 133 // Emit diagnostic information to the given file. 134 virtual void debug(int fd) = 0; 135 }; 136 137 public: 138 Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal); 139 140 ndk::ScopedAStatus getCapabilities(int32_t *_aidl_return) override; 141 ndk::ScopedAStatus off() override; 142 ndk::ScopedAStatus on(int32_t timeoutMs, 143 const std::shared_ptr<IVibratorCallback> &callback) override; 144 ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, 145 const std::shared_ptr<IVibratorCallback> &callback, 146 int32_t *_aidl_return) override; 147 ndk::ScopedAStatus getSupportedEffects(std::vector<Effect> *_aidl_return) override; 148 ndk::ScopedAStatus setAmplitude(float amplitude) override; 149 ndk::ScopedAStatus setExternalControl(bool enabled) override; 150 ndk::ScopedAStatus getCompositionDelayMax(int32_t *maxDelayMs); 151 ndk::ScopedAStatus getCompositionSizeMax(int32_t *maxSize); 152 ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive> *supported) override; 153 ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, 154 int32_t *durationMs) override; 155 ndk::ScopedAStatus compose(const std::vector<CompositeEffect> &composite, 156 const std::shared_ptr<IVibratorCallback> &callback) override; 157 ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect> *_aidl_return) override; 158 ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override; 159 ndk::ScopedAStatus alwaysOnDisable(int32_t id) override; 160 ndk::ScopedAStatus getResonantFrequency(float *resonantFreqHz) override; 161 ndk::ScopedAStatus getQFactor(float *qFactor) override; 162 ndk::ScopedAStatus getFrequencyResolution(float *freqResolutionHz) override; 163 ndk::ScopedAStatus getFrequencyMinimum(float *freqMinimumHz) override; 164 ndk::ScopedAStatus getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) override; 165 ndk::ScopedAStatus getPwlePrimitiveDurationMax(int32_t *durationMs) override; 166 ndk::ScopedAStatus getPwleCompositionSizeMax(int32_t *maxSize) override; 167 ndk::ScopedAStatus getSupportedBraking(std::vector<Braking> *supported) override; 168 ndk::ScopedAStatus composePwle(const std::vector<PrimitivePwle> &composite, 169 const std::shared_ptr<IVibratorCallback> &callback) override; 170 171 binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; 172 173 private: 174 ndk::ScopedAStatus on(uint32_t timeoutMs, uint32_t effectIndex, 175 const std::shared_ptr<IVibratorCallback> &callback); 176 // set 'amplitude' based on an arbitrary scale determined by 'maximum' 177 ndk::ScopedAStatus setEffectAmplitude(float amplitude, float maximum); 178 ndk::ScopedAStatus setGlobalAmplitude(bool set); 179 // 'simple' effects are those precompiled and loaded into the controller 180 ndk::ScopedAStatus getSimpleDetails(Effect effect, EffectStrength strength, 181 uint32_t *outEffectIndex, uint32_t *outTimeMs, 182 uint32_t *outVolLevel); 183 // 'compound' effects are those composed by stringing multiple 'simple' effects 184 ndk::ScopedAStatus getCompoundDetails(Effect effect, EffectStrength strength, 185 uint32_t *outTimeMs, uint32_t *outVolLevel, 186 std::string *outEffectQueue); 187 ndk::ScopedAStatus getPrimitiveDetails(CompositePrimitive primitive, uint32_t *outEffectIndex); 188 ndk::ScopedAStatus setEffectQueue(const std::string &effectQueue); 189 ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength, 190 const std::shared_ptr<IVibratorCallback> &callback, 191 int32_t *outTimeMs); 192 ndk::ScopedAStatus performEffect(uint32_t effectIndex, uint32_t volLevel, 193 const std::string *effectQueue, 194 const std::shared_ptr<IVibratorCallback> &callback); 195 ndk::ScopedAStatus setPwle(const std::string &pwleQueue); 196 bool isUnderExternalControl(); 197 void waitForComplete(std::shared_ptr<IVibratorCallback> &&callback); 198 uint32_t intensityToVolLevel(float intensity, uint32_t effectIndex); 199 bool findHapticAlsaDevice(int *card, int *device); 200 bool hasHapticAlsaDevice(); 201 bool enableHapticPcmAmp(struct pcm **haptic_pcm, bool enable, int card, int device); 202 void createPwleMaxLevelLimitMap(); 203 204 std::unique_ptr<HwApi> mHwApi; 205 std::unique_ptr<HwCal> mHwCal; 206 uint32_t mF0Offset; 207 std::array<uint32_t, 2> mTickEffectVol; 208 std::array<uint32_t, 2> mClickEffectVol; 209 std::array<uint32_t, 2> mLongEffectVol; 210 std::vector<uint32_t> mEffectDurations; 211 std::future<void> mAsyncHandle; 212 int32_t compositionSizeMax; 213 struct pcm *mHapticPcm; 214 int mCard; 215 int mDevice; 216 bool mHasHapticAlsaDevice; 217 bool mIsUnderExternalControl; 218 }; 219 220 } // namespace vibrator 221 } // namespace hardware 222 } // namespace android 223 } // namespace aidl 224