blob: ee11b6162db01e20505d668aede436f3e28483e4 [file] [log] [blame]
Yifan Hong4e01db82017-04-05 14:42:05 -07001/*
Yifan Hong4463d992017-05-01 18:43:36 -07002 * Copyright (C) 2017 The Android Open Source Project
Yifan Hong4e01db82017-04-05 14:42:05 -07003 *
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
Yifan Hong4463d992017-05-01 18:43:36 -070019#include <android-base/logging.h>
20
21#include <vector>
22#include <string>
Yifan Hong4e01db82017-04-05 14:42:05 -070023
Steven Moreland2279b252017-07-19 09:50:45 -070024#include <nativehelper/JNIHelp.h>
Yifan Hong4e01db82017-04-05 14:42:05 -070025#include <vintf/VintfObject.h>
Yifan Hongbf498212017-05-04 13:05:24 -070026#include <vintf/parse_string.h>
Yifan Hong4e01db82017-04-05 14:42:05 -070027#include <vintf/parse_xml.h>
28
29#include "core_jni_helpers.h"
30
Yifan Hong4463d992017-05-01 18:43:36 -070031static jclass gString;
Yifan Hongbf498212017-05-04 13:05:24 -070032static jclass gHashMapClazz;
33static jmethodID gHashMapInit;
34static jmethodID gHashMapPut;
Yifan Hong0e5e4722018-02-07 14:16:21 -080035static jclass gLongClazz;
36static jmethodID gLongValueOf;
Yifan Hong4463d992017-05-01 18:43:36 -070037
Yifan Hong4e01db82017-04-05 14:42:05 -070038namespace android {
39
Yifan Hongbf498212017-05-04 13:05:24 -070040using vintf::HalManifest;
Yifan Hong0e5e4722018-02-07 14:16:21 -080041using vintf::Level;
Yifan Hongbf498212017-05-04 13:05:24 -070042using vintf::SchemaType;
Yifan Hong4e01db82017-04-05 14:42:05 -070043using vintf::VintfObject;
Yifan Hongbf498212017-05-04 13:05:24 -070044using vintf::XmlConverter;
45using vintf::Vndk;
Yifan Hong4e01db82017-04-05 14:42:05 -070046using vintf::gHalManifestConverter;
Yifan Hong4463d992017-05-01 18:43:36 -070047using vintf::gCompatibilityMatrixConverter;
Yifan Hongbf498212017-05-04 13:05:24 -070048using vintf::to_string;
Yifan Hong4e01db82017-04-05 14:42:05 -070049
Yifan Hongbf498212017-05-04 13:05:24 -070050template<typename V>
51static inline jobjectArray toJavaStringArray(JNIEnv* env, const V& v) {
52 size_t i;
53 typename V::const_iterator it;
Yifan Hong4463d992017-05-01 18:43:36 -070054 jobjectArray ret = env->NewObjectArray(v.size(), gString, NULL /* init element */);
Yifan Hongbf498212017-05-04 13:05:24 -070055 for (i = 0, it = v.begin(); it != v.end(); ++i, ++it) {
56 env->SetObjectArrayElement(ret, i, env->NewStringUTF(it->c_str()));
Yifan Hong4e01db82017-04-05 14:42:05 -070057 }
Yifan Hong4463d992017-05-01 18:43:36 -070058 return ret;
Yifan Hong4e01db82017-04-05 14:42:05 -070059}
60
Yifan Hong4463d992017-05-01 18:43:36 -070061template<typename T>
Yifan Hongd666d5b2017-08-30 10:20:02 -070062static void tryAddSchema(const std::shared_ptr<const T>& object, const XmlConverter<T>& converter,
Yifan Hong4463d992017-05-01 18:43:36 -070063 const std::string& description,
64 std::vector<std::string>* cStrings) {
65 if (object == nullptr) {
66 LOG(WARNING) << __FUNCTION__ << "Cannot get " << description;
67 } else {
68 cStrings->push_back(converter(*object));
Yifan Hong4e01db82017-04-05 14:42:05 -070069 }
Yifan Hong4463d992017-05-01 18:43:36 -070070}
71
Yifan Hongd666d5b2017-08-30 10:20:02 -070072static void tryAddHalNamesAndVersions(const std::shared_ptr<const HalManifest>& manifest,
Yifan Hongbf498212017-05-04 13:05:24 -070073 const std::string& description,
74 std::set<std::string> *output) {
75 if (manifest == nullptr) {
76 LOG(WARNING) << __FUNCTION__ << "Cannot get " << description;
77 } else {
78 auto names = manifest->getHalNamesAndVersions();
79 output->insert(names.begin(), names.end());
80 }
81}
82
83static jobjectArray android_os_VintfObject_report(JNIEnv* env, jclass)
Yifan Hong4463d992017-05-01 18:43:36 -070084{
85 std::vector<std::string> cStrings;
86
87 tryAddSchema(VintfObject::GetDeviceHalManifest(), gHalManifestConverter,
88 "device manifest", &cStrings);
89 tryAddSchema(VintfObject::GetFrameworkHalManifest(), gHalManifestConverter,
90 "framework manifest", &cStrings);
91 tryAddSchema(VintfObject::GetDeviceCompatibilityMatrix(), gCompatibilityMatrixConverter,
92 "device compatibility matrix", &cStrings);
93 tryAddSchema(VintfObject::GetFrameworkCompatibilityMatrix(), gCompatibilityMatrixConverter,
94 "framework compatibility matrix", &cStrings);
95
96 return toJavaStringArray(env, cStrings);
Yifan Hong4e01db82017-04-05 14:42:05 -070097}
98
Yifan Hongc370a572019-01-29 16:45:45 -080099static jint android_os_VintfObject_verify(JNIEnv* env, jclass, jobjectArray packageInfo) {
Bowgo Tsai78934c92017-11-17 17:18:50 +0800100 std::vector<std::string> cPackageInfo;
101 if (packageInfo) {
102 size_t count = env->GetArrayLength(packageInfo);
103 cPackageInfo.resize(count);
104 for (size_t i = 0; i < count; ++i) {
105 jstring element = (jstring)env->GetObjectArrayElement(packageInfo, i);
106 const char *cString = env->GetStringUTFChars(element, NULL /* isCopy */);
107 cPackageInfo[i] = cString;
108 env->ReleaseStringUTFChars(element, cString);
109 }
Yifan Hong4e01db82017-04-05 14:42:05 -0700110 }
Yifan Hong87167af2017-07-05 16:39:51 -0700111 std::string error;
Yifan Hongc370a572019-01-29 16:45:45 -0800112 int32_t status = VintfObject::CheckCompatibility(cPackageInfo, &error);
Yifan Hong87167af2017-07-05 16:39:51 -0700113 if (status)
114 LOG(WARNING) << "VintfObject.verify() returns " << status << ": " << error;
Yifan Hong4e01db82017-04-05 14:42:05 -0700115 return status;
116}
117
Bowgo Tsai78934c92017-11-17 17:18:50 +0800118static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) {
Yifan Hongc370a572019-01-29 16:45:45 -0800119 std::string error;
120 int32_t status = VintfObject::CheckCompatibility({}, &error,
121 ::android::vintf::CheckFlags::DISABLE_AVB_CHECK);
122 if (status)
123 LOG(WARNING) << "VintfObject.verifyWithoutAvb() returns " << status << ": " << error;
124 return status;
Bowgo Tsai78934c92017-11-17 17:18:50 +0800125}
126
Yifan Hongbf498212017-05-04 13:05:24 -0700127static jobjectArray android_os_VintfObject_getHalNamesAndVersions(JNIEnv* env, jclass) {
128 std::set<std::string> halNames;
129 tryAddHalNamesAndVersions(VintfObject::GetDeviceHalManifest(),
130 "device manifest", &halNames);
131 tryAddHalNamesAndVersions(VintfObject::GetFrameworkHalManifest(),
132 "framework manifest", &halNames);
133 return toJavaStringArray(env, halNames);
134}
135
136static jstring android_os_VintfObject_getSepolicyVersion(JNIEnv* env, jclass) {
Yifan Hongd666d5b2017-08-30 10:20:02 -0700137 std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
Yifan Hongbf498212017-05-04 13:05:24 -0700138 if (manifest == nullptr || manifest->type() != SchemaType::DEVICE) {
139 LOG(WARNING) << __FUNCTION__ << "Cannot get device manifest";
140 return nullptr;
141 }
142 std::string cString = to_string(manifest->sepolicyVersion());
143 return env->NewStringUTF(cString.c_str());
144}
145
146static jobject android_os_VintfObject_getVndkSnapshots(JNIEnv* env, jclass) {
Yifan Hongd666d5b2017-08-30 10:20:02 -0700147 std::shared_ptr<const HalManifest> manifest = VintfObject::GetFrameworkHalManifest();
Yifan Hongbf498212017-05-04 13:05:24 -0700148 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);
Yifan Hong8da8798c2018-01-10 16:04:18 -0800153 for (const auto &vndk : manifest->vendorNdks()) {
Chih-Hung Hsiehc1340632018-07-13 13:37:03 -0700154 const std::string& key = vndk.version();
Yifan Hongbf498212017-05-04 13:05:24 -0700155 env->CallObjectMethod(jMap, gHashMapPut,
156 env->NewStringUTF(key.c_str()), toJavaStringArray(env, vndk.libraries()));
157 }
158 return jMap;
159}
160
Yifan Hong0e5e4722018-02-07 14:16:21 -0800161static 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
Yifan Hong4e01db82017-04-05 14:42:05 -0700169// ----------------------------------------------------------------------------
170
171static const JNINativeMethod gVintfObjectMethods[] = {
Yifan Hong4463d992017-05-01 18:43:36 -0700172 {"report", "()[Ljava/lang/String;", (void*)android_os_VintfObject_report},
173 {"verify", "([Ljava/lang/String;)I", (void*)android_os_VintfObject_verify},
Bowgo Tsai78934c92017-11-17 17:18:50 +0800174 {"verifyWithoutAvb", "()I", (void*)android_os_VintfObject_verifyWithoutAvb},
Yifan Hongbf498212017-05-04 13:05:24 -0700175 {"getHalNamesAndVersions", "()[Ljava/lang/String;", (void*)android_os_VintfObject_getHalNamesAndVersions},
176 {"getSepolicyVersion", "()Ljava/lang/String;", (void*)android_os_VintfObject_getSepolicyVersion},
177 {"getVndkSnapshots", "()Ljava/util/Map;", (void*)android_os_VintfObject_getVndkSnapshots},
Yifan Hong0e5e4722018-02-07 14:16:21 -0800178 {"getTargetFrameworkCompatibilityMatrixVersion", "()Ljava/lang/Long;", (void*)android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion},
Yifan Hong4e01db82017-04-05 14:42:05 -0700179};
180
181const char* const kVintfObjectPathName = "android/os/VintfObject";
182
183int register_android_os_VintfObject(JNIEnv* env)
184{
Yifan Hong4463d992017-05-01 18:43:36 -0700185
186 gString = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/String"));
Yifan Hongbf498212017-05-04 13:05:24 -0700187 gHashMapClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/util/HashMap"));
188 gHashMapInit = GetMethodIDOrDie(env, gHashMapClazz, "<init>", "()V");
189 gHashMapPut = GetMethodIDOrDie(env, gHashMapClazz,
190 "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
Yifan Hong0e5e4722018-02-07 14:16:21 -0800191 gLongClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Long"));
192 gLongValueOf = GetStaticMethodIDOrDie(env, gLongClazz, "valueOf", "(J)Ljava/lang/Long;");
Yifan Hong4463d992017-05-01 18:43:36 -0700193
Yifan Hong4e01db82017-04-05 14:42:05 -0700194 return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods,
195 NELEM(gVintfObjectMethods));
196}
197
198};