Create CommonConnectivityJni to share between modules

Flag: EXEMPT mainline
Test: TH
Change-Id: Ia85dfd3d979803baca9e971d1083f54dff46de53
This commit is contained in:
Jimi Chen 2025-06-11 23:46:57 +00:00
commit e5a4e6b0ba
3 changed files with 338 additions and 37 deletions

View file

@ -0,0 +1,48 @@
// Copyright (C) 2025 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 {
default_team: "trendy_team_fwk_core_networking",
default_applicable_licenses: ["Android-Apache-2.0"],
}
cc_library_static {
name: "libcommonconnectivityjni",
srcs: [
"com_android_net_module_util_CommonConnectivityJni.cpp",
],
header_libs: [
"jni_headers",
],
shared_libs: [
"liblog",
"libnativehelper_compat_libc++",
],
stl: "libc++_static",
cflags: [
"-Wall",
"-Werror",
"-Wno-unused-parameter",
],
sdk_version: "current",
min_sdk_version: "30",
apex_available: [
"com.android.tethering",
"//apex_available:platform",
],
visibility: [
"//packages/modules/Connectivity:__subpackages__",
"//packages/modules/NetworkStack:__subpackages__",
],
}

View file

@ -0,0 +1,69 @@
/*
* Copyright (C) 2025 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 <sys/timerfd.h>
#include <nativehelper/JNIHelp.h>
#define MSEC_PER_SEC 1000
#define NSEC_PER_MSEC 1000000
namespace android {
static jint createTimerFd(JNIEnv *env, jclass clazz) {
int tfd;
// For safety, the file descriptor should have O_NONBLOCK(TFD_NONBLOCK) set
// using fcntl during creation. This ensures that, in the worst-case
// scenario, an EAGAIN error is returned when reading.
tfd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK);
if (tfd == -1) {
jniThrowErrnoException(env, "createTimerFd", tfd);
}
return tfd;
}
static void setTimerFdTime(JNIEnv *env, jclass clazz, jint tfd,
jlong milliseconds) {
struct itimerspec new_value;
new_value.it_value.tv_sec = milliseconds / MSEC_PER_SEC;
new_value.it_value.tv_nsec = (milliseconds % MSEC_PER_SEC) * NSEC_PER_MSEC;
// Set the interval time to 0 because it's designed for repeated timer
// expirations after the initial expiration, which doesn't fit the current
// usage.
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;
int ret = timerfd_settime(tfd, 0, &new_value, NULL);
if (ret == -1) {
jniThrowErrnoException(env, "setTimerFdTime", ret);
}
}
//------------------------------------------------------------------------------
/*
* JNI registration.
*/
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"createTimerFd", "()I", (void *)createTimerFd},
{"setTimerFdTime", "(IJ)V", (void *)setTimerFdTime},
};
int register_CommonConnectivityJni(JNIEnv *env, char const *class_name) {
return jniRegisterNativeMethods(env, class_name, gMethods, NELEM(gMethods));
}
} // namespace android

View file

@ -52,35 +52,6 @@
namespace android {
static jint createTimerFd(JNIEnv *env, jclass clazz) {
int tfd;
// For safety, the file descriptor should have O_NONBLOCK(TFD_NONBLOCK) set
// using fcntl during creation. This ensures that, in the worst-case scenario,
// an EAGAIN error is returned when reading.
tfd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK);
if (tfd == -1) {
jniThrowErrnoException(env, "createTimerFd", tfd);
}
return tfd;
}
static void setTimerFdTime(JNIEnv *env, jclass clazz, jint tfd,
jlong milliseconds) {
struct itimerspec new_value;
new_value.it_value.tv_sec = milliseconds / MSEC_PER_SEC;
new_value.it_value.tv_nsec = (milliseconds % MSEC_PER_SEC) * NSEC_PER_MSEC;
// Set the interval time to 0 because it's designed for repeated timer
// expirations after the initial expiration, which doesn't fit the current
// usage.
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;
int ret = timerfd_settime(tfd, 0, &new_value, NULL);
if (ret == -1) {
jniThrowErrnoException(env, "setTimerFdTime", ret);
}
}
static jstring getDriverNameForInterface(JNIEnv *env, jclass clazz, jstring jifname) {
base::unique_fd fd(socket(AF_INET6, SOCK_DGRAM, 0));
if (!fd.ok()) {
@ -219,8 +190,6 @@ static void bringUpInterface(JNIEnv *env, jclass /* clazz */, jstring jIface) {
*/
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"createTimerFd", "()I", (void *)createTimerFd},
{"setTimerFdTime", "(IJ)V", (void *)setTimerFdTime},
{"getDriverNameForInterface", "(Ljava/lang/String;)Ljava/lang/String;",
(void *)getDriverNameForInterface},
{"setTunTapCarrierEnabled", "(Ljava/lang/String;IZ)V",