Vibrator.h 11 KB

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