1 /*
2  * Copyright (C) 2019 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_HARDWARE_H
17 #define ANDROID_HARDWARE_VIBRATOR_HARDWARE_H
18 
19 #include "../common/HardwareBase.h"
20 #include "Vibrator.h"
21 
22 namespace android {
23 namespace hardware {
24 namespace vibrator {
25 namespace V1_3 {
26 namespace implementation {
27 
28 using common::implementation::HwApiBase;
29 using common::implementation::HwCalBase;
30 
31 class HwApi : public Vibrator::HwApi, private HwApiBase {
32   public:
Create()33     static std::unique_ptr<HwApi> Create() {
34         auto hwapi = std::unique_ptr<HwApi>(new HwApi());
35         // the following streams are required
36         if (!hwapi->mActivate.is_open() || !hwapi->mDuration.is_open() ||
37             !hwapi->mState.is_open()) {
38             return nullptr;
39         }
40         return hwapi;
41     }
42 
setAutocal(std::string value)43     bool setAutocal(std::string value) override { return set(value, &mAutocal); }
setOlLraPeriod(uint32_t value)44     bool setOlLraPeriod(uint32_t value) override { return set(value, &mOlLraPeriod); }
setActivate(bool value)45     bool setActivate(bool value) override { return set(value, &mActivate); }
setDuration(uint32_t value)46     bool setDuration(uint32_t value) override { return set(value, &mDuration); }
setState(bool value)47     bool setState(bool value) override { return set(value, &mState); }
hasRtpInput()48     bool hasRtpInput() override { return has(mRtpInput); }
setRtpInput(int8_t value)49     bool setRtpInput(int8_t value) override { return set(value, &mRtpInput); }
setMode(std::string value)50     bool setMode(std::string value) override { return set(value, &mMode); }
setSequencer(std::string value)51     bool setSequencer(std::string value) override { return set(value, &mSequencer); }
setScale(uint8_t value)52     bool setScale(uint8_t value) override { return set(value, &mScale); }
setCtrlLoop(bool value)53     bool setCtrlLoop(bool value) override { return set(value, &mCtrlLoop); }
setLpTriggerEffect(uint32_t value)54     bool setLpTriggerEffect(uint32_t value) override { return set(value, &mLpTrigger); }
setLraWaveShape(uint32_t value)55     bool setLraWaveShape(uint32_t value) override { return set(value, &mLraWaveShape); }
setOdClamp(uint32_t value)56     bool setOdClamp(uint32_t value) override { return set(value, &mOdClamp); }
getUsbTemp(int32_t * value)57     bool getUsbTemp(int32_t *value) override { return get(value, &mUsbTemp); }
debug(int fd)58     void debug(int fd) override { HwApiBase::debug(fd); }
59 
60   private:
HwApi()61     HwApi() {
62         open("device/autocal", &mAutocal);
63         open("device/ol_lra_period", &mOlLraPeriod);
64         open("activate", &mActivate);
65         open("duration", &mDuration);
66         open("state", &mState);
67         open("device/rtp_input", &mRtpInput);
68         open("device/mode", &mMode);
69         open("device/set_sequencer", &mSequencer);
70         open("device/scale", &mScale);
71         open("device/ctrl_loop", &mCtrlLoop);
72         open("device/lp_trigger_effect", &mLpTrigger);
73         open("device/lra_wave_shape", &mLraWaveShape);
74         open("device/od_clamp", &mOdClamp);
75         // TODO: for future new architecture: b/149610125
76         openFull("/sys/devices/virtual/thermal/tz-by-name/usbc-therm-monitor/temp", &mUsbTemp);
77     }
78 
79   private:
80     std::ofstream mAutocal;
81     std::ofstream mOlLraPeriod;
82     std::ofstream mActivate;
83     std::ofstream mDuration;
84     std::ofstream mState;
85     std::ofstream mRtpInput;
86     std::ofstream mMode;
87     std::ofstream mSequencer;
88     std::ofstream mScale;
89     std::ofstream mCtrlLoop;
90     std::ofstream mLpTrigger;
91     std::ofstream mLraWaveShape;
92     std::ofstream mOdClamp;
93     std::ifstream mUsbTemp;
94 };
95 
96 class HwCal : public Vibrator::HwCal, private HwCalBase {
97   private:
98     static constexpr char AUTOCAL_CONFIG[] = "autocal";
99     static constexpr char LRA_PERIOD_CONFIG[] = "lra_period";
100     static constexpr char EFFECT_COEFF_CONFIG[] = "haptic_coefficient";
101     static constexpr char STEADY_AMP_MAX_CONFIG[] = "vibration_amp_max";
102 
103     static constexpr uint32_t WAVEFORM_CLICK_EFFECT_MS = 6;
104     static constexpr uint32_t WAVEFORM_TICK_EFFECT_MS = 2;
105     static constexpr uint32_t WAVEFORM_DOUBLE_CLICK_EFFECT_MS = 144;
106     static constexpr uint32_t WAVEFORM_HEAVY_CLICK_EFFECT_MS = 8;
107 
108     static constexpr uint32_t DEFAULT_LRA_PERIOD = 262;
109     static constexpr uint32_t DEFAULT_FREQUENCY_SHIFT = 10;
110     static constexpr uint32_t DEFAULT_VOLTAGE_MAX = 107;  // 2.15V;
111     static constexpr uint32_t DEFAULT_LP_TRIGGER_SUPPORT = 1;
112 
113   public:
HwCal()114     HwCal() {}
115 
getAutocal(std::string * value)116     bool getAutocal(std::string *value) override { return getPersist(AUTOCAL_CONFIG, value); }
getLraPeriod(uint32_t * value)117     bool getLraPeriod(uint32_t *value) override {
118         if (getPersist(LRA_PERIOD_CONFIG, value)) {
119             return true;
120         }
121         *value = DEFAULT_LRA_PERIOD;
122         return true;
123     }
getEffectCoeffs(std::array<float,4> * value)124     bool getEffectCoeffs(std::array<float, 4> *value) override {
125         if (getPersist(EFFECT_COEFF_CONFIG, value)) {
126             return true;
127         }
128         return false;
129     }
getSteadyAmpMax(float * value)130     bool getSteadyAmpMax(float *value) override {
131         if (getPersist(STEADY_AMP_MAX_CONFIG, value)) {
132             return true;
133         }
134         return false;
135     }
getCloseLoopThreshold(uint32_t * value)136     bool getCloseLoopThreshold(uint32_t *value) override {
137         return getProperty("closeloop.threshold", value, UINT32_MAX);
138         return true;
139     }
getDynamicConfig(bool * value)140     bool getDynamicConfig(bool *value) override {
141         return getProperty("config.dynamic", value, false);
142     }
getLongFrequencyShift(uint32_t * value)143     bool getLongFrequencyShift(uint32_t *value) override {
144         return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
145     }
getShortVoltageMax(uint32_t * value)146     bool getShortVoltageMax(uint32_t *value) override {
147         return getProperty("short.voltage", value, DEFAULT_VOLTAGE_MAX);
148     }
getLongVoltageMax(uint32_t * value)149     bool getLongVoltageMax(uint32_t *value) override {
150         return getProperty("long.voltage", value, DEFAULT_VOLTAGE_MAX);
151     }
getClickDuration(uint32_t * value)152     bool getClickDuration(uint32_t *value) override {
153         return getProperty("click.duration", value, WAVEFORM_CLICK_EFFECT_MS);
154     }
getTickDuration(uint32_t * value)155     bool getTickDuration(uint32_t *value) override {
156         return getProperty("tick.duration", value, WAVEFORM_TICK_EFFECT_MS);
157     }
getDoubleClickDuration(uint32_t * value)158     bool getDoubleClickDuration(uint32_t *value) override {
159         *value = WAVEFORM_DOUBLE_CLICK_EFFECT_MS;
160         return true;
161     }
getHeavyClickDuration(uint32_t * value)162     bool getHeavyClickDuration(uint32_t *value) override {
163         return getProperty("heavyclick.duration", value, WAVEFORM_HEAVY_CLICK_EFFECT_MS);
164     }
getEffectShape(uint32_t * value)165     bool getEffectShape(uint32_t *value) override {
166         return getProperty("effect.shape", value, UINT32_MAX);
167     }
getSteadyShape(uint32_t * value)168     bool getSteadyShape(uint32_t *value) override {
169         return getProperty("steady.shape", value, UINT32_MAX);
170     }
getTriggerEffectSupport(uint32_t * value)171     bool getTriggerEffectSupport(uint32_t *value) override {
172         return getProperty("lptrigger", value, DEFAULT_LP_TRIGGER_SUPPORT);
173     }
debug(int fd)174     void debug(int fd) override { HwCalBase::debug(fd); }
175 };
176 
177 }  // namespace implementation
178 }  // namespace V1_3
179 }  // namespace vibrator
180 }  // namespace hardware
181 }  // namespace android
182 
183 #endif  // ANDROID_HARDWARE_VIBRATOR_HARDWARE_H
184