• 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 <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     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)127 bool HwApiBase::poll(const T &value, std::istream *stream) {
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 
138     if (epoll_ctl(epollFd, EPOLL_CTL_ADD, fileFd, &event)) {
139         ALOGE("Failed to poll %s (%d): %s", mNames[stream].c_str(), errno, strerror(errno));
140         return false;
141     }
142 
143     while ((ret = get(&actual, stream)) && (actual != value)) {
144         epoll_wait(epollFd, &event, 1, -1);
145     }
146 
147     HWAPI_RECORD(value, stream);
148     return ret;
149 }
150 
151 template <typename T>
record(const char * func,const T & value,const std::ios * stream)152 void HwApiBase::record(const char *func, const T &value, const std::ios *stream) {
153     std::lock_guard<std::mutex> lock(mRecordsMutex);
154     mRecords.emplace_back(std::make_unique<Record<T>>(func, value, stream));
155     mRecords.pop_front();
156 }
157 
158 template <typename T>
toString(const NamesMap & names)159 std::string HwApiBase::Record<T>::toString(const NamesMap &names) {
160     using utils::operator<<;
161     std::stringstream ret;
162 
163     ret << mFunc << " '" << names.at(mStream) << "' = '" << mValue << "'";
164 
165     return ret.str();
166 }
167 
168 class HwCalBase {
169   public:
170     HwCalBase();
171     void debug(int fd);
172 
173   protected:
174     template <typename T>
175     bool getProperty(const char *key, T *value, const T defval);
176     template <typename T>
177     bool getPersist(const char *key, T *value);
178 
179   private:
180     std::string mPropertyPrefix;
181     std::map<std::string, std::string> mCalData;
182 };
183 
184 template <typename T>
getProperty(const char * key,T * outval,const T defval)185 bool HwCalBase::getProperty(const char *key, T *outval, const T defval) {
186     ATRACE_NAME("HwCal::getProperty");
187     *outval = utils::getProperty(mPropertyPrefix + key, defval);
188     return true;
189 }
190 
191 template <typename T>
getPersist(const char * key,T * value)192 bool HwCalBase::getPersist(const char *key, T *value) {
193     ATRACE_NAME("HwCal::getPersist");
194     auto it = mCalData.find(key);
195     if (it == mCalData.end()) {
196         ALOGE("Missing %s config!", key);
197         return false;
198     }
199     std::stringstream stream{it->second};
200     utils::unpack(stream, value);
201     if (!stream || !stream.eof()) {
202         ALOGE("Invalid %s config!", key);
203         return false;
204     }
205     return true;
206 }
207 
208 }  // namespace vibrator
209 }  // namespace hardware
210 }  // namespace android
211 }  // namespace aidl
212