/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_VINTF_VINTF_OBJECT_H_ #define ANDROID_VINTF_VINTF_OBJECT_H_ #include #include #include #include #include #include #include #include #include "CheckFlags.h" #include "CompatibilityMatrix.h" #include "FileSystem.h" #include "HalManifest.h" #include "Level.h" #include "ObjectFactory.h" #include "PropertyFetcher.h" #include "RuntimeInfo.h" namespace android { namespace vintf { namespace details { class VintfObjectAfterUpdate; template struct LockedSharedPtr { std::shared_ptr object; std::mutex mutex; bool fetchedOnce = false; }; struct LockedRuntimeInfoCache { std::shared_ptr object; std::mutex mutex; RuntimeInfo::FetchFlags fetchedFlags = RuntimeInfo::FetchFlag::NONE; }; } // namespace details namespace testing { class VintfObjectTestBase; class VintfObjectRuntimeInfoTest; class VintfObjectCompatibleTest; } // namespace testing /* * The top level class for libvintf. * An overall diagram of the public API: * VintfObject * + GetDeviceHalManfiest * | + getHidlTransport * | + checkCompatibility * + GetFrameworkHalManifest * | + getHidlTransport * | + checkCompatibility * + GetRuntimeInfo * + checkCompatibility * * Each of the function gathers all information and encapsulate it into the object. * If no error, it return the same singleton object in the future, and the HAL manifest * file won't be touched again. * If any error, nullptr is returned, and Get will try to parse the HAL manifest * again when it is called again. * All these operations are thread-safe. * If skipCache, always skip the cache in memory and read the files / get runtime information * again from the device. */ class VintfObject { public: virtual ~VintfObject() = default; /* * Return the API that access the device-side HAL manifests built from component pieces on the * vendor partition. */ virtual std::shared_ptr getDeviceHalManifest(); /* * Return the API that access the framework-side HAL manifest built from component pieces on the * system partition. */ virtual std::shared_ptr getFrameworkHalManifest(); /* * Return the API that access the device-side compatibility matrix built from component pieces * on the vendor partition. */ virtual std::shared_ptr getDeviceCompatibilityMatrix(); /* * Return the API that access the framework-side compatibility matrix built from component * pieces on the system partition. * * This automatically selects the right compatibility matrix according to the target-level * specified by the device. */ virtual std::shared_ptr getFrameworkCompatibilityMatrix(); /* * Return the API that access device runtime info. * * {skipCache == true, flags == ALL}: re-fetch everything * {skipCache == false, flags == ALL}: fetch everything if not previously fetched * {skipCache == true, flags == selected info}: re-fetch selected information * if not previously fetched. * {skipCache == false, flags == selected info}: fetch selected information * if not previously fetched. * * @param skipCache do not fetch if previously fetched * @param flags bitwise-or of RuntimeInfo::FetchFlag */ std::shared_ptr getRuntimeInfo( RuntimeInfo::FetchFlags flags = RuntimeInfo::FetchFlag::ALL); /** * Check compatibility on the device. * * @param error error message * @param flags flags to disable certain checks. See CheckFlags. * * @return = 0 if success (compatible) * > 0 if incompatible * < 0 if any error (mount partition fails, illformed XML, etc.) */ int32_t checkCompatibility(std::string* error = nullptr, CheckFlags::Type flags = CheckFlags::DEFAULT); /** * A std::function that abstracts a list of "provided" instance names. Given package, version * and interface, the function returns a list of instance names that matches. * This function can represent a manifest, an IServiceManager, etc. * If the source is passthrough service manager, a list of instance names cannot be provided. * Instead, the function should call getService on each of the "hintInstances", and * return those instances for which getService does not return a nullptr. This means that for * passthrough HALs, the deprecation on s cannot be enforced; only s * can be enforced. */ using ListInstances = std::function>( const std::string& package, Version version, const std::string& interface, const std::vector& hintInstances)>; /** * Check deprecation on framework matrices with a provided predicate. * * @param listInstances predicate that takes parameter in this format: * android.hardware.foo@1.0::IFoo * and returns {{"default", version}...} if HAL is in use, where version = * first version in interfaceChain where package + major version matches. * * @return = 0 if success (no deprecated HALs) * > 0 if there is at least one deprecated HAL * < 0 if any error (mount partition fails, illformed XML, etc.) */ int32_t checkDeprecation(const ListInstances& listInstances, const std::vector& hidlMetadata, std::string* error = nullptr); /** * Check deprecation on existing VINTF metadata. Use Device Manifest as the * predicate to check if a HAL is in use. * * @return = 0 if success (no deprecated HALs) * > 0 if there is at least one deprecated HAL * < 0 if any error (mount partition fails, illformed XML, etc.) */ int32_t checkDeprecation(const std::vector& hidlMetadata, std::string* error = nullptr); /** * Return kernel FCM version. * * If any error, UNSPECIFIED is returned, and error is set to an error message. */ Level getKernelLevel(std::string* error = nullptr); /** * Returns true if the framework compatibility matrix has extensions. In * other words, returns true if any of the following exists on the device: * - device framework compatibility matrix * - product framework compatibility matrix * - system_ext framework compatibility matrix * * Return result: * - true if framework compatibility matrix has extensions * - false if framework compatibility * matrix does not have extensions. * - !result.has_value() if any error. Check * result.error() for detailed message. */ android::base::Result hasFrameworkCompatibilityMatrixExtensions(); /** * Check that there are no unused HALs in HAL manifests. Currently, only * device manifest is checked against framework compatibility matrix. * * Return result: * - result.ok() if no unused HALs * - !result.ok() && result.error().code() == 0 if with unused HALs. Check * result.error() for detailed message. * - !result.ok() && result.error().code() != 0 if any error. Check * result.error() for detailed message. */ android::base::Result checkUnusedHals( const std::vector& hidlMetadata); private: std::unique_ptr mFileSystem; std::unique_ptr> mRuntimeInfoFactory; std::unique_ptr mPropertyFetcher; details::LockedSharedPtr mDeviceManifest; details::LockedSharedPtr mFrameworkManifest; details::LockedSharedPtr mDeviceMatrix; // Parent lock of the following fields. It should be acquired before locking the child locks. std::mutex mFrameworkCompatibilityMatrixMutex; details::LockedSharedPtr mFrameworkMatrix; details::LockedSharedPtr mCombinedFrameworkMatrix; // End of mFrameworkCompatibilityMatrixMutex details::LockedRuntimeInfoCache mDeviceRuntimeInfo; // Expose functions for testing and recovery friend class testing::VintfObjectTestBase; friend class testing::VintfObjectRuntimeInfoTest; friend class testing::VintfObjectCompatibleTest; // Expose functions to simulate dependency injection. friend class details::VintfObjectAfterUpdate; protected: virtual const std::unique_ptr& getFileSystem(); virtual const std::unique_ptr& getPropertyFetcher(); virtual const std::unique_ptr>& getRuntimeInfoFactory(); public: /* * Get global instance. By default, this fetches from root and cache results, * unless skipCache is specified. */ static std::shared_ptr GetInstance(); // Static variants of member functions. /* * Return the API that access the device-side HAL manifest built from component pieces on the * vendor partition. */ static std::shared_ptr GetDeviceHalManifest(); /* * Return the API that access the framework-side HAL manifest built from component pieces on the * system partition. */ static std::shared_ptr GetFrameworkHalManifest(); /* * Return the API that access the device-side compatibility matrix built from component pieces * on the vendor partition. */ static std::shared_ptr GetDeviceCompatibilityMatrix(); /* * Return the API that access the framework-side compatibility matrix built from component * pieces on the system partition. */ static std::shared_ptr GetFrameworkCompatibilityMatrix(); /* * Return the API that access device runtime info. * * {skipCache == true, flags == ALL}: re-fetch everything * {skipCache == false, flags == ALL}: fetch everything if not previously fetched * {skipCache == true, flags == selected info}: re-fetch selected information * if not previously fetched. * {skipCache == false, flags == selected info}: fetch selected information * if not previously fetched. * * @param skipCache do not fetch if previously fetched * @param flags bitwise-or of RuntimeInfo::FetchFlag */ static std::shared_ptr GetRuntimeInfo( RuntimeInfo::FetchFlags flags = RuntimeInfo::FetchFlag::ALL); private: status_t getCombinedFrameworkMatrix(const std::shared_ptr& deviceManifest, CompatibilityMatrix* out, std::string* error = nullptr); status_t getAllFrameworkMatrixLevels(std::vector* out, std::string* error = nullptr); status_t getOneMatrix(const std::string& path, CompatibilityMatrix* out, std::string* error = nullptr); status_t addDirectoryManifests(const std::string& directory, HalManifest* manifests, std::string* error = nullptr); status_t fetchDeviceHalManifest(HalManifest* out, std::string* error = nullptr); status_t fetchDeviceMatrix(CompatibilityMatrix* out, std::string* error = nullptr); status_t fetchOdmHalManifest(HalManifest* out, std::string* error = nullptr); status_t fetchOneHalManifest(const std::string& path, HalManifest* out, std::string* error = nullptr); status_t fetchVendorHalManifest(HalManifest* out, std::string* error = nullptr); status_t fetchFrameworkHalManifest(HalManifest* out, std::string* error = nullptr); using ChildrenMap = std::multimap; static bool IsHalDeprecated(const MatrixHal& oldMatrixHal, const CompatibilityMatrix& targetMatrix, const ListInstances& listInstances, const ChildrenMap& childrenMap, std::string* appendedError); static bool IsInstanceDeprecated(const MatrixInstance& oldMatrixInstance, const CompatibilityMatrix& targetMatrix, const ListInstances& listInstances, const ChildrenMap& childrenMap, std::string* appendedError); static android::base::Result> GetListedInstanceInheritance( const std::string& package, const Version& version, const std::string& interface, const std::string& instance, const ListInstances& listInstances, const ChildrenMap& childrenMap); static bool IsInstanceListed(const ListInstances& listInstances, const FqInstance& fqInstance); static android::base::Result IsFqInstanceDeprecated( const CompatibilityMatrix& targetMatrix, HalFormat format, const FqInstance& fqInstance, const ListInstances& listInstances); public: /** * Builder of VintfObject. If a dependency is not specified, the default behavior is used. * - FileSystem fetch from "/" for target and fetch no files for host * - ObjectFactory fetches default RuntimeInfo for target and nothing for host * - PropertyFetcher fetches properties for target and nothing for host */ class Builder { public: Builder(); Builder& setFileSystem(std::unique_ptr&&); Builder& setRuntimeInfoFactory(std::unique_ptr>&&); Builder& setPropertyFetcher(std::unique_ptr&&); std::unique_ptr build(); private: std::unique_ptr mObject; }; private: /* Empty VintfObject without any dependencies. Used by Builder. */ VintfObject() = default; }; enum : int32_t { COMPATIBLE = 0, INCOMPATIBLE = 1, NO_DEPRECATED_HALS = 0, DEPRECATED = 1, }; // exposed for testing. namespace details { extern const std::string kSystemVintfDir; extern const std::string kVendorVintfDir; extern const std::string kOdmVintfDir; extern const std::string kProductVintfDir; extern const std::string kSystemExtVintfDir; extern const std::string kOdmLegacyVintfDir; extern const std::string kOdmLegacyManifest; extern const std::string kVendorManifest; extern const std::string kSystemManifest; extern const std::string kVendorMatrix; extern const std::string kOdmManifest; extern const std::string kProductMatrix; extern const std::string kProductManifest; extern const std::string kSystemExtManifest; extern const std::string kVendorManifestFragmentDir; extern const std::string kSystemManifestFragmentDir; extern const std::string kOdmManifestFragmentDir; extern const std::string kProductManifestFragmentDir; extern const std::string kSystemExtManifestFragmentDir; extern const std::string kVendorLegacyManifest; extern const std::string kVendorLegacyMatrix; extern const std::string kSystemLegacyManifest; extern const std::string kSystemLegacyMatrix; // Convenience function to dump all files and directories that could be read // by calling Get(Framework|Device)(HalManifest|CompatibilityMatrix). The list // include files that may not actually be read when the four functions are called // because some files have a higher priority than others. The list does NOT // include "files" (including kernel interfaces) that are read when GetRuntimeInfo // is called. std::vector dumpFileList(); } // namespace details } // namespace vintf } // namespace android #endif // ANDROID_VINTF_VINTF_OBJECT_H_