ManifestHal.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include "ManifestHal.h"
  17. #include <unordered_set>
  18. #include "MapValueIterator.h"
  19. #include "parse_string.h"
  20. namespace android {
  21. namespace vintf {
  22. bool ManifestHal::isValid() const {
  23. std::unordered_set<size_t> existing;
  24. for (const auto &v : versions) {
  25. if (existing.find(v.majorVer) != existing.end()) {
  26. return false;
  27. }
  28. existing.insert(v.majorVer);
  29. }
  30. return transportArch.isValid();
  31. }
  32. bool ManifestHal::operator==(const ManifestHal &other) const {
  33. if (format != other.format)
  34. return false;
  35. if (name != other.name)
  36. return false;
  37. if (versions != other.versions)
  38. return false;
  39. if (!(transportArch == other.transportArch)) return false;
  40. if (interfaces != other.interfaces) return false;
  41. if (isOverride() != other.isOverride()) return false;
  42. if (mAdditionalInstances != other.mAdditionalInstances) return false;
  43. return true;
  44. }
  45. bool ManifestHal::forEachInstance(const std::function<bool(const ManifestInstance&)>& func) const {
  46. for (const auto& v : versions) {
  47. for (const auto& intf : iterateValues(interfaces)) {
  48. bool cont = intf.forEachInstance([&](const auto& interface, const auto& instance,
  49. bool /* isRegex */) {
  50. // TODO(b/73556059): Store ManifestInstance as well to avoid creating temps
  51. FqInstance fqInstance;
  52. if (fqInstance.setTo(getName(), v.majorVer, v.minorVer, interface, instance)) {
  53. if (!func(ManifestInstance(std::move(fqInstance), TransportArch{transportArch},
  54. format))) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. });
  60. if (!cont) {
  61. return false;
  62. }
  63. }
  64. }
  65. for (const auto& manifestInstance : mAdditionalInstances) {
  66. if (!func(manifestInstance)) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. bool ManifestHal::isDisabledHal() const {
  73. if (!isOverride()) return false;
  74. bool hasInstance = false;
  75. forEachInstance([&hasInstance](const auto&) {
  76. hasInstance = true;
  77. return false; // has at least one instance, stop here.
  78. });
  79. return !hasInstance;
  80. }
  81. void ManifestHal::appendAllVersions(std::set<Version>* ret) const {
  82. ret->insert(versions.begin(), versions.end());
  83. forEachInstance([&](const auto& e) {
  84. ret->insert(e.version());
  85. return true;
  86. });
  87. }
  88. bool ManifestHal::verifyInstance(const FqInstance& fqInstance, std::string* error) const {
  89. if (fqInstance.hasPackage() && fqInstance.getPackage() != this->getName()) {
  90. if (error) {
  91. *error = "Should not add \"" + fqInstance.string() + "\" to a HAL with name " +
  92. this->getName();
  93. }
  94. return false;
  95. }
  96. if (!fqInstance.hasVersion()) {
  97. if (error) *error = "Should specify version: \"" + fqInstance.string() + "\"";
  98. return false;
  99. }
  100. if (!fqInstance.hasInterface()) {
  101. if (error) *error = "Should specify interface: \"" + fqInstance.string() + "\"";
  102. return false;
  103. }
  104. if (!fqInstance.hasInstance()) {
  105. if (error) *error = "Should specify instance: \"" + fqInstance.string() + "\"";
  106. return false;
  107. }
  108. return true;
  109. }
  110. bool ManifestHal::insertInstances(const std::set<FqInstance>& fqInstances, std::string* error) {
  111. for (const FqInstance& e : fqInstances) {
  112. if (!insertInstance(e, error)) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. bool ManifestHal::insertInstance(const FqInstance& e, std::string* error) {
  119. if (!verifyInstance(e, error)) {
  120. return false;
  121. }
  122. size_t minorVer = e.getMinorVersion();
  123. for (auto it = mAdditionalInstances.begin(); it != mAdditionalInstances.end();) {
  124. if (it->version().majorVer == e.getMajorVersion() && it->interface() == e.getInterface() &&
  125. it->instance() == e.getInstance()) {
  126. minorVer = std::max(minorVer, it->version().minorVer);
  127. it = mAdditionalInstances.erase(it);
  128. } else {
  129. ++it;
  130. }
  131. }
  132. FqInstance toAdd;
  133. if (!toAdd.setTo(this->getName(), e.getMajorVersion(), minorVer, e.getInterface(),
  134. e.getInstance())) {
  135. if (error) {
  136. *error = "Cannot create FqInstance with package='" + this->getName() + "', version='" +
  137. to_string(Version(e.getMajorVersion(), minorVer)) + "', interface='" +
  138. e.getInterface() + "', instance='" + e.getInstance() + "'";
  139. }
  140. return false;
  141. }
  142. mAdditionalInstances.emplace(std::move(toAdd), this->transportArch, this->format);
  143. return true;
  144. }
  145. } // namespace vintf
  146. } // namespace android