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 #ifndef ANDROID_HARDWARE_VIBRATOR_HARDWARE_H
17 #define ANDROID_HARDWARE_VIBRATOR_HARDWARE_H
18 
19 #include "Vibrator.h"
20 #include "utils.h"
21 
22 namespace android {
23 namespace hardware {
24 namespace vibrator {
25 namespace V1_3 {
26 namespace implementation {
27 
28 class HwApi : public Vibrator::HwApi {
29   private:
30     using NamesMap = std::map<const void *, std::string>;
31 
32     class RecordInterface {
33       public:
34         virtual std::string toString(const NamesMap &names) = 0;
~RecordInterface()35         virtual ~RecordInterface() {}
36     };
37     template <typename T>
38     class Record : public RecordInterface {
39       public:
Record(const char * func,const T & value,const void * stream)40         Record(const char *func, const T &value, const void *stream)
41             : mFunc(func), mValue(value), mStream(stream) {}
42         std::string toString(const NamesMap &names) override;
43 
44       private:
45         const char *mFunc;
46         const T mValue;
47         const void *mStream;
48     };
49 
50     static constexpr uint32_t RECORDS_SIZE = 32;
51 
52   public:
53     HwApi();
setF0(uint32_t value)54     bool setF0(uint32_t value) override { return set(value, mF0); }
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); }
getEffectDuration(uint32_t * value)59     bool getEffectDuration(uint32_t *value) override { return get(value, mEffectDuration); }
setEffectIndex(uint32_t value)60     bool setEffectIndex(uint32_t value) override { return set(value, mEffectIndex); }
setEffectQueue(std::string value)61     bool setEffectQueue(std::string value) override { return set(value, mEffectQueue); }
hasEffectScale()62     bool hasEffectScale() override { return has(mEffectScale); }
setEffectScale(uint32_t value)63     bool setEffectScale(uint32_t value) override { return set(value, mEffectScale); }
setGlobalScale(uint32_t value)64     bool setGlobalScale(uint32_t value) override { return set(value, mGlobalScale); }
setState(bool value)65     bool setState(bool value) override { return set(value, mState); }
hasAspEnable()66     bool hasAspEnable() override { return has(mAspEnable); }
getAspEnable(bool * value)67     bool getAspEnable(bool *value) override { return get(value, mAspEnable); }
setAspEnable(bool value)68     bool setAspEnable(bool value) override { return set(value, mAspEnable); }
setGpioFallIndex(uint32_t value)69     bool setGpioFallIndex(uint32_t value) override { return set(value, mGpioFallIndex); }
setGpioFallScale(uint32_t value)70     bool setGpioFallScale(uint32_t value) override { return set(value, mGpioFallScale); }
setGpioRiseIndex(uint32_t value)71     bool setGpioRiseIndex(uint32_t value) override { return set(value, mGpioRiseIndex); }
setGpioRiseScale(uint32_t value)72     bool setGpioRiseScale(uint32_t value) override { return set(value, mGpioRiseScale); }
73     void debug(int fd) override;
74 
75   private:
76     template <typename T>
77     bool has(T &stream);
78     template <typename T, typename U>
79     bool get(T *value, U &stream);
80     template <typename T, typename U>
81     bool set(const T &value, U &stream);
82     template <typename T>
83     void record(const char *func, const T &value, void *stream);
84 
85   private:
86     NamesMap mNames;
87     std::vector<std::unique_ptr<RecordInterface>> mRecords{RECORDS_SIZE};
88     std::ofstream mF0;
89     std::ofstream mRedc;
90     std::ofstream mQ;
91     std::ofstream mActivate;
92     std::ofstream mDuration;
93     std::ifstream mEffectDuration;
94     std::ofstream mEffectIndex;
95     std::ofstream mEffectQueue;
96     std::ofstream mEffectScale;
97     std::ofstream mGlobalScale;
98     std::ofstream mState;
99     std::fstream mAspEnable;
100     std::ofstream mGpioFallIndex;
101     std::ofstream mGpioFallScale;
102     std::ofstream mGpioRiseIndex;
103     std::ofstream mGpioRiseScale;
104 };
105 
106 class HwCal : public Vibrator::HwCal {
107   private:
108     static constexpr char F0_CONFIG[] = "f0_measured";
109     static constexpr char REDC_CONFIG[] = "redc_measured";
110     static constexpr char Q_CONFIG[] = "q_measured";
111     static constexpr char Q_INDEX[] = "q_index";
112     static constexpr char VOLTAGES_CONFIG[] = "v_levels";
113 
114     static constexpr uint32_t Q_FLOAT_TO_FIXED = 1 << 16;
115     static constexpr float Q_INDEX_TO_FLOAT = 1.5f;
116     static constexpr uint32_t Q_INDEX_TO_FIXED = Q_INDEX_TO_FLOAT * Q_FLOAT_TO_FIXED;
117     static constexpr uint32_t Q_INDEX_OFFSET = 2.0f * Q_FLOAT_TO_FIXED;
118 
119     static constexpr uint32_t Q_DEFAULT = 15.5 * Q_FLOAT_TO_FIXED;
120     static constexpr std::array<uint32_t, 6> V_LEVELS_DEFAULT = {60, 70, 80, 90, 100, 76};
121 
122   public:
123     HwCal();
getF0(uint32_t * value)124     bool getF0(uint32_t *value) override { return get(F0_CONFIG, value); }
getRedc(uint32_t * value)125     bool getRedc(uint32_t *value) override { return get(REDC_CONFIG, value); }
getQ(uint32_t * value)126     bool getQ(uint32_t *value) override {
127         if (get(Q_CONFIG, value)) {
128             return true;
129         }
130         if (get(Q_INDEX, value)) {
131             *value = *value * Q_INDEX_TO_FIXED + Q_INDEX_OFFSET;
132             return true;
133         }
134         *value = Q_DEFAULT;
135         return true;
136     }
getVolLevels(std::array<uint32_t,6> * value)137     bool getVolLevels(std::array<uint32_t, 6> *value) override {
138         if (get(VOLTAGES_CONFIG, value)) {
139             return true;
140         }
141         *value = V_LEVELS_DEFAULT;
142         return true;
143     }
144     void debug(int fd) override;
145 
146   private:
147     template <typename T>
148     bool get(const char *key, T *value);
149 
150   private:
151     std::map<std::string, std::string> mCalData;
152 };
153 
154 }  // namespace implementation
155 }  // namespace V1_3
156 }  // namespace vibrator
157 }  // namespace hardware
158 }  // namespace android
159 
160 #endif  // ANDROID_HARDWARE_VIBRATOR_HARDWARE_H
161