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 package com.android.compatibility.common.deviceinfo;
17 
18 import android.os.Build;
19 import android.os.VintfObject;
20 import android.os.VintfRuntimeInfo;
21 
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.Map;
25 
26 import com.android.compatibility.common.util.DeviceInfoStore;
27 
28 /**
29  * VINTF device info collector.
30  * Keep name in sync with SELinuxHostTest#VINTF_DEVICE_CLASS.
31  */
32 public final class VintfDeviceInfo extends DeviceInfo {
33 
34     private static final String[] sEmptyStringArray = new String[0];
35 
36     @Override
collectDeviceInfo(DeviceInfoStore store)37     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
38         // VintfRuntimeInfo is available Android O onward.
39         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
40            return;
41         }
42         store.addResult("cpu_info", VintfRuntimeInfo.getCpuInfo());
43         store.addResult("os_name", VintfRuntimeInfo.getOsName());
44         store.addResult("node_name", VintfRuntimeInfo.getNodeName());
45         store.addResult("os_release", VintfRuntimeInfo.getOsRelease());
46         store.addResult("os_version", VintfRuntimeInfo.getOsVersion());
47         store.addResult("hardware_id", VintfRuntimeInfo.getHardwareId());
48         store.addResult("kernel_version", VintfRuntimeInfo.getKernelVersion());
49         store.addResult("sepolicy_version", VintfObject.getSepolicyVersion());
50 
51         String[] hals = VintfObject.getHalNamesAndVersions();
52         store.addListResult("hals", hals == null
53                 ? Collections.emptyList() : Arrays.<String>asList(hals));
54 
55         Map<String, String[]> vndks = VintfObject.getVndkSnapshots();
56         if (vndks == null) vndks = Collections.emptyMap();
57         store.startArray("vndk_snapshots");
58         for (Map.Entry<String, String[]> e : vndks.entrySet()) {
59             store.startGroup();
60             store.addResult("version", e.getKey());
61             String[] libraries = e.getValue();
62             store.addListResult("libraries", libraries == null
63                     ? Collections.emptyList() : Arrays.<String>asList(libraries));
64             store.endGroup();
65         }
66         store.endArray();
67 
68         // getTargetFrameworkCompatibilityMatrixVersion is available Android P onward.
69         // (Use O_MR1 until P is released.)
70         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
71            return;
72         }
73         Long version = VintfObject.getTargetFrameworkCompatibilityMatrixVersion();
74         if (version != null) {
75             store.addResult("target_fcm_version", version);
76         }
77 
78         // getPlatformSepolicyVersion is available Android S onward.
79         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
80             store.addResult("platform_sepolicy_version", VintfObject.getPlatformSepolicyVersion());
81         }
82     }
83 }
84