From bde383f7639ce2c32383b7446fa7e1c8fc9e2c2f Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Fri, 9 Dec 2022 16:10:23 -0500 Subject: [PATCH] [hal] Replace const char* with std::string_view in Driver Station sim functions (#4532) --- .../src/lib/native/include/glass/other/FMS.h | 2 +- .../include/glass/networktables/NTFMS.h | 2 +- .../athena/mockdata/DriverStationData.cpp | 6 +-- .../jni/simulation/DriverStationDataJNI.cpp | 9 ++-- .../hal/simulation/DriverStationData.h | 8 ++-- .../native/sim/mockdata/DriverStationData.cpp | 45 ++++++++++++------- .../sim/mockdata/DriverStationDataInternal.h | 6 +-- .../src/main/native/cpp/DriverStationGui.cpp | 4 +- .../native/cpp/WSProvider_DriverStation.cpp | 4 +- .../cpp/simulation/DriverStationSim.cpp | 12 ++--- .../include/frc/simulation/DriverStationSim.h | 6 +-- 11 files changed, 60 insertions(+), 44 deletions(-) diff --git a/glass/src/lib/native/include/glass/other/FMS.h b/glass/src/lib/native/include/glass/other/FMS.h index a920f967a2..5970e93e22 100644 --- a/glass/src/lib/native/include/glass/other/FMS.h +++ b/glass/src/lib/native/include/glass/other/FMS.h @@ -38,7 +38,7 @@ class FMSModel : public Model { virtual void SetEnabled(bool val) = 0; virtual void SetTest(bool val) = 0; virtual void SetAutonomous(bool val) = 0; - virtual void SetGameSpecificMessage(const char* val) = 0; + virtual void SetGameSpecificMessage(std::string_view val) = 0; }; /** diff --git a/glass/src/libnt/native/include/glass/networktables/NTFMS.h b/glass/src/libnt/native/include/glass/networktables/NTFMS.h index b8940013e2..5898080f12 100644 --- a/glass/src/libnt/native/include/glass/networktables/NTFMS.h +++ b/glass/src/libnt/native/include/glass/networktables/NTFMS.h @@ -47,7 +47,7 @@ class NTFMSModel : public FMSModel { void SetEnabled(bool val) override {} void SetTest(bool val) override {} void SetAutonomous(bool val) override {} - void SetGameSpecificMessage(const char* val) override {} + void SetGameSpecificMessage(std::string_view val) override {} void Update() override; bool Exists() override; diff --git a/hal/src/main/native/athena/mockdata/DriverStationData.cpp b/hal/src/main/native/athena/mockdata/DriverStationData.cpp index ba519fcb80..76a8e8bf1e 100644 --- a/hal/src/main/native/athena/mockdata/DriverStationData.cpp +++ b/hal/src/main/native/athena/mockdata/DriverStationData.cpp @@ -103,13 +103,13 @@ void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {} void HALSIM_SetJoystickType(int32_t stick, int32_t type) {} -void HALSIM_SetJoystickName(int32_t stick, const char* name) {} +void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size) {} void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) {} -void HALSIM_SetGameSpecificMessage(const char* message) {} +void HALSIM_SetGameSpecificMessage(const char* message, size_t size) {} -void HALSIM_SetEventName(const char* name) {} +void HALSIM_SetEventName(const char* name, size_t size) {} void HALSIM_SetMatchType(HAL_MatchType type) {} diff --git a/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp b/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp index acecacb2dc..c50b8e306e 100644 --- a/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp +++ b/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp @@ -732,7 +732,8 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickName (JNIEnv* env, jclass, jint stick, jstring name) { - HALSIM_SetJoystickName(stick, JStringRef{env, name}.c_str()); + JStringRef nameJString{env, name}; + HALSIM_SetJoystickName(stick, nameJString.c_str(), nameJString.size()); } /* @@ -756,7 +757,8 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setGameSpecificMessage (JNIEnv* env, jclass, jstring message) { - HALSIM_SetGameSpecificMessage(JStringRef{env, message}.c_str()); + JStringRef messageJString{env, message}; + HALSIM_SetGameSpecificMessage(messageJString.c_str(), messageJString.size()); } /* @@ -768,7 +770,8 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setEventName (JNIEnv* env, jclass, jstring name) { - HALSIM_SetEventName(JStringRef{env, name}.c_str()); + JStringRef nameJString{env, name}; + HALSIM_SetEventName(nameJString.c_str(), nameJString.size()); } /* diff --git a/hal/src/main/native/include/hal/simulation/DriverStationData.h b/hal/src/main/native/include/hal/simulation/DriverStationData.h index 87223ecaf5..b10cf03262 100644 --- a/hal/src/main/native/include/hal/simulation/DriverStationData.h +++ b/hal/src/main/native/include/hal/simulation/DriverStationData.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "hal/DriverStationTypes.h" #include "hal/Types.h" #include "hal/simulation/NotifyListener.h" @@ -145,11 +147,11 @@ void HALSIM_GetJoystickCounts(int32_t stick, int32_t* axisCount, void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox); void HALSIM_SetJoystickType(int32_t stick, int32_t type); -void HALSIM_SetJoystickName(int32_t stick, const char* name); +void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size); void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type); -void HALSIM_SetGameSpecificMessage(const char* message); -void HALSIM_SetEventName(const char* name); +void HALSIM_SetGameSpecificMessage(const char* message, size_t size); +void HALSIM_SetEventName(const char* name, size_t size); void HALSIM_SetMatchType(HAL_MatchType type); void HALSIM_SetMatchNumber(int32_t matchNumber); void HALSIM_SetReplayNumber(int32_t replayNumber); diff --git a/hal/src/main/native/sim/mockdata/DriverStationData.cpp b/hal/src/main/native/sim/mockdata/DriverStationData.cpp index 3756d6e492..a0cae1c9a2 100644 --- a/hal/src/main/native/sim/mockdata/DriverStationData.cpp +++ b/hal/src/main/native/sim/mockdata/DriverStationData.cpp @@ -339,14 +339,17 @@ void DriverStationData::SetJoystickType(int32_t stick, int32_t type) { m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor); } -void DriverStationData::SetJoystickName(int32_t stick, const char* name) { +void DriverStationData::SetJoystickName(int32_t stick, const char* name, + size_t size) { if (stick < 0 || stick >= kNumJoysticks) { return; } std::scoped_lock lock(m_joystickDataMutex); - std::strncpy(m_joystickData[stick].descriptor.name, name, - sizeof(m_joystickData[stick].descriptor.name) - 1); - *(std::end(m_joystickData[stick].descriptor.name) - 1) = '\0'; + if (size > sizeof(m_joystickData[stick].descriptor.name) - 1) { + size = sizeof(m_joystickData[stick].descriptor.name) - 1; + } + std::strncpy(m_joystickData[stick].descriptor.name, name, size); + m_joystickData[stick].descriptor.name[size] = '\0'; m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor); } @@ -363,19 +366,27 @@ void DriverStationData::SetJoystickAxisType(int32_t stick, int32_t axis, m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor); } -void DriverStationData::SetGameSpecificMessage(const char* message) { +void DriverStationData::SetGameSpecificMessage(const char* message, + size_t size) { std::scoped_lock lock(m_matchInfoMutex); + if (size > sizeof(m_matchInfo.gameSpecificMessage) - 1) { + size = sizeof(m_matchInfo.gameSpecificMessage) - 1; + } std::strncpy(reinterpret_cast(m_matchInfo.gameSpecificMessage), - message, sizeof(m_matchInfo.gameSpecificMessage) - 1); - *(std::end(m_matchInfo.gameSpecificMessage) - 1) = '\0'; - m_matchInfo.gameSpecificMessageSize = std::strlen(message); + message, size); + m_matchInfo.gameSpecificMessage[size] = '\0'; + m_matchInfo.gameSpecificMessageSize = + std::strlen(reinterpret_cast(m_matchInfo.gameSpecificMessage)); m_matchInfoCallbacks(&m_matchInfo); } -void DriverStationData::SetEventName(const char* name) { +void DriverStationData::SetEventName(const char* name, size_t size) { std::scoped_lock lock(m_matchInfoMutex); - std::strncpy(m_matchInfo.eventName, name, sizeof(m_matchInfo.eventName) - 1); - *(std::end(m_matchInfo.eventName) - 1) = '\0'; + if (size > sizeof(m_matchInfo.eventName) - 1) { + size = sizeof(m_matchInfo.eventName) - 1; + } + std::strncpy(m_matchInfo.eventName, name, size); + m_matchInfo.eventName[size] = '\0'; m_matchInfoCallbacks(&m_matchInfo); } @@ -540,20 +551,20 @@ void HALSIM_SetJoystickType(int32_t stick, int32_t type) { SimDriverStationData->SetJoystickType(stick, type); } -void HALSIM_SetJoystickName(int32_t stick, const char* name) { - SimDriverStationData->SetJoystickName(stick, name); +void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size) { + SimDriverStationData->SetJoystickName(stick, name, size); } void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) { SimDriverStationData->SetJoystickAxisType(stick, axis, type); } -void HALSIM_SetGameSpecificMessage(const char* message) { - SimDriverStationData->SetGameSpecificMessage(message); +void HALSIM_SetGameSpecificMessage(const char* message, size_t size) { + SimDriverStationData->SetGameSpecificMessage(message, size); } -void HALSIM_SetEventName(const char* name) { - SimDriverStationData->SetEventName(name); +void HALSIM_SetEventName(const char* name, size_t size) { + SimDriverStationData->SetEventName(name, size); } void HALSIM_SetMatchType(HAL_MatchType type) { diff --git a/hal/src/main/native/sim/mockdata/DriverStationDataInternal.h b/hal/src/main/native/sim/mockdata/DriverStationDataInternal.h index 2470b048f8..763b465f32 100644 --- a/hal/src/main/native/sim/mockdata/DriverStationDataInternal.h +++ b/hal/src/main/native/sim/mockdata/DriverStationDataInternal.h @@ -107,11 +107,11 @@ class DriverStationData { void SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox); void SetJoystickType(int32_t stick, int32_t type); - void SetJoystickName(int32_t stick, const char* name); + void SetJoystickName(int32_t stick, const char* name, size_t size); void SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type); - void SetGameSpecificMessage(const char* message); - void SetEventName(const char* name); + void SetGameSpecificMessage(const char* message, size_t size); + void SetEventName(const char* name, size_t size); void SetMatchType(HAL_MatchType type); void SetMatchNumber(int32_t matchNumber); void SetReplayNumber(int32_t replayNumber); diff --git a/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp b/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp index 866b14a105..e7271fdd6b 100644 --- a/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp +++ b/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp @@ -254,8 +254,8 @@ class FMSSimModel : public glass::FMSModel { void SetAutonomous(bool val) override { HALSIM_SetDriverStationAutonomous(val); } - void SetGameSpecificMessage(const char* val) override { - HALSIM_SetGameSpecificMessage(val); + void SetGameSpecificMessage(std::string_view val) override { + HALSIM_SetGameSpecificMessage(val.data(), val.size()); } void Update() override; diff --git a/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_DriverStation.cpp b/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_DriverStation.cpp index 6fbc3d7a3c..7307b4839d 100644 --- a/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_DriverStation.cpp +++ b/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_DriverStation.cpp @@ -166,8 +166,8 @@ void HALSimWSProviderDriverStation::OnNetValueChanged(const wpi::json& json) { HALSIM_SetDriverStationMatchTime(it.value()); } if ((it = json.find(">game_data")) != json.end()) { - HALSIM_SetGameSpecificMessage( - it.value().get_ref().c_str()); + std::string message = it.value().get_ref(); + HALSIM_SetGameSpecificMessage(message.c_str(), message.length()); } // Only notify usercode if we get the new data message diff --git a/wpilibc/src/main/native/cpp/simulation/DriverStationSim.cpp b/wpilibc/src/main/native/cpp/simulation/DriverStationSim.cpp index 61c29492ae..345204a82a 100644 --- a/wpilibc/src/main/native/cpp/simulation/DriverStationSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/DriverStationSim.cpp @@ -234,20 +234,20 @@ void DriverStationSim::SetJoystickType(int stick, int type) { HALSIM_SetJoystickType(stick, type); } -void DriverStationSim::SetJoystickName(int stick, const char* name) { - HALSIM_SetJoystickName(stick, name); +void DriverStationSim::SetJoystickName(int stick, std::string_view name) { + HALSIM_SetJoystickName(stick, name.data(), name.size()); } void DriverStationSim::SetJoystickAxisType(int stick, int axis, int type) { HALSIM_SetJoystickAxisType(stick, axis, type); } -void DriverStationSim::SetGameSpecificMessage(const char* message) { - HALSIM_SetGameSpecificMessage(message); +void DriverStationSim::SetGameSpecificMessage(std::string_view message) { + HALSIM_SetGameSpecificMessage(message.data(), message.size()); } -void DriverStationSim::SetEventName(const char* name) { - HALSIM_SetEventName(name); +void DriverStationSim::SetEventName(std::string_view name) { + HALSIM_SetEventName(name.data(), name.size()); } void DriverStationSim::SetMatchType(DriverStation::MatchType type) { diff --git a/wpilibc/src/main/native/include/frc/simulation/DriverStationSim.h b/wpilibc/src/main/native/include/frc/simulation/DriverStationSim.h index 412a9218c5..232c12326f 100644 --- a/wpilibc/src/main/native/include/frc/simulation/DriverStationSim.h +++ b/wpilibc/src/main/native/include/frc/simulation/DriverStationSim.h @@ -337,7 +337,7 @@ class DriverStationSim { * @param stick The joystick number * @param name The value of name */ - static void SetJoystickName(int stick, const char* name); + static void SetJoystickName(int stick, std::string_view name); /** * Sets the types of Axes for a joystick. @@ -353,14 +353,14 @@ class DriverStationSim { * * @param message the game specific message */ - static void SetGameSpecificMessage(const char* message); + static void SetGameSpecificMessage(std::string_view message); /** * Sets the event name. * * @param name the event name */ - static void SetEventName(const char* name); + static void SetEventName(std::string_view name); /** * Sets the match type.