1 /* 2 * Copyright 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 17 #ifndef __VTS_DRIVER_HAL_VTSHALDRIVERMANAGER_H 18 #define __VTS_DRIVER_HAL_VTSHALDRIVERMANAGER_H 19 20 #include <map> 21 #include <string> 22 23 #include <resource_manager/VtsResourceManager.h> 24 #include "component_loader/HalDriverLoader.h" 25 #include "driver_base/DriverBase.h" 26 #include "test/vts/proto/ComponentSpecificationMessage.pb.h" 27 28 using namespace std; 29 using DriverId = int32_t; 30 31 namespace android { 32 namespace vts { 33 34 class VtsHalDriverManager { 35 public: 36 // Constructor where the first argument is the path of a dir which contains 37 // all available interface specification files. 38 VtsHalDriverManager(const string& spec_dir, const int epoch_count, 39 const string& callback_socket_name, 40 VtsResourceManager* resource_manager); 41 42 // Loads the driver library for the target HAL, creates the corresponding 43 // driver instance, assign it a driver id and registers the created driver 44 // instance in hal_driver_map_. 45 // Returns the generated driver id. 46 // Args: 47 // version_major: int, hal major version, e.g. 1.0 -> 1. 48 // version_minor: int, hal minor version, e.g. 1.0 -> 0. 49 // 50 DriverId LoadTargetComponent(const string& dll_file_name, 51 const string& spec_lib_file_path, 52 const int component_class, 53 const int component_type, 54 const int version_major, const int version_minor, 55 const string& package_name, 56 const string& component_name, 57 const string& hw_binder_service_name); 58 59 // Call the API specified in func_msg with the provided parameter using the 60 // the corresonding driver instance. If func_msg specified the driver_id, 61 // use the driver instance corresponds to driver_id, otherwise, use the 62 // default driver instance (with driver_id = 0). 63 // Returns a string which contians the return results (a text format of the 64 // returned protobuf). 65 // For error cases, returns string "error"; 66 // TODO (zhuoyao): use FunctionCallMessage instead of 67 // FunctionSpecificationMessage which contains info such as component name and 68 // driver id. 69 string CallFunction(FunctionCallMessage* func_msg); 70 71 // Searches hal_driver_map_ for Hidl HAL driver instance with the given 72 // package name, version and component (interface) name. If found, returns 73 // the correponding driver instance, otherwise, creates a new driver instance 74 // with the given info, registers it in hal_driver_map_ and returns the 75 // generated driver instance. This is used by VTS replay test. 76 // Args: 77 // version_major: int, hal major version, e.g. 1.0 -> 1. 78 // version_minor: int, hal minor version, e.g. 1.0 -> 0. 79 // 80 DriverId GetDriverIdForHidlHalInterface(const string& package_name, 81 const int version_major, 82 const int version_minor, 83 const string& interface_name, 84 const string& hal_service_name); 85 86 // Verify the return result of a function call matches the expected result. 87 // This is used by VTS replay test. 88 bool VerifyResults(DriverId id, 89 const FunctionSpecificationMessage& expected_result, 90 const FunctionSpecificationMessage& actual_result); 91 92 // Loads the specification message for component with given component info 93 // such as component_class etc. Used to server the ReadSpecification request 94 // from host. 95 // Returns true if load successfully, false otherwise. 96 // Args: 97 // version_major: int, hal major version, e.g. 1.0 -> 1. 98 // version_minor: int, hal minor version, e.g. 1.0 -> 0. 99 // 100 bool FindComponentSpecification(const int component_class, 101 const int component_type, 102 const int version_major, 103 const int version_minor, 104 const string& package_name, 105 const string& component_name, 106 ComponentSpecificationMessage* spec_msg); 107 108 // Returns the specification message for default driver. Used to serve the 109 // ListFunctions request from host. 110 // TODO (zhuoyao): needs to revisit this after supporting multi-hal testing. 111 ComponentSpecificationMessage* GetComponentSpecification(); 112 113 // Used to serve the GetAttribute request from host. Only supported by 114 // conventional HAL. 115 // TODO (zhuoyao): consider deprecate this method. 116 string GetAttribute(FunctionCallMessage* func_msg); 117 118 private: 119 // Internal method to register a HAL driver in hal_driver_map_. 120 // Returns the driver id of registed driver. 121 DriverId RegisterDriver(std::unique_ptr<DriverBase> driver, 122 const ComponentSpecificationMessage& spec_msg, 123 const uint64_t interface_pt); 124 125 // Internal method to get the HAL driver based on the driver id. Returns 126 // nullptr if no driver instance existes with given id. 127 DriverBase* GetDriverById(const DriverId id); 128 129 // Internal method to get the registered driver pointer based on driver id. 130 // Returns -1 if no driver instance existes with given id. 131 uint64_t GetDriverPointerById(const DriverId id); 132 133 // Internal method to get the HAL driver based on FunctionCallMessage. 134 DriverBase* GetDriverWithCallMsg(const FunctionCallMessage& call_msg); 135 136 // Internal method to find the driver id based on component spec and 137 // (for Hidl HAL) address to the hidl proxy. 138 DriverId FindDriverIdInternal(const ComponentSpecificationMessage& spec_msg, 139 const uint64_t interface_pt = 0, 140 bool with_interface_pointer = false); 141 142 // Internal method to process function return results for library. 143 string ProcessFuncResultsForLibrary(FunctionSpecificationMessage* func_msg, 144 void* result); 145 146 // Util method to generate debug message with component info. 147 string GetComponentDebugMsg(const int component_class, 148 const int component_type, const string& version, 149 const string& package_name, 150 const string& component_name); 151 152 // Recursively preprocess HAL function call arguments that have special types 153 // such as TYPE_HIDL_INTERFACE, TYPE_FMQ_SYNC, TYPE_FMQ_UNSYNC, 154 // TYPE_HIDL_MEMORY, TYPE_HANDLE. 155 // 156 // @param arg argument for a HAL function call. 157 // 158 // @return true if preprocessing succeeds, false otherwise. 159 bool PreprocessHidlHalFunctionCallArgs(VariableSpecificationMessage* arg); 160 161 // Recursively set HAL function call return values that have special types 162 // such as TYPE_HIDL_INTERFACE, TYPE_FMQ_SYNC, TYPE_FMQ_UNSYNC, 163 // TYPE_HIDL_MEMORY, TYPE_HANDLE. 164 // 165 // @param return_val return value for a HAL function call. 166 // 167 // @return true if setting results succeeds, false otherwise. 168 bool SetHidlHalFunctionCallResults(VariableSpecificationMessage* return_val); 169 170 // ============== attributes =================== 171 172 // The server socket port # of the agent. 173 const string callback_socket_name_; 174 // A HalDriverLoader instance. 175 HalDriverLoader hal_driver_loader_; 176 177 // struct that store the driver instance and its corresponding meta info. 178 struct HalDriverInfo { 179 // Spcification for the HAL. 180 ComponentSpecificationMessage spec_msg; 181 // Pointer to the HAL client proxy, used for HIDL HAL only. 182 uint64_t hidl_hal_proxy_pt; 183 // A HAL driver instance. 184 std::unique_ptr<DriverBase> driver; 185 186 // Constructor for halDriverInfo HalDriverInfoHalDriverInfo187 HalDriverInfo(const ComponentSpecificationMessage& spec_msg, 188 const uint64_t interface_pt, 189 std::unique_ptr<DriverBase> driver) 190 : spec_msg(spec_msg), 191 hidl_hal_proxy_pt(interface_pt), 192 driver(std::move(driver)) {} 193 }; 194 // map to keep all the active HAL driver instances and their corresponding 195 // meta info. 196 // TODO(zhuoyao): consider to use unordered_map for performance optimization. 197 map<DriverId, HalDriverInfo> hal_driver_map_; 198 // TODO(zhuoyao): use mutex to protect hal_driver_map_; 199 200 // Hold onto a resource_manager because some function calls need to reference 201 // resources allocated on the target side. 202 // driver_manager doesn't own resource_manager because driver_manager and 203 // resource_manager are both started by the agent. driver_manager only holds 204 // this pointer because it is easy to call functions in resource_manager. 205 VtsResourceManager* resource_manager_; 206 }; 207 208 } // namespace vts 209 } // namespace android 210 #endif //__VTS_DRIVER_HAL_VTSHALDRIVERMANAGER_H 211