Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 1 | /* |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 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 |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include <vector> |
| 22 | #include <string> |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 23 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 24 | #include <nativehelper/JNIHelp.h> |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 25 | #include <vintf/VintfObject.h> |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 26 | #include <vintf/parse_string.h> |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 27 | #include <vintf/parse_xml.h> |
| 28 | |
| 29 | #include "core_jni_helpers.h" |
| 30 | |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 31 | static jclass gString; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 32 | static jclass gHashMapClazz; |
| 33 | static jmethodID gHashMapInit; |
| 34 | static jmethodID gHashMapPut; |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 35 | static jclass gLongClazz; |
| 36 | static jmethodID gLongValueOf; |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 37 | |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 40 | using vintf::HalManifest; |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 41 | using vintf::Level; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 42 | using vintf::SchemaType; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 43 | using vintf::VintfObject; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 44 | using vintf::XmlConverter; |
| 45 | using vintf::Vndk; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 46 | using vintf::gHalManifestConverter; |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 47 | using vintf::gCompatibilityMatrixConverter; |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 48 | using vintf::to_string; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 49 | |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 50 | template<typename V> |
| 51 | static inline jobjectArray toJavaStringArray(JNIEnv* env, const V& v) { |
| 52 | size_t i; |
| 53 | typename V::const_iterator it; |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 54 | jobjectArray ret = env->NewObjectArray(v.size(), gString, NULL /* init element */); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 55 | for (i = 0, it = v.begin(); it != v.end(); ++i, ++it) { |
| 56 | env->SetObjectArrayElement(ret, i, env->NewStringUTF(it->c_str())); |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 57 | } |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 58 | return ret; |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 61 | template<typename T> |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 62 | static void tryAddSchema(const std::shared_ptr<const T>& object, const XmlConverter<T>& converter, |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 63 | 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 Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 69 | } |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 72 | static void tryAddHalNamesAndVersions(const std::shared_ptr<const HalManifest>& manifest, |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 73 | 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 | |
| 83 | static jobjectArray android_os_VintfObject_report(JNIEnv* env, jclass) |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 84 | { |
| 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 Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 99 | static jint android_os_VintfObject_verifyWithoutAvb(JNIEnv* env, jclass) { |
Yifan Hong | c370a57 | 2019-01-29 16:45:45 -0800 | [diff] [blame] | 100 | std::string error; |
Yifan Hong | 12ebf36 | 2019-12-18 12:52:55 -0800 | [diff] [blame] | 101 | int32_t status = VintfObject::GetInstance()->checkCompatibility(&error, |
Yifan Hong | c370a57 | 2019-01-29 16:45:45 -0800 | [diff] [blame] | 102 | ::android::vintf::CheckFlags::DISABLE_AVB_CHECK); |
| 103 | if (status) |
| 104 | LOG(WARNING) << "VintfObject.verifyWithoutAvb() returns " << status << ": " << error; |
| 105 | return status; |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 108 | static 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 | |
| 117 | static jstring android_os_VintfObject_getSepolicyVersion(JNIEnv* env, jclass) { |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 118 | std::shared_ptr<const HalManifest> manifest = VintfObject::GetDeviceHalManifest(); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 119 | 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 | |
| 127 | static jobject android_os_VintfObject_getVndkSnapshots(JNIEnv* env, jclass) { |
Yifan Hong | d666d5b | 2017-08-30 10:20:02 -0700 | [diff] [blame] | 128 | std::shared_ptr<const HalManifest> manifest = VintfObject::GetFrameworkHalManifest(); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 129 | 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 Hong | 8da8798c | 2018-01-10 16:04:18 -0800 | [diff] [blame] | 134 | for (const auto &vndk : manifest->vendorNdks()) { |
Chih-Hung Hsieh | c134063 | 2018-07-13 13:37:03 -0700 | [diff] [blame] | 135 | const std::string& key = vndk.version(); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 136 | env->CallObjectMethod(jMap, gHashMapPut, |
| 137 | env->NewStringUTF(key.c_str()), toJavaStringArray(env, vndk.libraries())); |
| 138 | } |
| 139 | return jMap; |
| 140 | } |
| 141 | |
Yifan Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 142 | static 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 Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 150 | // ---------------------------------------------------------------------------- |
| 151 | |
| 152 | static const JNINativeMethod gVintfObjectMethods[] = { |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 153 | {"report", "()[Ljava/lang/String;", (void*)android_os_VintfObject_report}, |
Bowgo Tsai | 78934c9 | 2017-11-17 17:18:50 +0800 | [diff] [blame] | 154 | {"verifyWithoutAvb", "()I", (void*)android_os_VintfObject_verifyWithoutAvb}, |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 155 | {"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 Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 158 | {"getTargetFrameworkCompatibilityMatrixVersion", "()Ljava/lang/Long;", (void*)android_os_VintfObject_getTargetFrameworkCompatibilityMatrixVersion}, |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | const char* const kVintfObjectPathName = "android/os/VintfObject"; |
| 162 | |
| 163 | int register_android_os_VintfObject(JNIEnv* env) |
| 164 | { |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 165 | |
| 166 | gString = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/String")); |
Yifan Hong | bf49821 | 2017-05-04 13:05:24 -0700 | [diff] [blame] | 167 | 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 Hong | 0e5e472 | 2018-02-07 14:16:21 -0800 | [diff] [blame] | 171 | gLongClazz = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Long")); |
| 172 | gLongValueOf = GetStaticMethodIDOrDie(env, gLongClazz, "valueOf", "(J)Ljava/lang/Long;"); |
Yifan Hong | 4463d99 | 2017-05-01 18:43:36 -0700 | [diff] [blame] | 173 | |
Yifan Hong | 4e01db8 | 2017-04-05 14:42:05 -0700 | [diff] [blame] | 174 | return RegisterMethodsOrDie(env, kVintfObjectPathName, gVintfObjectMethods, |
| 175 | NELEM(gVintfObjectMethods)); |
| 176 | } |
| 177 | |
| 178 | }; |