1 /*
2  * Copyright (C) 2021 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("calibration/f0_stored", &mF0);
30         open("default/f0_offset", &mF0Offset);
31         open("calibration/redc_stored", &mRedc);
32         open("calibration/q_stored", &mQ);
33         open("default/vibe_state", &mVibeState);
34         open("default/num_waves", &mEffectCount);
35         open("default/owt_free_space", &mOwtFreeSpace);
36     }
37 
setF0(std::string value)38     bool setF0(std::string value) override { return set(value, &mF0); }
setF0Offset(uint32_t value)39     bool setF0Offset(uint32_t value) override { return set(value, &mF0Offset); }
setRedc(std::string value)40     bool setRedc(std::string value) override { return set(value, &mRedc); }
setQ(std::string value)41     bool setQ(std::string value) override { return set(value, &mQ); }
getEffectCount(uint32_t * value)42     bool getEffectCount(uint32_t *value) override { return getStr(value, &mEffectCount); }
pollVibeState(std::string value,int32_t timeoutMs)43     bool pollVibeState(std::string value, int32_t timeoutMs) override {
44         return pollStr(value, &mVibeState, timeoutMs);
45     }
setClabEnable(bool value)46     bool setClabEnable(bool value) override { return set(value, &mClabEnable); }
getAvailablePwleSegments(uint32_t * value)47     bool getAvailablePwleSegments(uint32_t *value) override {
48         return getStr(value, &mAvailablePwleSegments);
49     }
setPwle(std::string value)50     bool setPwle(std::string value) override { return set(value, &mPwle); }
hasOwtFreeSpace()51     bool hasOwtFreeSpace() override { return has(mOwtFreeSpace); }
getOwtFreeSpace(uint32_t * value)52     bool getOwtFreeSpace(uint32_t *value) override { return get(value, &mOwtFreeSpace); }
debug(int fd)53     void debug(int fd) override { HwApiBase::debug(fd); }
54 
55   private:
56     std::ofstream mF0;
57     std::ofstream mF0Offset;
58     std::ofstream mRedc;
59     std::ofstream mQ;
60     std::ifstream mEffectCount;
61     std::ifstream mVibeState;
62     std::ofstream mClabEnable;
63     std::ifstream mAvailablePwleSegments;
64     std::ofstream mPwle;
65     std::ifstream mOwtFreeSpace;
66 };
67 
68 class HwCal : public Vibrator::HwCal, private HwCalBase {
69   private:
70     static constexpr char VERSION[] = "version";
71     static constexpr char F0_CONFIG[] = "f0_measured";
72     static constexpr char REDC_CONFIG[] = "redc_measured";
73     static constexpr char Q_CONFIG[] = "q_measured";
74     static constexpr char TICK_VOLTAGES_CONFIG[] = "v_tick";
75     static constexpr char CLICK_VOLTAGES_CONFIG[] = "v_click";
76     static constexpr char LONG_VOLTAGES_CONFIG[] = "v_long";
77 
78     static constexpr uint32_t VERSION_DEFAULT = 2;
79     static constexpr int32_t DEFAULT_FREQUENCY_SHIFT = 0;
80     static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {1, 100};
81     static constexpr std::array<uint32_t, 2> V_CTICK_DEFAULT = {1, 100};
82     static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {1, 100};
83 
84   public:
HwCal()85     HwCal() {}
86 
getVersion(uint32_t * value)87     bool getVersion(uint32_t *value) override {
88         if (getPersist(VERSION, value)) {
89             return true;
90         }
91         *value = VERSION_DEFAULT;
92         return true;
93     }
getLongFrequencyShift(int32_t * value)94     bool getLongFrequencyShift(int32_t *value) override {
95         return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
96     }
getF0(std::string * value)97     bool getF0(std::string *value) override { return getPersist(F0_CONFIG, value); }
getRedc(std::string * value)98     bool getRedc(std::string *value) override { return getPersist(REDC_CONFIG, value); }
getQ(std::string * value)99     bool getQ(std::string *value) override { return getPersist(Q_CONFIG, value); }
getTickVolLevels(std::array<uint32_t,2> * value)100     bool getTickVolLevels(std::array<uint32_t, 2> *value) override {
101         if (getPersist(TICK_VOLTAGES_CONFIG, value)) {
102             return true;
103         }
104         *value = V_TICK_DEFAULT;
105         return true;
106     }
getClickVolLevels(std::array<uint32_t,2> * value)107     bool getClickVolLevels(std::array<uint32_t, 2> *value) override {
108         if (getPersist(CLICK_VOLTAGES_CONFIG, value)) {
109             return true;
110         }
111         *value = V_CTICK_DEFAULT;
112         return true;
113     }
getLongVolLevels(std::array<uint32_t,2> * value)114     bool getLongVolLevels(std::array<uint32_t, 2> *value) override {
115         if (getPersist(LONG_VOLTAGES_CONFIG, value)) {
116             return true;
117         }
118         *value = V_LONG_DEFAULT;
119         return true;
120     }
isChirpEnabled()121     bool isChirpEnabled() override {
122         bool value;
123         getProperty("chirp.enabled", &value, false);
124         return value;
125     }
getSupportedPrimitives(uint32_t * value)126     bool getSupportedPrimitives(uint32_t *value) override {
127         return getProperty("supported_primitives", value, (uint32_t)0);
128     }
debug(int fd)129     void debug(int fd) override { HwCalBase::debug(fd); }
130 };
131 
132 }  // namespace vibrator
133 }  // namespace hardware
134 }  // namespace android
135 }  // namespace aidl
136