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/rf-bottom-therm/temp", &mPATemp);
72     }
73 
74     template <typename T>
openFull(const std::string & name,T * stream)75     void openFull(const std::string &name, T *stream) {
76         saveName(name, stream);
77         utils::openNoCreate(name, stream);
78     }
79 
80   private:
81     std::ofstream mAutocal;
82     std::ofstream mOlLraPeriod;
83     std::ofstream mActivate;
84     std::ofstream mDuration;
85     std::ofstream mState;
86     std::ofstream mRtpInput;
87     std::ofstream mMode;
88     std::ofstream mSequencer;
89     std::ofstream mScale;
90     std::ofstream mCtrlLoop;
91     std::ofstream mLpTrigger;
92     std::ofstream mLraWaveShape;
93     std::ofstream mOdClamp;
94     std::ifstream mPATemp;
95 };
96 
97 class HwCal : public Vibrator::HwCal, private HwCalBase {
98   private:
99     static constexpr char AUTOCAL_CONFIG[] = "autocal";
100     static constexpr char LRA_PERIOD_CONFIG[] = "lra_period";
101     static constexpr char EFFECT_COEFF_CONFIG[] = "haptic_coefficient";
102     static constexpr char STEADY_AMP_MAX_CONFIG[] = "vibration_amp_max";
103     static constexpr char STEADY_COEFF_CONFIG[] = "vibration_coefficient";
104 
105     static constexpr uint32_t WAVEFORM_CLICK_EFFECT_MS = 6;
106     static constexpr uint32_t WAVEFORM_TICK_EFFECT_MS = 2;
107     static constexpr uint32_t WAVEFORM_DOUBLE_CLICK_EFFECT_MS = 180;
108     static constexpr uint32_t WAVEFORM_HEAVY_CLICK_EFFECT_MS = 8;
109 
110     static constexpr uint32_t DEFAULT_LRA_PERIOD = 262;
111     static constexpr uint32_t DEFAULT_FREQUENCY_SHIFT = 10;
112     static constexpr uint32_t DEFAULT_VOLTAGE_MAX = 107;  // 2.15V;
113     static constexpr uint32_t DEFAULT_LP_TRIGGER_SUPPORT = 1;
114 
115   public:
HwCal()116     HwCal() {}
117 
getAutocal(std::string * value)118     bool getAutocal(std::string *value) override { return getPersist(AUTOCAL_CONFIG, value); }
getLraPeriod(uint32_t * value)119     bool getLraPeriod(uint32_t *value) override {
120         if (getPersist(LRA_PERIOD_CONFIG, value)) {
121             return true;
122         }
123         *value = DEFAULT_LRA_PERIOD;
124         return true;
125     }
getEffectCoeffs(std::array<float,4> * value)126     bool getEffectCoeffs(std::array<float, 4> *value) override {
127         if (getPersist(EFFECT_COEFF_CONFIG, value)) {
128             return true;
129         }
130         return false;
131     }
getSteadyAmpMax(float * value)132     bool getSteadyAmpMax(float *value) override {
133         if (getPersist(STEADY_AMP_MAX_CONFIG, value)) {
134             return true;
135         }
136         return false;
137     }
getSteadyCoeffs(std::array<float,4> * value)138     bool getSteadyCoeffs(std::array<float, 4> *value) override {
139         if (getPersist(STEADY_COEFF_CONFIG, value)) {
140             return true;
141         }
142         return false;
143     }
getCloseLoopThreshold(uint32_t * value)144     bool getCloseLoopThreshold(uint32_t *value) override {
145         return getProperty("closeloop.threshold", value, UINT32_MAX);
146         return true;
147     }
getDynamicConfig(bool * value)148     bool getDynamicConfig(bool *value) override {
149         return getProperty("config.dynamic", value, false);
150     }
getLongFrequencyShift(uint32_t * value)151     bool getLongFrequencyShift(uint32_t *value) override {
152         return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
153     }
getShortVoltageMax(uint32_t * value)154     bool getShortVoltageMax(uint32_t *value) override {
155         return getProperty("short.voltage", value, DEFAULT_VOLTAGE_MAX);
156     }
getLongVoltageMax(uint32_t * value)157     bool getLongVoltageMax(uint32_t *value) override {
158         return getProperty("long.voltage", value, DEFAULT_VOLTAGE_MAX);
159     }
getClickDuration(uint32_t * value)160     bool getClickDuration(uint32_t *value) override {
161         return getProperty("click.duration", value, WAVEFORM_CLICK_EFFECT_MS);
162     }
getTickDuration(uint32_t * value)163     bool getTickDuration(uint32_t *value) override {
164         return getProperty("tick.duration", value, WAVEFORM_TICK_EFFECT_MS);
165     }
getDoubleClickDuration(uint32_t * value)166     bool getDoubleClickDuration(uint32_t *value) override {
167         *value = WAVEFORM_DOUBLE_CLICK_EFFECT_MS;
168         return true;
169     }
getHeavyClickDuration(uint32_t * value)170     bool getHeavyClickDuration(uint32_t *value) override {
171         return getProperty("heavyclick.duration", value, WAVEFORM_HEAVY_CLICK_EFFECT_MS);
172     }
getEffectShape(uint32_t * value)173     bool getEffectShape(uint32_t *value) override {
174         return getProperty("effect.shape", value, UINT32_MAX);
175     }
getSteadyShape(uint32_t * value)176     bool getSteadyShape(uint32_t *value) override {
177         return getProperty("steady.shape", value, UINT32_MAX);
178     }
getTriggerEffectSupport(uint32_t * value)179     bool getTriggerEffectSupport(uint32_t *value) override {
180         return getProperty("lptrigger", value, DEFAULT_LP_TRIGGER_SUPPORT);
181     }
debug(int fd)182     void debug(int fd) override { HwCalBase::debug(fd); }
183 };
184 
185 }  // namespace vibrator
186 }  // namespace hardware
187 }  // namespace android
188 }  // namespace aidl
189