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:
HwApi()28     HwApi() {
29         open("device/f0_stored", &mF0);
30         open("device/f0_offset", &mF0Offset);
31         open("device/redc_stored", &mRedc);
32         open("device/q_stored", &mQ);
33         open("activate", &mActivate);
34         open("duration", &mDuration);
35         open("state", &mState);
36         open("device/cp_trigger_duration", &mEffectDuration);
37         open("device/cp_trigger_index", &mEffectIndex);
38         open("device/cp_trigger_queue", &mEffectQueue);
39         open("device/cp_dig_scale", &mEffectScale);
40         open("device/dig_scale", &mGlobalScale);
41         open("device/asp_enable", &mAspEnable);
42         open("device/gpio1_fall_index", &mGpioFallIndex);
43         open("device/gpio1_fall_dig_scale", &mGpioFallScale);
44         open("device/gpio1_rise_index", &mGpioRiseIndex);
45         open("device/gpio1_rise_dig_scale", &mGpioRiseScale);
46         open("device/vibe_state", &mVibeState);
47         open("device/num_waves", &mEffectCount);
48         open("device/clab_enable", &mClabEnable);
49         open("device/available_pwle_segments", &mAvailablePwleSegments);
50         open("device/pwle", &mPwle);
51     }
52 
setF0(uint32_t value)53     bool setF0(uint32_t value) override { return set(value, &mF0); }
setF0Offset(uint32_t value)54     bool setF0Offset(uint32_t value) override { return set(value, &mF0Offset); }
setRedc(uint32_t value)55     bool setRedc(uint32_t value) override { return set(value, &mRedc); }
setQ(uint32_t value)56     bool setQ(uint32_t value) override { return set(value, &mQ); }
setActivate(bool value)57     bool setActivate(bool value) override { return set(value, &mActivate); }
setDuration(uint32_t value)58     bool setDuration(uint32_t value) override { return set(value, &mDuration); }
getEffectCount(uint32_t * value)59     bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); }
getEffectDuration(uint32_t * value)60     bool getEffectDuration(uint32_t *value) override { return get(value, &mEffectDuration); }
setEffectIndex(uint32_t value)61     bool setEffectIndex(uint32_t value) override { return set(value, &mEffectIndex); }
setEffectQueue(std::string value)62     bool setEffectQueue(std::string value) override { return set(value, &mEffectQueue); }
hasEffectScale()63     bool hasEffectScale() override { return has(mEffectScale); }
setEffectScale(uint32_t value)64     bool setEffectScale(uint32_t value) override { return set(value, &mEffectScale); }
setGlobalScale(uint32_t value)65     bool setGlobalScale(uint32_t value) override { return set(value, &mGlobalScale); }
setState(bool value)66     bool setState(bool value) override { return set(value, &mState); }
hasAspEnable()67     bool hasAspEnable() override { return has(mAspEnable); }
getAspEnable(bool * value)68     bool getAspEnable(bool *value) override { return get(value, &mAspEnable); }
setAspEnable(bool value)69     bool setAspEnable(bool value) override { return set(value, &mAspEnable); }
setGpioFallIndex(uint32_t value)70     bool setGpioFallIndex(uint32_t value) override { return set(value, &mGpioFallIndex); }
setGpioFallScale(uint32_t value)71     bool setGpioFallScale(uint32_t value) override { return set(value, &mGpioFallScale); }
setGpioRiseIndex(uint32_t value)72     bool setGpioRiseIndex(uint32_t value) override { return set(value, &mGpioRiseIndex); }
setGpioRiseScale(uint32_t value)73     bool setGpioRiseScale(uint32_t value) override { return set(value, &mGpioRiseScale); }
pollVibeState(bool value)74     bool pollVibeState(bool value) override { return poll(value, &mVibeState); }
setClabEnable(bool value)75     bool setClabEnable(bool value) override { return set(value, &mClabEnable); }
getAvailablePwleSegments(uint32_t * value)76     bool getAvailablePwleSegments(uint32_t *value) override { return get(value, &mAvailablePwleSegments); }
hasPwle()77     bool hasPwle() override { return has(mPwle); }
setPwle(std::string value)78     bool setPwle(std::string value) override { return set(value, &mPwle); }
debug(int fd)79     void debug(int fd) override { HwApiBase::debug(fd); }
80 
81   private:
82     std::ofstream mF0;
83     std::ofstream mF0Offset;
84     std::ofstream mRedc;
85     std::ofstream mQ;
86     std::ofstream mActivate;
87     std::ofstream mDuration;
88     std::ifstream mEffectCount;
89     std::ifstream mEffectDuration;
90     std::ofstream mEffectIndex;
91     std::ofstream mEffectQueue;
92     std::ofstream mEffectScale;
93     std::ofstream mGlobalScale;
94     std::ofstream mState;
95     std::fstream mAspEnable;
96     std::ofstream mGpioFallIndex;
97     std::ofstream mGpioFallScale;
98     std::ofstream mGpioRiseIndex;
99     std::ofstream mGpioRiseScale;
100     std::ifstream mVibeState;
101     std::ofstream mClabEnable;
102     std::ifstream mAvailablePwleSegments;
103     std::ofstream mPwle;
104 };
105 
106 class HwCal : public Vibrator::HwCal, private HwCalBase {
107   private:
108     static constexpr char VERSION[] = "version";
109     static constexpr char F0_CONFIG[] = "f0_measured";
110     static constexpr char REDC_CONFIG[] = "redc_measured";
111     static constexpr char Q_CONFIG[] = "q_measured";
112     static constexpr char Q_INDEX[] = "q_index";
113     static constexpr char VOLTAGES_CONFIG[] = "v_levels";
114     static constexpr char TICK_VOLTAGES_CONFIG[] = "v_tick";
115     static constexpr char CLICK_VOLTAGES_CONFIG[] = "v_click";
116     static constexpr char LONG_VOLTAGES_CONFIG[] = "v_long";
117 
118     static constexpr uint32_t Q_FLOAT_TO_FIXED = 1 << 16;
119     static constexpr float Q_INDEX_TO_FLOAT = 1.5f;
120     static constexpr uint32_t Q_INDEX_TO_FIXED = Q_INDEX_TO_FLOAT * Q_FLOAT_TO_FIXED;
121     static constexpr uint32_t Q_INDEX_OFFSET = 2.0f * Q_FLOAT_TO_FIXED;
122 
123     static constexpr uint32_t VERSION_DEFAULT = 1;
124     static constexpr int32_t DEFAULT_FREQUENCY_SHIFT = 0;
125     static constexpr uint32_t Q_DEFAULT = 15.5 * Q_FLOAT_TO_FIXED;
126     static constexpr std::array<uint32_t, 6> V_LEVELS_DEFAULT = {60, 70, 80, 90, 100, 76};
127     static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {10, 70};
128     static constexpr std::array<uint32_t, 2> V_CTICK_DEFAULT = {10, 70};
129     static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {10, 70};
130 
131   public:
HwCal()132     HwCal() {}
133 
getVersion(uint32_t * value)134     bool getVersion(uint32_t *value) override {
135         if (getPersist(VERSION, value)) {
136             return true;
137         }
138         *value = VERSION_DEFAULT;
139         return true;
140     }
getLongFrequencyShift(int32_t * value)141     bool getLongFrequencyShift(int32_t *value) override {
142         return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
143     }
getF0(uint32_t * value)144     bool getF0(uint32_t *value) override { return getPersist(F0_CONFIG, value); }
getRedc(uint32_t * value)145     bool getRedc(uint32_t *value) override { return getPersist(REDC_CONFIG, value); }
getQ(uint32_t * value)146     bool getQ(uint32_t *value) override {
147         if (getPersist(Q_CONFIG, value)) {
148             return true;
149         }
150         if (getPersist(Q_INDEX, value)) {
151             *value = *value * Q_INDEX_TO_FIXED + Q_INDEX_OFFSET;
152             return true;
153         }
154         *value = Q_DEFAULT;
155         return true;
156     }
getVolLevels(std::array<uint32_t,6> * value)157     bool getVolLevels(std::array<uint32_t, 6> *value) override {
158         if (getPersist(VOLTAGES_CONFIG, value)) {
159             return true;
160         }
161         *value = V_LEVELS_DEFAULT;
162         return true;
163     }
getTickVolLevels(std::array<uint32_t,2> * value)164     bool getTickVolLevels(std::array<uint32_t, 2> *value) override {
165         if (getPersist(TICK_VOLTAGES_CONFIG, value)) {
166             return true;
167         }
168         *value = V_TICK_DEFAULT;
169         return true;
170     }
getClickVolLevels(std::array<uint32_t,2> * value)171     bool getClickVolLevels(std::array<uint32_t, 2> *value) override {
172         if (getPersist(CLICK_VOLTAGES_CONFIG, value)) {
173             return true;
174         }
175         *value = V_CTICK_DEFAULT;
176         return true;
177     }
getLongVolLevels(std::array<uint32_t,2> * value)178     bool getLongVolLevels(std::array<uint32_t, 2> *value) override {
179         if (getPersist(LONG_VOLTAGES_CONFIG, value)) {
180             return true;
181         }
182         *value = V_LONG_DEFAULT;
183         return true;
184     }
debug(int fd)185     void debug(int fd) override { HwCalBase::debug(fd); }
186 };
187 
188 }  // namespace vibrator
189 }  // namespace hardware
190 }  // namespace android
191 }  // namespace aidl
192