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()) {
35 ALOGE("Failed 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 std::ifstream calfile_dual;
75 auto propertyPrefix = std::getenv("PROPERTY_PREFIX");
76
77 if (propertyPrefix != NULL) {
78 mPropertyPrefix = std::string(propertyPrefix);
79 } else {
80 ALOGE("Failed get property prefix!");
81 }
82
83 utils::fileFromEnv("CALIBRATION_FILEPATH", &calfile);
84
85 for (std::string line; std::getline(calfile, line);) {
86 if (line.empty() || line[0] == '#') {
87 continue;
88 }
89 std::istringstream is_line(line);
90 std::string key, value;
91 if (std::getline(is_line, key, ':') && std::getline(is_line, value)) {
92 mCalData[utils::trim(key)] = utils::trim(value);
93 }
94 }
95
96 utils::fileFromEnv("CALIBRATION_FILEPATH_DUAL", &calfile_dual);
97
98 for (std::string line; std::getline(calfile_dual, line);) {
99 if (line.empty() || line[0] == '#') {
100 continue;
101 }
102 std::istringstream is_line(line);
103 std::string key, value;
104 if (std::getline(is_line, key, ':') && std::getline(is_line, value)) {
105 key = utils::trim(key) + "_dual";
106 mCalData[key] = utils::trim(value);
107 }
108 }
109 }
110
debug(int fd)111 void HwCalBase::debug(int fd) {
112 std::ifstream stream;
113 std::string path;
114 std::string line;
115 struct context {
116 HwCalBase *obj;
117 int fd;
118 } context{this, fd};
119
120 dprintf(fd, "Properties:\n");
121
122 property_list(
123 [](const char *key, const char *value, void *cookie) {
124 struct context *context = static_cast<struct context *>(cookie);
125 HwCalBase *obj = context->obj;
126 int fd = context->fd;
127 const std::string expect{obj->mPropertyPrefix};
128 const std::string actual{key, std::min(strlen(key), expect.size())};
129 if (actual == expect) {
130 dprintf(fd, " %s:\n", key);
131 dprintf(fd, " %s\n", value);
132 }
133 },
134 &context);
135
136 dprintf(fd, "\n");
137
138 dprintf(fd, "Persist:\n");
139
140 utils::fileFromEnv("CALIBRATION_FILEPATH", &stream, &path);
141
142 dprintf(fd, " %s:\n", path.c_str());
143 while (std::getline(stream, line)) {
144 dprintf(fd, " %s\n", line.c_str());
145 }
146 }
147
148 } // namespace vibrator
149 } // namespace hardware
150 } // namespace android
151 } // namespace aidl
152