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