diff --git a/service/Android.bp b/service/Android.bp index 94061a46e1..e6caf9d28b 100644 --- a/service/Android.bp +++ b/service/Android.bp @@ -90,6 +90,7 @@ cc_library_shared { static_libs: [ "libnet_utils_device_common_bpfjni", "libnet_utils_device_common_bpfutils", + "libnet_utils_device_common_timerfdjni", ], shared_libs: [ "liblog", diff --git a/staticlibs/native/timerfdutils/Android.bp b/staticlibs/native/timerfdutils/Android.bp new file mode 100644 index 0000000000..939a2d2c2b --- /dev/null +++ b/staticlibs/native/timerfdutils/Android.bp @@ -0,0 +1,46 @@ +// 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 { + default_team: "trendy_team_fwk_core_networking", + default_applicable_licenses: ["Android-Apache-2.0"], +} + +cc_library_static { + name: "libnet_utils_device_common_timerfdjni", + srcs: [ + "com_android_net_module_util_TimerFdUtils.cpp", + ], + header_libs: [ + "jni_headers", + ], + shared_libs: [ + "liblog", + "libnativehelper_compat_libc++", + ], + 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__", + ], +} diff --git a/staticlibs/native/timerfdutils/com_android_net_module_util_TimerFdUtils.cpp b/staticlibs/native/timerfdutils/com_android_net_module_util_TimerFdUtils.cpp new file mode 100644 index 0000000000..c4c960d8a5 --- /dev/null +++ b/staticlibs/native/timerfdutils/com_android_net_module_util_TimerFdUtils.cpp @@ -0,0 +1,79 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MSEC_PER_SEC 1000 +#define NSEC_PER_MSEC 1000000 + +namespace android { + +static jint +com_android_net_module_util_TimerFdUtils_createTimerFd(JNIEnv *env, + jclass clazz) { + int tfd; + tfd = timerfd_create(CLOCK_BOOTTIME, 0); + if (tfd == -1) { + jniThrowErrnoException(env, "createTimerFd", tfd); + } + return tfd; +} + +static void +com_android_net_module_util_TimerFdUtils_setTime(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, "setTime", ret); + } +} + +/* + * JNI registration. + */ +static const JNINativeMethod gMethods[] = { + /* name, signature, funcPtr */ + {"createTimerFd", "()I", + (void *)com_android_net_module_util_TimerFdUtils_createTimerFd}, + {"setTime", "(IJ)V", + (void *)com_android_net_module_util_TimerFdUtils_setTime}, +}; + +int register_com_android_net_module_util_TimerFdUtils(JNIEnv *env, + char const *class_name) { + return jniRegisterNativeMethods(env, class_name, gMethods, NELEM(gMethods)); +} + +}; // namespace android diff --git a/tests/unit/jni/Android.bp b/tests/unit/jni/Android.bp index 57a157d2fc..50971e7359 100644 --- a/tests/unit/jni/Android.bp +++ b/tests/unit/jni/Android.bp @@ -42,6 +42,7 @@ cc_library_shared { ], static_libs: [ "libnet_utils_device_common_bpfjni", + "libnet_utils_device_common_timerfdjni", "libtcutils", ], shared_libs: [