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/redc_stored", &mRedc);
31         open("device/q_stored", &mQ);
32         open("activate", &mActivate);
33         open("duration", &mDuration);
34         open("state", &mState);
35         open("device/cp_trigger_duration", &mEffectDuration);
36         open("device/cp_trigger_index", &mEffectIndex);
37         open("device/cp_trigger_queue", &mEffectQueue);
38         open("device/cp_dig_scale", &mEffectScale);
39         open("device/dig_scale", &mGlobalScale);
40         open("device/asp_enable", &mAspEnable);
41         open("device/gpio1_fall_index", &mGpioFallIndex);
42         open("device/gpio1_fall_dig_scale", &mGpioFallScale);
43         open("device/gpio1_rise_index", &mGpioRiseIndex);
44         open("device/gpio1_rise_dig_scale", &mGpioRiseScale);
45         open("device/vibe_state", &mVibeState);
46         open("device/num_waves", &mEffectCount);
47     }
48 
setF0(uint32_t value)49     bool setF0(uint32_t value) override { return set(value, &mF0); }
setRedc(uint32_t value)50     bool setRedc(uint32_t value) override { return set(value, &mRedc); }
setQ(uint32_t value)51     bool setQ(uint32_t value) override { return set(value, &mQ); }
setActivate(bool value)52     bool setActivate(bool value) override { return set(value, &mActivate); }
setDuration(uint32_t value)53     bool setDuration(uint32_t value) override { return set(value, &mDuration); }
getEffectCount(uint32_t * value)54     bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); }
getEffectDuration(uint32_t * value)55     bool getEffectDuration(uint32_t *value) override { return get(value, &mEffectDuration); }
setEffectIndex(uint32_t value)56     bool setEffectIndex(uint32_t value) override { return set(value, &mEffectIndex); }
setEffectQueue(std::string value)57     bool setEffectQueue(std::string value) override { return set(value, &mEffectQueue); }
hasEffectScale()58     bool hasEffectScale() override { return has(mEffectScale); }
setEffectScale(uint32_t value)59     bool setEffectScale(uint32_t value) override { return set(value, &mEffectScale); }
setGlobalScale(uint32_t value)60     bool setGlobalScale(uint32_t value) override { return set(value, &mGlobalScale); }
setState(bool value)61     bool setState(bool value) override { return set(value, &mState); }
hasAspEnable()62     bool hasAspEnable() override { return has(mAspEnable); }
getAspEnable(bool * value)63     bool getAspEnable(bool *value) override { return get(value, &mAspEnable); }
setAspEnable(bool value)64     bool setAspEnable(bool value) override { return set(value, &mAspEnable); }
setGpioFallIndex(uint32_t value)65     bool setGpioFallIndex(uint32_t value) override { return set(value, &mGpioFallIndex); }
setGpioFallScale(uint32_t value)66     bool setGpioFallScale(uint32_t value) override { return set(value, &mGpioFallScale); }
setGpioRiseIndex(uint32_t value)67     bool setGpioRiseIndex(uint32_t value) override { return set(value, &mGpioRiseIndex); }
setGpioRiseScale(uint32_t value)68     bool setGpioRiseScale(uint32_t value) override { return set(value, &mGpioRiseScale); }
pollVibeState(bool value)69     bool pollVibeState(bool value) override { return poll(value, &mVibeState); }
debug(int fd)70     void debug(int fd) override { HwApiBase::debug(fd); }
71 
72   private:
73     std::ofstream mF0;
74     std::ofstream mRedc;
75     std::ofstream mQ;
76     std::ofstream mActivate;
77     std::ofstream mDuration;
78     std::ifstream mEffectCount;
79     std::ifstream mEffectDuration;
80     std::ofstream mEffectIndex;
81     std::ofstream mEffectQueue;
82     std::ofstream mEffectScale;
83     std::ofstream mGlobalScale;
84     std::ofstream mState;
85     std::fstream mAspEnable;
86     std::ofstream mGpioFallIndex;
87     std::ofstream mGpioFallScale;
88     std::ofstream mGpioRiseIndex;
89     std::ofstream mGpioRiseScale;
90     std::ifstream mVibeState;
91 };
92 
93 class HwCal : public Vibrator::HwCal, private HwCalBase {
94   private:
95     static constexpr char F0_CONFIG[] = "f0_measured";
96     static constexpr char REDC_CONFIG[] = "redc_measured";
97     static constexpr char Q_CONFIG[] = "q_measured";
98     static constexpr char Q_INDEX[] = "q_index";
99     static constexpr char VOLTAGES_CONFIG[] = "v_levels";
100 
101     static constexpr uint32_t Q_FLOAT_TO_FIXED = 1 << 16;
102     static constexpr float Q_INDEX_TO_FLOAT = 1.5f;
103     static constexpr uint32_t Q_INDEX_TO_FIXED = Q_INDEX_TO_FLOAT * Q_FLOAT_TO_FIXED;
104     static constexpr uint32_t Q_INDEX_OFFSET = 2.0f * Q_FLOAT_TO_FIXED;
105 
106     static constexpr uint32_t Q_DEFAULT = 15.5 * Q_FLOAT_TO_FIXED;
107     static constexpr std::array<uint32_t, 6> V_LEVELS_DEFAULT = {60, 70, 80, 90, 100, 76};
108 
109   public:
HwCal()110     HwCal() {}
111 
getF0(uint32_t * value)112     bool getF0(uint32_t *value) override { return getPersist(F0_CONFIG, value); }
getRedc(uint32_t * value)113     bool getRedc(uint32_t *value) override { return getPersist(REDC_CONFIG, value); }
getQ(uint32_t * value)114     bool getQ(uint32_t *value) override {
115         if (getPersist(Q_CONFIG, value)) {
116             return true;
117         }
118         if (getPersist(Q_INDEX, value)) {
119             *value = *value * Q_INDEX_TO_FIXED + Q_INDEX_OFFSET;
120             return true;
121         }
122         *value = Q_DEFAULT;
123         return true;
124     }
getVolLevels(std::array<uint32_t,6> * value)125     bool getVolLevels(std::array<uint32_t, 6> *value) override {
126         if (getPersist(VOLTAGES_CONFIG, value)) {
127             return true;
128         }
129         *value = V_LEVELS_DEFAULT;
130         return true;
131     }
debug(int fd)132     void debug(int fd) override { HwCalBase::debug(fd); }
133 };
134 
135 }  // namespace vibrator
136 }  // namespace hardware
137 }  // namespace android
138 }  // namespace aidl
139