From 1cad4f64a41b5d19b1aadefeafe84be133cd304b Mon Sep 17 00:00:00 2001 From: Thad House Date: Thu, 16 Jan 2025 09:49:40 -0800 Subject: [PATCH] [hal] Add high level way of getting systemserver NT instance (#7683) --- hal/BUILD.bazel | 4 ++++ hal/CMakeLists.txt | 2 +- .../edu/wpi/first/hal/SystemServerJNI.java | 23 ++++++++++++++++++ .../main/native/cpp/jni/SystemServerJNI.cpp | 24 +++++++++++++++++++ .../main/native/include/hal/SystemServer.h | 17 +++++++++++++ hal/src/main/native/sim/SystemServer.cpp | 12 ++++++++++ .../native/systemcore/FRCDriverStation.cpp | 2 +- hal/src/main/native/systemcore/HAL.cpp | 2 +- hal/src/main/native/systemcore/SmartIo.cpp | 2 +- .../main/native/systemcore/SystemServer.cpp | 10 +++++++- ...{SystemServer.h => SystemServerInternal.h} | 0 .../main/java/NetworkTableInstance.java.jinja | 10 ++++++++ .../networktables/NetworkTableInstance.java | 10 ++++++++ wpilibc/src/main/native/cpp/SystemServer.cpp | 15 ++++++++++++ .../main/native/include/frc/SystemServer.h | 18 ++++++++++++++ .../edu/wpi/first/wpilibj/SystemServer.java | 23 ++++++++++++++++++ 16 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 hal/src/main/java/edu/wpi/first/hal/SystemServerJNI.java create mode 100644 hal/src/main/native/cpp/jni/SystemServerJNI.cpp create mode 100644 hal/src/main/native/include/hal/SystemServer.h create mode 100644 hal/src/main/native/sim/SystemServer.cpp rename hal/src/main/native/systemcore/{SystemServer.h => SystemServerInternal.h} (100%) create mode 100644 wpilibc/src/main/native/cpp/SystemServer.cpp create mode 100644 wpilibc/src/main/native/include/frc/SystemServer.h create mode 100644 wpilibj/src/main/java/edu/wpi/first/wpilibj/SystemServer.java diff --git a/hal/BUILD.bazel b/hal/BUILD.bazel index f0f54b1e1d..6f8097b070 100644 --- a/hal/BUILD.bazel +++ b/hal/BUILD.bazel @@ -69,6 +69,8 @@ cc_library( ":generated_cc_headers", ":mrc_cc_headers", ":generated_mrc_cc_headers", + "//ntcore:ntcore.static", + "//wpinet:wpinet.static", "//wpiutil:wpiutil.static", ] + HAL_DEPS, ) @@ -78,6 +80,8 @@ java_library( srcs = [":generated_java"] + glob(["src/main/java/**/*.java"]), visibility = ["//visibility:public"], deps = [ + "//ntcore:networktables-java", + "//wpinet:wpinet-java", "//wpiutil:wpiutil-java", ], ) diff --git a/hal/CMakeLists.txt b/hal/CMakeLists.txt index ebcd1d6e48..84a5555491 100644 --- a/hal/CMakeLists.txt +++ b/hal/CMakeLists.txt @@ -22,7 +22,7 @@ target_include_directories( $ $ ) -target_link_libraries(hal PUBLIC wpiutil) +target_link_libraries(hal PUBLIC ntcore wpiutil) set_property(TARGET hal PROPERTY FOLDER "libraries") diff --git a/hal/src/main/java/edu/wpi/first/hal/SystemServerJNI.java b/hal/src/main/java/edu/wpi/first/hal/SystemServerJNI.java new file mode 100644 index 0000000000..b039eb02c2 --- /dev/null +++ b/hal/src/main/java/edu/wpi/first/hal/SystemServerJNI.java @@ -0,0 +1,23 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.hal; + +/** + * SystemServer JNI Functions. + * + * @see "SystemServer.h" + */ +public class SystemServerJNI extends JNIWrapper { + /** + * Gets the system server NT handle. + * + * @return The system server NT handle + * @see "HAL_GetSystemServerHandle" + */ + public static native int getSystemServerHandle(); + + /** Utility class. */ + private SystemServerJNI() {} +} diff --git a/hal/src/main/native/cpp/jni/SystemServerJNI.cpp b/hal/src/main/native/cpp/jni/SystemServerJNI.cpp new file mode 100644 index 0000000000..8fbf836130 --- /dev/null +++ b/hal/src/main/native/cpp/jni/SystemServerJNI.cpp @@ -0,0 +1,24 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include + +#include "edu_wpi_first_hal_SystemServerJNI.h" +#include "hal/SystemServer.h" + +extern "C" { + +/* + * Class: edu_wpi_first_hal_SystemServerJNI + * Method: getSystemServerHandle + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_SystemServerJNI_getSystemServerHandle + (JNIEnv*, jclass) +{ + return HAL_GetSystemServerHandle(); +} + +} // extern "C" diff --git a/hal/src/main/native/include/hal/SystemServer.h b/hal/src/main/native/include/hal/SystemServer.h new file mode 100644 index 0000000000..efcfad16de --- /dev/null +++ b/hal/src/main/native/include/hal/SystemServer.h @@ -0,0 +1,17 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +NT_Inst HAL_GetSystemServerHandle(void); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/hal/src/main/native/sim/SystemServer.cpp b/hal/src/main/native/sim/SystemServer.cpp new file mode 100644 index 0000000000..5b0b964bc8 --- /dev/null +++ b/hal/src/main/native/sim/SystemServer.cpp @@ -0,0 +1,12 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include "hal/SystemServer.h" + +extern "C" { + +NT_Inst HAL_GetSystemServerHandle(void) { + return 0; +} +} // extern "C" diff --git a/hal/src/main/native/systemcore/FRCDriverStation.cpp b/hal/src/main/native/systemcore/FRCDriverStation.cpp index 613a6cfff3..0d3d0938d9 100644 --- a/hal/src/main/native/systemcore/FRCDriverStation.cpp +++ b/hal/src/main/native/systemcore/FRCDriverStation.cpp @@ -27,7 +27,7 @@ #include #include "HALInitializer.h" -#include "SystemServer.h" +#include "SystemServerInternal.h" #include "hal/DriverStation.h" #include "hal/Errors.h" #include "hal/proto/ControlData.h" diff --git a/hal/src/main/native/systemcore/HAL.cpp b/hal/src/main/native/systemcore/HAL.cpp index 2775a612c9..a573723257 100644 --- a/hal/src/main/native/systemcore/HAL.cpp +++ b/hal/src/main/native/systemcore/HAL.cpp @@ -27,7 +27,7 @@ #include "CANInternal.h" #include "HALInitializer.h" #include "HALInternal.h" -#include "SystemServer.h" +#include "SystemServerInternal.h" #include "hal/DriverStation.h" #include "hal/Errors.h" #include "hal/Notifier.h" diff --git a/hal/src/main/native/systemcore/SmartIo.cpp b/hal/src/main/native/systemcore/SmartIo.cpp index d5179c73ce..763c0c63e6 100644 --- a/hal/src/main/native/systemcore/SmartIo.cpp +++ b/hal/src/main/native/systemcore/SmartIo.cpp @@ -7,7 +7,7 @@ #include #include "HALInitializer.h" -#include "SystemServer.h" +#include "SystemServerInternal.h" namespace hal { diff --git a/hal/src/main/native/systemcore/SystemServer.cpp b/hal/src/main/native/systemcore/SystemServer.cpp index ff38070eed..ea5b01b8ae 100644 --- a/hal/src/main/native/systemcore/SystemServer.cpp +++ b/hal/src/main/native/systemcore/SystemServer.cpp @@ -2,8 +2,9 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. -#include "SystemServer.h" +#include "hal/SystemServer.h" +#include "SystemServerInternal.h" #include "mrc/NtNetComm.h" namespace hal { @@ -33,3 +34,10 @@ nt::NetworkTableInstance GetSystemServer() { } } // namespace hal + +extern "C" { + +NT_Inst HAL_GetSystemServerHandle(void) { + return hal::ServerInstance.GetHandle(); +} +} // extern "C" diff --git a/hal/src/main/native/systemcore/SystemServer.h b/hal/src/main/native/systemcore/SystemServerInternal.h similarity index 100% rename from hal/src/main/native/systemcore/SystemServer.h rename to hal/src/main/native/systemcore/SystemServerInternal.h diff --git a/ntcore/src/generate/main/java/NetworkTableInstance.java.jinja b/ntcore/src/generate/main/java/NetworkTableInstance.java.jinja index 529b25015e..50883074a3 100644 --- a/ntcore/src/generate/main/java/NetworkTableInstance.java.jinja +++ b/ntcore/src/generate/main/java/NetworkTableInstance.java.jinja @@ -134,6 +134,16 @@ public final class NetworkTableInstance implements AutoCloseable { return inst; } + /** + * Create an instance from a native handle. This instance is not owned, closing will do nothing. + * + * @param handle Native instance handle + * @return Instance + */ + public static NetworkTableInstance fromNativeHandle(int handle) { + return new NetworkTableInstance(handle); + } + /** * Gets the native handle for the instance. * diff --git a/ntcore/src/generated/main/java/edu/wpi/first/networktables/NetworkTableInstance.java b/ntcore/src/generated/main/java/edu/wpi/first/networktables/NetworkTableInstance.java index 361e670d83..5b116148f7 100644 --- a/ntcore/src/generated/main/java/edu/wpi/first/networktables/NetworkTableInstance.java +++ b/ntcore/src/generated/main/java/edu/wpi/first/networktables/NetworkTableInstance.java @@ -134,6 +134,16 @@ public final class NetworkTableInstance implements AutoCloseable { return inst; } + /** + * Create an instance from a native handle. This instance is not owned, closing will do nothing. + * + * @param handle Native instance handle + * @return Instance + */ + public static NetworkTableInstance fromNativeHandle(int handle) { + return new NetworkTableInstance(handle); + } + /** * Gets the native handle for the instance. * diff --git a/wpilibc/src/main/native/cpp/SystemServer.cpp b/wpilibc/src/main/native/cpp/SystemServer.cpp new file mode 100644 index 0000000000..0e914f7c38 --- /dev/null +++ b/wpilibc/src/main/native/cpp/SystemServer.cpp @@ -0,0 +1,15 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include "frc/SystemServer.h" + +#include + +namespace frc { + +nt::NetworkTableInstance SystemServer::GetSystemServer() { + return nt::NetworkTableInstance{HAL_GetSystemServerHandle()}; +} + +} // namespace frc diff --git a/wpilibc/src/main/native/include/frc/SystemServer.h b/wpilibc/src/main/native/include/frc/SystemServer.h new file mode 100644 index 0000000000..eda65595f2 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/SystemServer.h @@ -0,0 +1,18 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#pragma once + +#include + +namespace frc { + +class SystemServer { + public: + SystemServer() = delete; + + static nt::NetworkTableInstance GetSystemServer(); +}; + +} // namespace frc diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SystemServer.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SystemServer.java new file mode 100644 index 0000000000..aa47afdf15 --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SystemServer.java @@ -0,0 +1,23 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.wpilibj; + +import edu.wpi.first.hal.SystemServerJNI; +import edu.wpi.first.networktables.NetworkTableInstance; + +/** Class to get system server NT instance. */ +public final class SystemServer { + /** + * Gets the system server NT Instance. + * + * @return NT Instance + */ + public static NetworkTableInstance getSystemServer() { + return NetworkTableInstance.fromNativeHandle(SystemServerJNI.getSystemServerHandle()); + } + + /** Utility Class. */ + private SystemServer() {} +}