Merge "[Thread] add ServiceManagerWrapper" into main

This commit is contained in:
Kangping Dong 2023-11-08 12:13:26 +00:00 • committed by Gerrit Code Review
commit 4a7c729a27
5 changed files with 108 additions and 0 deletions

View file

@ -107,11 +107,16 @@ cc_library_shared {
"-Werror",
"-Wno-unused-parameter",
"-Wthread-safety",
// AServiceManager_waitForService is available on only 31+, but it's still safe for Thread
// service because it's enabled on only 34+
"-Wno-unguarded-availability",
],
srcs: [
":services.connectivity-netstats-jni-sources",
"jni/com_android_server_BpfNetMaps.cpp",
"jni/com_android_server_connectivity_ClatCoordinator.cpp",
"jni/com_android_server_ServiceManagerWrapper.cpp",
"jni/com_android_server_TestNetworkService.cpp",
"jni/onload.cpp",
],
@ -128,6 +133,7 @@ cc_library_shared {
],
shared_libs: [
"libbase",
"libbinder_ndk",
"libcutils",
"libnetdutils",
"liblog",
@ -203,6 +209,8 @@ java_library {
visibility: [
"//packages/modules/Connectivity/service-t",
"//packages/modules/Connectivity/tests:__subpackages__",
"//packages/modules/Connectivity/thread/service:__subpackages__",
"//packages/modules/Connectivity/thread/tests:__subpackages__",
],
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 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_ibinder_jni.h>
#include <android/binder_manager.h>
#include <jni.h>
#include "nativehelper/JNIHelp.h"
#include <nativehelper/ScopedUtfChars.h>
#include <private/android_filesystem_config.h>
namespace android {
static jobject com_android_server_ServiceManagerWrapper_waitForService(
JNIEnv* env, jobject clazz, jstring serviceName) {
ScopedUtfChars name(env, serviceName);
return AIBinder_toJavaBinder(env, AServiceManager_waitForService(name.c_str()));
}
/*
* JNI registration.
*/
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"nativeWaitForService",
"(Ljava/lang/String;)Landroid/os/IBinder;",
(void*)com_android_server_ServiceManagerWrapper_waitForService},
};
int register_com_android_server_ServiceManagerWrapper(JNIEnv* env) {
return jniRegisterNativeMethods(env,
"android/net/connectivity/com/android/server/ServiceManagerWrapper",
gMethods, NELEM(gMethods));
}
}; // namespace android

View file

@ -26,6 +26,7 @@ int register_com_android_server_BpfNetMaps(JNIEnv* env);
int register_com_android_server_connectivity_ClatCoordinator(JNIEnv* env);
int register_android_server_net_NetworkStatsFactory(JNIEnv* env);
int register_android_server_net_NetworkStatsService(JNIEnv* env);
int register_com_android_server_ServiceManagerWrapper(JNIEnv* env);
extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
JNIEnv *env;
@ -38,6 +39,10 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
return JNI_ERR;
}
if (register_com_android_server_ServiceManagerWrapper(env) < 0) {
return JNI_ERR;
}
if (android::modules::sdklevel::IsAtLeastT()) {
if (register_com_android_server_BpfNetMaps(env) < 0) {
return JNI_ERR;

View file

@ -0,0 +1,46 @@
/*
* Copyright (C) 2023 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;
import android.annotation.RequiresApi;
import android.os.IBinder;
import android.os.Build;
import android.os.ServiceManager;
/** Provides a way to access {@link ServiceManager#waitForService} API. */
@RequiresApi(Build.VERSION_CODES.S)
public final class ServiceManagerWrapper {
static {
System.loadLibrary("service-connectivity");
}
private ServiceManagerWrapper() {}
/**
* 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 IBinder waitForService(String serviceName) {
return nativeWaitForService(serviceName);
}
private static native IBinder nativeWaitForService(String serviceName);
}

View file

@ -34,6 +34,7 @@ java_library {
srcs: [":service-thread-sources"],
libs: [
"framework-connectivity-t-pre-jarjar",
"service-connectivity-pre-jarjar",
],
static_libs: [
"net-utils-device-common",