1 # 2 # Copyright (C) 2016 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 import logging 18 19 from vts.runners.host import errors 20 21 _DEFAULT_HWBINDER_SERVICE = "default" 22 23 COMPONENT_CLASS_DICT = { 24 "hal_conventional": 1, 25 "hal_conventional_submodule": 2, 26 "hal_legacy": 3, 27 "hal_hidl": 4, 28 "hal_hidl_wrapped_conventional": 5, 29 "lib_shared": 11 30 } 31 32 COMPONENT_TYPE_DICT = { 33 "audio": 1, 34 "camera": 2, 35 "gps": 3, 36 "gnss": 3, 37 "light": 4, 38 "wifi": 5, 39 "mobile": 6, 40 "bluetooth": 7, 41 "nfc": 8, 42 "vibrator": 12, 43 "thermal": 13, 44 "tv_input": 14, 45 "tv_cec": 15, 46 "sensors": 16, 47 "vehicle": 17, 48 "vr": 18, 49 "graphics_allocator": 19, 50 "graphics_mapper": 20, 51 "radio": 21, 52 "contexthub": 22, 53 "graphics_composer": 23, 54 "media_omx": 24, 55 "bionic_libm": 1001, 56 "bionic_libc": 1002, 57 "vndk_libcutils": 1101 58 } 59 60 61 class MirrorObject(object): 62 """The class that mirrors objects on the native side. 63 64 Attributes: 65 _client: VtsTcpClient, the client instance that can be used to send 66 commands to the target-side's agent. 67 _caller_uid: string, the caller's UID if not None. 68 """ 69 70 def __init__(self, client, caller_uid=None): 71 self._client = client 72 self._caller_uid = caller_uid 73 74 def CleanUp(self): 75 if self._client: 76 self._client.Disconnect() 77 78 def SetCallerUid(self, uid): 79 """Sets target-side caller's UID. 80 81 Args: 82 uid: string, the caller's UID. 83 """ 84 self._caller_uid = uid 85 86 def LaunchMirrorDriver(self, 87 driver_type, 88 target_class, 89 target_type, 90 target_version, 91 target_package="", 92 target_filename=None, 93 target_component_name=None, 94 handler_name=None, 95 service_name=None, 96 hw_binder_service_name=_DEFAULT_HWBINDER_SERVICE, 97 bits=64): 98 """Initiates the driver for a lib on the target device and creates a top 99 level MirroObject for it. 100 101 Args: 102 driver_type: type of 103 target_class: string, the target class name (e.g., lib). 104 target_type: string, the target type name (e.g., light, camera). 105 target_version: float, the target component version (e.g., 1.0). 106 target_basepaths: list of strings, the paths to look for target 107 files in. Default is _DEFAULT_TARGET_BASE_PATHS. 108 target_package: . separated string (e.g., a.b.c) to denote the 109 package name of target component. 110 target_filename: string, the target file name (e.g., libm.so). 111 handler_name: string, the name of the handler. target_type is used 112 by default. 113 bits: integer, processor architecture indicator: 32 or 64. 114 115 Raises: 116 errors.ComponentLoadingError is raised when error occurs trying to 117 create a MirrorObject. 118 """ 119 if bits not in [32, 64]: 120 raise error.ComponentLoadingError("Invalid value for bits: %s" % 121 bits) 122 if not handler_name: 123 handler_name = target_type 124 if not service_name: 125 service_name = "vts_driver_%s" % handler_name 126 127 # Launch the corresponding driver of the requested HAL on the target. 128 logging.info("Init the driver service for %s", target_type) 129 target_class_id = COMPONENT_CLASS_DICT[target_class.lower()] 130 target_type_id = COMPONENT_TYPE_DICT[target_type.lower()] 131 132 driver_id = self._client.LaunchDriverService( 133 driver_type=driver_type, 134 service_name=service_name, 135 bits=bits, 136 file_path=target_filename, 137 target_class=target_class_id, 138 target_type=target_type_id, 139 target_version=target_version, 140 target_package=target_package, 141 target_component_name=target_component_name, 142 hw_binder_service_name=hw_binder_service_name) 143 144 if driver_id == -1: 145 raise errors.ComponentLoadingError( 146 "Failed to launch driver service %s from file path %s" % 147 (target_type, target_filename)) 148 149 return driver_id 150 151 def Ping(self): 152 """Returns true iff pinging the agent is successful, False otherwise.""" 153 return self._client.Ping()