diff --git a/hal/src/main/java/edu/wpi/first/hal/sim/NotifierSim.java b/hal/src/main/java/edu/wpi/first/hal/sim/NotifierSim.java new file mode 100644 index 0000000000..f19a674a08 --- /dev/null +++ b/hal/src/main/java/edu/wpi/first/hal/sim/NotifierSim.java @@ -0,0 +1,23 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.hal.sim; + +import edu.wpi.first.hal.sim.mockdata.NotifierDataJNI; + +public final class NotifierSim { + private NotifierSim() { + } + + public static long getNextTimeout() { + return NotifierDataJNI.getNextTimeout(); + } + + public static int getNumNotifiers() { + return NotifierDataJNI.getNumNotifiers(); + } +} diff --git a/hal/src/main/java/edu/wpi/first/hal/sim/mockdata/NotifierDataJNI.java b/hal/src/main/java/edu/wpi/first/hal/sim/mockdata/NotifierDataJNI.java new file mode 100644 index 0000000000..ecc0842245 --- /dev/null +++ b/hal/src/main/java/edu/wpi/first/hal/sim/mockdata/NotifierDataJNI.java @@ -0,0 +1,15 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.hal.sim.mockdata; + +import edu.wpi.first.hal.JNIWrapper; + +public class NotifierDataJNI extends JNIWrapper { + public static native long getNextTimeout(); + public static native int getNumNotifiers(); +} diff --git a/hal/src/main/native/include/mockdata/NotifierData.h b/hal/src/main/native/include/mockdata/NotifierData.h new file mode 100644 index 0000000000..b1ed50f292 --- /dev/null +++ b/hal/src/main/native/include/mockdata/NotifierData.h @@ -0,0 +1,38 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include "hal/Types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct HALSIM_NotifierInfo { + HAL_NotifierHandle handle; + char name[64]; + uint64_t timeout; + HAL_Bool running; +}; + +uint64_t HALSIM_GetNextNotifierTimeout(void); + +int32_t HALSIM_GetNumNotifiers(void); + +/** + * Gets detailed information about each notifier. + * + * @param arr array of information to be filled + * @param size size of arr + * @return Number of notifiers; note: may be larger than passed-in size + */ +int32_t HALSIM_GetNotifierInfo(struct HALSIM_NotifierInfo* arr, int32_t size); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/hal/src/main/native/sim/Notifier.cpp b/hal/src/main/native/sim/Notifier.cpp index 21f24b8280..1e94d1e344 100644 --- a/hal/src/main/native/sim/Notifier.cpp +++ b/hal/src/main/native/sim/Notifier.cpp @@ -8,6 +8,8 @@ #include "hal/Notifier.h" #include +#include +#include #include #include @@ -166,4 +168,46 @@ uint64_t HAL_WaitForNotifierAlarm(HAL_NotifierHandle notifierHandle, return 0; } +uint64_t HALSIM_GetNextNotifierTimeout(void) { + uint64_t timeout = UINT64_MAX; + notifierHandles->ForEach([&](HAL_NotifierHandle, Notifier* notifier) { + std::scoped_lock lock(notifier->mutex); + if (notifier->active && notifier->running && timeout > notifier->waitTime) + timeout = notifier->waitTime; + }); + return timeout; +} + +int32_t HALSIM_GetNumNotifiers(void) { + int32_t count = 0; + notifierHandles->ForEach([&](HAL_NotifierHandle, Notifier* notifier) { + std::scoped_lock lock(notifier->mutex); + if (notifier->active) ++count; + }); + return count; +} + +int32_t HALSIM_GetNotifierInfo(struct HALSIM_NotifierInfo* arr, int32_t size) { + int32_t num = 0; + notifierHandles->ForEach([&](HAL_NotifierHandle handle, Notifier* notifier) { + std::scoped_lock lock(notifier->mutex); + if (!notifier->active) return; + if (num < size) { + arr[num].handle = handle; + if (notifier->name.empty()) { + std::snprintf(arr[num].name, sizeof(arr[num].name), "Notifier%d", + static_cast(getHandleIndex(handle))); + } else { + std::strncpy(arr[num].name, notifier->name.c_str(), + sizeof(arr[num].name)); + arr[num].name[sizeof(arr[num].name) - 1] = '\0'; + } + arr[num].timeout = notifier->waitTime; + arr[num].running = notifier->running; + } + ++num; + }); + return num; +} + } // extern "C" diff --git a/hal/src/main/native/sim/jni/NotifierDataJNI.cpp b/hal/src/main/native/sim/jni/NotifierDataJNI.cpp new file mode 100644 index 0000000000..b59b6e0741 --- /dev/null +++ b/hal/src/main/native/sim/jni/NotifierDataJNI.cpp @@ -0,0 +1,37 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "edu_wpi_first_hal_sim_mockdata_NotifierDataJNI.h" +#include "mockdata/NotifierData.h" + +extern "C" { + +/* + * Class: edu_wpi_first_hal_sim_mockdata_NotifierDataJNI + * Method: getNextTimeout + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_edu_wpi_first_hal_sim_mockdata_NotifierDataJNI_getNextTimeout + (JNIEnv*, jclass) +{ + return HALSIM_GetNextNotifierTimeout(); +} + +/* + * Class: edu_wpi_first_hal_sim_mockdata_NotifierDataJNI + * Method: getNumNotifiers + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_edu_wpi_first_hal_sim_mockdata_NotifierDataJNI_getNumNotifiers + (JNIEnv*, jclass) +{ + return HALSIM_GetNumNotifiers(); +} + +} // extern "C"