Create a timerfd JNI class
The timerfd delivers timer expiration notifications via a file descriptor, which can be used to implement delayed callbacks. Create a JNI class to interface with the timerfd system calls. Bug: 366373064 Test: m Change-Id: I0ae7a4cf618838e614b2fa4523b55a4d0e8f74e2
This commit is contained in:
parent
511faf74e9
commit
eb73d4ddda
4 changed files with 127 additions and 0 deletions
|
|
@ -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",
|
||||
|
|
|
|||
46
staticlibs/native/timerfdutils/Android.bp
Normal file
46
staticlibs/native/timerfdutils/Android.bp
Normal file
|
|
@ -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__",
|
||||
],
|
||||
}
|
||||
|
|
@ -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 <errno.h>
|
||||
#include <jni.h>
|
||||
#include <nativehelper/JNIHelp.h>
|
||||
#include <nativehelper/scoped_utf_chars.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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
|
||||
|
|
@ -42,6 +42,7 @@ cc_library_shared {
|
|||
],
|
||||
static_libs: [
|
||||
"libnet_utils_device_common_bpfjni",
|
||||
"libnet_utils_device_common_timerfdjni",
|
||||
"libtcutils",
|
||||
],
|
||||
shared_libs: [
|
||||
|
|
|
|||
Loading…
Reference in a new issue