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 // Obtains the external effect target G 98 virtual bool getEffectTargetG(std::array<float, 5> *value) = 0; 99 // Obtain the max steady G value 100 virtual bool getSteadyAmpMax(float *value) = 0; 101 // Obtains the steady coeffs to calculate the target voltage 102 virtual bool getSteadyCoeffs(std::array<float, 4> *value) = 0; 103 // Obtains the external steady target G 104 virtual bool getSteadyTargetG(std::array<float, 3> *value) = 0; 105 // Obtains threshold in ms, above which close-loop should be used. 106 virtual bool getCloseLoopThreshold(uint32_t *value) = 0; 107 // Obtains dynamic/static configuration choice. 108 virtual bool getDynamicConfig(bool *value) = 0; 109 // Obtains LRA frequency shift for long (steady) vibrations. 110 virtual bool getLongFrequencyShift(uint32_t *value) = 0; 111 // Obtains maximum voltage for short (effect) vibrations 112 virtual bool getShortVoltageMax(uint32_t *value) = 0; 113 // Obtains maximum voltage for long (steady) vibrations 114 virtual bool getLongVoltageMax(uint32_t *value) = 0; 115 // Obtains the duration for the click effect 116 virtual bool getClickDuration(uint32_t *value) = 0; 117 // Obtains the duration for the tick effect 118 virtual bool getTickDuration(uint32_t *value) = 0; 119 // Obtains the duration for the double-click effect 120 virtual bool getDoubleClickDuration(uint32_t *value) = 0; 121 // Obtains the duration for the heavy-click effect 122 virtual bool getHeavyClickDuration(uint32_t *value) = 0; 123 // Obtains the wave shape for effect haptics 124 virtual bool getEffectShape(uint32_t *value) = 0; 125 // Obtains the wave shape for steady vibration 126 virtual bool getSteadyShape(uint32_t *value) = 0; 127 // Obtains the trigger effect support 128 virtual bool getTriggerEffectSupport(uint32_t *value) = 0; 129 // Obtains device hardware version 130 virtual bool getDevHwVer(std::string *value) = 0; 131 // Emit diagnostic information to the given file. 132 virtual void debug(int fd) = 0; 133 }; 134 135 private: 136 enum class LoopControl : bool { 137 CLOSE = false, 138 OPEN = true, 139 }; 140 141 enum class WaveShape : uint32_t { 142 SQUARE = 0, 143 SINE = 1, 144 }; 145 146 struct VibrationConfig { 147 WaveShape shape; 148 uint32_t *odClamp; 149 uint32_t olLraPeriod; 150 }; 151 152 enum OdClampOffset : uint32_t { 153 TEXTURE_TICK, 154 TICK, 155 CLICK, 156 HEAVY_CLICK, 157 }; 158 159 public: 160 Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal); 161 162 ndk::ScopedAStatus getCapabilities(int32_t *_aidl_return) override; 163 ndk::ScopedAStatus off() override; 164 ndk::ScopedAStatus on(int32_t timeoutMs, 165 const std::shared_ptr<IVibratorCallback> &callback) override; 166 ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, 167 const std::shared_ptr<IVibratorCallback> &callback, 168 int32_t *_aidl_return) override; 169 ndk::ScopedAStatus getSupportedEffects(std::vector<Effect> *_aidl_return) override; 170 ndk::ScopedAStatus setAmplitude(float amplitude) override; 171 ndk::ScopedAStatus setExternalControl(bool enabled) override; 172 ndk::ScopedAStatus getCompositionDelayMax(int32_t *maxDelayMs); 173 ndk::ScopedAStatus getCompositionSizeMax(int32_t *maxSize); 174 ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive> *supported) override; 175 ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, 176 int32_t *durationMs) override; 177 ndk::ScopedAStatus compose(const std::vector<CompositeEffect> &composite, 178 const std::shared_ptr<IVibratorCallback> &callback) override; 179 ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect> *_aidl_return) override; 180 ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override; 181 ndk::ScopedAStatus alwaysOnDisable(int32_t id) override; 182 183 binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; 184 185 private: 186 ndk::ScopedAStatus on(uint32_t timeoutMs, const char mode[], 187 const std::unique_ptr<VibrationConfig> &config, const int8_t volOffset); 188 ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength, int32_t *outTimeMs); 189 190 std::unique_ptr<HwApi> mHwApi; 191 std::unique_ptr<HwCal> mHwCal; 192 uint32_t mCloseLoopThreshold; 193 std::unique_ptr<VibrationConfig> mSteadyConfig; 194 std::unique_ptr<VibrationConfig> mEffectConfig; 195 uint32_t mClickDuration; 196 uint32_t mTickDuration; 197 uint32_t mDoubleClickDuration; 198 uint32_t mHeavyClickDuration; 199 std::array<uint32_t, 5> mEffectTargetOdClamp; 200 std::array<uint32_t, 3> mSteadyTargetOdClamp; 201 uint32_t mSteadyOlLraPeriod; 202 uint32_t mSteadyOlLraPeriodShift; 203 bool mDynamicConfig; 204 }; 205 206 } // namespace vibrator 207 } // namespace hardware 208 } // namespace android 209 } // namespace aidl 210