• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #define LOG_TAG "VintfObject"
18 //#define LOG_NDEBUG 0
19 #include <android-base/logging.h>
20 
21 #include <vector>
22 #include <string>
23 
24 #include <nativehelper/JNIHelp.h>
25 #include <vintf/VintfObject.h>
26 #include <vintf/parse_string.h>
27 #include <vintf/parse_xml.h>
28 
29 #include "core_jni_helpers.h"
30 
31 static jclass gString;
32 static jclass gHashMapClazz;
33 static jmethodID gHashMapInit;
34 static jmethodID gHashMapPut;
35 static jclass gLongClazz;
36 static jmethodID gLongValueOf;
37 
38 namespace android {
39 
40 using vintf::CompatibilityMatrix;
41 using vintf::HalManifest;
42 using vintf::Level;
43 using vintf::SchemaType;
44 using vintf::to_string;
45 using vintf::toXml;
46 using vintf::Version;
47 using vintf::VintfObject;
48 using vintf::Vndk;
49 
50 template<typename V>
toJavaStringArray(JNIEnv * env,const V & v)51 static inline jobjectArray toJavaStringArray(JNIEnv* env, const V& v) {
52     size_t i;
53     typename V::const_iterator it;
54     jobjectArray ret = env->NewObjectArray(v.size(), gString, NULL /* init element */);
55     for (i = 0, it = v.begin(); it != v.end(); ++i, ++it) {
56         env->SetObjectArrayElement(ret, i, env->NewStringUTF(it->c_str()));
57     }
58     return ret;
59 }
60 
61 template <typename T>
tryAddSchema(const std::shared_ptr<const T> & object,const std::string & description,std::vector<std::string> * cStrings)62 static void tryAddSchema(const std::shared_ptr<const T>& object, const std::string& description,
63                          std::vector<std::string>* cStrings) {
64     if (object == nullptr) {
65         LOG(WARNING) << __FUNCTION__ << "Cannot get " << description;
66     } else {
67         cStrings->push_back(toXml(*object));
68     }
69 }
70 
tryAddHalNamesAndVersions(const std::shared_ptr<const HalManifest> & manifest,const std::string & description,std::set<std::string> * output)71 static void tryAddHalNamesAndVersions(const std::shared_ptr<const HalManifest>& manifest,
72         const std::string& description,
73         std::set<std::string> *output) {
74     if (manifest == nullptr) {
75         LOG(WARNING) << __FUNCTION__ << "Cannot get " << description;
76     } else {
77         auto names = manifest->getHalNamesAndVersions();
78         output->insert(names.begin(), names.end());
79     }
80 }
81 
android_os_VintfObject_report(JNIEnv * env,jclass)82 static jobjectArray android_os_VintfObject_report(JNIEnv* env, jclass)
83 {
84     std::vector<std::string> cStrings;
85 
86     tryAddSchema(VintfObject::GetDeviceHalManifest(), "device manifest", &cStrings);
87     tryAddSchema(VintfObject::GetFrameworkHalManifest(), "framework manifest", &cStrings);
88     tryAddSchema(VintfObject::GetDeviceCompatibilityMatrix(), "device compatibility matrix",
89                  &cStrings);
90     tryAddSchema(VintfObject::GetFrameworkCompatibilityMatrix(), "framework compatibility matrix",
91                  &cStrings);
92 
93     return toJavaStringArray(env, cStrings);
94 }
95 
android_os_VintfObject_verifyWithoutAvb(JNIEnv * env,jclass)96 static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) {
97     std::string error;
98     int32_t status = VintfObject::GetInstance()->checkCompatibility(&error,
99             ::android::vintf::CheckFlags::DISABLE_AVB_CHECK);
100     if (status)
101         LOG(WARNING) << "VintfObject.verifyWithoutAvb() returns " << status << ": " << error;
102     return status;
103 }
104 
android_os_VintfObject_getHalNamesAndVersions(JNIEnv * env,jclass)105 static jobjectArray android_os_VintfObject_getHalNamesAndVersions(JNIEnv* env, jclass) {
106     std::set<std::string> halNames;
107     tryAddHalNamesAndVersions(VintfObject::GetDeviceHalManifest(),
108             "device manifest", &halNames);
109     tryAddHalNamesAndVersions(VintfObject::GetFrameworkHalManifest(),
110             "framework manifest", &halNames);
111     return toJavaStringArray(env, halNames);
112 }
113 
android_os_VintfObject_getSepolicyVersion(JNIEnv * env,jclass)114 static jstring android_os_VintfObject_getSepolicyVersion(JNIEnv* env, jclass) {
115     std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
116     if (manifest == nullptr || manifest->type() != SchemaType::DEVICE) {
117         LOG(WARNING) << __FUNCTION__ << "Cannot get device manifest";
118         return nullptr;
119     }
120     std::string cString = to_string(manifest->sepolicyVersion());
121     return env->NewStringUTF(cString.c_str());
122 }
123 
android_os_VintfObject_getPlatformSepolicyVersion(JNIEnv * env,jclass)124 static jstring android_os_VintfObject_getPlatformSepolicyVersion(JNIEnv* env, jclass) {
125     std::shared_ptr<const CompatibilityMatrix> matrix =
126             VintfObject::GetFrameworkCompatibilityMatrix();
127     if (matrix == nullptr || matrix->type() != SchemaType::FRAMEWORK) {
128         jniThrowRuntimeException(env, "Cannot get framework compatibility matrix");
129         return nullptr;
130     }
131 
132     auto versions = matrix->getSepolicyVersions();
133     if (versions.empty()) {
134         jniThrowRuntimeException(env,
135                                  "sepolicy_version in framework compatibility matrix is empty");
136         return nullptr;
137     }
138 
139     Version latest;
140     for (const auto& range : versions) {
141         latest = std::max(latest, range.maxVer());
142     }
143     return env->NewStringUTF(to_string(latest).c_str());
144 }
145 
android_os_VintfObject_getVndkSnapshots(JNIEnv * env,jclass)146 static jobject android_os_VintfObject_getVndkSnapshots(JNIEnv* env, jclass) {
147     std::shared_ptr<const HalManifest> manifest = VintfObject::GetFrameworkHalManifest();
148     if (manifest == nullptr || manifest->type() != SchemaType::FRAMEWORK) {
149         LOG(WARNING) << __FUNCTION__ << "Cannot get framework manifest";
150         return nullptr;
151     }
152     jobject jMap = env->NewObject(gHashMapClazz, gHashMapInit);
153     for (const auto &vndk : manifest->vendorNdks()) {
154         const std::string& key = vndk.version();
155         env->CallObjectMethod(jMap, gHashMapPut,
156                 env->NewStringUTF(key.c_str()), toJavaStringArray(env, vndk.libraries()));
157     }
158     return jMap;
159 }
160 
android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion(JNIEnv * env,jclass)161 static jobject android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion(JNIEnv* env, jclass) {
162     std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
163     if (manifest == nullptr || manifest->level() == Level::UNSPECIFIED) {
164         return nullptr;
165     }
166     return env->CallStaticObjectMethod(gLongClazz, gLongValueOf, static_cast<jlong>(manifest->level()));
167 }
168 
169 // ----------------------------------------------------------------------------
170 
171 static const JNINativeMethod gVintfObjectMethods[] = {
172         {"report", "()[Ljava/lang/String;", (void*)android_os_VintfObject_report},
173         {"verifyWithoutAvb", "()I", (void*)android_os_VintfObject_verifyWithoutAvb},
174         {"getHalNamesAndVersions", "()[Ljava/lang/String;",
175          (void*)android_os_VintfObject_getHalNamesAndVersions},
176         {"getSepolicyVersion", "()Ljava/lang/String;",
177          (void*)android_os_VintfObject_getSepolicyVersion},
178         {"getPlatformSepolicyVersion", "()Ljava/lang/String;",
179          (void*)android_os_VintfObject_getPlatformSepolicyVersion},
180         {"getVndkSnapshots", "()Ljava/util/Map;", (void*)android_os_VintfObject_getVndkSnapshots},
181         {"getTargetFrameworkCompatibilityMatrixVersion", "()Ljava/lang/Long;",
182          (void*)android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion},
183 };
184 
185 const char* const kVintfObjectPathName = "android/os/VintfObject";
186 
register_android_os_VintfObject(JNIEnv * env)187 int register_android_os_VintfObject(JNIEnv* env)
188 {
189 
190     gString = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/String"));
191     gHashMapClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/util/HashMap"));
192     gHashMapInit = GetMethodIDOrDie(env, gHashMapClazz, "<init>", "()V");
193     gHashMapPut = GetMethodIDOrDie(env, gHashMapClazz,
194             "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
195     gLongClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Long"));
196     gLongValueOf = GetStaticMethodIDOrDie(env, gLongClazz, "valueOf", "(J)Ljava/lang/Long;");
197 
198     return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods,
199             NELEM(gVintfObjectMethods));
200 }
201 
202 };
203