blob: e8ef349f5b7a6b88edda167f900a011415e2d204 [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 verify(JNIEnv* env, jobjectArray packageInfo, android::vintf::DisabledChecks checks) {
100 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;
Bowgo Tsai78934c92017-11-17 17:18:50 +0800112 int32_t status = VintfObject::CheckCompatibility(cPackageInfo, &error, checks);
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_verify(JNIEnv* env, jclass, jobjectArray packageInfo) {
119 return verify(env, packageInfo, ::android::vintf::ENABLE_ALL_CHECKS);
120}
121
122static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) {
123 return verify(env, nullptr, ::android::vintf::DISABLE_AVB_CHECK);
124}
125
Yifan Hongbf498212017-05-04 13:05:24 -0700126static jobjectArray android_os_VintfObject_getHalNamesAndVersions(JNIEnv* env, jclass) {
127 std::set<std::string> halNames;
128 tryAddHalNamesAndVersions(VintfObject::GetDeviceHalManifest(),
129 "device manifest", &halNames);
130 tryAddHalNamesAndVersions(VintfObject::GetFrameworkHalManifest(),
131 "framework manifest", &halNames);
132 return toJavaStringArray(env, halNames);
133}
134
135static jstring android_os_VintfObject_getSepolicyVersion(JNIEnv* env, jclass) {
Yifan Hongd666d5b2017-08-30 10:20:02 -0700136 std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
Yifan Hongbf498212017-05-04 13:05:24 -0700137 if (manifest == nullptr || manifest->type() != SchemaType::DEVICE) {
138 LOG(WARNING) << __FUNCTION__ << "Cannot get device manifest";
139 return nullptr;
140 }
141 std::string cString = to_string(manifest->sepolicyVersion());
142 return env->NewStringUTF(cString.c_str());
143}
144
145static jobject android_os_VintfObject_getVndkSnapshots(JNIEnv* env, jclass) {
Yifan Hongd666d5b2017-08-30 10:20:02 -0700146 std::shared_ptr<const HalManifest> manifest = VintfObject::GetFrameworkHalManifest();
Yifan Hongbf498212017-05-04 13:05:24 -0700147 if (manifest == nullptr || manifest->type() != SchemaType::FRAMEWORK) {
148 LOG(WARNING) << __FUNCTION__ << "Cannot get framework manifest";
149 return nullptr;
150 }
151 jobject jMap = env->NewObject(gHashMapClazz, gHashMapInit);
Yifan Hong8da8798c2018-01-10 16:04:18 -0800152 for (const auto &vndk : manifest->vendorNdks()) {
153 std::string key = vndk.version();
Yifan Hongbf498212017-05-04 13:05:24 -0700154 env->CallObjectMethod(jMap, gHashMapPut,
155 env->NewStringUTF(key.c_str()), toJavaStringArray(env, vndk.libraries()));
156 }
157 return jMap;
158}
159
Yifan Hong0e5e4722018-02-07 14:16:21 -0800160static jobject android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion(JNIEnv* env, jclass) {
161 std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest();
162 if (manifest == nullptr || manifest->level() == Level::UNSPECIFIED) {
163 return nullptr;
164 }
165 return env->CallStaticObjectMethod(gLongClazz, gLongValueOf, static_cast<jlong>(manifest->level()));
166}
167
Yifan Hong4e01db82017-04-05 14:42:05 -0700168// ----------------------------------------------------------------------------
169
170static const JNINativeMethod gVintfObjectMethods[] = {
Yifan Hong4463d992017-05-01 18:43:36 -0700171 {"report", "()[Ljava/lang/String;", (void*)android_os_VintfObject_report},
172 {"verify", "([Ljava/lang/String;)I", (void*)android_os_VintfObject_verify},
Bowgo Tsai78934c92017-11-17 17:18:50 +0800173 {"verifyWithoutAvb", "()I", (void*)android_os_VintfObject_verifyWithoutAvb},
Yifan Hongbf498212017-05-04 13:05:24 -0700174 {"getHalNamesAndVersions", "()[Ljava/lang/String;", (void*)android_os_VintfObject_getHalNamesAndVersions},
175 {"getSepolicyVersion", "()Ljava/lang/String;", (void*)android_os_VintfObject_getSepolicyVersion},
176 {"getVndkSnapshots", "()Ljava/util/Map;", (void*)android_os_VintfObject_getVndkSnapshots},
Yifan Hong0e5e4722018-02-07 14:16:21 -0800177 {"getTargetFrameworkCompatibilityMatrixVersion", "()Ljava/lang/Long;", (void*)android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion},
Yifan Hong4e01db82017-04-05 14:42:05 -0700178};
179
180const char* const kVintfObjectPathName = "android/os/VintfObject";
181
182int register_android_os_VintfObject(JNIEnv* env)
183{
Yifan Hong4463d992017-05-01 18:43:36 -0700184
185 gString = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/String"));
Yifan Hongbf498212017-05-04 13:05:24 -0700186 gHashMapClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/util/HashMap"));
187 gHashMapInit = GetMethodIDOrDie(env, gHashMapClazz, "<init>", "()V");
188 gHashMapPut = GetMethodIDOrDie(env, gHashMapClazz,
189 "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
Yifan Hong0e5e4722018-02-07 14:16:21 -0800190 gLongClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Long"));
191 gLongValueOf = GetStaticMethodIDOrDie(env, gLongClazz, "valueOf", "(J)Ljava/lang/Long;");
Yifan Hong4463d992017-05-01 18:43:36 -0700192
Yifan Hong4e01db82017-04-05 14:42:05 -0700193 return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods,
194 NELEM(gVintfObjectMethods));
195}
196
197};