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 <list>
19 #include <map>
20 #include <sstream>
21 #include <string>
22 
23 #include <android-base/unique_fd.h>
24 #include <log/log.h>
25 #include <sys/epoll.h>
26 #include <utils/Trace.h>
27 
28 #include "utils.h"
29 
30 namespace aidl {
31 namespace android {
32 namespace hardware {
33 namespace vibrator {
34 
35 using ::android::base::unique_fd;
36 
37 class HwApiBase {
38   private:
39     using NamesMap = std::map<const std::ios *, std::string>;
40 
41     class RecordInterface {
42       public:
43         virtual std::string toString(const NamesMap &names) = 0;
~RecordInterface()44         virtual ~RecordInterface() {}
45     };
46     template <typename T>
47     class Record : public RecordInterface {
48       public:
Record(const char * func,const T & value,const std::ios * stream)49         Record(const char *func, const T &value, const std::ios *stream)
50             : mFunc(func), mValue(value), mStream(stream) {}
51         std::string toString(const NamesMap &names) override;
52 
53       private:
54         const char *mFunc;
55         const T mValue;
56         const std::ios *mStream;
57     };
58     using Records = std::list<std::unique_ptr<RecordInterface>>;
59 
60     static constexpr uint32_t RECORDS_SIZE = 32;
61 
62   public:
63     HwApiBase();
64     void debug(int fd);
65 
66   protected:
67     void saveName(const std::string &name, const std::ios *stream);
68     template <typename T>
69     void open(const std::string &name, T *stream);
70     bool has(const std::ios &stream);
71     template <typename T>
72     bool get(T *value, std::istream *stream);
73     template <typename T>
74     bool set(const T &value, std::ostream *stream);
75     template <typename T>
76     bool poll(const T &value, std::istream *stream);
77     template <typename T>
78     void record(const char *func, const T &value, const std::ios *stream);
79 
80   private:
81     std::string mPathPrefix;
82     NamesMap mNames;
83     Records mRecords{RECORDS_SIZE};
84     std::mutex mRecordsMutex;
85 };
86 
87 #define HWAPI_RECORD(args...) HwApiBase::record(__FUNCTION__, ##args)
88 
89 template <typename T>
open(const std::string & name,T * stream)90 void HwApiBase::open(const std::string &name, T *stream) {
91     saveName(name, stream);
92     utils::openNoCreate(mPathPrefix + name, stream);
93 }
94 
95 template <typename T>
get(T * value,std::istream * stream)96 bool HwApiBase::get(T *value, std::istream *stream) {
97     ATRACE_NAME("HwApi::get");
98     bool ret;
99     stream->seekg(0);
100     *stream >> *value;
101     if (!(ret = !!*stream)) {
102         ALOGE("Failed to read %s (%d): %s", mNames[stream].c_str(), errno, strerror(errno));
103     }
104     stream->clear();
105     HWAPI_RECORD(*value, stream);
106     return ret;
107 }
108 
109 template <typename T>
set(const T & value,std::ostream * stream)110 bool HwApiBase::set(const T &value, std::ostream *stream) {
111     ATRACE_NAME("HwApi::set");
112     using utils::operator<<;
113     bool ret;
114     *stream << value << std::endl;
115     if (!(ret = !!*stream)) {
116         ALOGE("Failed to write %s (%d): %s", mNames[stream].c_str(), errno, strerror(errno));
117         stream->clear();
118     }
119     HWAPI_RECORD(value, stream);
120     return ret;
121 }
122 
123 template <typename T>
poll(const T & value,std::istream * stream)124 bool HwApiBase::poll(const T &value, std::istream *stream) {
125     ATRACE_NAME("HwApi::poll");
126     auto path = mPathPrefix + mNames[stream];
127     unique_fd fileFd{::open(path.c_str(), O_RDONLY)};
128     unique_fd epollFd{epoll_create(1)};
129     epoll_event event = {
130             .events = EPOLLPRI | EPOLLET,
131     };
132     T actual;
133     bool ret;
134 
135     if (epoll_ctl(epollFd, EPOLL_CTL_ADD, fileFd, &event)) {
136         ALOGE("Failed to poll %s (%d): %s", mNames[stream].c_str(), errno, strerror(errno));
137         return false;
138     }
139 
140     while ((ret = get(&actual, stream)) && (actual != value)) {
141         epoll_wait(epollFd, &event, 1, -1);
142     }
143 
144     HWAPI_RECORD(value, stream);
145     return ret;
146 }
147 
148 template <typename T>
record(const char * func,const T & value,const std::ios * stream)149 void HwApiBase::record(const char *func, const T &value, const std::ios *stream) {
150     std::lock_guard<std::mutex> lock(mRecordsMutex);
151     mRecords.emplace_back(std::make_unique<Record<T>>(func, value, stream));
152     mRecords.pop_front();
153 }
154 
155 template <typename T>
toString(const NamesMap & names)156 std::string HwApiBase::Record<T>::toString(const NamesMap &names) {
157     using utils::operator<<;
158     std::stringstream ret;
159 
160     ret << mFunc << " '" << names.at(mStream) << "' = '" << mValue << "'";
161 
162     return ret.str();
163 }
164 
165 class HwCalBase {
166   public:
167     HwCalBase();
168     void debug(int fd);
169 
170   protected:
171     template <typename T>
172     bool getProperty(const char *key, T *value, const T defval);
173     template <typename T>
174     bool getPersist(const char *key, T *value);
175 
176   private:
177     std::string mPropertyPrefix;
178     std::map<std::string, std::string> mCalData;
179 };
180 
181 template <typename T>
getProperty(const char * key,T * outval,const T defval)182 bool HwCalBase::getProperty(const char *key, T *outval, const T defval) {
183     ATRACE_NAME("HwCal::getProperty");
184     *outval = utils::getProperty(mPropertyPrefix + key, defval);
185     return true;
186 }
187 
188 template <typename T>
getPersist(const char * key,T * value)189 bool HwCalBase::getPersist(const char *key, T *value) {
190     ATRACE_NAME("HwCal::getPersist");
191     auto it = mCalData.find(key);
192     if (it == mCalData.end()) {
193         ALOGE("Missing %s config!", key);
194         return false;
195     }
196     std::stringstream stream{it->second};
197     utils::unpack(stream, value);
198     if (!stream || !stream.eof()) {
199         ALOGE("Invalid %s config!", key);
200         return false;
201     }
202     return true;
203 }
204 
205 }  // namespace vibrator
206 }  // namespace hardware
207 }  // namespace android
208 }  // namespace aidl
209