diff options
author | 2024-10-08 21:42:03 +0000 | |
---|---|---|
committer | 2024-10-08 22:44:55 +0000 | |
commit | d0d2305d938b262e5081d1cb5cdd11a2806f8385 (patch) | |
tree | acbedcff0a466c83ac6860fe8b7df6bb51dec920 | |
parent | 354a333d785f82c7530e6c4fc1bacce030ae5fcb (diff) |
Add JNI method to retrieve the binder
for services in the mainline module.
Bug: 365585450
Flag: com.android.wifi.flags.mainline_supplicant
Test: Manual test - verify that we can
retrieve the binder and call a method
in the mainline supplicant service
Change-Id: I1c25f839826042098f2186b03b0eb807edb76db8
-rw-r--r-- | apex/Android.bp | 3 | ||||
-rw-r--r-- | jni/Android.bp | 17 | ||||
-rw-r--r-- | jni/com_android_server_ServiceManagerWrapper.cpp | 41 | ||||
-rw-r--r-- | service/java/com/android/server/wifi/mainline_supplicant/ServiceManagerWrapper.java | 49 |
4 files changed, 110 insertions, 0 deletions
diff --git a/apex/Android.bp b/apex/Android.bp index a878695458..f570b8cebd 100644 --- a/apex/Android.bp +++ b/apex/Android.bp @@ -35,6 +35,9 @@ apex_defaults { "ServiceWifiResources", "WifiDialog", ], + jni_libs: [ + "libservice-wifi-jni", + ], defaults: ["r-launched-apex-module"], // Indicates that pre-installed version of this apex can be compressed. // Whether it actually will be compressed is controlled on per-device basis. diff --git a/jni/Android.bp b/jni/Android.bp new file mode 100644 index 0000000000..d67c6aaf3f --- /dev/null +++ b/jni/Android.bp @@ -0,0 +1,17 @@ +cc_library_shared { + name: "libservice-wifi-jni", + min_sdk_version: "30", + cflags: [ + "-Wall", + "-Werror", + ], + srcs: [ + "com_android_server_ServiceManagerWrapper.cpp", + ], + shared_libs: [ + "libbinder_ndk", + ], + apex_available: [ + "com.android.wifi", + ], +} diff --git a/jni/com_android_server_ServiceManagerWrapper.cpp b/jni/com_android_server_ServiceManagerWrapper.cpp new file mode 100644 index 0000000000..2a7a37dd80 --- /dev/null +++ b/jni/com_android_server_ServiceManagerWrapper.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <android/binder_auto_utils.h> +#include <android/binder_ibinder_jni.h> +#include <android/binder_manager.h> +#include <jni.h> + +namespace android { + +// nativeWaitForService +extern "C" JNIEXPORT jobject JNICALL + Java_com_android_server_wifi_mainline_1supplicant_ServiceManagerWrapper_nativeWaitForService__Ljava_lang_String_2( + JNIEnv* env, jobject /* clazz */, jstring serviceNameJni) { + // AServiceManager_isDeclared and AServiceManager_waitForService were added in Android 31. + // Because this method will only be called on 35+, we can suppress the availability warning. + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wunguarded-availability" + const char* serviceName = env->GetStringUTFChars(serviceNameJni, nullptr); + if (AServiceManager_isDeclared(serviceName)) { + return AIBinder_toJavaBinder(env, AServiceManager_waitForService(serviceName)); + } else { + return nullptr; + } + #pragma clang diagnostic pop +} + +}; // namespace android diff --git a/service/java/com/android/server/wifi/mainline_supplicant/ServiceManagerWrapper.java b/service/java/com/android/server/wifi/mainline_supplicant/ServiceManagerWrapper.java new file mode 100644 index 0000000000..828d694431 --- /dev/null +++ b/service/java/com/android/server/wifi/mainline_supplicant/ServiceManagerWrapper.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi.mainline_supplicant; + +import android.annotation.Nullable; +import android.os.IBinder; + +import com.android.modules.utils.build.SdkLevel; + +/** + * Wrapper around ServiceManager APIs that are not directly available to the mainline module. + */ +public final class ServiceManagerWrapper { + static { + System.loadLibrary("service-wifi-jni"); + } + + /** + * Returns the specified service from the service manager. + * + * If the service is not running, service manager will attempt to start it, and this function + * will wait for it to be ready. + * + * @return {@code null} only if there are permission problems or fatal errors + */ + public static @Nullable IBinder waitForService(String serviceName) { + // Underlying implementation requires SDK 31+ + if (!SdkLevel.isAtLeastS()) { + return null; + } + return nativeWaitForService(serviceName); + } + + private static native IBinder nativeWaitForService(String serviceName); +} |