1 /* 2 * Copyright (C) 2021 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 <android-base/unique_fd.h> 20 #include <tinyalsa/asoundlib.h> 21 22 #include <array> 23 #include <fstream> 24 #include <future> 25 26 namespace aidl { 27 namespace android { 28 namespace hardware { 29 namespace vibrator { 30 31 class Vibrator : public BnVibrator { 32 public: 33 // APIs for interfacing with the kernel driver. 34 class HwApi { 35 public: 36 virtual ~HwApi() = default; 37 // Stores the LRA resonant frequency to be used for PWLE playback 38 // and click compensation. 39 virtual bool setF0(std::string value) = 0; 40 // Stores the frequency offset for long vibrations. 41 virtual bool setF0Offset(uint32_t value) = 0; 42 // Stores the LRA series resistance to be used for click 43 // compensation. 44 virtual bool setRedc(std::string value) = 0; 45 // Stores the LRA Q factor to be used for Q-dependent waveform 46 // selection. 47 virtual bool setQ(std::string value) = 0; 48 // Reports the number of effect waveforms loaded in firmware. 49 virtual bool getEffectCount(uint32_t *value) = 0; 50 // Blocks until vibrator reaches desired state 51 // ("Vibe state: Haptic" means enabled). 52 // ("Vibe state: Stopped" means disabled). 53 virtual bool pollVibeState(std::string value, int32_t timeoutMs = -1) = 0; 54 // Enables/disables closed-loop active braking. 55 virtual bool setClabEnable(bool value) = 0; 56 // Reports the number of available PWLE segments. 57 virtual bool getAvailablePwleSegments(uint32_t *value) = 0; 58 // Specifies piecewise-linear specifications to generate complex 59 // waveforms. 60 virtual bool setPwle(std::string value) = 0; 61 // Reports whether getOwtFreeSpace() is supported. 62 virtual bool hasOwtFreeSpace() = 0; 63 // Reports the available OWT bytes. 64 virtual bool getOwtFreeSpace(uint32_t *value) = 0; 65 // Emit diagnostic information to the given file. 66 virtual void debug(int fd) = 0; 67 }; 68 69 // APIs for obtaining calibration/configuration data from persistent memory. 70 class HwCal { 71 public: 72 virtual ~HwCal() = default; 73 // Obtain the calibration version 74 virtual bool getVersion(uint32_t *value) = 0; 75 // Obtains the LRA resonant frequency to be used for PWLE playback 76 // and click compensation. 77 virtual bool getF0(std::string *value) = 0; 78 // Obtains the LRA series resistance to be used for click 79 // compensation. 80 virtual bool getRedc(std::string *value) = 0; 81 // Obtains the LRA Q factor to be used for Q-dependent waveform 82 // selection. 83 virtual bool getQ(std::string *value) = 0; 84 // Obtains frequency shift for long vibrations. 85 virtual bool getLongFrequencyShift(int32_t *value) = 0; 86 // Obtains the v0/v1(min/max) voltage levels to be applied for 87 // tick/click/long in units of 1%. 88 virtual bool getTickVolLevels(std::array<uint32_t, 2> *value) = 0; 89 virtual bool getClickVolLevels(std::array<uint32_t, 2> *value) = 0; 90 virtual bool getLongVolLevels(std::array<uint32_t, 2> *value) = 0; 91 // Checks if the chirp feature is enabled. 92 virtual bool isChirpEnabled() = 0; 93 // Obtains the supported primitive effects. 94 virtual bool getSupportedPrimitives(uint32_t *value) = 0; 95 // Emit diagnostic information to the given file. 96 virtual void debug(int fd) = 0; 97 }; 98 99 public: 100 Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal); 101 102 ndk::ScopedAStatus getCapabilities(int32_t *_aidl_return) override; 103 ndk::ScopedAStatus off() override; 104 ndk::ScopedAStatus on(int32_t timeoutMs, 105 const std::shared_ptr<IVibratorCallback> &callback) override; 106 ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, 107 const std::shared_ptr<IVibratorCallback> &callback, 108 int32_t *_aidl_return) override; 109 ndk::ScopedAStatus getSupportedEffects(std::vector<Effect> *_aidl_return) override; 110 ndk::ScopedAStatus setAmplitude(float amplitude) override; 111 ndk::ScopedAStatus setExternalControl(bool enabled) override; 112 ndk::ScopedAStatus getCompositionDelayMax(int32_t *maxDelayMs); 113 ndk::ScopedAStatus getCompositionSizeMax(int32_t *maxSize); 114 ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive> *supported) override; 115 ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive, 116 int32_t *durationMs) override; 117 ndk::ScopedAStatus compose(const std::vector<CompositeEffect> &composite, 118 const std::shared_ptr<IVibratorCallback> &callback) override; 119 ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect> *_aidl_return) override; 120 ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override; 121 ndk::ScopedAStatus alwaysOnDisable(int32_t id) override; 122 ndk::ScopedAStatus getResonantFrequency(float *resonantFreqHz) override; 123 ndk::ScopedAStatus getQFactor(float *qFactor) override; 124 ndk::ScopedAStatus getFrequencyResolution(float *freqResolutionHz) override; 125 ndk::ScopedAStatus getFrequencyMinimum(float *freqMinimumHz) override; 126 ndk::ScopedAStatus getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) override; 127 ndk::ScopedAStatus getPwlePrimitiveDurationMax(int32_t *durationMs) override; 128 ndk::ScopedAStatus getPwleCompositionSizeMax(int32_t *maxSize) override; 129 ndk::ScopedAStatus getSupportedBraking(std::vector<Braking> *supported) override; 130 ndk::ScopedAStatus composePwle(const std::vector<PrimitivePwle> &composite, 131 const std::shared_ptr<IVibratorCallback> &callback) override; 132 133 binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; 134 135 private: 136 ndk::ScopedAStatus on(uint32_t timeoutMs, uint32_t effectIndex, 137 const std::shared_ptr<IVibratorCallback> &callback); 138 // set 'amplitude' based on an arbitrary scale determined by 'maximum' 139 ndk::ScopedAStatus setEffectAmplitude(float amplitude, float maximum); 140 ndk::ScopedAStatus setGlobalAmplitude(bool set); 141 // 'simple' effects are those precompiled and loaded into the controller 142 ndk::ScopedAStatus getSimpleDetails(Effect effect, EffectStrength strength, 143 uint32_t *outEffectIndex, uint32_t *outTimeMs, 144 uint32_t *outVolLevel); 145 // 'compound' effects are those composed by stringing multiple 'simple' effects 146 ndk::ScopedAStatus getCompoundDetails(Effect effect, EffectStrength strength, 147 uint32_t *outTimeMs, struct dspmem_chunk *outCh); 148 ndk::ScopedAStatus getPrimitiveDetails(CompositePrimitive primitive, uint32_t *outEffectIndex); 149 ndk::ScopedAStatus uploadOwtEffect(uint8_t *owtData, uint32_t num_bytes, 150 uint32_t *outEffectIndex); 151 ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength, 152 const std::shared_ptr<IVibratorCallback> &callback, 153 int32_t *outTimeMs); 154 ndk::ScopedAStatus performEffect(uint32_t effectIndex, uint32_t volLevel, 155 struct dspmem_chunk *ch, 156 const std::shared_ptr<IVibratorCallback> &callback); 157 ndk::ScopedAStatus setPwle(const std::string &pwleQueue); 158 bool isUnderExternalControl(); 159 void waitForComplete(std::shared_ptr<IVibratorCallback> &&callback); 160 uint32_t intensityToVolLevel(float intensity, uint32_t effectIndex); 161 bool findHapticAlsaDevice(int *card, int *device); 162 bool hasHapticAlsaDevice(); 163 bool enableHapticPcmAmp(struct pcm **haptic_pcm, bool enable, int card, int device); 164 165 std::unique_ptr<HwApi> mHwApi; 166 std::unique_ptr<HwCal> mHwCal; 167 uint32_t mF0Offset; 168 std::array<uint32_t, 2> mTickEffectVol; 169 std::array<uint32_t, 2> mClickEffectVol; 170 std::array<uint32_t, 2> mLongEffectVol; 171 std::vector<uint32_t> mEffectDurations; 172 std::future<void> mAsyncHandle; 173 int32_t compositionSizeMax; 174 ::android::base::unique_fd mInputFd; 175 int8_t mActiveId{-1}; 176 struct pcm *mHapticPcm; 177 int mCard; 178 int mDevice; 179 bool mHasHapticAlsaDevice; 180 bool mIsUnderExternalControl; 181 float mLongEffectScale = 1.0; 182 bool mIsChirpEnabled; 183 uint32_t mSupportedPrimitivesBits = 0x0; 184 std::vector<CompositePrimitive> mSupportedPrimitives; 185 }; 186 187 } // namespace vibrator 188 } // namespace hardware 189 } // namespace android 190 } // namespace aidl 191