Home | History | Annotate | Download | only in driver_manager
      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 "component_loader/HalDriverLoader.h"
     24 #include "driver_base/DriverBase.h"
     25 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
     26 
     27 using namespace std;
     28 using DriverId = int32_t;
     29 
     30 namespace android {
     31 namespace vts {
     32 
     33 class VtsHalDriverManager {
     34  public:
     35   // Constructor where the first argument is the path of a dir which contains
     36   // all available interface specification files.
     37   VtsHalDriverManager(const string& spec_dir, const int epoch_count,
     38                       const string& callback_socket_name);
     39 
     40   // Loads the driver library for the target HAL, creates the corresponding
     41   // driver instance, assign it a driver id and registers the created driver
     42   // instance in hal_driver_map_.
     43   // Returns the generated driver id.
     44   DriverId LoadTargetComponent(const string& dll_file_name,
     45                                const string& spec_lib_file_path,
     46                                const int component_class,
     47                                const int component_type, const float version,
     48                                const string& package_name,
     49                                const string& component_name,
     50                                const string& hw_binder_service_name);
     51 
     52   // Call the API specified in func_msg with the provided parameter using the
     53   // the corresonding driver instance. If func_msg specified the driver_id,
     54   // use the driver instance corresponds to driver_id, otherwise, use the
     55   // default driver instance (with driver_id = 0).
     56   // Returns a string which contians the return results (a text format of the
     57   // returned protobuf).
     58   // For error cases, returns string "error";
     59   // TODO (zhuoyao): use FunctionCallMessage instead of
     60   // FunctionSpecificationMessage which contains info such as component name and
     61   // driver id.
     62   string CallFunction(FunctionCallMessage* func_msg);
     63 
     64   // Searches hal_driver_map_ for Hidl HAL driver instance with the given
     65   // package name, version and component (interface) name. If found, returns
     66   // the correponding driver instance, otherwise, creates a new driver instance
     67   // with the given info, registers it in hal_driver_map_ and returns the
     68   // generated driver instance. This is used by VTS replay test.
     69   DriverId GetDriverIdForHidlHalInterface(const string& package_name,
     70                                           const float version,
     71                                           const string& interface_name,
     72                                           const string& hal_service_name);
     73 
     74   // Verify the return result of a function call matches the expected result.
     75   // This is used by VTS replay test.
     76   bool VerifyResults(DriverId id,
     77                      const FunctionSpecificationMessage& expected_result,
     78                      const FunctionSpecificationMessage& actual_result);
     79 
     80   // Loads the specification message for component with given component info
     81   // such as component_class etc. Used to server the ReadSpecification request
     82   // from host.
     83   // Returns true if load successfully, false otherwise.
     84   bool FindComponentSpecification(const int component_class,
     85                                   const int component_type, const float version,
     86                                   const string& package_name,
     87                                   const string& component_name,
     88                                   ComponentSpecificationMessage* spec_msg);
     89 
     90   // Returns the specification message for default driver. Used to serve the
     91   // ListFunctions request from host.
     92   // TODO (zhuoyao): needs to revisit this after supporting multi-hal testing.
     93   ComponentSpecificationMessage* GetComponentSpecification();
     94 
     95   // Used to serve the GetAttribute request from host. Only supported by
     96   // conventional HAL.
     97   // TODO (zhuoyao): consider deprecate this method.
     98   string GetAttribute(FunctionCallMessage* func_msg);
     99 
    100  private:
    101   // Internal method to register a HAL driver in hal_driver_map_.
    102   // Returns the driver id of registed driver.
    103   DriverId RegisterDriver(std::unique_ptr<DriverBase> driver,
    104                           const ComponentSpecificationMessage& spec_msg,
    105                           const uint64_t interface_pt);
    106 
    107   // Internal method to get the HAL driver based on the driver id. Returns
    108   // nullptr if no driver instance existes with given id.
    109   DriverBase* GetDriverById(const DriverId id);
    110 
    111   // Internal method to get the registered driver pointer based on driver id.
    112   // Returns -1 if no driver instance existes with given id.
    113   uint64_t GetDriverPointerById(const DriverId id);
    114 
    115   // Internal method to get the HAL driver based on FunctionCallMessage.
    116   DriverBase* GetDriverWithCallMsg(const FunctionCallMessage& call_msg);
    117 
    118   // Internal method to find the driver id based on component spec and
    119   // (for Hidl HAL) address to the hidl proxy.
    120   DriverId FindDriverIdInternal(const ComponentSpecificationMessage& spec_msg,
    121                                 const uint64_t interface_pt = 0,
    122                                 bool with_interface_pointer = false);
    123 
    124   // Internal method to process function return results for library.
    125   string ProcessFuncResultsForLibrary(FunctionSpecificationMessage* func_msg,
    126                                       void* result);
    127 
    128   // Util method to generate debug message with component info.
    129   string GetComponentDebugMsg(const int component_class,
    130                               const int component_type, const string& version,
    131                               const string& package_name,
    132                               const string& component_name);
    133   // ============== attributes ===================
    134 
    135   // The server socket port # of the agent.
    136   const string callback_socket_name_;
    137   // A HalDriverLoader instance.
    138   HalDriverLoader hal_driver_loader_;
    139 
    140   // struct that store the driver instance and its corresponding meta info.
    141   struct HalDriverInfo {
    142     // Spcification for the HAL.
    143     ComponentSpecificationMessage spec_msg;
    144     // Pointer to the HAL client proxy, used for HIDL HAL only.
    145     uint64_t hidl_hal_proxy_pt;
    146     // A HAL driver instance.
    147     std::unique_ptr<DriverBase> driver;
    148 
    149     // Constructor for halDriverInfo
    150     HalDriverInfo(const ComponentSpecificationMessage& spec_msg,
    151                   const uint64_t interface_pt,
    152                   std::unique_ptr<DriverBase> driver)
    153         : spec_msg(spec_msg),
    154           hidl_hal_proxy_pt(interface_pt),
    155           driver(std::move(driver)) {}
    156   };
    157   // map to keep all the active HAL driver instances and their corresponding
    158   // meta info.
    159   // TODO(zhuoyao): consider to use unordered_map for performance optimization.
    160   map<DriverId, HalDriverInfo> hal_driver_map_;
    161   // TODO(zhuoyao): use mutex to protect hal_driver_map_;
    162 };
    163 
    164 }  // namespace vts
    165 }  // namespace android
    166 #endif  //__VTS_DRIVER_HAL_VTSHALDRIVERMANAGER_H
    167