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