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         open("device/pwle_ramp_down", &mPwleRampDown);
52     }
53 
setF0(uint32_t value)54     bool setF0(uint32_t value) override { return set(value, &mF0); }
setF0Offset(uint32_t value)55     bool setF0Offset(uint32_t value) override { return set(value, &mF0Offset); }
setRedc(uint32_t value)56     bool setRedc(uint32_t value) override { return set(value, &mRedc); }
setQ(uint32_t value)57     bool setQ(uint32_t value) override { return set(value, &mQ); }
setActivate(bool value)58     bool setActivate(bool value) override { return set(value, &mActivate); }
setDuration(uint32_t value)59     bool setDuration(uint32_t value) override { return set(value, &mDuration); }
getEffectCount(uint32_t * value)60     bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); }
getEffectDuration(uint32_t * value)61     bool getEffectDuration(uint32_t *value) override { return get(value, &mEffectDuration); }
setEffectIndex(uint32_t value)62     bool setEffectIndex(uint32_t value) override { return set(value, &mEffectIndex); }
setEffectQueue(std::string value)63     bool setEffectQueue(std::string value) override { return set(value, &mEffectQueue); }
hasEffectScale()64     bool hasEffectScale() override { return has(mEffectScale); }
setEffectScale(uint32_t value)65     bool setEffectScale(uint32_t value) override { return set(value, &mEffectScale); }
setGlobalScale(uint32_t value)66     bool setGlobalScale(uint32_t value) override { return set(value, &mGlobalScale); }
setState(bool value)67     bool setState(bool value) override { return set(value, &mState); }
hasAspEnable()68     bool hasAspEnable() override { return has(mAspEnable); }
getAspEnable(bool * value)69     bool getAspEnable(bool *value) override { return get(value, &mAspEnable); }
setAspEnable(bool value)70     bool setAspEnable(bool value) override { return set(value, &mAspEnable); }
setGpioFallIndex(uint32_t value)71     bool setGpioFallIndex(uint32_t value) override { return set(value, &mGpioFallIndex); }
setGpioFallScale(uint32_t value)72     bool setGpioFallScale(uint32_t value) override { return set(value, &mGpioFallScale); }
setGpioRiseIndex(uint32_t value)73     bool setGpioRiseIndex(uint32_t value) override { return set(value, &mGpioRiseIndex); }
setGpioRiseScale(uint32_t value)74     bool setGpioRiseScale(uint32_t value) override { return set(value, &mGpioRiseScale); }
pollVibeState(uint32_t value,int32_t timeoutMs)75     bool pollVibeState(uint32_t value, int32_t timeoutMs) override {
76         return poll(value, &mVibeState, timeoutMs);
77     }
setClabEnable(bool value)78     bool setClabEnable(bool value) override { return set(value, &mClabEnable); }
getAvailablePwleSegments(uint32_t * value)79     bool getAvailablePwleSegments(uint32_t *value) override {
80         return get(value, &mAvailablePwleSegments);
81     }
hasPwle()82     bool hasPwle() override { return has(mPwle); }
setPwle(std::string value)83     bool setPwle(std::string value) override { return set(value, &mPwle); }
setPwleRampDown(uint32_t value)84     bool setPwleRampDown(uint32_t value) override { return set(value, &mPwleRampDown); }
debug(int fd)85     void debug(int fd) override { HwApiBase::debug(fd); }
86 
87   private:
88     std::ofstream mF0;
89     std::ofstream mF0Offset;
90     std::ofstream mRedc;
91     std::ofstream mQ;
92     std::ofstream mActivate;
93     std::ofstream mDuration;
94     std::ifstream mEffectCount;
95     std::ifstream mEffectDuration;
96     std::ofstream mEffectIndex;
97     std::ofstream mEffectQueue;
98     std::ofstream mEffectScale;
99     std::ofstream mGlobalScale;
100     std::ofstream mState;
101     std::fstream mAspEnable;
102     std::ofstream mGpioFallIndex;
103     std::ofstream mGpioFallScale;
104     std::ofstream mGpioRiseIndex;
105     std::ofstream mGpioRiseScale;
106     std::ifstream mVibeState;
107     std::ofstream mClabEnable;
108     std::ifstream mAvailablePwleSegments;
109     std::ofstream mPwle;
110     std::ofstream mPwleRampDown;
111 };
112 
113 class HwCal : public Vibrator::HwCal, private HwCalBase {
114   private:
115     static constexpr char VERSION[] = "version";
116     static constexpr char F0_CONFIG[] = "f0_measured";
117     static constexpr char REDC_CONFIG[] = "redc_measured";
118     static constexpr char Q_CONFIG[] = "q_measured";
119     static constexpr char Q_INDEX[] = "q_index";
120     static constexpr char VOLTAGES_CONFIG[] = "v_levels";
121     static constexpr char TICK_VOLTAGES_CONFIG[] = "v_tick";
122     static constexpr char CLICK_VOLTAGES_CONFIG[] = "v_click";
123     static constexpr char LONG_VOLTAGES_CONFIG[] = "v_long";
124 
125     static constexpr uint32_t Q_FLOAT_TO_FIXED = 1 << 16;
126     static constexpr float Q_INDEX_TO_FLOAT = 1.5f;
127     static constexpr uint32_t Q_INDEX_TO_FIXED = Q_INDEX_TO_FLOAT * Q_FLOAT_TO_FIXED;
128     static constexpr uint32_t Q_INDEX_OFFSET = 2.0f * Q_FLOAT_TO_FIXED;
129 
130     static constexpr uint32_t VERSION_DEFAULT = 1;
131     static constexpr int32_t DEFAULT_FREQUENCY_SHIFT = 0;
132     static constexpr float DEFAULT_DEVICE_MASS = 0.21;
133     static constexpr float DEFAULT_LOC_COEFF = 0.5;
134     static constexpr uint32_t Q_DEFAULT = 15.5 * Q_FLOAT_TO_FIXED;
135     static constexpr std::array<uint32_t, 6> V_LEVELS_DEFAULT = {60, 70, 80, 90, 100, 76};
136     static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {10, 70};
137     static constexpr std::array<uint32_t, 2> V_CTICK_DEFAULT = {10, 70};
138     static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {10, 70};
139 
140   public:
HwCal()141     HwCal() {}
142 
getVersion(uint32_t * value)143     bool getVersion(uint32_t *value) override {
144         if (getPersist(VERSION, value)) {
145             return true;
146         }
147         *value = VERSION_DEFAULT;
148         return true;
149     }
getLongFrequencyShift(int32_t * value)150     bool getLongFrequencyShift(int32_t *value) override {
151         return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
152     }
getDeviceMass(float * value)153     bool getDeviceMass(float *value) override {
154         return getProperty("device.mass", value, DEFAULT_DEVICE_MASS);
155     }
getLocCoeff(float * value)156     bool getLocCoeff(float *value) override {
157         return getProperty("loc.coeff", value, DEFAULT_LOC_COEFF);
158     }
getF0(uint32_t * value)159     bool getF0(uint32_t *value) override { return getPersist(F0_CONFIG, value); }
getRedc(uint32_t * value)160     bool getRedc(uint32_t *value) override { return getPersist(REDC_CONFIG, value); }
getQ(uint32_t * value)161     bool getQ(uint32_t *value) override {
162         if (getPersist(Q_CONFIG, value)) {
163             return true;
164         }
165         if (getPersist(Q_INDEX, value)) {
166             *value = *value * Q_INDEX_TO_FIXED + Q_INDEX_OFFSET;
167             return true;
168         }
169         *value = Q_DEFAULT;
170         return true;
171     }
getVolLevels(std::array<uint32_t,6> * value)172     bool getVolLevels(std::array<uint32_t, 6> *value) override {
173         if (getPersist(VOLTAGES_CONFIG, value)) {
174             return true;
175         }
176         *value = V_LEVELS_DEFAULT;
177         return true;
178     }
getTickVolLevels(std::array<uint32_t,2> * value)179     bool getTickVolLevels(std::array<uint32_t, 2> *value) override {
180         if (getPersist(TICK_VOLTAGES_CONFIG, value)) {
181             return true;
182         }
183         *value = V_TICK_DEFAULT;
184         return true;
185     }
getClickVolLevels(std::array<uint32_t,2> * value)186     bool getClickVolLevels(std::array<uint32_t, 2> *value) override {
187         if (getPersist(CLICK_VOLTAGES_CONFIG, value)) {
188             return true;
189         }
190         *value = V_CTICK_DEFAULT;
191         return true;
192     }
getLongVolLevels(std::array<uint32_t,2> * value)193     bool getLongVolLevels(std::array<uint32_t, 2> *value) override {
194         if (getPersist(LONG_VOLTAGES_CONFIG, value)) {
195             return true;
196         }
197         *value = V_LONG_DEFAULT;
198         return true;
199     }
isChirpEnabled()200     bool isChirpEnabled() override {
201         return utils::getProperty("persist.vendor.vibrator.hal.chirp.enabled", false);
202     }
debug(int fd)203     void debug(int fd) override { HwCalBase::debug(fd); }
204 };
205 
206 }  // namespace vibrator
207 }  // namespace hardware
208 }  // namespace android
209 }  // namespace aidl
210