HardwareBase.cpp 3.6 KB

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