• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/unique_fd.h>
19 #include <log/log.h>
20 #include <sys/epoll.h>
21 #include <utils/Trace.h>
22 
23 #include <list>
24 #include <map>
25 #include <sstream>
26 #include <string>
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, const int32_t timeout = -1);
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     std::mutex mIoMutex;
86 };
87 
88 #define HWAPI_RECORD(args...) HwApiBase::record(__FUNCTION__, ##args)
89 
90 template <typename T>
open(const std::string & name,T * stream)91 void HwApiBase::open(const std::string &name, T *stream) {
92     saveName(name, stream);
93     utils::openNoCreate(mPathPrefix + name, stream);
94 }
95 
96 template <typename T>
get(T * value,std::istream * stream)97 bool HwApiBase::get(T *value, std::istream *stream) {
98     ATRACE_NAME("HwApi::get");
99     std::scoped_lock ioLock{mIoMutex};
100     bool ret;
101     stream->seekg(0);
102     *stream >> *value;
103     if (!(ret = !!*stream)) {
104         ALOGE("Failed to read %s (%d): %s", mNames[stream].c_str(), errno, strerror(errno));
105     }
106     stream->clear();
107     HWAPI_RECORD(*value, stream);
108     return ret;
109 }
110 
111 template <typename T>
set(const T & value,std::ostream * stream)112 bool HwApiBase::set(const T &value, std::ostream *stream) {
113     ATRACE_NAME("HwApi::set");
114     using utils::operator<<;
115     std::scoped_lock ioLock{mIoMutex};
116     bool ret;
117     *stream << value << std::endl;
118     if (!(ret = !!*stream)) {
119         ALOGE("Failed to write %s (%d): %s", mNames[stream].c_str(), errno, strerror(errno));
120         stream->clear();
121     }
122     HWAPI_RECORD(value, stream);
123     return ret;
124 }
125 
126 template <typename T>
poll(const T & value,std::istream * stream,const int32_t timeoutMs)127 bool HwApiBase::poll(const T &value, std::istream *stream, const int32_t timeoutMs) {
128     ATRACE_NAME("HwApi::poll");
129     auto path = mPathPrefix + mNames[stream];
130     unique_fd fileFd{::open(path.c_str(), O_RDONLY)};
131     unique_fd epollFd{epoll_create(1)};
132     epoll_event event = {
133             .events = EPOLLPRI | EPOLLET,
134     };
135     T actual;
136     bool ret;
137     int epollRet;
138 
139     if (timeoutMs < -1) {
140         ALOGE("Invalid polling timeout!");
141         return false;
142     }
143 
144     if (epoll_ctl(epollFd, EPOLL_CTL_ADD, fileFd, &event)) {
145         ALOGE("Failed to poll %s (%d): %s", mNames[stream].c_str(), errno, strerror(errno));
146         return false;
147     }
148 
149     while ((ret = get(&actual, stream)) && (actual != value)) {
150         epollRet = epoll_wait(epollFd, &event, 1, timeoutMs);
151         if (epollRet <= 0) {
152             ALOGE("Polling error or timeout! (%d)", epollRet);
153             return false;
154         }
155     }
156 
157     HWAPI_RECORD(value, stream);
158     return ret;
159 }
160 
161 template <typename T>
record(const char * func,const T & value,const std::ios * stream)162 void HwApiBase::record(const char *func, const T &value, const std::ios *stream) {
163     std::lock_guard<std::mutex> lock(mRecordsMutex);
164     mRecords.emplace_back(std::make_unique<Record<T>>(func, value, stream));
165     mRecords.pop_front();
166 }
167 
168 template <typename T>
toString(const NamesMap & names)169 std::string HwApiBase::Record<T>::toString(const NamesMap &names) {
170     using utils::operator<<;
171     std::stringstream ret;
172 
173     ret << mFunc << " '" << names.at(mStream) << "' = '" << mValue << "'";
174 
175     return ret.str();
176 }
177 
178 class HwCalBase {
179   public:
180     HwCalBase();
181     void debug(int fd);
182 
183   protected:
184     template <typename T>
185     bool getProperty(const char *key, T *value, const T defval);
186     template <typename T>
187     bool getPersist(const char *key, T *value);
188 
189   private:
190     std::string mPropertyPrefix;
191     std::map<std::string, std::string> mCalData;
192 };
193 
194 template <typename T>
getProperty(const char * key,T * outval,const T defval)195 bool HwCalBase::getProperty(const char *key, T *outval, const T defval) {
196     ATRACE_NAME("HwCal::getProperty");
197     *outval = utils::getProperty(mPropertyPrefix + key, defval);
198     return true;
199 }
200 
201 template <typename T>
getPersist(const char * key,T * value)202 bool HwCalBase::getPersist(const char *key, T *value) {
203     ATRACE_NAME("HwCal::getPersist");
204     auto it = mCalData.find(key);
205     if (it == mCalData.end()) {
206         ALOGE("Missing %s config!", key);
207         return false;
208     }
209     std::stringstream stream{it->second};
210     utils::unpack(stream, value);
211     if (!stream || !stream.eof()) {
212         ALOGE("Invalid %s config!", key);
213         return false;
214     }
215     return true;
216 }
217 
218 }  // namespace vibrator
219 }  // namespace hardware
220 }  // namespace android
221 }  // namespace aidl
222