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 LRA resonant frequency to be used for PWLE playback
37         // and click compensation.
38         virtual bool setF0(uint32_t value) = 0;
39         // Stores the LRA series resistance to be used for click
40         // compensation.
41         virtual bool setRedc(uint32_t value) = 0;
42         // Stores the LRA Q factor to be used for Q-dependent waveform
43         // selection.
44         virtual bool setQ(uint32_t value) = 0;
45         // Activates/deactivates the vibrator for durations specified by
46         // setDuration().
47         virtual bool setActivate(bool value) = 0;
48         // Specifies the vibration duration in milliseconds.
49         virtual bool setDuration(uint32_t value) = 0;
50         // Reports the duration of the waveform selected by
51         // setEffectIndex(), measured in 48-kHz periods.
52         virtual bool getEffectDuration(uint32_t *value) = 0;
53         // Selects the waveform associated with vibration calls from
54         // the Android vibrator HAL.
55         virtual bool setEffectIndex(uint32_t value) = 0;
56         // Specifies an array of waveforms, delays, and repetition markers to
57         // generate complex waveforms.
58         virtual bool setEffectQueue(std::string value) = 0;
59         // Reports whether setEffectScale() is supported.
60         virtual bool hasEffectScale() = 0;
61         // Indicates the number of 0.125-dB steps of attenuation to apply to
62         // waveforms triggered in response to vibration calls from the
63         // Android vibrator HAL.
64         virtual bool setEffectScale(uint32_t value) = 0;
65         // Indicates the number of 0.125-dB steps of attenuation to apply to
66         // any output waveform (additive to all other set*Scale()
67         // controls).
68         virtual bool setGlobalScale(uint32_t value) = 0;
69         // Specifies the active state of the vibrator
70         // (true = enabled, false = disabled).
71         virtual bool setState(bool value) = 0;
72         // Reports whether getAspEnable()/setAspEnable() is supported.
73         virtual bool hasAspEnable() = 0;
74         // Enables/disables ASP playback.
75         virtual bool getAspEnable(bool *value) = 0;
76         // Reports enabled/disabled state of ASP playback.
77         virtual bool setAspEnable(bool value) = 0;
78         // Selects the waveform associated with a GPIO1 falling edge.
79         virtual bool setGpioFallIndex(uint32_t value) = 0;
80         // Indicates the number of 0.125-dB steps of attenuation to apply to
81         // waveforms triggered in response to a GPIO1 falling edge.
82         virtual bool setGpioFallScale(uint32_t value) = 0;
83         // Selects the waveform associated with a GPIO1 rising edge.
84         virtual bool setGpioRiseIndex(uint32_t value) = 0;
85         // Indicates the number of 0.125-dB steps of attenuation to apply to
86         // waveforms triggered in response to a GPIO1 rising edge.
87         virtual bool setGpioRiseScale(uint32_t value) = 0;
88         // Emit diagnostic information to the given file.
89         virtual void debug(int fd) = 0;
90     };
91 
92     // APIs for obtaining calibration/configuration data from persistent memory.
93     class HwCal {
94       public:
95         virtual ~HwCal() = default;
96         // Obtains the LRA resonant frequency to be used for PWLE playback
97         // and click compensation.
98         virtual bool getF0(uint32_t *value) = 0;
99         // Obtains the LRA series resistance to be used for click
100         // compensation.
101         virtual bool getRedc(uint32_t *value) = 0;
102         // Obtains the LRA Q factor to be used for Q-dependent waveform
103         // selection.
104         virtual bool getQ(uint32_t *value) = 0;
105         // Obtains the discreet voltage levels to be applied for the various
106         // waveforms, in units of 1%.
107         virtual bool getVolLevels(std::array<uint32_t, 6> *value) = 0;
108         // Emit diagnostic information to the given file.
109         virtual void debug(int fd) = 0;
110     };
111 
112   public:
113     Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal);
114 
115     // Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
116     using Status = ::android::hardware::vibrator::V1_0::Status;
117     Return<Status> on(uint32_t timeoutMs) override;
118     Return<Status> off() override;
119     Return<bool> supportsAmplitudeControl() override;
120     Return<Status> setAmplitude(uint8_t amplitude) override;
121 
122     // Methods from ::android::hardware::vibrator::V1_3::IVibrator follow.
123     Return<bool> supportsExternalControl() override;
124     Return<Status> setExternalControl(bool enabled) override;
125 
126     using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
127     Return<void> perform(V1_0::Effect effect, EffectStrength strength,
128                          perform_cb _hidl_cb) override;
129     Return<void> perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
130                              perform_cb _hidl_cb) override;
131     Return<void> perform_1_2(V1_2::Effect effect, EffectStrength strength,
132                              perform_cb _hidl_cb) override;
133     Return<void> perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) override;
134 
135     // Methods from ::android.hidl.base::V1_0::IBase follow.
136     Return<void> debug(const hidl_handle &handle, const hidl_vec<hidl_string> &options) override;
137 
138   private:
139     Return<Status> on(uint32_t timeoutMs, uint32_t effectIndex);
140     template <typename T>
141     Return<void> performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb);
142     // set 'amplitude' based on an arbitrary scale determined by 'maximum'
143     Return<Status> setEffectAmplitude(uint8_t amplitude, uint8_t maximum);
144     Return<Status> setGlobalAmplitude(bool set);
145     // 'simple' effects are those precompiled and loaded into the controller
146     Return<Status> getSimpleDetails(Effect effect, EffectStrength strength, uint32_t *outTimeMs,
147                                     uint32_t *outVolLevel);
148     // 'compound' effects are those composed by stringing multiple 'simple' effects
149     Return<Status> getCompoundDetails(Effect effect, EffectStrength strength, uint32_t *outTimeMs,
150                                       uint32_t *outVolLevel, std::string *outEffectQueue);
151     Return<Status> setEffectQueue(const std::string &effectQueue);
152     Return<void> performEffect(Effect effect, EffectStrength strength, perform_cb _hidl_cb);
153     bool isUnderExternalControl();
154     std::unique_ptr<HwApi> mHwApi;
155     std::unique_ptr<HwCal> mHwCal;
156     std::array<uint32_t, 6> mVolLevels;
157     uint32_t mSimpleEffectDuration;
158 };
159 
160 }  // namespace implementation
161 }  // namespace V1_3
162 }  // namespace vibrator
163 }  // namespace hardware
164 }  // namespace android
165 
166 #endif  // ANDROID_HARDWARE_VIBRATOR_V1_3_VIBRATOR_H
167