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 #ifndef ANDROID_HARDWARE_VIBRATOR_V1_3_VIBRATOR_H
17 #define ANDROID_HARDWARE_VIBRATOR_V1_3_VIBRATOR_H
18 
19 #include <android/hardware/vibrator/1.3/IVibrator.h>
20 #include <hidl/Status.h>
21 
22 #include <fstream>
23 
24 namespace android {
25 namespace hardware {
26 namespace vibrator {
27 namespace V1_3 {
28 namespace implementation {
29 
30 class Vibrator : public IVibrator {
31   public:
32     // APIs for interfacing with the kernel driver.
33     class HwApi {
34       public:
35         virtual ~HwApi() = default;
36         // Stores the COMP, BEMF, and GAIN calibration values to use.
37         //   <COMP> <BEMF> <GAIN>
38         virtual bool setAutocal(std::string value) = 0;
39         // Stores the open-loop LRA frequency to be used.
40         virtual bool setOlLraPeriod(uint32_t value) = 0;
41         // Activates/deactivates the vibrator for durations specified by
42         // setDuration().
43         virtual bool setActivate(bool value) = 0;
44         // Specifies the vibration duration in milliseconds.
45         virtual bool setDuration(uint32_t value) = 0;
46         // Specifies the active state of the vibrator
47         // (true = enabled, false = disabled).
48         virtual bool setState(bool value) = 0;
49         // Reports whether setRtpInput() is supported.
50         virtual bool hasRtpInput() = 0;
51         // Specifies the playback amplitude of the haptic waveforms in RTP mode.
52         // Negative numbers indicates braking.
53         virtual bool setRtpInput(int8_t value) = 0;
54         // Specifies the mode of operation.
55         //   rtp        - RTP Mode
56         //   waveform   - Waveform Sequencer Mode
57         //   diag       - Diagnostics Routine
58         //   autocal    - Automatic Level Calibration Routine
59         virtual bool setMode(std::string value) = 0;
60         // Specifies a waveform sequence in index-count pairs.
61         //   <index-1> <count-1> [<index-2> <cound-2> ...]
62         virtual bool setSequencer(std::string value) = 0;
63         // Specifies the scaling of effects in Waveform mode.
64         //   0 - 100%
65         //   1 - 75%
66         //   2 - 50%
67         //   3 - 25%
68         virtual bool setScale(uint8_t value) = 0;
69         // Selects either closed loop or open loop mode.
70         // (true = open, false = closed).
71         virtual bool setCtrlLoop(bool value) = 0;
72         // Specifies waveform index to be played in low-power trigger mode.
73         //   0  - Disabled
74         //   1+ - Waveform Index
75         virtual bool setLpTriggerEffect(uint32_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         // Get battery temperature sensor value
85         virtual bool getUsbTemp(int32_t *value) = 0;
86         // Emit diagnostic information to the given file.
87         virtual void debug(int fd) = 0;
88     };
89 
90     // APIs for obtaining calibration/configuration data from persistent memory.
91     class HwCal {
92       public:
93         virtual ~HwCal() = default;
94         // Obtains the COMP, BEMF, and GAIN calibration values to use.
95         virtual bool getAutocal(std::string *value) = 0;
96         // Obtains the open-loop LRA frequency to be used.
97         virtual bool getLraPeriod(uint32_t *value) = 0;
98         // Obtains the effect coeffs to calculate the target voltage
99         virtual bool getEffectCoeffs(std::array<float, 4> *value) = 0;
100         // Obtain the max steady G value
101         virtual bool getSteadyAmpMax(float *value) = 0;
102         // Obtains threshold in ms, above which close-loop should be used.
103         virtual bool getCloseLoopThreshold(uint32_t *value) = 0;
104         // Obtains dynamic/static configuration choice.
105         virtual bool getDynamicConfig(bool *value) = 0;
106         // Obtains LRA frequency shift for long (steady) vibrations.
107         virtual bool getLongFrequencyShift(uint32_t *value) = 0;
108         // Obtains maximum voltage for short (effect) vibrations
109         virtual bool getShortVoltageMax(uint32_t *value) = 0;
110         // Obtains maximum voltage for long (steady) vibrations
111         virtual bool getLongVoltageMax(uint32_t *value) = 0;
112         // Obtains the duration for the click effect
113         virtual bool getClickDuration(uint32_t *value) = 0;
114         // Obtains the duration for the tick effect
115         virtual bool getTickDuration(uint32_t *value) = 0;
116         // Obtains the duration for the double-click effect
117         virtual bool getDoubleClickDuration(uint32_t *value) = 0;
118         // Obtains the duration for the heavy-click effect
119         virtual bool getHeavyClickDuration(uint32_t *value) = 0;
120         // Obtains the wave shape for effect haptics
121         virtual bool getEffectShape(uint32_t *value) = 0;
122         // Obtains the wave shape for steady vibration
123         virtual bool getSteadyShape(uint32_t *value) = 0;
124         // Obtains the trigger effect support
125         virtual bool getTriggerEffectSupport(uint32_t *value) = 0;
126         // Emit diagnostic information to the given file.
127         virtual void debug(int fd) = 0;
128     };
129 
130   private:
131     enum class LoopControl : bool {
132         CLOSE = false,
133         OPEN = true,
134     };
135 
136     enum class WaveShape : uint32_t {
137         SQUARE = 0,
138         SINE = 1,
139     };
140 
141     struct VibrationConfig {
142         WaveShape shape;
143         uint32_t *odClamp;
144         uint32_t olLraPeriod;
145     };
146 
147     enum OdClampOffset : uint32_t {
148         TEXTURE_TICK,
149         TICK,
150         CLICK,
151         HEAVY_CLICK,
152     };
153 
154   public:
155     Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal);
156 
157     // Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
158     using Status = ::android::hardware::vibrator::V1_0::Status;
159     Return<Status> on(uint32_t timeoutMs) override;
160     Return<Status> off() override;
161     Return<bool> supportsAmplitudeControl() override;
162     Return<Status> setAmplitude(uint8_t amplitude) override;
163 
164     // Methods from ::android::hardware::vibrator::V1_3::IVibrator follow.
165     Return<bool> supportsExternalControl() override;
166     Return<Status> setExternalControl(bool enabled) override;
167 
168     using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
169     Return<void> perform(V1_0::Effect effect, EffectStrength strength,
170                          perform_cb _hidl_cb) override;
171     Return<void> perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
172                              perform_cb _hidl_cb) override;
173     Return<void> perform_1_2(V1_2::Effect effect, EffectStrength strength,
174                              perform_cb _hidl_cb) override;
175     Return<void> perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) override;
176 
177     // Methods from ::android.hidl.base::V1_0::IBase follow.
178     Return<void> debug(const hidl_handle &handle, const hidl_vec<hidl_string> &options) override;
179 
180   private:
181     Return<Status> on(uint32_t timeoutMs, const char mode[],
182                       const std::unique_ptr<VibrationConfig> &config, const int8_t volOffset);
183     template <typename T>
184     Return<void> performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb);
185     Return<void> performEffect(Effect effect, EffectStrength strength, perform_cb _hidl_cb);
186     std::unique_ptr<HwApi> mHwApi;
187     std::unique_ptr<HwCal> mHwCal;
188     uint32_t mCloseLoopThreshold;
189     std::unique_ptr<VibrationConfig> mSteadyConfig;
190     std::unique_ptr<VibrationConfig> mEffectConfig;
191     uint32_t mClickDuration;
192     uint32_t mTickDuration;
193     uint32_t mDoubleClickDuration;
194     uint32_t mHeavyClickDuration;
195     std::array<uint32_t, 5> mEffectTargetOdClamp;
196     uint32_t mSteadyTargetOdClamp;
197     uint32_t mSteadyOlLraPeriod;
198     uint32_t mSteadyOlLraPeriodShift;
199     bool mDynamicConfig;
200 };
201 
202 }  // namespace implementation
203 }  // namespace V1_3
204 }  // namespace vibrator
205 }  // namespace hardware
206 }  // namespace android
207 
208 #endif  // ANDROID_HARDWARE_VIBRATOR_V1_3_VIBRATOR_H
209