utils.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2017 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. #ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_UTILS_H_
  17. #define FRAMEWORK_NATIVE_CMDS_LSHAL_UTILS_H_
  18. #include <iomanip>
  19. #include <iostream>
  20. #include <string>
  21. #include <sstream>
  22. #include <utility>
  23. #include <vector>
  24. namespace android {
  25. namespace lshal {
  26. enum : unsigned int {
  27. OK = 0,
  28. // Return to Lshal::main to print help info.
  29. USAGE = 1 << 0,
  30. // no service managers
  31. NO_BINDERIZED_MANAGER = 1 << 1,
  32. NO_PASSTHROUGH_MANAGER = 1 << 2,
  33. // general error in getting information from the three sources
  34. DUMP_BINDERIZED_ERROR = 1 << 3,
  35. DUMP_PASSTHROUGH_ERROR = 1 << 4,
  36. DUMP_ALL_LIBS_ERROR = 1 << 5,
  37. // I/O error in reading files
  38. IO_ERROR = 1 << 6,
  39. // Interface does not exist (IServiceManager::get fails)
  40. NO_INTERFACE = 1 << 7,
  41. // Transaction error from hwbinder transactions
  42. TRANSACTION_ERROR = 1 << 8,
  43. // No transaction error, but return value is unexpected.
  44. BAD_IMPL = 1 << 9,
  45. // Cannot fetch VINTF data.
  46. VINTF_ERROR = 1 << 10,
  47. };
  48. using Status = unsigned int;
  49. struct Arg {
  50. int argc;
  51. char **argv;
  52. };
  53. template <typename A>
  54. std::string join(const A &components, const std::string &separator) {
  55. std::stringstream out;
  56. bool first = true;
  57. for (const auto &component : components) {
  58. if (!first) {
  59. out << separator;
  60. }
  61. out << component;
  62. first = false;
  63. }
  64. return out.str();
  65. }
  66. std::string toHexString(uint64_t t);
  67. template<typename String>
  68. std::pair<String, String> splitFirst(const String &s, char c) {
  69. const char *pos = strchr(s.c_str(), c);
  70. if (pos == nullptr) {
  71. return {s, {}};
  72. }
  73. return {String(s.c_str(), pos - s.c_str()), String(pos + 1)};
  74. }
  75. std::vector<std::string> split(const std::string &s, char c);
  76. void replaceAll(std::string *s, char from, char to);
  77. } // namespace lshal
  78. } // namespace android
  79. #endif // FRAMEWORK_NATIVE_CMDS_LSHAL_UTILS_H_