[hal,wpilib] Move Alert to HAL (#8646)

SystemCore implementation is not yet connected to MRCComm.
This commit is contained in:
Peter Johnson
2026-03-03 21:58:47 -07:00
committed by GitHub
parent f4935a2ea9
commit 733cfa4b07
33 changed files with 1719 additions and 1121 deletions

View File

@@ -0,0 +1,124 @@
// 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 <cassert>
#include <cstdio>
#include "HALUtil.h"
#include "org_wpilib_hardware_hal_AlertJNI.h"
#include "wpi/hal/Alert.h"
#include "wpi/util/jni_util.hpp"
#include "wpi/util/string.h"
using namespace wpi::hal;
using namespace wpi::util::java;
extern "C" {
/*
* Class: org_wpilib_hardware_hal_AlertJNI
* Method: createAlert
* Signature: (Ljava/lang/String;Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_AlertJNI_createAlert
(JNIEnv* env, jclass, jstring group, jstring text, jint level)
{
int32_t status = 0;
wpi::util::java::JStringRef jgroup{env, group};
wpi::util::java::JStringRef jtext{env, text};
WPI_String wpiGroup = wpi::util::make_string(jgroup);
WPI_String wpiText = wpi::util::make_string(jtext);
HAL_AlertHandle alertHandle =
HAL_CreateAlert(&wpiGroup, &wpiText, level, &status);
if (alertHandle <= 0 || !CheckStatusForceThrow(env, status)) {
return 0; // something went wrong in HAL
}
return (jint)alertHandle;
}
/*
* Class: org_wpilib_hardware_hal_AlertJNI
* Method: destroyAlert
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_AlertJNI_destroyAlert
(JNIEnv* env, jclass, jint alertHandle)
{
if (alertHandle != HAL_kInvalidHandle) {
HAL_DestroyAlert((HAL_AlertHandle)alertHandle);
}
}
/*
* Class: org_wpilib_hardware_hal_AlertJNI
* Method: setAlertActive
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_AlertJNI_setAlertActive
(JNIEnv* env, jclass cls, jint alertHandle, jboolean active)
{
int32_t status = 0;
HAL_SetAlertActive((HAL_AlertHandle)alertHandle, active, &status);
CheckStatus(env, status);
}
/*
* Class: org_wpilib_hardware_hal_AlertJNI
* Method: isAlertActive
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL
Java_org_wpilib_hardware_hal_AlertJNI_isAlertActive
(JNIEnv* env, jclass cls, jint alertHandle)
{
int32_t status = 0;
jboolean active = HAL_IsAlertActive((HAL_AlertHandle)alertHandle, &status);
CheckStatus(env, status);
return active;
}
/*
* Class: org_wpilib_hardware_hal_AlertJNI
* Method: setAlertText
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_AlertJNI_setAlertText
(JNIEnv* env, jclass cls, jint alertHandle, jstring text)
{
int32_t status = 0;
wpi::util::java::JStringRef jtext{env, text};
WPI_String wpiText = wpi::util::make_string(jtext);
HAL_SetAlertText((HAL_AlertHandle)alertHandle, &wpiText, &status);
CheckStatus(env, status);
}
/*
* Class: org_wpilib_hardware_hal_AlertJNI
* Method: getAlertText
* Signature: (I)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_org_wpilib_hardware_hal_AlertJNI_getAlertText
(JNIEnv* env, jclass cls, jint alertHandle)
{
int32_t status = 0;
WPI_String text;
HAL_GetAlertText((HAL_AlertHandle)alertHandle, &text, &status);
if (!CheckStatus(env, status)) {
return nullptr;
}
jstring rv = MakeJString(env, wpi::util::to_string_view(&text));
WPI_FreeString(&text);
return rv;
}
} // extern "C"

View File

@@ -0,0 +1,91 @@
// 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 "AlertDataJNI.hpp"
#include <jni.h>
#include "org_wpilib_hardware_hal_simulation_AlertDataJNI.h"
#include "wpi/hal/simulation/AlertData.h"
#include "wpi/util/jni_util.hpp"
using namespace wpi::util::java;
static JClass alertInfoCls;
static jobject MakeAlertInfoJava(JNIEnv* env, const HALSIM_AlertInfo& info) {
static jmethodID func = env->GetMethodID(
alertInfoCls, "<init>", "(ILjava/lang/String;Ljava/lang/String;JI)V");
return env->NewObject(
alertInfoCls, func, static_cast<jint>(info.handle),
MakeJString(env, wpi::util::to_string_view(&info.group)),
MakeJString(env, wpi::util::to_string_view(&info.text)),
static_cast<jlong>(info.activeStartTime), static_cast<jint>(info.level));
}
namespace wpi::hal::sim {
bool InitializeAlertDataJNI(JNIEnv* env) {
alertInfoCls =
JClass(env, "org/wpilib/hardware/hal/simulation/AlertDataJNI$AlertInfo");
if (!alertInfoCls) {
return false;
}
return true;
}
void FreeAlertDataJNI(JNIEnv* env) {
alertInfoCls.free(env);
}
} // namespace wpi::hal::sim
extern "C" {
/*
* Class: org_wpilib_hardware_hal_simulation_AlertDataJNI
* Method: getNumAlerts
* Signature: ()I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_simulation_AlertDataJNI_getNumAlerts
(JNIEnv*, jclass)
{
return HALSIM_GetNumAlerts();
}
/*
* Class: org_wpilib_hardware_hal_simulation_AlertDataJNI
* Method: getAlerts
* Signature: ()[Ljava/lang/Object;
*/
JNIEXPORT jobjectArray JNICALL
Java_org_wpilib_hardware_hal_simulation_AlertDataJNI_getAlerts
(JNIEnv* env, jclass)
{
int32_t allocLen = HALSIM_GetNumAlerts();
HALSIM_AlertInfo* arr = new HALSIM_AlertInfo[allocLen];
int32_t len = HALSIM_GetAlerts(arr, allocLen);
jobjectArray ret = env->NewObjectArray(len, alertInfoCls, nullptr);
for (int32_t i = 0; i < len; ++i) {
env->SetObjectArrayElement(ret, i, MakeAlertInfoJava(env, arr[i]));
}
HALSIM_FreeAlerts(arr, len < allocLen ? len : allocLen);
delete[] arr;
return ret;
}
/*
* Class: org_wpilib_hardware_hal_simulation_AlertDataJNI
* Method: resetData
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_AlertDataJNI_resetData
(JNIEnv*, jclass)
{
HALSIM_ResetAlertData();
}
} // extern "C"

View File

@@ -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.
#pragma once
#include <jni.h>
namespace wpi::hal::sim {
bool InitializeAlertDataJNI(JNIEnv* env);
void FreeAlertDataJNI(JNIEnv* env);
} // namespace wpi::hal::sim

View File

@@ -4,6 +4,7 @@
#include "SimulatorJNI.h"
#include "AlertDataJNI.hpp"
#include "BufferCallbackStore.h"
#include "CallbackStore.h"
#include "ConstBufferCallbackStore.h"
@@ -78,6 +79,9 @@ jint SimOnLoad(JavaVM* vm, void* reserved) {
InitializeBufferStore();
InitializeConstBufferStore();
InitializeOpModeOptionsStore();
if (!InitializeAlertDataJNI(env)) {
return JNI_ERR;
}
if (!InitializeSimDeviceDataJNI(env)) {
return JNI_ERR;
}
@@ -95,6 +99,7 @@ void SimOnUnload(JavaVM* vm, void* reserved) {
bufferCallbackCls.free(env);
constBufferCallbackCls.free(env);
biConsumerCls.free(env);
FreeAlertDataJNI(env);
FreeSimDeviceDataJNI(env);
jvm = nullptr;
}