1#
2# Copyright (C) 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
17import logging
18
19from vts.utils.python.android import api
20
21
22def IsVndkRuntimeEnforced(dut):
23    """Returns whether VNDK run-time enforcement is enabled on the device.
24
25    VNDK run-time enforcement is optional in O-MR1 (API 27); mandatory after P.
26    If VNDK run-time enforcement is disabled, the system property named
27    ro.vndk.lite must be set to true. The usage of this function is to decide
28    whether to skip VNDK test cases.
29
30    Args:
31        dut: The AndroidDevice under test.
32
33    Returns:
34        A boolean, whether VNDK runtime should be enabled.
35    """
36    api_level = dut.getLaunchApiLevel(strict=False)
37    if not api_level:
38        logging.error("Cannot get first API level. "
39                      "Assume VNDK runtime to be enforced.")
40        return True
41    if api_level <= api.PLATFORM_API_LEVEL_O_MR1:
42        return not dut.vndk_lite
43    # For P-launching devices, VNDK run-time enforcement is mandatory.
44    return True
45
46
47def FormatVndkPath(pattern, bitness, version=""):
48    """Formats a VNDK path.
49
50    Args:
51        pattern: The path pattern containing {LIB} and {VER}.
52        bitness: A string or an integer, 32 or 64.
53        version: A string, the VNDK version.
54
55    Returns:
56        A string, the formatted path.
57    """
58    return pattern.format(LIB=("lib64" if str(bitness) == "64" else "lib"),
59                          VER=version)
60
61
62def GetVndkDirectory(bitness, version):
63    """Returns the path to VNDK directory on device.
64
65    Args:
66        bitness: A string or an integer, 32 or 64.
67        version: A string, the VNDK version.
68
69    Returns:
70        A string, the path to VNDK directory.
71    """
72    return FormatVndkPath("/apex/com.android.vndk.v{VER}/{LIB}",
73                          bitness, version)
74
75
76def GetVndkExtDirectories(bitness):
77    """Returns the paths to VNDK extension directories on device.
78
79    Args:
80        bitness: A string or an integer, 32 or 64.
81
82    Returns:
83        A list of strings, the paths to VNDK extension directories.
84    """
85    return [FormatVndkPath("/odm/{LIB}/vndk", bitness),
86            FormatVndkPath("/vendor/{LIB}/vndk", bitness)]
87
88
89def GetVndkSpExtDirectories(bitness):
90    """Returns the paths to VNDK-SP extension directories on device.
91
92    Args:
93        bitness: A string or an integer, 32 or 64.
94
95    Returns:
96        A list of strings, the paths to VNDK-SP extension directories.
97    """
98    return [FormatVndkPath("/odm/{LIB}/vndk-sp", bitness),
99            FormatVndkPath("/vendor/{LIB}/vndk-sp", bitness)]
100