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