blob: 25ffbabd6199ab81f4fa6ac0042047a8293a4d77 [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
Bowgo Tsai78934c92017-11-17 17:18:50 +080099static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) {
Yifan Hongc370a572019-01-29 16:45:45 -0800100 std::string error;
Yifan Hong12ebf362019-12-18 12:52:55 -0800101 int32_t status = VintfObject::GetInstance()->checkCompatibility(&error,
Yifan Hongc370a572019-01-29 16:45:45 -0800102 ::android::vintf::CheckFlags::DISABLE_AVB_CHECK);
103 if (status)
104 LOG(WARNING) << "VintfObject.verifyWithoutAvb() returns " << status << ": " << error;
105 return status;
Bowgo Tsai78934c92017-11-17 17:18:50 +0800106}
107
Yifan Hongbf498212017-05-04 13:05:24 -0700108static jobjectArray android_os_VintfObject_getHalNamesAndVersions(JNIEnv* env, jclass) {
109 std::set<std::string> halNames;
110 tryAddHalNamesAndVersions(VintfObject::GetDeviceHalManifest(),
111 "device manifest", &halNames);
112 tryAddHalNamesAndVersions(VintfObject::GetFrameworkHalManifest(),
113 "framework manifest", &halNames);
114 return toJavaStringArray(env, halNames);
115}
116
117static jstring android_os_VintfObject_getSepolicyVersion(JNIEnv* env, jclass) {
Yifan Hongd666d5b2017-08-30 10:20:02 -0700118 std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
Yifan Hongbf498212017-05-04 13:05:24 -0700119 if (manifest == nullptr || manifest->type() != SchemaType::DEVICE) {
120 LOG(WARNING) << __FUNCTION__ << "Cannot get device manifest";
121 return nullptr;
122 }
123 std::string cString = to_string(manifest->sepolicyVersion());
124 return env->NewStringUTF(cString.c_str());
125}
126
127static jobject android_os_VintfObject_getVndkSnapshots(JNIEnv* env, jclass) {
Yifan Hongd666d5b2017-08-30 10:20:02 -0700128 std::shared_ptr<const HalManifest> manifest = VintfObject::GetFrameworkHalManifest();
Yifan Hongbf498212017-05-04 13:05:24 -0700129 if (manifest == nullptr || manifest->type() != SchemaType::FRAMEWORK) {
130 LOG(WARNING) << __FUNCTION__ << "Cannot get framework manifest";
131 return nullptr;
132 }
133 jobject jMap = env->NewObject(gHashMapClazz, gHashMapInit);
Yifan Hong8da8798c2018-01-10 16:04:18 -0800134 for (const auto &vndk : manifest->vendorNdks()) {
Chih-Hung Hsiehc1340632018-07-13 13:37:03 -0700135 const std::string& key = vndk.version();
Yifan Hongbf498212017-05-04 13:05:24 -0700136 env->CallObjectMethod(jMap, gHashMapPut,
137 env->NewStringUTF(key.c_str()), toJavaStringArray(env, vndk.libraries()));
138 }
139 return jMap;
140}
141
Yifan Hong0e5e4722018-02-07 14:16:21 -0800142static jobject android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion(JNIEnv* env, jclass) {
143 std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
144 if (manifest == nullptr || manifest->level() == Level::UNSPECIFIED) {
145 return nullptr;
146 }
147 return env->CallStaticObjectMethod(gLongClazz, gLongValueOf, static_cast<jlong>(manifest->level()));
148}
149
Yifan Hong4e01db82017-04-05 14:42:05 -0700150// ----------------------------------------------------------------------------
151
152static const JNINativeMethod gVintfObjectMethods[] = {
Yifan Hong4463d992017-05-01 18:43:36 -0700153 {"report", "()[Ljava/lang/String;", (void*)android_os_VintfObject_report},
Bowgo Tsai78934c92017-11-17 17:18:50 +0800154 {"verifyWithoutAvb", "()I", (void*)android_os_VintfObject_verifyWithoutAvb},
Yifan Hongbf498212017-05-04 13:05:24 -0700155 {"getHalNamesAndVersions", "()[Ljava/lang/String;", (void*)android_os_VintfObject_getHalNamesAndVersions},
156 {"getSepolicyVersion", "()Ljava/lang/String;", (void*)android_os_VintfObject_getSepolicyVersion},
157 {"getVndkSnapshots", "()Ljava/util/Map;", (void*)android_os_VintfObject_getVndkSnapshots},
Yifan Hong0e5e4722018-02-07 14:16:21 -0800158 {"getTargetFrameworkCompatibilityMatrixVersion", "()Ljava/lang/Long;", (void*)android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion},
Yifan Hong4e01db82017-04-05 14:42:05 -0700159};
160
161const char* const kVintfObjectPathName = "android/os/VintfObject";
162
163int register_android_os_VintfObject(JNIEnv* env)
164{
Yifan Hong4463d992017-05-01 18:43:36 -0700165
166 gString = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/String"));
Yifan Hongbf498212017-05-04 13:05:24 -0700167 gHashMapClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/util/HashMap"));
168 gHashMapInit = GetMethodIDOrDie(env, gHashMapClazz, "<init>", "()V");
169 gHashMapPut = GetMethodIDOrDie(env, gHashMapClazz,
170 "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
Yifan Hong0e5e4722018-02-07 14:16:21 -0800171 gLongClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Long"));
172 gLongValueOf = GetStaticMethodIDOrDie(env, gLongClazz, "valueOf", "(J)Ljava/lang/Long;");
Yifan Hong4463d992017-05-01 18:43:36 -0700173
Yifan Hong4e01db82017-04-05 14:42:05 -0700174 return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods,
175 NELEM(gVintfObjectMethods));
176}
177
178};