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 "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, &mLpTriggerEffect); }
setLpTriggerScale(uint8_t value)50     bool setLpTriggerScale(uint8_t value) override { return set(value, &mLpTriggerScale); }
setLraWaveShape(uint32_t value)51     bool setLraWaveShape(uint32_t value) override { return set(value, &mLraWaveShape); }
setOdClamp(uint32_t value)52     bool setOdClamp(uint32_t value) override { return set(value, &mOdClamp); }
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", &mLpTriggerEffect);
68         open("device/lp_trigger_scale", &mLpTriggerScale);
69         open("device/lra_wave_shape", &mLraWaveShape);
70         open("device/od_clamp", &mOdClamp);
71     }
72 
73   private:
74     std::ofstream mAutocal;
75     std::ofstream mOlLraPeriod;
76     std::ofstream mActivate;
77     std::ofstream mDuration;
78     std::ofstream mState;
79     std::ofstream mRtpInput;
80     std::ofstream mMode;
81     std::ofstream mSequencer;
82     std::ofstream mScale;
83     std::ofstream mCtrlLoop;
84     std::ofstream mLpTriggerEffect;
85     std::ofstream mLpTriggerScale;
86     std::ofstream mLraWaveShape;
87     std::ofstream mOdClamp;
88 };
89 
90 class HwCal : public Vibrator::HwCal, private HwCalBase {
91   private:
92     static constexpr char AUTOCAL_CONFIG[] = "autocal";
93     static constexpr char LRA_PERIOD_CONFIG[] = "lra_period";
94 
95     static constexpr uint32_t WAVEFORM_CLICK_EFFECT_MS = 6;
96     static constexpr uint32_t WAVEFORM_TICK_EFFECT_MS = 2;
97     static constexpr uint32_t WAVEFORM_DOUBLE_CLICK_EFFECT_MS = 135;
98     static constexpr uint32_t WAVEFORM_HEAVY_CLICK_EFFECT_MS = 8;
99 
100     static constexpr uint32_t DEFAULT_LRA_PERIOD = 262;
101     static constexpr uint32_t DEFAULT_FREQUENCY_SHIFT = 10;
102     static constexpr uint32_t DEFAULT_VOLTAGE_MAX = 107;  // 2.15V;
103 
104   public:
HwCal()105     HwCal() {}
106 
getAutocal(std::string * value)107     bool getAutocal(std::string *value) override { return getPersist(AUTOCAL_CONFIG, value); }
getLraPeriod(uint32_t * value)108     bool getLraPeriod(uint32_t *value) override {
109         if (getPersist(LRA_PERIOD_CONFIG, value)) {
110             return true;
111         }
112         *value = DEFAULT_LRA_PERIOD;
113         return true;
114     }
getCloseLoopThreshold(uint32_t * value)115     bool getCloseLoopThreshold(uint32_t *value) override {
116         return getProperty("closeloop.threshold", value, UINT32_MAX);
117         return true;
118     }
getDynamicConfig(bool * value)119     bool getDynamicConfig(bool *value) override {
120         return getProperty("config.dynamic", value, false);
121     }
getLongFrequencyShift(uint32_t * value)122     bool getLongFrequencyShift(uint32_t *value) override {
123         return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
124     }
getShortVoltageMax(uint32_t * value)125     bool getShortVoltageMax(uint32_t *value) override {
126         return getProperty("short.voltage", value, DEFAULT_VOLTAGE_MAX);
127     }
getLongVoltageMax(uint32_t * value)128     bool getLongVoltageMax(uint32_t *value) override {
129         return getProperty("long.voltage", value, DEFAULT_VOLTAGE_MAX);
130     }
getClickDuration(uint32_t * value)131     bool getClickDuration(uint32_t *value) override {
132         return getProperty("click.duration", value, WAVEFORM_CLICK_EFFECT_MS);
133     }
getTickDuration(uint32_t * value)134     bool getTickDuration(uint32_t *value) override {
135         return getProperty("tick.duration", value, WAVEFORM_TICK_EFFECT_MS);
136     }
getDoubleClickDuration(uint32_t * value)137     bool getDoubleClickDuration(uint32_t *value) override {
138         return getProperty("double_click.duration", value, WAVEFORM_DOUBLE_CLICK_EFFECT_MS);
139     }
getHeavyClickDuration(uint32_t * value)140     bool getHeavyClickDuration(uint32_t *value) override {
141         return getProperty("heavyclick.duration", value, WAVEFORM_HEAVY_CLICK_EFFECT_MS);
142     }
debug(int fd)143     void debug(int fd) override { HwCalBase::debug(fd); }
144 };
145 
146 }  // namespace vibrator
147 }  // namespace hardware
148 }  // namespace android
149 }  // namespace aidl
150