Skip to content

Commit

Permalink
[hal] Add high level way of getting systemserver NT instance (wpilibs…
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse authored Jan 16, 2025
1 parent 58cb395 commit 1cad4f6
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 5 deletions.
4 changes: 4 additions & 0 deletions hal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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",
],
)
Expand Down
2 changes: 1 addition & 1 deletion hal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ target_include_directories(
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/generated/main/native/include>
$<INSTALL_INTERFACE:${include_dest}/hal>
)
target_link_libraries(hal PUBLIC wpiutil)
target_link_libraries(hal PUBLIC ntcore wpiutil)

set_property(TARGET hal PROPERTY FOLDER "libraries")

Expand Down
23 changes: 23 additions & 0 deletions hal/src/main/java/edu/wpi/first/hal/SystemServerJNI.java
Original file line number Diff line number Diff line change
@@ -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() {}
}
24 changes: 24 additions & 0 deletions hal/src/main/native/cpp/jni/SystemServerJNI.cpp
Original file line number Diff line number Diff line change
@@ -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 <jni.h>

#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"
17 changes: 17 additions & 0 deletions hal/src/main/native/include/hal/SystemServer.h
Original file line number Diff line number Diff line change
@@ -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 <ntcore_c.h>

#ifdef __cplusplus
extern "C" {
#endif

NT_Inst HAL_GetSystemServerHandle(void);

#ifdef __cplusplus
} // extern "C"
#endif
12 changes: 12 additions & 0 deletions hal/src/main/native/sim/SystemServer.cpp
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion hal/src/main/native/systemcore/FRCDriverStation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <wpi/timestamp.h>

#include "HALInitializer.h"
#include "SystemServer.h"
#include "SystemServerInternal.h"
#include "hal/DriverStation.h"
#include "hal/Errors.h"
#include "hal/proto/ControlData.h"
Expand Down
2 changes: 1 addition & 1 deletion hal/src/main/native/systemcore/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion hal/src/main/native/systemcore/SmartIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <atomic>

#include "HALInitializer.h"
#include "SystemServer.h"
#include "SystemServerInternal.h"

namespace hal {

Expand Down
10 changes: 9 additions & 1 deletion hal/src/main/native/systemcore/SystemServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -33,3 +34,10 @@ nt::NetworkTableInstance GetSystemServer() {
}

} // namespace hal

extern "C" {

NT_Inst HAL_GetSystemServerHandle(void) {
return hal::ServerInstance.GetHandle();
}
} // extern "C"
10 changes: 10 additions & 0 deletions ntcore/src/generate/main/java/NetworkTableInstance.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions wpilibc/src/main/native/cpp/SystemServer.cpp
Original file line number Diff line number Diff line change
@@ -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 <hal/SystemServer.h>

namespace frc {

nt::NetworkTableInstance SystemServer::GetSystemServer() {
return nt::NetworkTableInstance{HAL_GetSystemServerHandle()};
}

} // namespace frc
18 changes: 18 additions & 0 deletions wpilibc/src/main/native/include/frc/SystemServer.h
Original file line number Diff line number Diff line change
@@ -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 <networktables/NetworkTableInstance.h>

namespace frc {

class SystemServer {
public:
SystemServer() = delete;

static nt::NetworkTableInstance GetSystemServer();
};

} // namespace frc
23 changes: 23 additions & 0 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/SystemServer.java
Original file line number Diff line number Diff line change
@@ -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() {}
}

0 comments on commit 1cad4f6

Please sign in to comment.