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
17 #include "HardwareBase.h"
18
19 #include <cutils/properties.h>
20 #include <log/log.h>
21
22 #include <fstream>
23 #include <sstream>
24
25 #include "utils.h"
26
27 namespace aidl {
28 namespace android {
29 namespace hardware {
30 namespace vibrator {
31
HwApiBase()32 HwApiBase::HwApiBase() {
33 mPathPrefix = std::getenv("HWAPI_PATH_PREFIX") ?: "";
34 if (mPathPrefix.empty() && (std::getenv("INPUT_EVENT_NAME") == NULL)) {
35 ALOGE("Failed to get HWAPI path prefix!");
36 }
37 }
38
saveName(const std::string & name,const std::ios * stream)39 void HwApiBase::saveName(const std::string &name, const std::ios *stream) {
40 mNames[stream] = name;
41 }
42
has(const std::ios & stream)43 bool HwApiBase::has(const std::ios &stream) {
44 return !!stream;
45 }
46
debug(int fd)47 void HwApiBase::debug(int fd) {
48 dprintf(fd, "Kernel:\n");
49
50 for (auto &entry : utils::pathsFromEnv("HWAPI_DEBUG_PATHS", mPathPrefix)) {
51 auto &path = entry.first;
52 auto &stream = entry.second;
53 std::string line;
54
55 dprintf(fd, " %s:\n", path.c_str());
56 while (std::getline(stream, line)) {
57 dprintf(fd, " %s\n", line.c_str());
58 }
59 }
60
61 mRecordsMutex.lock();
62 dprintf(fd, " Records:\n");
63 for (auto &r : mRecords) {
64 if (r == nullptr) {
65 continue;
66 }
67 dprintf(fd, " %s\n", r->toString(mNames).c_str());
68 }
69 mRecordsMutex.unlock();
70 }
71
HwCalBase()72 HwCalBase::HwCalBase() {
73 std::ifstream calfile;
74 auto propertyPrefix = std::getenv("PROPERTY_PREFIX");
75
76 if (propertyPrefix != NULL) {
77 mPropertyPrefix = std::string(propertyPrefix);
78 } else {
79 ALOGE("Failed get property prefix!");
80 }
81
82 utils::fileFromEnv("CALIBRATION_FILEPATH", &calfile);
83
84 for (std::string line; std::getline(calfile, line);) {
85 if (line.empty() || line[0] == '#') {
86 continue;
87 }
88 std::istringstream is_line(line);
89 std::string key, value;
90 if (std::getline(is_line, key, ':') && std::getline(is_line, value)) {
91 mCalData[utils::trim(key)] = utils::trim(value);
92 }
93 }
94 }
95
debug(int fd)96 void HwCalBase::debug(int fd) {
97 std::ifstream stream;
98 std::string path;
99 std::string line;
100 struct context {
101 HwCalBase *obj;
102 int fd;
103 } context{this, fd};
104
105 dprintf(fd, "Properties:\n");
106
107 property_list(
108 [](const char *key, const char *value, void *cookie) {
109 struct context *context = static_cast<struct context *>(cookie);
110 HwCalBase *obj = context->obj;
111 int fd = context->fd;
112 const std::string expect{obj->mPropertyPrefix};
113 const std::string actual{key, std::min(strlen(key), expect.size())};
114 if (actual == expect) {
115 dprintf(fd, " %s:\n", key);
116 dprintf(fd, " %s\n", value);
117 }
118 },
119 &context);
120
121 dprintf(fd, "\n");
122
123 dprintf(fd, "Persist:\n");
124
125 utils::fileFromEnv("CALIBRATION_FILEPATH", &stream, &path);
126
127 dprintf(fd, " %s:\n", path.c_str());
128 while (std::getline(stream, line)) {
129 dprintf(fd, " %s\n", line.c_str());
130 }
131 }
132
133 } // namespace vibrator
134 } // namespace hardware
135 } // namespace android
136 } // namespace aidl
137