Vibrator.h 12 KB

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