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