1 /*
2 * Copyright (C) 2020 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 #include <libvts_vintf_test_common/common.h>
18 #include <vintf/parse_string.h>
19
20 namespace android::vintf::testing {
21
22 // kApiLevel2FcmMap is associated with API level. There can be multiple
23 // Framework Compatibility Matrix Version (FCM Version) per API level, or
24 // multiple API levels per FCM version.
25 // kApiLevel2FcmMap is defined apart from android::vintf::Level. Level is an
26 // integer designed to be irrelevant with API level; the O / O_MR1 values are
27 // historic values for convenience, and should be removed (b/70628538). Hence
28 // these values are not used here.
29 // For example:
30 // ...
31 // // Assume devices launch with Android X must implement FCM version >= 9
32 // X = 9,
33 // // Assume devices launch with Android Y and Android Z must implement
34 // // FCM version >= 11
35 // Y = 11,
36 // Z = 11
37 static const std::map<uint64_t /* Vendor API Level */, Level /* FCM Version */>
38 kApiLevel2FcmMap{{
39 // N. The test runs on devices that launch with N and
40 // become a Treble device when upgrading to O.
41 {25, Level::O},
42
43 {26, Level::O},
44 {27, Level::O_MR1},
45 {28, Level::P},
46 {29, Level::Q},
47 {30, Level::R},
48 {31, Level::S},
49 {32, Level::S},
50 {33, Level::T},
51 {34, Level::U},
52 // Starting from 2024Q2, vendor api level has YYYYMM format.
53 {202404, Level::V},
54 {202504, Level::W}, // TODO(b/346861728) placeholder level
55 }};
56
GetFcmVersionFromApiLevel(uint64_t api_level)57 android::base::Result<Level> GetFcmVersionFromApiLevel(uint64_t api_level) {
58 auto it = android::vintf::testing::kApiLevel2FcmMap.find(api_level);
59 if (it == android::vintf::testing::kApiLevel2FcmMap.end()) {
60 return android::base::Error()
61 << "Can't find corresponding VINTF level for API level " << api_level
62 << ". Is the test updated?";
63 }
64 return it->second;
65 }
66
TestTargetFcmVersion(Level target_fcm_version,uint64_t vendor_api_level)67 android::base::Result<void> TestTargetFcmVersion(Level target_fcm_version,
68 uint64_t vendor_api_level) {
69 if (vendor_api_level == 0u) {
70 return android::base::Error()
71 << "Device's vendor API level cannot be determined.";
72 }
73
74 if (target_fcm_version == Level::UNSPECIFIED) {
75 // O / O-MR1 vendor image doesn't have target FCM version declared and
76 // target FCM version is inferred from vendor API level, hence it always
77 // meets the requirement.
78 if (vendor_api_level <= 27) { // O-MR1
79 return {};
80 }
81 return android::base::Error() << "Target FCM version (device manifest "
82 "target-level) must be set for "
83 "device with vendor api level "
84 << vendor_api_level;
85 }
86
87 if (vendor_api_level < kApiLevel2FcmMap.begin()->first /* 25 */) {
88 return android::base::Error() << "Pre-N devices should not run this test.";
89 }
90
91 auto it = kApiLevel2FcmMap.find(vendor_api_level);
92 if (it == kApiLevel2FcmMap.end()) {
93 return android::base::Error()
94 << "No launch requirement is set yet for vendor API level "
95 << vendor_api_level << ". Please update the test.";
96 }
97
98 Level required_fcm_version = it->second;
99 if (target_fcm_version < required_fcm_version) {
100 return android::base::Error()
101 << "Vendor API level == " << vendor_api_level
102 << " requires Target FCM Version >= " << required_fcm_version
103 << " (but is " << target_fcm_version << ")";
104 }
105
106 return {};
107 }
108
109 } // namespace android::vintf::testing
110