Skip to content

Commit

Permalink
[hal] Change usage reporting to string-based (wpilibsuite#7763)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson authored Feb 7, 2025
1 parent bfff891 commit 764ada9
Show file tree
Hide file tree
Showing 188 changed files with 637 additions and 2,298 deletions.
3 changes: 0 additions & 3 deletions .github/actions/pregen/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ runs:
wget https://github.com/HebiRobotics/QuickBuffers/releases/download/1.3.3/protoc-gen-quickbuf-1.3.3-linux-x86_64.exe
chmod +x protoc-gen-quickbuf-1.3.3-linux-x86_64.exe
shell: bash
- name: Regenerate hal
run: ./hal/generate_usage_reporting.py
shell: bash

- name: Regenerate ntcore
run: ./ntcore/generate_topics.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,7 @@ private CameraServer() {}
* @return The USB camera capturing images.
*/
public static UsbCamera startAutomaticCapture() {
UsbCamera camera = startAutomaticCapture(m_defaultUsbDevice.getAndIncrement());
CameraServerSharedStore.getCameraServerShared().reportUsbCamera(camera.getHandle());
return camera;
return startAutomaticCapture(m_defaultUsbDevice.getAndIncrement());
}

/**
Expand All @@ -557,7 +555,7 @@ public static UsbCamera startAutomaticCapture() {
public static UsbCamera startAutomaticCapture(int dev) {
UsbCamera camera = new UsbCamera("USB Camera " + dev, dev);
startAutomaticCapture(camera);
CameraServerSharedStore.getCameraServerShared().reportUsbCamera(camera.getHandle());
CameraServerSharedStore.reportUsage("UsbCamera[" + dev + "]", "auto");
return camera;
}

Expand All @@ -571,7 +569,7 @@ public static UsbCamera startAutomaticCapture(int dev) {
public static UsbCamera startAutomaticCapture(String name, int dev) {
UsbCamera camera = new UsbCamera(name, dev);
startAutomaticCapture(camera);
CameraServerSharedStore.getCameraServerShared().reportUsbCamera(camera.getHandle());
CameraServerSharedStore.reportUsage("UsbCamera[" + dev + "]", "name");
return camera;
}

Expand All @@ -585,7 +583,7 @@ public static UsbCamera startAutomaticCapture(String name, int dev) {
public static UsbCamera startAutomaticCapture(String name, String path) {
UsbCamera camera = new UsbCamera(name, path);
startAutomaticCapture(camera);
CameraServerSharedStore.getCameraServerShared().reportUsbCamera(camera.getHandle());
CameraServerSharedStore.reportUsage("UsbCamera[" + path + "]", "path");
return camera;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,12 @@ public interface CameraServerShared {
void reportDriverStationError(String error);

/**
* Report an video server usage.
* Report usage.
*
* @param id the usage id
* @param resource the resource name
* @param data arbitrary string data
*/
void reportVideoServer(int id);

/**
* Report a usb camera usage.
*
* @param id the usage id
*/
void reportUsbCamera(int id);

/**
* Report an axis camera usage.
*
* @param id the usage id
*/
void reportAxisCamera(int id);
void reportUsage(String resource, String data);

/**
* Get if running on a roboRIO.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@ public static synchronized CameraServerShared getCameraServerShared() {
cameraServerShared =
new CameraServerShared() {
@Override
public void reportVideoServer(int id) {}

@Override
public void reportUsbCamera(int id) {}
public void reportUsage(String resource, String data) {}

@Override
public void reportDriverStationError(String error) {}

@Override
public void reportAxisCamera(int id) {}

@Override
public Long getRobotMainThreadId() {
return null;
Expand All @@ -40,6 +34,16 @@ public Long getRobotMainThreadId() {
return cameraServerShared;
}

/**
* Report usage.
*
* @param resource the resource name
* @param data arbitrary string data
*/
public static void reportUsage(String resource, String data) {
getCameraServerShared().reportUsage(resource, data);
}

/**
* Set the CameraServerShared object.
*
Expand Down
12 changes: 4 additions & 8 deletions cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,19 +471,15 @@ Instance::Instance() {
}

cs::UsbCamera CameraServer::StartAutomaticCapture() {
cs::UsbCamera camera =
StartAutomaticCapture(::GetInstance().m_defaultUsbDevice++);
auto csShared = GetCameraServerShared();
csShared->ReportUsbCamera(camera.GetHandle());
return camera;
return StartAutomaticCapture(::GetInstance().m_defaultUsbDevice++);
}

cs::UsbCamera CameraServer::StartAutomaticCapture(int dev) {
::GetInstance();
cs::UsbCamera camera{fmt::format("USB Camera {}", dev), dev};
StartAutomaticCapture(camera);
auto csShared = GetCameraServerShared();
csShared->ReportUsbCamera(camera.GetHandle());
csShared->ReportUsage(fmt::format("UsbCamera[{}]", dev), "auto");
return camera;
}

Expand All @@ -493,7 +489,7 @@ cs::UsbCamera CameraServer::StartAutomaticCapture(std::string_view name,
cs::UsbCamera camera{name, dev};
StartAutomaticCapture(camera);
auto csShared = GetCameraServerShared();
csShared->ReportUsbCamera(camera.GetHandle());
csShared->ReportUsage(fmt::format("UsbCamera[{}]", dev), "name");
return camera;
}

Expand All @@ -503,7 +499,7 @@ cs::UsbCamera CameraServer::StartAutomaticCapture(std::string_view name,
cs::UsbCamera camera{name, path};
StartAutomaticCapture(camera);
auto csShared = GetCameraServerShared();
csShared->ReportUsbCamera(camera.GetHandle());
csShared->ReportUsage(fmt::format("UsbCamera[{}]", path), "path");
return camera;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
namespace {
class DefaultCameraServerShared : public frc::CameraServerShared {
public:
void ReportUsbCamera(int id) override {}
void ReportAxisCamera(int id) override {}
void ReportVideoServer(int id) override {}
void ReportUsage(std::string_view resource, std::string_view data) override {}
void SetCameraServerErrorV(fmt::string_view format,
fmt::format_args args) override {}
void SetVisionRunnerErrorV(fmt::string_view format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <memory>
#include <string_view>
#include <thread>
#include <utility>

Expand All @@ -14,9 +15,8 @@ namespace frc {
class CameraServerShared {
public:
virtual ~CameraServerShared() = default;
virtual void ReportUsbCamera(int id) = 0;
virtual void ReportAxisCamera(int id) = 0;
virtual void ReportVideoServer(int id) = 0;
virtual void ReportUsage(std::string_view resource,
std::string_view data) = 0;
virtual void SetCameraServerErrorV(fmt::string_view format,
fmt::format_args args) = 0;
virtual void SetVisionRunnerErrorV(fmt::string_view format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public void writeEpilogueFile(
out.println("import static edu.wpi.first.units.Units.Seconds;");
out.println();

out.println("import edu.wpi.first.hal.FRCNetComm;");
out.println("import edu.wpi.first.hal.HAL;");
out.println();

Expand Down Expand Up @@ -89,10 +88,7 @@ public void writeEpilogueFile(
out.println(
"""
static {
HAL.report(
FRCNetComm.tResourceType.kResourceType_LoggingFramework,
FRCNetComm.tInstances.kLoggingFramework_Epilogue
);
HAL.reportUsage("Epilogue", "");
}
""");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ class Example {
import static edu.wpi.first.units.Units.Seconds;
import edu.wpi.first.hal.FRCNetComm;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.epilogue.ExampleLogger;
public final class Epilogue {
static {
HAL.report(
FRCNetComm.tResourceType.kResourceType_LoggingFramework,
FRCNetComm.tInstances.kLoggingFramework_Epilogue
);
HAL.reportUsage("Epilogue", "");
}
private static final EpilogueConfiguration config = new EpilogueConfiguration();
Expand Down Expand Up @@ -92,17 +88,13 @@ public void endCompetition() {}
import static edu.wpi.first.units.Units.Seconds;
import edu.wpi.first.hal.FRCNetComm;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.epilogue.ExampleLogger;
public final class Epilogue {
static {
HAL.report(
FRCNetComm.tResourceType.kResourceType_LoggingFramework,
FRCNetComm.tInstances.kLoggingFramework_Epilogue
);
HAL.reportUsage("Epilogue", "");
}
private static final EpilogueConfiguration config = new EpilogueConfiguration();
Expand Down Expand Up @@ -146,17 +138,13 @@ class Example extends edu.wpi.first.wpilibj.TimedRobot {
import static edu.wpi.first.units.Units.Seconds;
import edu.wpi.first.hal.FRCNetComm;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.epilogue.ExampleLogger;
public final class Epilogue {
static {
HAL.report(
FRCNetComm.tResourceType.kResourceType_LoggingFramework,
FRCNetComm.tInstances.kLoggingFramework_Epilogue
);
HAL.reportUsage("Epilogue", "");
}
private static final EpilogueConfiguration config = new EpilogueConfiguration();
Expand Down Expand Up @@ -234,18 +222,14 @@ class BetaBot extends edu.wpi.first.wpilibj.TimedRobot { }
import static edu.wpi.first.units.Units.Seconds;
import edu.wpi.first.hal.FRCNetComm;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.epilogue.AlphaBotLogger;
import edu.wpi.first.epilogue.BetaBotLogger;
public final class Epilogue {
static {
HAL.report(
FRCNetComm.tResourceType.kResourceType_LoggingFramework,
FRCNetComm.tInstances.kLoggingFramework_Epilogue
);
HAL.reportUsage("Epilogue", "");
}
private static final EpilogueConfiguration config = new EpilogueConfiguration();
Expand Down Expand Up @@ -371,18 +355,14 @@ class Example {
import static edu.wpi.first.units.Units.Seconds;
import edu.wpi.first.hal.FRCNetComm;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.epilogue.ExampleLogger;
import edu.wpi.first.epilogue.CustomLogger;
public final class Epilogue {
static {
HAL.report(
FRCNetComm.tResourceType.kResourceType_LoggingFramework,
FRCNetComm.tInstances.kLoggingFramework_Epilogue
);
HAL.reportUsage("Epilogue", "");
}
private static final EpilogueConfiguration config = new EpilogueConfiguration();
Expand Down
17 changes: 1 addition & 16 deletions hal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ load("@rules_java//java:defs.bzl", "java_binary")
load("//shared/bazel/rules:java_rules.bzl", "wpilib_java_junit5_test")
load("//shared/bazel/rules:jni_rules.bzl", "wpilib_jni_cc_library", "wpilib_jni_java_library")

cc_library(
name = "generated_cc_headers",
hdrs = glob(["src/generated/main/native/include/**"]),
includes = ["src/generated/main/native/include"],
strip_include_prefix = "src/generated/main/native/include",
visibility = ["//hal:__subpackages__"],
)

cc_library(
name = "mrc_cc_headers",
hdrs = glob(["src/mrc/include/**"]),
Expand All @@ -27,12 +19,6 @@ cc_library(
visibility = ["//hal:__subpackages__"],
)

filegroup(
name = "generated_java",
srcs = glob(["src/generated/main/java/**/*.java"]),
visibility = ["//hal:__subpackages__"],
)

SYSTEMCORE_SRCS = glob(["src/main/native/systemcore/**"])

SIM_SRCS = glob(["src/main/native/sim/**"])
Expand All @@ -59,7 +45,6 @@ cc_library(
strip_include_prefix = "src/main/native/include",
visibility = ["//visibility:public"],
deps = [
":generated_cc_headers",
":generated_mrc_cc_headers",
":mrc_cc_headers",
"//ntcore:ntcore.static",
Expand All @@ -80,7 +65,7 @@ wpilib_jni_cc_library(

wpilib_jni_java_library(
name = "hal-java",
srcs = [":generated_java"] + glob(["src/main/java/**/*.java"]),
srcs = glob(["src/main/java/**/*.java"]),
native_libs = [":wpiHaljni"],
visibility = ["//visibility:public"],
deps = [
Expand Down
8 changes: 2 additions & 6 deletions hal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ target_include_directories(
hal
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/main/native/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/generated/main/native/include>
$<INSTALL_INTERFACE:${include_dest}/hal>
)
target_link_libraries(hal PUBLIC ntcore wpiutil)
Expand All @@ -29,7 +28,6 @@ set_property(TARGET hal PROPERTY FOLDER "libraries")
install(TARGETS hal EXPORT hal)
export(TARGETS hal FILE hal.cmake NAMESPACE hal::)
install(DIRECTORY src/main/native/include/ DESTINATION "${include_dest}/hal")
install(DIRECTORY src/generated/main/native/include/ DESTINATION "${include_dest}/hal")

configure_file(hal-config.cmake.in ${WPILIB_BINARY_DIR}/hal-config.cmake)
install(FILES ${WPILIB_BINARY_DIR}/hal-config.cmake DESTINATION share/hal)
Expand All @@ -41,7 +39,7 @@ if(WITH_JAVA)

file(GLOB_RECURSE hal_shared_jni_src src/main/native/cpp/jni/*.cpp)

file(GLOB_RECURSE JAVA_SOURCES src/main/java/*.java src/generated/main/java/*.java)
file(GLOB_RECURSE JAVA_SOURCES src/main/java/*.java)
set(CMAKE_JNI_TARGET true)

add_jar(
Expand Down Expand Up @@ -77,9 +75,7 @@ if(WITH_JAVA_SOURCE)
include(CreateSourceJar)
add_source_jar(
hal_src_jar
BASE_DIRECTORIES
${CMAKE_CURRENT_SOURCE_DIR}/src/main/java
${CMAKE_CURRENT_SOURCE_DIR}/src/generated/main/java
BASE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/src/main/java
OUTPUT_NAME wpiHal-sources
OUTPUT_DIR ${WPILIB_BINARY_DIR}/${java_lib_dest}
)
Expand Down
Loading

0 comments on commit 764ada9

Please sign in to comment.