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 20 #include <fstream> 21 22 namespace aidl { 23 namespace android { 24 namespace hardware { 25 namespace vibrator { 26 27 class Vibrator : public BnVibrator { 28 public: 29 // APIs for interfacing with the kernel driver. 30 class HwApi { 31 public: 32 virtual ~HwApi() = default; 33 // Stores the COMP, BEMF, and GAIN calibration values to use. 34 // <COMP> <BEMF> <GAIN> 35 virtual bool setAutocal(std::string value) = 0; 36 // Stores the open-loop LRA frequency to be used. 37 virtual bool setOlLraPeriod(uint32_t value) = 0; 38 // Activates/deactivates the vibrator for durations specified by 39 // setDuration(). 40 virtual bool setActivate(bool value) = 0; 41 // Specifies the vibration duration in milliseconds. 42 virtual bool setDuration(uint32_t value) = 0; 43 // Specifies the active state of the vibrator 44 // (true = enabled, false = disabled). 45 virtual bool setState(bool value) = 0; 46 // Reports whether setRtpInput() is supported. 47 virtual bool hasRtpInput() = 0; 48 // Specifies the playback amplitude of the haptic waveforms in RTP mode. 49 // Negative numbers indicates braking. 50 virtual bool setRtpInput(int8_t value) = 0; 51 // Specifies the mode of operation. 52 // rtp - RTP Mode 53 // waveform - Waveform Sequencer Mode 54 // diag - Diagnostics Routine 55 // autocal - Automatic Level Calibration Routine 56 virtual bool setMode(std::string value) = 0; 57 // Specifies a waveform sequence in index-count pairs. 58 // <index-1> <count-1> [<index-2> <cound-2> ...] 59 virtual bool setSequencer(std::string value) = 0; 60 // Specifies the scaling of effects in Waveform mode. 61 // 0 - 100% 62 // 1 - 75% 63 // 2 - 50% 64 // 3 - 25% 65 virtual bool setScale(uint8_t value) = 0; 66 // Selects either closed loop or open loop mode. 67 // (true = open, false = closed). 68 virtual bool setCtrlLoop(bool value) = 0; 69 // Specifies waveform index to be played in low-power trigger mode. 70 // 0 - Disabled 71 // 1+ - Waveform Index 72 virtual bool setLpTriggerEffect(uint32_t value) = 0; 73 // Specifies which shape to use for driving the LRA when in open loop 74 // mode. 75 // 0 - Square Wave 76 // 1 - Sine Wave 77 virtual bool setLraWaveShape(uint32_t value) = 0; 78 // Specifies the maximum voltage for automatic overdrive and automatic 79 // braking periods. 80 virtual bool setOdClamp(uint32_t value) = 0; 81 // Get usb temperature sensor value 82 virtual bool getPATemp(int32_t *value) = 0; 83 // Emit diagnostic information to the given file. 84 virtual void debug(int fd) = 0; 85 }; 86 87 // APIs for obtaining calibration/configuration data from persistent memory. 88 class HwCal { 89 public: 90 virtual ~HwCal() = default; 91 // Obtains the COMP, BEMF, and GAIN calibration values to use. 92 virtual bool getAutocal(std::string *value) = 0; 93 // Obtains the open-loop LRA frequency to be used. 94 virtual bool getLraPeriod(uint32_t *value) = 0; 95 // Obtains the effect coeffs to calculate the target voltage 96 virtual bool getEffectCoeffs(std::array<float, 4> *value) = 0; 97 // Obtain the max steady G value 98 virtual bool getSteadyAmpMax(float *value) = 0; 99 // Obtains the steady coeffs to calculate the target voltage 100 virtual bool getSteadyCoeffs(std::array<float, 4> *value) = 0; 101 // Obtains threshold in ms, above which close-loop should be used. 102 virtual bool getCloseLoopThreshold(uint32_t *value) = 0; 103 // Obtains dynamic/static configuration choice. 104 virtual bool getDynamicConfig(bool *value) = 0; 105 // Obtains LRA frequency shift for long (steady) vibrations. 106 virtual bool getLongFrequencyShift(uint32_t *value) = 0; 107 // Obtains maximum voltage for short (effect) vibrations 108 virtual bool getShortVoltageMax(uint32_t *value) = 0; 109 // Obtains maximum voltage for long (steady) vibrations 110 virtual bool getLongVoltageMax(uint32_t *value) = 0; 111 // Obtains the duration for the click effect 112 virtual bool getClickDuration(uint32_t *value) = 0; 113 // Obtains the duration for the tick effect 114 virtual bool getTickDuration(uint32_t *value) = 0; 115 // Obtains the duration for the double-click effect 116 virtual bool getDoubleClickDuration(uint32_t *value) = 0; 117 // Obtains the duration for the heavy-click effect 118 virtual bool getHeavyClickDuration(uint32_t *value) = 0; 119 // Obtains the wave shape for effect haptics 120 virtual bool getEffectShape(uint32_t *value) = 0; 121 // Obtains the wave shape for steady vibration 122 virtual bool getSteadyShape(uint32_t *value) = 0; 123 // Obtains the trigger effect support 124 virtual bool getTriggerEffectSupport(uint32_t *value) = 0; 125 // Emit diagnostic information to the given file. 126 virtual void debug(int fd) = 0; 127 }; 128 129 private: 130 enum class LoopControl : bool { 131 CLOSE = false, 132 OPEN = true, 133 }; 134 135 enum class WaveShape : uint32_t { 136 SQUARE = 0, 137 SINE = 1, 138 }; 139 140 struct VibrationConfig { 141 WaveShape shape; 142 uint32_t *odClamp; 143 uint32_t olLraPeriod; 144 }; 145 146 enum OdClampOffset : uint32_t { 147 TEXTURE_TICK, 148 TICK, 149 CLICK, 150 HEAVY_CLICK, 151 }; 152 153 public: 154 Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal); 155 156 ndk::ScopedAStatus getCapabilities(int32_t *_aidl_return) override; 157 ndk::ScopedAStatus off() override; 158 ndk::ScopedAStatus on(int32_t timeoutMs, 159 const std::shared_ptr<IVibratorCallback> &callback) override; 160 ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, 161 const std::shared_ptr<IVibratorCallback> &callback, 162 int32_t *_aidl_return) override; 163 ndk::ScopedAStatus getSupportedEffects(std::vector<Effect> *_aidl_return) override; 164 ndk::ScopedAStatus setAmplitude(float amplitude) override; 165 ndk::ScopedAStatus setExternalControl(bool enabled) override; 166 ndk::ScopedAStatus getCompositionDelayMax(int32_t *maxDelayMs); 167 ndk::ScopedAStatus getCompositionSizeMax(int32_t *maxSize); 168 ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive> *supported) override; 169 ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, 170 int32_t *durationMs) override; 171 ndk::ScopedAStatus compose(const std::vector<CompositeEffect> &composite, 172 const std::shared_ptr<IVibratorCallback> &callback) override; 173 ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect> *_aidl_return) override; 174 ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override; 175 ndk::ScopedAStatus alwaysOnDisable(int32_t id) override; 176 ndk::ScopedAStatus getResonantFrequency(float *resonantFreqHz) override; 177 ndk::ScopedAStatus getQFactor(float *qFactor) override; 178 ndk::ScopedAStatus getFrequencyResolution(float *freqResolutionHz) override; 179 ndk::ScopedAStatus getFrequencyMinimum(float *freqMinimumHz) override; 180 ndk::ScopedAStatus getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) override; 181 ndk::ScopedAStatus getPwlePrimitiveDurationMax(int32_t *durationMs) override; 182 ndk::ScopedAStatus getPwleCompositionSizeMax(int32_t *maxSize) override; 183 ndk::ScopedAStatus getSupportedBraking(std::vector<Braking> *supported) override; 184 ndk::ScopedAStatus composePwle(const std::vector<PrimitivePwle> &composite, 185 const std::shared_ptr<IVibratorCallback> &callback) override; 186 187 binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; 188 189 private: 190 ndk::ScopedAStatus on(uint32_t timeoutMs, const char mode[], 191 const std::unique_ptr<VibrationConfig> &config, const int8_t volOffset); 192 ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength, int32_t *outTimeMs); 193 194 std::unique_ptr<HwApi> mHwApi; 195 std::unique_ptr<HwCal> mHwCal; 196 uint32_t mCloseLoopThreshold; 197 std::unique_ptr<VibrationConfig> mSteadyConfig; 198 std::unique_ptr<VibrationConfig> mEffectConfig; 199 uint32_t mClickDuration; 200 uint32_t mTickDuration; 201 uint32_t mDoubleClickDuration; 202 uint32_t mHeavyClickDuration; 203 std::array<uint32_t, 5> mEffectTargetOdClamp; 204 std::array<uint32_t, 3> mSteadyTargetOdClamp; 205 uint32_t mSteadyOlLraPeriod; 206 uint32_t mSteadyOlLraPeriodShift; 207 bool mDynamicConfig; 208 }; 209 210 } // namespace vibrator 211 } // namespace hardware 212 } // namespace android 213 } // namespace aidl 214