[hal,wpilib] Switch to new game data (#8584)

Game data is now limited to 8 bytes, and comes through the UDP packets.
This commit is contained in:
Thad House
2026-02-06 21:38:15 -08:00
committed by GitHub
parent ac45c694f3
commit 85adbf990e
45 changed files with 820 additions and 695 deletions

View File

@@ -312,6 +312,29 @@ Java_org_wpilib_hardware_hal_DriverStationJNI_getMatchInfo
return status;
}
/*
* Class: org_wpilib_hardware_hal_DriverStationJNI
* Method: getGameData
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_org_wpilib_hardware_hal_DriverStationJNI_getGameData
(JNIEnv* env, jclass, jstring existing)
{
HAL_GameData gameData;
HAL_GetGameData(&gameData);
std::string_view newView{gameData.gameData};
if (existing != nullptr) {
// Load existing, see if it matches return old
JStringRef existingStr{env, existing};
std::string_view existingView{existingStr};
if (existingView == newView) {
return existing;
}
}
return MakeJString(env, newView);
}
/*
* Class: org_wpilib_hardware_hal_DriverStationJNI
* Method: sendError

View File

@@ -284,17 +284,12 @@ void SetCanStatusObject(JNIEnv* env, jobject canStatus,
void SetMatchInfoObject(JNIEnv* env, jobject matchStatus,
const HAL_MatchInfo& matchInfo) {
static jmethodID func =
env->GetMethodID(matchInfoDataCls, "setData",
"(Ljava/lang/String;Ljava/lang/String;III)V");
env->GetMethodID(matchInfoDataCls, "setData", "(Ljava/lang/String;III)V");
env->CallVoidMethod(
matchStatus, func, MakeJString(env, matchInfo.eventName),
MakeJString(env,
{reinterpret_cast<const char*>(matchInfo.gameSpecificMessage),
matchInfo.gameSpecificMessageSize}),
static_cast<jint>(matchInfo.matchNumber),
static_cast<jint>(matchInfo.replayNumber),
static_cast<jint>(matchInfo.matchType));
env->CallVoidMethod(matchStatus, func, MakeJString(env, matchInfo.eventName),
static_cast<jint>(matchInfo.matchNumber),
static_cast<jint>(matchInfo.replayNumber),
static_cast<jint>(matchInfo.matchType));
}
jbyteArray SetCANReceiveMessageObject(JNIEnv* env, jobject canData,

View File

@@ -583,31 +583,39 @@ Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_getJoystickRumble
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setMatchInfo
* Signature: (Ljava/lang/String;Ljava/lang/String;III)V
* Signature: (Ljava/lang/String;III)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setMatchInfo
(JNIEnv* env, jclass, jstring eventName, jstring gameSpecificMessage,
jint matchNumber, jint replayNumber, jint matchType)
(JNIEnv* env, jclass, jstring eventName, jint matchNumber, jint replayNumber,
jint matchType)
{
JStringRef eventNameRef{env, eventName};
JStringRef gameSpecificMessageRef{env, gameSpecificMessage};
HAL_MatchInfo halMatchInfo;
wpi::util::format_to_n_c_str(halMatchInfo.eventName,
sizeof(halMatchInfo.eventName), "{}",
eventNameRef.str());
wpi::util::format_to_n_c_str(
reinterpret_cast<char*>(halMatchInfo.gameSpecificMessage),
sizeof(halMatchInfo.gameSpecificMessage), "{}",
gameSpecificMessageRef.str());
halMatchInfo.gameSpecificMessageSize = gameSpecificMessageRef.size();
halMatchInfo.matchType = (HAL_MatchType)matchType;
halMatchInfo.matchNumber = matchNumber;
halMatchInfo.replayNumber = replayNumber;
HALSIM_SetMatchInfo(&halMatchInfo);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setGameData
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setGameData
(JNIEnv* env, jclass, jstring gameData)
{
JStringRef gameDataRef{env, gameData};
auto str = wpi::util::make_string(gameDataRef.str());
HALSIM_SetGameDataString(&str);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: registerAllCallbacks
@@ -838,20 +846,6 @@ Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setJoystickName
HALSIM_SetJoystickName(stick, &str);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setGameSpecificMessage
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setGameSpecificMessage
(JNIEnv* env, jclass, jstring message)
{
JStringRef messageJString{env, message};
auto str = wpi::util::make_string(messageJString);
HALSIM_SetGameSpecificMessage(&str);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setEventName

View File

@@ -2,6 +2,7 @@
// 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 <string>
#include <utility>
#include "wpi/hal/proto/ControlData.h"
@@ -65,12 +66,14 @@ constexpr mrc::ControlFlags ToControlWord(uint32_t Word) {
std::optional<mrc::ControlData> wpi::util::Protobuf<mrc::ControlData>::Unpack(
InputStream& Stream) {
wpi::util::UnpackCallback<mrc::Joystick, MRC_MAX_NUM_JOYSTICKS> JoystickCb;
wpi::util::UnpackCallback<std::string> GameDataCb;
mrc_proto_ProtobufControlData Msg{
.MatchTime = 0,
.Joysticks = JoystickCb.Callback(),
.CurrentOpMode = 0,
.ControlWord = 0,
.GameData = GameDataCb.Callback(),
};
if (!Stream.Decode(Msg)) {
@@ -78,6 +81,7 @@ std::optional<mrc::ControlData> wpi::util::Protobuf<mrc::ControlData>::Unpack(
}
auto Joysticks = JoystickCb.Items();
auto GameData = GameDataCb.Items();
mrc::ControlData ControlData;
@@ -90,6 +94,10 @@ std::optional<mrc::ControlData> wpi::util::Protobuf<mrc::ControlData>::Unpack(
ControlData.Joysticks()[i] = std::move(Joysticks[i]);
}
if (GameData.size() >= 1) {
ControlData.MoveGameData(std::move(GameData[0]));
}
return ControlData;
}
@@ -97,12 +105,15 @@ bool wpi::util::Protobuf<mrc::ControlData>::Pack(
OutputStream& Stream, const mrc::ControlData& Value) {
std::span<const mrc::Joystick> Sticks = Value.Joysticks();
wpi::util::PackCallback Joysticks{Sticks};
std::string_view GameData = Value.GetGameData();
wpi::util::PackCallback GameDataCb{&GameData};
mrc_proto_ProtobufControlData Msg{
.MatchTime = Value.MatchTime,
.Joysticks = Joysticks.Callback(),
.CurrentOpMode = Value.CurrentOpMode.ToValue(),
.ControlWord = FromControlWord(Value.ControlWord),
.GameData = GameDataCb.Callback(),
};
return Stream.Encode(Msg);