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 <array>
21 #include <fstream>
22 #include <future>
23 
24 namespace aidl {
25 namespace android {
26 namespace hardware {
27 namespace vibrator {
28 
29 class Vibrator : public BnVibrator {
30   public:
31     // APIs for interfacing with the kernel driver.
32     class HwApi {
33       public:
34         virtual ~HwApi() = default;
35         // Stores the LRA resonant frequency to be used for PWLE playback
36         // and click compensation.
37         virtual bool setF0(uint32_t value) = 0;
38         // Stores the LRA series resistance to be used for click
39         // compensation.
40         virtual bool setRedc(uint32_t value) = 0;
41         // Stores the LRA Q factor to be used for Q-dependent waveform
42         // selection.
43         virtual bool setQ(uint32_t value) = 0;
44         // Activates/deactivates the vibrator for durations specified by
45         // setDuration().
46         virtual bool setActivate(bool value) = 0;
47         // Specifies the vibration duration in milliseconds.
48         virtual bool setDuration(uint32_t value) = 0;
49         // Reports the number of effect waveforms loaded in firmware.
50         virtual bool getEffectCount(uint32_t *value) = 0;
51         // Reports the duration of the waveform selected by
52         // setEffectIndex(), measured in 48-kHz periods.
53         virtual bool getEffectDuration(uint32_t *value) = 0;
54         // Selects the waveform associated with vibration calls from
55         // the Android vibrator HAL.
56         virtual bool setEffectIndex(uint32_t value) = 0;
57         // Specifies an array of waveforms, delays, and repetition markers to
58         // generate complex waveforms.
59         virtual bool setEffectQueue(std::string value) = 0;
60         // Reports whether setEffectScale() is supported.
61         virtual bool hasEffectScale() = 0;
62         // Indicates the number of 0.125-dB steps of attenuation to apply to
63         // waveforms triggered in response to vibration calls from the
64         // Android vibrator HAL.
65         virtual bool setEffectScale(uint32_t value) = 0;
66         // Indicates the number of 0.125-dB steps of attenuation to apply to
67         // any output waveform (additive to all other set*Scale()
68         // controls).
69         virtual bool setGlobalScale(uint32_t value) = 0;
70         // Specifies the active state of the vibrator
71         // (true = enabled, false = disabled).
72         virtual bool setState(bool value) = 0;
73         // Reports whether getAspEnable()/setAspEnable() is supported.
74         virtual bool hasAspEnable() = 0;
75         // Enables/disables ASP playback.
76         virtual bool getAspEnable(bool *value) = 0;
77         // Reports enabled/disabled state of ASP playback.
78         virtual bool setAspEnable(bool value) = 0;
79         // Selects the waveform associated with a GPIO1 falling edge.
80         virtual bool setGpioFallIndex(uint32_t value) = 0;
81         // Indicates the number of 0.125-dB steps of attenuation to apply to
82         // waveforms triggered in response to a GPIO1 falling edge.
83         virtual bool setGpioFallScale(uint32_t value) = 0;
84         // Selects the waveform associated with a GPIO1 rising edge.
85         virtual bool setGpioRiseIndex(uint32_t value) = 0;
86         // Indicates the number of 0.125-dB steps of attenuation to apply to
87         // waveforms triggered in response to a GPIO1 rising edge.
88         virtual bool setGpioRiseScale(uint32_t value) = 0;
89         // Blocks until vibrator reaches desired state
90         // (true = enabled, false = disabled).
91         virtual bool pollVibeState(bool value) = 0;
92         // Emit diagnostic information to the given file.
93         virtual void debug(int fd) = 0;
94     };
95 
96     // APIs for obtaining calibration/configuration data from persistent memory.
97     class HwCal {
98       public:
99         virtual ~HwCal() = default;
100         // Obtains the LRA resonant frequency to be used for PWLE playback
101         // and click compensation.
102         virtual bool getF0(uint32_t *value) = 0;
103         // Obtains the LRA series resistance to be used for click
104         // compensation.
105         virtual bool getRedc(uint32_t *value) = 0;
106         // Obtains the LRA Q factor to be used for Q-dependent waveform
107         // selection.
108         virtual bool getQ(uint32_t *value) = 0;
109         // Obtains the discreet voltage levels to be applied for the various
110         // waveforms, in units of 1%.
111         virtual bool getVolLevels(std::array<uint32_t, 6> *value) = 0;
112         // Emit diagnostic information to the given file.
113         virtual void debug(int fd) = 0;
114     };
115 
116   public:
117     Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal);
118 
119     ndk::ScopedAStatus getCapabilities(int32_t *_aidl_return) override;
120     ndk::ScopedAStatus off() override;
121     ndk::ScopedAStatus on(int32_t timeoutMs,
122                           const std::shared_ptr<IVibratorCallback> &callback) override;
123     ndk::ScopedAStatus perform(Effect effect, EffectStrength strength,
124                                const std::shared_ptr<IVibratorCallback> &callback,
125                                int32_t *_aidl_return) override;
126     ndk::ScopedAStatus getSupportedEffects(std::vector<Effect> *_aidl_return) override;
127     ndk::ScopedAStatus setAmplitude(float amplitude) override;
128     ndk::ScopedAStatus setExternalControl(bool enabled) override;
129     ndk::ScopedAStatus getCompositionDelayMax(int32_t *maxDelayMs);
130     ndk::ScopedAStatus getCompositionSizeMax(int32_t *maxSize);
131     ndk::ScopedAStatus getSupportedPrimitives(std::vector<CompositePrimitive> *supported) override;
132     ndk::ScopedAStatus getPrimitiveDuration(CompositePrimitive primitive,
133                                             int32_t *durationMs) override;
134     ndk::ScopedAStatus compose(const std::vector<CompositeEffect> &composite,
135                                const std::shared_ptr<IVibratorCallback> &callback) override;
136     ndk::ScopedAStatus getSupportedAlwaysOnEffects(std::vector<Effect> *_aidl_return) override;
137     ndk::ScopedAStatus alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) override;
138     ndk::ScopedAStatus alwaysOnDisable(int32_t id) override;
139 
140     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
141 
142   private:
143     ndk::ScopedAStatus on(uint32_t timeoutMs, uint32_t effectIndex,
144                           const std::shared_ptr<IVibratorCallback> &callback);
145     // set 'amplitude' based on an arbitrary scale determined by 'maximum'
146     ndk::ScopedAStatus setEffectAmplitude(float amplitude, float maximum);
147     ndk::ScopedAStatus setGlobalAmplitude(bool set);
148     // 'simple' effects are those precompiled and loaded into the controller
149     ndk::ScopedAStatus getSimpleDetails(Effect effect, EffectStrength strength,
150                                         uint32_t *outEffectIndex, uint32_t *outTimeMs,
151                                         uint32_t *outVolLevel);
152     // 'compound' effects are those composed by stringing multiple 'simple' effects
153     ndk::ScopedAStatus getCompoundDetails(Effect effect, EffectStrength strength,
154                                           uint32_t *outTimeMs, uint32_t *outVolLevel,
155                                           std::string *outEffectQueue);
156     ndk::ScopedAStatus getPrimitiveDetails(CompositePrimitive primitive, uint32_t *outEffectIndex);
157     ndk::ScopedAStatus setEffectQueue(const std::string &effectQueue);
158     ndk::ScopedAStatus performEffect(Effect effect, EffectStrength strength,
159                                      const std::shared_ptr<IVibratorCallback> &callback,
160                                      int32_t *outTimeMs);
161     ndk::ScopedAStatus performEffect(uint32_t effectIndex, uint32_t volLevel,
162                                      const std::string *effectQueue,
163                                      const std::shared_ptr<IVibratorCallback> &callback);
164     bool isUnderExternalControl();
165     void waitForComplete(std::shared_ptr<IVibratorCallback> &&callback);
166     uint32_t intensityToVolLevel(float intensity);
167 
168     std::unique_ptr<HwApi> mHwApi;
169     std::unique_ptr<HwCal> mHwCal;
170     uint32_t mEffectVolMin;
171     uint32_t mEffectVolMax;
172     uint32_t mGlobalVolMax;
173     std::vector<uint32_t> mEffectDurations;
174     std::future<void> mAsyncHandle;
175 };
176 
177 }  // namespace vibrator
178 }  // namespace hardware
179 }  // namespace android
180 }  // namespace aidl
181