Add Notifier HALSIM access

This commit is contained in:
Peter Johnson
2019-11-09 11:41:58 -08:00
parent 3e049e02f0
commit f5446c7409
5 changed files with 157 additions and 0 deletions

View File

@@ -8,6 +8,8 @@
#include "hal/Notifier.h"
#include <chrono>
#include <cstdio>
#include <cstring>
#include <string>
#include <wpi/condition_variable.h>
@@ -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<int>(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"

View File

@@ -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"