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 timeout or vibrator reaches desired state 93 // (true = enabled, false = disabled). 94 virtual bool pollVibeState(uint32_t value, int32_t timeoutMs = -1) = 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 // Specifies the coefficient required for a ramp down when a waveform 105 // ends 106 virtual bool setPwleRampDown(uint32_t value) = 0; 107 // Emit diagnostic information to the given file. 108 virtual void debug(int fd) = 0; 109 }; 110 111 // APIs for obtaining calibration/configuration data from persistent memory. 112 class HwCal { 113 public: 114 virtual ~HwCal() = default; 115 // Obtain the calibration version 116 virtual bool getVersion(uint32_t *value) = 0; 117 // Obtains the LRA resonant frequency to be used for PWLE playback 118 // and click compensation. 119 virtual bool getF0(uint32_t *value) = 0; 120 // Obtains the LRA series resistance to be used for click 121 // compensation. 122 virtual bool getRedc(uint32_t *value) = 0; 123 // Obtains the LRA Q factor to be used for Q-dependent waveform 124 // selection. 125 virtual bool getQ(uint32_t *value) = 0; 126 // Obtains frequency shift for long vibrations. 127 virtual bool getLongFrequencyShift(int32_t *value) = 0; 128 // Obtains device mass for calculating the bandwidth amplitude map 129 virtual bool getDeviceMass(float *value) = 0; 130 // Obtains loc coeff for calculating the bandwidth amplitude map 131 virtual bool getLocCoeff(float *value) = 0; 132 // Obtains the discreet voltage levels to be applied for the various 133 // waveforms, in units of 1%. 134 virtual bool getVolLevels(std::array<uint32_t, 6> *value) = 0; 135 // Obtains the v0/v1(min/max) voltage levels to be applied for 136 // tick/click/long in units of 1%. 137 virtual bool getTickVolLevels(std::array<uint32_t, 2> *value) = 0; 138 virtual bool getClickVolLevels(std::array<uint32_t, 2> *value) = 0; 139 virtual bool getLongVolLevels(std::array<uint32_t, 2> *value) = 0; 140 // Checks if the chirp feature is enabled. 141 virtual bool isChirpEnabled() = 0; 142 // Emit diagnostic information to the given file. 143 virtual void debug(int fd) = 0; 144 }; 145 146 // APIs for logging data to statistics backend 147 class StatsApi { 148 public: 149 virtual ~StatsApi() = default; 150 // Increment count for effect 151 virtual bool logPrimitive(uint16_t effectIndex) = 0; 152 // Increment count for long/short waveform and duration bucket 153 virtual bool logWaveform(uint16_t effectIndex, int32_t duration) = 0; 154 // Increment count for error 155 virtual bool logError(uint16_t errorIndex) = 0; 156 // Start new latency measurement 157 virtual bool logLatencyStart(uint16_t latencyIndex) = 0; 158 // Finish latency measurement and update latency statistics with result 159 virtual bool logLatencyEnd() = 0; 160 // Emit diagnostic information to the given file. 161 virtual void debug(int fd) = 0; 162 }; 163 164 public: 165 Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal, 166 std::unique_ptr<StatsApi> statsapi); 167 168 ndk::ScopedAStatus getCapabilities(int32_t *_aidl_return) override; 169 ndk::ScopedAStatus off() override; 170 ndk::ScopedAStatus on(int32_t timeoutMs, 171 const std::shared_ptr<IVibratorCallback> &callback) override; 172 ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, 173 const std::shared_ptr<IVibratorCallback> &callback, 174 int32_t *_aidl_return) override; 175 ndk::ScopedAStatus getSupportedEffects(std::vector<Effect> *_aidl_return) override; 176 ndk::ScopedAStatus setAmplitude(float amplitude) override; 177 ndk::ScopedAStatus setExternalControl(bool enabled) override; 178 ndk::ScopedAStatus getCompositionDelayMax(int32_t *maxDelayMs); 179 ndk::ScopedAStatus getCompositionSizeMax(int32_t *maxSize); 180 ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive> *supported) override; 181 ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, 182 int32_t *durationMs) override; 183 ndk::ScopedAStatus compose(const std::vector<CompositeEffect> &composite, 184 const std::shared_ptr<IVibratorCallback> &callback) override; 185 ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect> *_aidl_return) override; 186 ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override; 187 ndk::ScopedAStatus alwaysOnDisable(int32_t id) override; 188 ndk::ScopedAStatus getResonantFrequency(float *resonantFreqHz) override; 189 ndk::ScopedAStatus getQFactor(float *qFactor) override; 190 ndk::ScopedAStatus getFrequencyResolution(float *freqResolutionHz) override; 191 ndk::ScopedAStatus getFrequencyMinimum(float *freqMinimumHz) override; 192 ndk::ScopedAStatus getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) override; 193 ndk::ScopedAStatus getPwlePrimitiveDurationMax(int32_t *durationMs) override; 194 ndk::ScopedAStatus getPwleCompositionSizeMax(int32_t *maxSize) override; 195 ndk::ScopedAStatus getSupportedBraking(std::vector<Braking> *supported) override; 196 ndk::ScopedAStatus composePwle(const std::vector<PrimitivePwle> &composite, 197 const std::shared_ptr<IVibratorCallback> &callback) override; 198 199 binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; 200 201 private: 202 ndk::ScopedAStatus on(uint32_t timeoutMs, uint32_t effectIndex, 203 const std::shared_ptr<IVibratorCallback> &callback); 204 // set 'amplitude' based on an arbitrary scale determined by 'maximum' 205 ndk::ScopedAStatus setEffectAmplitude(float amplitude, float maximum); 206 ndk::ScopedAStatus setGlobalAmplitude(bool set); 207 // 'simple' effects are those precompiled and loaded into the controller 208 ndk::ScopedAStatus getSimpleDetails(Effect effect, EffectStrength strength, 209 uint32_t *outEffectIndex, uint32_t *outTimeMs, 210 uint32_t *outVolLevel); 211 // 'compound' effects are those composed by stringing multiple 'simple' effects 212 ndk::ScopedAStatus getCompoundDetails(Effect effect, EffectStrength strength, 213 uint32_t *outTimeMs, uint32_t *outVolLevel, 214 std::string *outEffectQueue); 215 ndk::ScopedAStatus getPrimitiveDetails(CompositePrimitive primitive, uint32_t *outEffectIndex); 216 ndk::ScopedAStatus setEffectQueue(const std::string &effectQueue); 217 ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength, 218 const std::shared_ptr<IVibratorCallback> &callback, 219 int32_t *outTimeMs); 220 ndk::ScopedAStatus performEffect(uint32_t effectIndex, uint32_t volLevel, 221 const std::string *effectQueue, 222 const std::shared_ptr<IVibratorCallback> &callback); 223 ndk::ScopedAStatus setPwle(const std::string &pwleQueue); 224 bool isUnderExternalControl(); 225 void waitForComplete(std::shared_ptr<IVibratorCallback> &&callback); 226 uint32_t intensityToVolLevel(float intensity, uint32_t effectIndex); 227 bool findHapticAlsaDevice(int *card, int *device); 228 bool hasHapticAlsaDevice(); 229 bool enableHapticPcmAmp(struct pcm **haptic_pcm, bool enable, int card, int device); 230 void createPwleMaxLevelLimitMap(); 231 void setPwleRampDown(); 232 std::vector<float> generateBandwidthAmplitudeMap(); 233 234 std::unique_ptr<HwApi> mHwApi; 235 std::unique_ptr<HwCal> mHwCal; 236 std::unique_ptr<StatsApi> mStatsApi; 237 uint32_t mF0Offset; 238 std::array<uint32_t, 2> mTickEffectVol; 239 std::array<uint32_t, 2> mClickEffectVol; 240 std::array<uint32_t, 2> mLongEffectVol; 241 std::vector<uint32_t> mEffectDurations; 242 std::future<void> mAsyncHandle; 243 int32_t mCompositionSizeMax; 244 struct pcm *mHapticPcm; 245 int mCard; 246 int mDevice; 247 bool mHasHapticAlsaDevice; 248 bool mIsUnderExternalControl; 249 float mResonantFrequency; 250 uint32_t mRedc{0}; 251 int8_t mActiveId{-1}; 252 bool mIsChirpEnabled; 253 std::vector<float> mBandwidthAmplitudeMap; 254 bool mGenerateBandwidthAmplitudeMapDone; 255 uint32_t mTotalDuration{0}; 256 std::mutex mTotalDurationMutex; 257 }; 258 259 } // namespace vibrator 260 } // namespace hardware 261 } // namespace android 262 } // namespace aidl 263