| Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame] | 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 |  | 
|  | 17 | #include "interface_utils.h" | 
|  | 18 |  | 
|  | 19 | #include <fstream> | 
|  | 20 | #include <sstream> | 
|  | 21 |  | 
|  | 22 | #include <android-base/strings.h> | 
|  | 23 | #include <hidl-util/FqInstance.h> | 
|  | 24 | #include <json/json.h> | 
|  | 25 |  | 
|  | 26 | using android::FqInstance; | 
|  | 27 | using android::FQName; | 
|  | 28 | using android::base::Error; | 
|  | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 | namespace init { | 
|  | 32 |  | 
|  | 33 | namespace { | 
|  | 34 |  | 
|  | 35 | std::string FQNamesToString(const std::set<FQName>& fqnames) { | 
|  | 36 | std::set<std::string> fqname_strings; | 
|  | 37 | for (const FQName& fqname : fqnames) { | 
|  | 38 | fqname_strings.insert(fqname.string()); | 
|  | 39 | } | 
|  | 40 | return android::base::Join(fqname_strings, " "); | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | }  // namespace | 
|  | 44 |  | 
|  | 45 | Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy( | 
|  | 46 | const std::string& path) { | 
|  | 47 | Json::Value root; | 
|  | 48 | Json::Reader reader; | 
|  | 49 | std::ifstream stream(path); | 
|  | 50 | if (!reader.parse(stream, root)) { | 
|  | 51 | return Error() << "Failed to read interface inheritance hierarchy file: " << path << "\n" | 
|  | 52 | << reader.getFormattedErrorMessages(); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | InterfaceInheritanceHierarchyMap result; | 
|  | 56 | for (const Json::Value& entry : root) { | 
|  | 57 | std::set<FQName> inherited_interfaces; | 
|  | 58 | for (const Json::Value& intf : entry["inheritedInterfaces"]) { | 
|  | 59 | FQName fqname; | 
|  | 60 | if (!fqname.setTo(intf.asString())) { | 
|  | 61 | return Error() << "Unable to parse interface '" << intf.asString() << "'"; | 
|  | 62 | } | 
|  | 63 | inherited_interfaces.insert(fqname); | 
|  | 64 | } | 
|  | 65 | std::string intf_string = entry["interface"].asString(); | 
|  | 66 | FQName fqname; | 
|  | 67 | if (!fqname.setTo(intf_string)) { | 
|  | 68 | return Error() << "Unable to parse interface '" << intf_string << "'"; | 
|  | 69 | } | 
|  | 70 | result[fqname] = inherited_interfaces; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | return result; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | Result<void> CheckInterfaceInheritanceHierarchy(const std::set<std::string>& instances, | 
|  | 77 | const InterfaceInheritanceHierarchyMap& hierarchy) { | 
|  | 78 | std::set<FQName> interface_fqnames; | 
|  | 79 | for (const std::string& instance : instances) { | 
|  | 80 | FqInstance fqinstance; | 
|  | 81 | if (!fqinstance.setTo(instance)) { | 
|  | 82 | return Error() << "Unable to parse interface instance '" << instance << "'"; | 
|  | 83 | } | 
|  | 84 | interface_fqnames.insert(fqinstance.getFqName()); | 
|  | 85 | } | 
|  | 86 | return CheckInterfaceInheritanceHierarchy(interface_fqnames, hierarchy); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | Result<void> CheckInterfaceInheritanceHierarchy(const std::set<FQName>& interfaces, | 
|  | 90 | const InterfaceInheritanceHierarchyMap& hierarchy) { | 
|  | 91 | std::ostringstream error_stream; | 
|  | 92 | for (const FQName& intf : interfaces) { | 
|  | 93 | if (hierarchy.count(intf) == 0) { | 
|  | 94 | error_stream << "\nInterface is not in the known set of hidl_interfaces: '" | 
|  | 95 | << intf.string() | 
|  | 96 | << "'. Please ensure the interface is spelled correctly and built " | 
|  | 97 | << "by a hidl_interface target."; | 
|  | 98 | continue; | 
|  | 99 | } | 
|  | 100 | const std::set<FQName>& required_interfaces = hierarchy.at(intf); | 
|  | 101 | std::set<FQName> diff; | 
|  | 102 | std::set_difference(required_interfaces.begin(), required_interfaces.end(), | 
|  | 103 | interfaces.begin(), interfaces.end(), | 
|  | 104 | std::inserter(diff, diff.begin())); | 
|  | 105 | if (!diff.empty()) { | 
|  | 106 | error_stream << "\nInterface '" << intf.string() << "' requires its full inheritance " | 
|  | 107 | << "hierarchy to be listed in this init_rc file. Missing " | 
|  | 108 | << "interfaces: [" << FQNamesToString(diff) << "]"; | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 | const std::string& errors = error_stream.str(); | 
|  | 112 | if (!errors.empty()) { | 
|  | 113 | return Error() << errors; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | return {}; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | std::optional<std::set<FQName>> known_interfaces; | 
|  | 120 |  | 
|  | 121 | void SetKnownInterfaces(const InterfaceInheritanceHierarchyMap& hierarchy) { | 
|  | 122 | known_interfaces = std::set<FQName>(); | 
|  | 123 | for (const auto& [intf, inherited_interfaces] : hierarchy) { | 
|  | 124 | known_interfaces->insert(intf); | 
|  | 125 | } | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | Result<void> IsKnownInterface(const std::string& instance) { | 
|  | 129 | FqInstance fqinstance; | 
|  | 130 | if (!fqinstance.setTo(instance)) { | 
|  | 131 | return Error() << "Unable to parse interface instance '" << instance << "'"; | 
|  | 132 | } | 
|  | 133 | return IsKnownInterface(fqinstance.getFqName()); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | Result<void> IsKnownInterface(const FQName& intf) { | 
|  | 137 | if (!known_interfaces) { | 
|  | 138 | return Error() << "No known interfaces have been loaded."; | 
|  | 139 | } | 
|  | 140 | if (known_interfaces->count(intf) == 0) { | 
|  | 141 | return Error() << "Interface is not in the known set of hidl_interfaces: '" << intf.string() | 
|  | 142 | << "'. Please ensure the interface is spelled correctly and built " | 
|  | 143 | << "by a hidl_interface target."; | 
|  | 144 | } | 
|  | 145 | return {}; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | }  // namespace init | 
|  | 149 | }  // namespace android |