HalManifest.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 ANDROID_VINTF_HAL_MANIFEST_H
  17. #define ANDROID_VINTF_HAL_MANIFEST_H
  18. #include <utils/Errors.h>
  19. #include <map>
  20. #include <optional>
  21. #include <string>
  22. #include <vector>
  23. #include "FileSystem.h"
  24. #include "HalGroup.h"
  25. #include "KernelInfo.h"
  26. #include "Level.h"
  27. #include "ManifestHal.h"
  28. #include "ManifestInstance.h"
  29. #include "MapValueIterator.h"
  30. #include "SchemaType.h"
  31. #include "SystemSdk.h"
  32. #include "VendorNdk.h"
  33. #include "Version.h"
  34. #include "Vndk.h"
  35. #include "XmlFileGroup.h"
  36. namespace android {
  37. namespace vintf {
  38. struct MatrixHal;
  39. struct CompatibilityMatrix;
  40. namespace details {
  41. using InstancesOfVersion =
  42. std::map<std::string /* interface */, std::set<std::string /* instance */>>;
  43. using Instances = std::map<Version, InstancesOfVersion>;
  44. } // namespace details
  45. // A HalManifest is reported by the hardware and query-able from
  46. // framework code. This is the API for the framework.
  47. struct HalManifest : public HalGroup<ManifestHal>, public XmlFileGroup<ManifestXmlFile> {
  48. public:
  49. // Construct a device HAL manifest.
  50. HalManifest() : mType(SchemaType::DEVICE) {}
  51. bool add(ManifestHal&& hal) override;
  52. // Given a component name (e.g. "android.hardware.camera"),
  53. // return getHal(name)->transport if the component exist and v exactly matches
  54. // one of the versions in that component, else EMPTY
  55. Transport getTransport(const std::string &name, const Version &v,
  56. const std::string &interfaceName, const std::string &instanceName) const;
  57. // Check compatibility against a compatibility matrix. Considered compatible if
  58. // - framework manifest vs. device compat-mat
  59. // - checkIncompatibility for HALs returns only optional HALs
  60. // - one of manifest.vndk match compat-mat.vndk
  61. // - device manifest vs. framework compat-mat
  62. // - checkIncompatibility for HALs returns only optional HALs
  63. // - manifest.sepolicy.version match one of compat-mat.sepolicy.sepolicy-version
  64. bool checkCompatibility(const CompatibilityMatrix &mat, std::string *error = nullptr) const;
  65. // Generate a compatibility matrix such that checkCompatibility will return true.
  66. CompatibilityMatrix generateCompatibleMatrix() const;
  67. // Returns all component names.
  68. std::set<std::string> getHalNames() const;
  69. // Returns all component names and versions, e.g.
  70. // "[email protected]", "[email protected]",
  71. // "[email protected]"]
  72. std::set<std::string> getHalNamesAndVersions() const;
  73. // Type of the manifest. FRAMEWORK or DEVICE.
  74. SchemaType type() const;
  75. void setType(SchemaType type);
  76. // FCM version that it implements.
  77. Level level() const;
  78. // device.mSepolicyVersion. Assume type == device.
  79. // Abort if type != device.
  80. const Version &sepolicyVersion() const;
  81. // framework.mVendorNdks. Assume type == framework.
  82. // Abort if type != framework.
  83. const std::vector<VendorNdk>& vendorNdks() const;
  84. // If the corresponding <xmlfile> with the given version exists,
  85. // - Return the overridden <path> if it is present,
  86. // - otherwise the default value: /{system,vendor}/etc/<name>_V<major>_<minor>.xml
  87. // Otherwise if the <xmlfile> entry does not exist, "" is returned.
  88. std::string getXmlFilePath(const std::string& xmlFileName, const Version& version) const;
  89. // Get metaversion of this manifest.
  90. Version getMetaVersion() const;
  91. bool forEachInstanceOfVersion(
  92. const std::string& package, const Version& expectVersion,
  93. const std::function<bool(const ManifestInstance&)>& func) const override;
  94. // Alternative to forEachInstance if you just need a set of instance names instead.
  95. std::set<std::string> getInstances(const std::string& halName, const Version& version,
  96. const std::string& interfaceName) const;
  97. // Return whether instance is in getInstances(...).
  98. bool hasInstance(const std::string& halName, const Version& version,
  99. const std::string& interfaceName, const std::string& instance) const;
  100. // Insert the given instance. After inserting it, the instance will be available via
  101. // forEachInstance* functions. This modifies the manifest.
  102. // Return whether this operation is successful.
  103. bool insertInstance(const FqInstance& fqInstance, Transport transport, Arch arch, HalFormat fmt,
  104. std::string* error = nullptr);
  105. // Get the <kernel> tag. Assumes type() == DEVICE.
  106. const std::optional<KernelInfo>& kernel() const;
  107. // Add everything from another manifest. If no errors (return true), it is guaranteed
  108. // that other->empty() == true after execution.
  109. [[nodiscard]] bool addAll(HalManifest* other, std::string* error = nullptr);
  110. protected:
  111. // Check before add()
  112. bool shouldAdd(const ManifestHal& toAdd) const override;
  113. bool shouldAddXmlFile(const ManifestXmlFile& toAdd) const override;
  114. private:
  115. friend struct HalManifestConverter;
  116. friend class VintfObject;
  117. friend class AssembleVintfImpl;
  118. friend struct LibVintfTest;
  119. friend std::string dump(const HalManifest &vm);
  120. friend bool operator==(const HalManifest &lft, const HalManifest &rgt);
  121. status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
  122. std::string* error = nullptr);
  123. details::Instances expandInstances(const std::string& name) const;
  124. // Check if all instances in matrixHal is supported in this manifest.
  125. bool isCompatible(const details::Instances& instances, const MatrixHal& matrixHal) const;
  126. // Return a list of error messages (for each <hal> name) that does NOT conform to
  127. // the given compatibility matrix. It does not contain components that are optional.
  128. // That is, return empty list iff
  129. // (instance in matrix) => (instance in manifest).
  130. std::vector<std::string> checkIncompatibleHals(const CompatibilityMatrix& mat) const;
  131. void removeHals(const std::string& name, size_t majorVer);
  132. // Returns a list of instance names that are in this manifest but
  133. // are not specified in the given matrix, whether the HAL is specified as an optional or
  134. // required HAL.
  135. // That is, return empty list iff
  136. // (instance in manifest) => (instance in matrix).
  137. std::set<std::string> checkUnusedHals(const CompatibilityMatrix& mat) const;
  138. // Check that manifest has no entries.
  139. bool empty() const;
  140. SchemaType mType;
  141. Level mLevel = Level::UNSPECIFIED;
  142. // version attribute. Default is 1.0 for manifests created programatically.
  143. Version mMetaVersion{1, 0};
  144. // entries for device hal manifest only
  145. struct {
  146. Version mSepolicyVersion;
  147. std::optional<KernelInfo> mKernel;
  148. } device;
  149. // entries for framework hal manifest only
  150. struct {
  151. #pragma clang diagnostic push
  152. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  153. std::vector<Vndk> mVndks;
  154. #pragma clang diagnostic pop
  155. std::vector<VendorNdk> mVendorNdks;
  156. SystemSdk mSystemSdk;
  157. } framework;
  158. };
  159. } // namespace vintf
  160. } // namespace android
  161. #endif // ANDROID_VINTF_HAL_MANIFEST_H