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
17import logging
18
19from vts.runners.host import errors
20
21_DEFAULT_HWBINDER_SERVICE = "default"
22
23COMPONENT_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
32COMPONENT_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    "tests_msgq": 25,
56    "tests_memory": 26,
57    "dumpstate": 27,
58    "media_c2": 28,
59    "bionic_libm": 1001,
60    "bionic_libc": 1002,
61    "vndk_libcutils": 1101
62}
63
64
65class MirrorObject(object):
66    """The class that mirrors objects on the native side.
67
68    Attributes:
69        _client: VtsTcpClient, the client instance that can be used to send
70         commands to the target-side's agent.
71        _caller_uid: string, the caller's UID if not None.
72    """
73
74    def __init__(self, client, caller_uid=None):
75        self._client = client
76        self._caller_uid = caller_uid
77
78    def CleanUp(self):
79        if self._client:
80            self._client.Disconnect()
81
82    def SetCallerUid(self, uid):
83        """Sets target-side caller's UID.
84
85        Args:
86            uid: string, the caller's UID.
87        """
88        self._caller_uid = uid
89
90    def LaunchMirrorDriver(self,
91                           driver_type,
92                           target_class,
93                           target_type,
94                           target_version_major,
95                           target_version_minor,
96                           target_package="",
97                           target_filename=None,
98                           target_component_name=None,
99                           handler_name=None,
100                           service_name=None,
101                           hw_binder_service_name=_DEFAULT_HWBINDER_SERVICE,
102                           bits=64,
103                           is_test_hal=False):
104        """Initiates the driver for a lib on the target device and creates a top
105        level MirroObject for it.
106
107        Args:
108            driver_type: type of
109            target_class: string, the target class name (e.g., lib).
110            target_type: string, the target type name (e.g., light, camera).
111            target_version_major:
112              int, the target component major version (e.g. 1.0 -> 1).
113            target_version_minor:
114              int, the target component minor version (e.g. 1.0 -> 0).
115            target_basepaths: list of strings, the paths to look for target
116                             files in. Default is _DEFAULT_TARGET_BASE_PATHS.
117            target_package: . separated string (e.g., a.b.c) to denote the
118                            package name of target component.
119            target_filename: string, the target file name (e.g., libm.so).
120            handler_name: string, the name of the handler. target_type is used
121                          by default.
122            bits: integer, processor architecture indicator: 32 or 64.
123                  Default is 64 bits.
124            is_test_hal: bool, whether the HAL service is a test HAL
125                         (e.g. msgq).
126
127        Raises:
128            errors.ComponentLoadingError is raised when error occurs trying to
129            create a MirrorObject.
130        """
131        if bits not in [32, 64]:
132            raise error.ComponentLoadingError(
133                "Invalid value for bits: %s" % bits)
134        if not handler_name:
135            handler_name = target_type
136        if not service_name:
137            service_name = "vts_driver_%s" % handler_name
138
139        # Launch the corresponding driver of the requested HAL on the target.
140        logging.debug("Init the driver service for %s", target_type)
141        target_class_id = COMPONENT_CLASS_DICT[target_class.lower()]
142        target_type_id = COMPONENT_TYPE_DICT[target_type.lower()]
143
144        driver_id = self._client.LaunchDriverService(
145            driver_type=driver_type,
146            service_name=service_name,
147            bits=bits,
148            file_path=target_filename,
149            target_class=target_class_id,
150            target_type=target_type_id,
151            target_version_major=target_version_major,
152            target_version_minor=target_version_minor,
153            target_package=target_package,
154            target_component_name=target_component_name,
155            hw_binder_service_name=hw_binder_service_name,
156            is_test_hal=is_test_hal)
157
158        if driver_id == -1:
159            raise errors.ComponentLoadingError(
160                "Failed to launch driver service %s from file path %s" %
161                (target_type, target_filename))
162
163        return driver_id
164
165    def Ping(self):
166        """Returns true iff pinging the agent is successful, False otherwise."""
167        return self._client.Ping()
168