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 scale to be used in low-power trigger mode.
74         // See setScale().
75         virtual bool setLpTriggerScale(uint8_t value) = 0;
76         // Specifies which shape to use for driving the LRA when in open loop
77         // mode.
78         //   0 - Square Wave
79         //   1 - Sine Wave
80         virtual bool setLraWaveShape(uint32_t value) = 0;
81         // Specifies the maximum voltage for automatic overdrive and automatic
82         // braking periods.
83         virtual bool setOdClamp(uint32_t value) = 0;
84         // Emit diagnostic information to the given file.
85         virtual void debug(int fd) = 0;
86     };
87 
88     // APIs for obtaining calibration/configuration data from persistent memory.
89     class HwCal {
90       public:
91         virtual ~HwCal() = default;
92         // Obtains the COMP, BEMF, and GAIN calibration values to use.
93         virtual bool getAutocal(std::string *value) = 0;
94         // Obtains the open-loop LRA frequency to be used.
95         virtual bool getLraPeriod(uint32_t *value) = 0;
96         // Obtains threshold in ms, above which close-loop should be used.
97         virtual bool getCloseLoopThreshold(uint32_t *value) = 0;
98         // Obtains dynamic/static configuration choice.
99         virtual bool getDynamicConfig(bool *value) = 0;
100         // Obtains LRA frequency shift for long (steady) vibrations.
101         virtual bool getLongFrequencyShift(uint32_t *value) = 0;
102         // Obtains maximum voltage for short (effect) vibrations
103         virtual bool getShortVoltageMax(uint32_t *value) = 0;
104         // Obtains maximum voltage for long (steady) vibrations
105         virtual bool getLongVoltageMax(uint32_t *value) = 0;
106         // Obtains the duration for the click effect
107         virtual bool getClickDuration(uint32_t *value) = 0;
108         // Obtains the duration for the tick effect
109         virtual bool getTickDuration(uint32_t *value) = 0;
110         // Obtains the duration for the double-click effect
111         virtual bool getDoubleClickDuration(uint32_t *value) = 0;
112         // Obtains the duration for the heavy-click effect
113         virtual bool getHeavyClickDuration(uint32_t *value) = 0;
114         // Emit diagnostic information to the given file.
115         virtual void debug(int fd) = 0;
116     };
117 
118   private:
119     enum class LoopControl : bool {
120         CLOSE = false,
121         OPEN = true,
122     };
123 
124     enum class WaveShape : uint32_t {
125         SQUARE = 0,
126         SINE = 1,
127     };
128 
129     struct VibrationConfig {
130         WaveShape shape;
131         uint32_t odClamp;
132         uint32_t olLraPeriod;
133     };
134 
135   public:
136     Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal);
137 
138     ndk::ScopedAStatus getCapabilities(int32_t *_aidl_return) override;
139     ndk::ScopedAStatus off() override;
140     ndk::ScopedAStatus on(int32_t timeoutMs,
141                           const std::shared_ptr<IVibratorCallback> &callback) override;
142     ndk::ScopedAStatus perform(Effect effect, EffectStrength strength,
143                                const std::shared_ptr<IVibratorCallback> &callback,
144                                int32_t *_aidl_return) override;
145     ndk::ScopedAStatus getSupportedEffects(std::vector<Effect> *_aidl_return) override;
146     ndk::ScopedAStatus setAmplitude(float amplitude) override;
147     ndk::ScopedAStatus setExternalControl(bool enabled) override;
148     ndk::ScopedAStatus getCompositionDelayMax(int32_t *maxDelayMs);
149     ndk::ScopedAStatus getCompositionSizeMax(int32_t *maxSize);
150     ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive> *supported) override;
151     ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive,
152                                             int32_t *durationMs) override;
153     ndk::ScopedAStatus compose(const std::vector<CompositeEffect> &composite,
154                                const std::shared_ptr<IVibratorCallback> &callback) override;
155     ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect> *_aidl_return) override;
156     ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override;
157     ndk::ScopedAStatus alwaysOnDisable(int32_t id) override;
158     ndk::ScopedAStatus getResonantFrequency(float *resonantFreqHz) override;
159     ndk::ScopedAStatus getQFactor(float *qFactor) override;
160     ndk::ScopedAStatus getFrequencyResolution(float *freqResolutionHz) override;
161     ndk::ScopedAStatus getFrequencyMinimum(float *freqMinimumHz) override;
162     ndk::ScopedAStatus getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) override;
163     ndk::ScopedAStatus getPwlePrimitiveDurationMax(int32_t *durationMs) override;
164     ndk::ScopedAStatus getPwleCompositionSizeMax(int32_t *maxSize) override;
165     ndk::ScopedAStatus getSupportedBraking(std::vector<Braking> *supported) override;
166     ndk::ScopedAStatus composePwle(const std::vector<PrimitivePwle> &composite,
167                                    const std::shared_ptr<IVibratorCallback> &callback) override;
168 
169     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
170 
171   private:
172     ndk::ScopedAStatus on(uint32_t timeoutMs, const char mode[],
173                           const std::unique_ptr<VibrationConfig> &config);
174     ndk::ScopedAStatus getEffectDetails(Effect effect, uint8_t *outIndex, uint32_t *outTimeMs);
175     ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength, int32_t *outTimeMs);
176 
177     std::unique_ptr<HwApi> mHwApi;
178     std::unique_ptr<HwCal> mHwCal;
179     uint32_t mCloseLoopThreshold;
180     std::unique_ptr<VibrationConfig> mSteadyConfig;
181     std::unique_ptr<VibrationConfig> mEffectConfig;
182     uint32_t mClickDuration;
183     uint32_t mTickDuration;
184     uint32_t mDoubleClickDuration;
185     uint32_t mHeavyClickDuration;
186 };
187 
188 }  // namespace vibrator
189 }  // namespace hardware
190 }  // namespace android
191 }  // namespace aidl
192