mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
[hal,wpilib] Move Alert to HAL (#8646)
SystemCore implementation is not yet connected to MRCComm.
This commit is contained in:
124
hal/src/main/native/cpp/jni/AlertJNI.cpp
Normal file
124
hal/src/main/native/cpp/jni/AlertJNI.cpp
Normal 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"
|
||||
91
hal/src/main/native/cpp/jni/simulation/AlertDataJNI.cpp
Normal file
91
hal/src/main/native/cpp/jni/simulation/AlertDataJNI.cpp
Normal 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"
|
||||
12
hal/src/main/native/cpp/jni/simulation/AlertDataJNI.hpp
Normal file
12
hal/src/main/native/cpp/jni/simulation/AlertDataJNI.hpp
Normal 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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
113
hal/src/main/native/include/wpi/hal/Alert.h
Normal file
113
hal/src/main/native/include/wpi/hal/Alert.h
Normal file
@@ -0,0 +1,113 @@
|
||||
// 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 <stdint.h>
|
||||
|
||||
#include "wpi/hal/Types.h"
|
||||
#include "wpi/util/string.h"
|
||||
|
||||
/**
|
||||
* @defgroup hal_alert Alert Functions
|
||||
* @ingroup hal_capi
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Represents an alert's level of urgency. */
|
||||
HAL_ENUM(HAL_AlertLevel) {
|
||||
/**
|
||||
* High priority alert - displayed first with a red "X" symbol. Use this type
|
||||
* for problems which will seriously affect the robot's functionality and thus
|
||||
* require immediate attention.
|
||||
*/
|
||||
HAL_ALERT_HIGH = 0,
|
||||
HAL_ALERT_ERROR = HAL_ALERT_HIGH,
|
||||
|
||||
/**
|
||||
* Medium priority alert - displayed second with a yellow "!" symbol. Use this
|
||||
* type for problems which could affect the robot's functionality but do not
|
||||
* necessarily require immediate attention.
|
||||
*/
|
||||
HAL_ALERT_MEDIUM = 1,
|
||||
HAL_ALERT_WARNING = HAL_ALERT_MEDIUM,
|
||||
|
||||
/**
|
||||
* Low priority alert - displayed last with a green "i" symbol. Use this type
|
||||
* for problems which are unlikely to affect the robot's functionality, or any
|
||||
* other alerts which do not fall under the other categories.
|
||||
*/
|
||||
HAL_ALERT_LOW = 2,
|
||||
HAL_ALERT_INFO = HAL_ALERT_LOW
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates an alert.
|
||||
*
|
||||
* @param group Group identifier
|
||||
* @param text Text to be displayed when the alert is active
|
||||
* @param level Alert urgency level (HAL_AlertLevel)
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
* @return the created alert
|
||||
*/
|
||||
HAL_AlertHandle HAL_CreateAlert(const struct WPI_String* group,
|
||||
const struct WPI_String* text, int32_t level,
|
||||
int32_t* status);
|
||||
|
||||
/**
|
||||
* Destroys an alert.
|
||||
*
|
||||
* @param alertHandle the alert handle
|
||||
*/
|
||||
void HAL_DestroyAlert(HAL_AlertHandle alertHandle);
|
||||
|
||||
/**
|
||||
* Sets whether the alert should be displayed. This method can be
|
||||
* safely be called periodically.
|
||||
*
|
||||
* @param alertHandle the alert handle
|
||||
* @param active true to display the alert, false to hide it
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
void HAL_SetAlertActive(HAL_AlertHandle alertHandle, HAL_Bool active,
|
||||
int32_t* status);
|
||||
|
||||
/**
|
||||
* Checks if an alert is active.
|
||||
*
|
||||
* @param alertHandle the alert handle
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
* @return true if the alert is active
|
||||
*/
|
||||
HAL_Bool HAL_IsAlertActive(HAL_AlertHandle alertHandle, int32_t* status);
|
||||
|
||||
/**
|
||||
* Updates the text of an alert. Use this method to dynamically change the
|
||||
* displayed alert, such as including more details about the detected problem.
|
||||
*
|
||||
* @param alertHandle the alert handle
|
||||
* @param text new text to be displayed when the alert is active
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
void HAL_SetAlertText(HAL_AlertHandle alertHandle,
|
||||
const struct WPI_String* text, int32_t* status);
|
||||
|
||||
/**
|
||||
* Gets the text of an alert.
|
||||
*
|
||||
* @param alertHandle the alert handle
|
||||
* @param text pointer to a WPI_String to be filled with the current text
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
void HAL_GetAlertText(HAL_AlertHandle alertHandle, struct WPI_String* text,
|
||||
int32_t* status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
/** @} */
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
typedef int32_t HAL_Handle;
|
||||
|
||||
typedef HAL_Handle HAL_AlertHandle;
|
||||
|
||||
typedef HAL_Handle HAL_AnalogInputHandle;
|
||||
|
||||
typedef HAL_Handle HAL_AnalogOutputHandle;
|
||||
|
||||
@@ -72,6 +72,7 @@ enum class HAL_HandleEnum {
|
||||
REVPDH = 26,
|
||||
REVPH = 27,
|
||||
CANStream = 28,
|
||||
Alert = 29,
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
60
hal/src/main/native/include/wpi/hal/simulation/AlertData.h
Normal file
60
hal/src/main/native/include/wpi/hal/simulation/AlertData.h
Normal file
@@ -0,0 +1,60 @@
|
||||
// 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 <stdint.h>
|
||||
|
||||
#include "wpi/hal/Types.h"
|
||||
#include "wpi/util/string.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Information about an alert. */
|
||||
struct HALSIM_AlertInfo {
|
||||
HAL_AlertHandle handle;
|
||||
struct WPI_String group;
|
||||
struct WPI_String text;
|
||||
int64_t activeStartTime; // 0 if not active
|
||||
int32_t level; // HAL_AlertLevel
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the number of alerts. Note: this is not guaranteed to be consistent
|
||||
* with the number of alerts returned by HALSIM_GetAlerts, so the latter's
|
||||
* return value should be used to determine how many alerts were actually filled
|
||||
* in.
|
||||
*
|
||||
* @return the number of alerts
|
||||
*/
|
||||
int32_t HALSIM_GetNumAlerts(void);
|
||||
|
||||
/**
|
||||
* Gets detailed information about each alert.
|
||||
*
|
||||
* @param arr array of information to be filled
|
||||
* @param length length of arr
|
||||
* @return Number of alerts; note: may be larger or smaller than passed-in
|
||||
* length
|
||||
*/
|
||||
int32_t HALSIM_GetAlerts(struct HALSIM_AlertInfo* arr, int32_t length);
|
||||
|
||||
/**
|
||||
* Frees an array of alert information returned by HALSIM_GetAlerts.
|
||||
*
|
||||
* @param arr array to free
|
||||
* @param length number of alerts in arr (as returned by HALSIM_GetAlerts)
|
||||
*/
|
||||
void HALSIM_FreeAlerts(struct HALSIM_AlertInfo* arr, int32_t length);
|
||||
|
||||
/**
|
||||
* Resets all alert simulation data.
|
||||
*/
|
||||
void HALSIM_ResetAlertData(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
155
hal/src/main/native/sim/Alert.cpp
Normal file
155
hal/src/main/native/sim/Alert.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
// 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 "wpi/hal/Alert.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "wpi/hal/Errors.h"
|
||||
#include "wpi/hal/HALBase.h"
|
||||
#include "wpi/hal/Types.h"
|
||||
#include "wpi/hal/handles/UnlimitedHandleResource.h"
|
||||
#include "wpi/hal/simulation/AlertData.h"
|
||||
#include "wpi/util/mutex.hpp"
|
||||
#include "wpi/util/string.h"
|
||||
|
||||
namespace {
|
||||
struct Alert {
|
||||
Alert(std::string_view group, std::string_view text, int32_t level)
|
||||
: group{group}, text{text}, level{level} {}
|
||||
|
||||
wpi::util::mutex textMutex;
|
||||
std::string group;
|
||||
std::string text;
|
||||
std::atomic<int64_t> activeStartTime{0}; // non-zero when active
|
||||
int32_t level = 0;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
using namespace wpi::hal;
|
||||
|
||||
static UnlimitedHandleResource<HAL_AlertHandle, Alert, HAL_HandleEnum::Alert>*
|
||||
alertHandles;
|
||||
|
||||
namespace wpi::hal::init {
|
||||
void InitializeAlert() {
|
||||
static UnlimitedHandleResource<HAL_AlertHandle, Alert, HAL_HandleEnum::Alert>
|
||||
aH;
|
||||
alertHandles = &aH;
|
||||
}
|
||||
} // namespace wpi::hal::init
|
||||
|
||||
extern "C" {
|
||||
|
||||
HAL_AlertHandle HAL_CreateAlert(const WPI_String* group, const WPI_String* text,
|
||||
int32_t level, int32_t* status) {
|
||||
wpi::hal::init::CheckInit();
|
||||
std::shared_ptr<Alert> alert = std::make_shared<Alert>(
|
||||
wpi::util::to_string_view(group), wpi::util::to_string_view(text), level);
|
||||
HAL_AlertHandle handle = alertHandles->Allocate(alert);
|
||||
if (handle == HAL_kInvalidHandle) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
void HAL_DestroyAlert(HAL_AlertHandle alertHandle) {
|
||||
alertHandles->Free(alertHandle);
|
||||
}
|
||||
|
||||
void HAL_SetAlertActive(HAL_AlertHandle alertHandle, HAL_Bool active,
|
||||
int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (!alert) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
if (active) {
|
||||
if (alert->activeStartTime.load(std::memory_order_relaxed) != 0) {
|
||||
// Already active, do nothing (avoids cost of getting time)
|
||||
return;
|
||||
}
|
||||
int64_t now = HAL_GetFPGATime(status);
|
||||
int64_t expected = 0;
|
||||
// use compare-exchange to avoid potential race
|
||||
alert->activeStartTime.compare_exchange_strong(expected, now);
|
||||
} else {
|
||||
alert->activeStartTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_Bool HAL_IsAlertActive(HAL_AlertHandle alertHandle, int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (!alert) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return false;
|
||||
}
|
||||
return alert->activeStartTime != 0;
|
||||
}
|
||||
|
||||
void HAL_SetAlertText(HAL_AlertHandle alertHandle, const WPI_String* text,
|
||||
int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (!alert) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(alert->textMutex);
|
||||
alert->text = wpi::util::to_string_view(text);
|
||||
}
|
||||
|
||||
void HAL_GetAlertText(HAL_AlertHandle alertHandle, struct WPI_String* text,
|
||||
int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (alert) {
|
||||
std::scoped_lock lock(alert->textMutex);
|
||||
*text = wpi::util::alloc_wpi_string(alert->text);
|
||||
} else {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
*text = WPI_String{};
|
||||
}
|
||||
}
|
||||
|
||||
int32_t HALSIM_GetNumAlerts(void) {
|
||||
int32_t count = 0;
|
||||
alertHandles->ForEach([&](auto, auto) { ++count; });
|
||||
return count;
|
||||
}
|
||||
|
||||
int32_t HALSIM_GetAlerts(struct HALSIM_AlertInfo* arr, int32_t length) {
|
||||
int32_t num = 0;
|
||||
alertHandles->ForEach([&](HAL_AlertHandle handle, Alert* alert) {
|
||||
if (num < length) {
|
||||
arr[num].handle = handle;
|
||||
arr[num].group = wpi::util::alloc_wpi_string(alert->group);
|
||||
{
|
||||
std::scoped_lock lock(alert->textMutex);
|
||||
arr[num].text = wpi::util::alloc_wpi_string(alert->text);
|
||||
}
|
||||
arr[num].activeStartTime =
|
||||
alert->activeStartTime.load(std::memory_order_relaxed);
|
||||
arr[num].level = alert->level;
|
||||
}
|
||||
++num;
|
||||
});
|
||||
return num;
|
||||
}
|
||||
|
||||
void HALSIM_FreeAlerts(struct HALSIM_AlertInfo* arr, int32_t length) {
|
||||
for (int32_t i = 0; i < length; ++i) {
|
||||
WPI_FreeString(&arr[i].group);
|
||||
WPI_FreeString(&arr[i].text);
|
||||
}
|
||||
}
|
||||
|
||||
void HALSIM_ResetAlertData(void) {
|
||||
alertHandles->ResetHandles();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -82,6 +82,7 @@ void InitializeHAL() {
|
||||
InitializeRoboRioData();
|
||||
InitializeSimDeviceData();
|
||||
InitializeAddressableLED();
|
||||
InitializeAlert();
|
||||
InitializeAnalogInput();
|
||||
InitializeAnalogInternal();
|
||||
InitializeCAN();
|
||||
|
||||
@@ -34,6 +34,7 @@ extern void InitializePWMData();
|
||||
extern void InitializeRoboRioData();
|
||||
extern void InitializeSimDeviceData();
|
||||
extern void InitializeAddressableLED();
|
||||
extern void InitializeAlert();
|
||||
extern void InitializeAnalogInput();
|
||||
extern void InitializeAnalogInternal();
|
||||
extern void InitializeCAN();
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "../PortsInternal.h"
|
||||
#include "wpi/hal/simulation/AddressableLEDData.h"
|
||||
#include "wpi/hal/simulation/AlertData.h"
|
||||
#include "wpi/hal/simulation/AnalogInData.h"
|
||||
#include "wpi/hal/simulation/CTREPCMData.h"
|
||||
#include "wpi/hal/simulation/CanData.h"
|
||||
@@ -70,4 +71,5 @@ extern "C" void HALSIM_ResetAllSimData(void) {
|
||||
|
||||
HALSIM_ResetRoboRioData();
|
||||
HALSIM_ResetSimDeviceData();
|
||||
HALSIM_ResetAlertData();
|
||||
}
|
||||
|
||||
131
hal/src/main/native/systemcore/Alert.cpp
Normal file
131
hal/src/main/native/systemcore/Alert.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
// 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 "wpi/hal/Alert.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "wpi/hal/Errors.h"
|
||||
#include "wpi/hal/HALBase.h"
|
||||
#include "wpi/hal/Types.h"
|
||||
#include "wpi/hal/handles/UnlimitedHandleResource.h"
|
||||
#include "wpi/hal/simulation/AlertData.h"
|
||||
#include "wpi/util/mutex.hpp"
|
||||
#include "wpi/util/string.h"
|
||||
|
||||
namespace {
|
||||
struct Alert {
|
||||
Alert(std::string_view group, std::string_view text, int32_t level)
|
||||
: group{group}, text{text}, level{level} {}
|
||||
|
||||
wpi::util::mutex textMutex;
|
||||
std::string group;
|
||||
std::string text;
|
||||
std::atomic<int64_t> activeStartTime{0}; // non-zero when active
|
||||
int32_t level = 0;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
using namespace wpi::hal;
|
||||
|
||||
static UnlimitedHandleResource<HAL_AlertHandle, Alert, HAL_HandleEnum::Alert>*
|
||||
alertHandles;
|
||||
|
||||
namespace wpi::hal::init {
|
||||
void InitializeAlert() {
|
||||
static UnlimitedHandleResource<HAL_AlertHandle, Alert, HAL_HandleEnum::Alert>
|
||||
aH;
|
||||
alertHandles = &aH;
|
||||
}
|
||||
} // namespace wpi::hal::init
|
||||
|
||||
extern "C" {
|
||||
|
||||
HAL_AlertHandle HAL_CreateAlert(const WPI_String* group, const WPI_String* text,
|
||||
int32_t level, int32_t* status) {
|
||||
wpi::hal::init::CheckInit();
|
||||
std::shared_ptr<Alert> alert = std::make_shared<Alert>(
|
||||
wpi::util::to_string_view(group), wpi::util::to_string_view(text), level);
|
||||
HAL_AlertHandle handle = alertHandles->Allocate(alert);
|
||||
if (handle == HAL_kInvalidHandle) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
void HAL_DestroyAlert(HAL_AlertHandle alertHandle) {
|
||||
alertHandles->Free(alertHandle);
|
||||
}
|
||||
|
||||
void HAL_SetAlertActive(HAL_AlertHandle alertHandle, HAL_Bool active,
|
||||
int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (!alert) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
if (active) {
|
||||
if (alert->activeStartTime.load(std::memory_order_relaxed) != 0) {
|
||||
// Already active, do nothing (avoids cost of getting time)
|
||||
return;
|
||||
}
|
||||
int64_t now = HAL_GetFPGATime(status);
|
||||
int64_t expected = 0;
|
||||
// use compare-exchange to avoid potential race
|
||||
alert->activeStartTime.compare_exchange_strong(expected, now);
|
||||
} else {
|
||||
alert->activeStartTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_Bool HAL_IsAlertActive(HAL_AlertHandle alertHandle, int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (!alert) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return false;
|
||||
}
|
||||
return alert->activeStartTime != 0;
|
||||
}
|
||||
|
||||
void HAL_SetAlertText(HAL_AlertHandle alertHandle, const WPI_String* text,
|
||||
int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (!alert) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(alert->textMutex);
|
||||
alert->text = wpi::util::to_string_view(text);
|
||||
}
|
||||
|
||||
void HAL_GetAlertText(HAL_AlertHandle alertHandle, struct WPI_String* text,
|
||||
int32_t* status) {
|
||||
auto alert = alertHandles->Get(alertHandle);
|
||||
if (alert) {
|
||||
std::scoped_lock lock(alert->textMutex);
|
||||
*text = wpi::util::alloc_wpi_string(alert->text);
|
||||
} else {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
*text = WPI_String{};
|
||||
}
|
||||
}
|
||||
|
||||
int32_t HALSIM_GetNumAlerts(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t HALSIM_GetAlerts(struct HALSIM_AlertInfo* arr, int32_t length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void HALSIM_FreeAlerts(struct HALSIM_AlertInfo* arr, int32_t length) {}
|
||||
|
||||
void HALSIM_ResetAlertData(void) {}
|
||||
|
||||
} // extern "C"
|
||||
Reference in New Issue
Block a user