[hal] Add RobotController.getSerialNumber() (#4783)

This commit is contained in:
Ryan Blue
2022-12-09 00:58:55 -05:00
committed by GitHub
parent 69a66ec5ec
commit 5a52b51443
19 changed files with 292 additions and 0 deletions

View File

@@ -18,6 +18,9 @@
#include <FRC_NetworkCommunication/LoadOut.h>
#include <FRC_NetworkCommunication/UsageReporting.h>
#include <fmt/format.h>
#include <wpi/MemoryBuffer.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
#include <wpi/mutex.h>
#include <wpi/timestamp.h>
@@ -270,6 +273,20 @@ int64_t HAL_GetFPGARevision(int32_t* status) {
return global->readRevision(status);
}
size_t HAL_GetSerialNumber(char* buffer, size_t size) {
const char* serialNum = std::getenv("serialnum");
if (serialNum) {
std::strncpy(buffer, serialNum, size);
buffer[size - 1] = '\0';
return std::strlen(buffer);
} else {
if (size > 0) {
buffer[0] = '\0';
}
return 0;
}
}
uint64_t HAL_GetFPGATime(int32_t* status) {
hal::init::CheckInit();
if (!global) {

View File

@@ -29,6 +29,19 @@ DEFINE_CAPI(int32_t, UserFaults5V, 0)
DEFINE_CAPI(int32_t, UserFaults3V3, 0)
DEFINE_CAPI(double, BrownoutVoltage, 6.75)
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {
return 0;
}
void HALSIM_CancelRoboRioSerialNumberCallback(int32_t uid) {}
size_t HALSIM_GetRoboRioSerialNumber(char* buffer, size_t size) {
if (size > 0) {
buffer[0] = '\0';
}
return 0;
}
void HALSIM_SetRoboRioSerialNumber(const char* buffer, size_t size) {}
void HALSIM_RegisterRoboRioAllCallbacks(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {}
} // extern "C"

View File

@@ -456,6 +456,20 @@ Java_edu_wpi_first_hal_HALUtil_getFPGARevision
return returnValue;
}
/*
* Class: edu_wpi_first_hal_HALUtil
* Method: getSerialNumber
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_edu_wpi_first_hal_HALUtil_getSerialNumber
(JNIEnv* env, jclass)
{
char serialNum[9];
size_t len = HAL_GetSerialNumber(serialNum, sizeof(serialNum));
return MakeJString(env, std::string_view(serialNum, len));
}
/*
* Class: edu_wpi_first_hal_HALUtil
* Method: getFPGATime

View File

@@ -4,11 +4,14 @@
#include <jni.h>
#include <wpi/jni_util.h>
#include "CallbackStore.h"
#include "edu_wpi_first_hal_simulation_RoboRioDataJNI.h"
#include "hal/simulation/RoboRioData.h"
using namespace hal;
using namespace wpi::java;
extern "C" {
@@ -825,6 +828,34 @@ Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_setBrownoutVoltage
HALSIM_SetRoboRioBrownoutVoltage(value);
}
/*
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
* Method: getSerialNumber
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_getSerialNumber
(JNIEnv* env, jclass)
{
char serialNum[9];
size_t len = HALSIM_GetRoboRioSerialNumber(serialNum, sizeof(serialNum));
return MakeJString(env, std::string_view(serialNum, len));
}
/*
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
* Method: setSerialNumber
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_setSerialNumber
(JNIEnv* env, jclass, jstring serialNumber)
{
JStringRef serialNumberJString{env, serialNumber};
HALSIM_SetRoboRioSerialNumber(serialNumberJString.c_str(),
serialNumberJString.size());
}
/*
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
* Method: resetData

View File

@@ -6,6 +6,14 @@
#include <stdint.h>
#ifdef __cplusplus
#include <cstddef>
#else
#include <stddef.h> // NOLINT(build/include_order)
#endif
#include "hal/Types.h"
/**
@@ -66,6 +74,13 @@ int32_t HAL_GetFPGAVersion(int32_t* status);
*/
int64_t HAL_GetFPGARevision(int32_t* status);
/**
* Returns the serial number.
*
* @return Serial number.
*/
size_t HAL_GetSerialNumber(char* buffer, size_t size);
/**
* Returns the runtime type of the HAL.
*

View File

@@ -4,9 +4,14 @@
#pragma once
#include <cstddef>
#include "hal/Types.h"
#include "hal/simulation/NotifyListener.h"
typedef void (*HAL_RoboRioStringCallback)(const char* name, void* param,
const char* str, size_t size);
#ifdef __cplusplus
extern "C" {
#endif
@@ -121,6 +126,12 @@ void HALSIM_CancelRoboRioBrownoutVoltageCallback(int32_t uid);
double HALSIM_GetRoboRioBrownoutVoltage(void);
void HALSIM_SetRoboRioBrownoutVoltage(double brownoutVoltage);
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify);
void HALSIM_CancelRoboRioSerialNumberCallback(int32_t uid);
size_t HALSIM_GetRoboRioSerialNumber(char* buffer, size_t size);
void HALSIM_SetRoboRioSerialNumber(const char* serialNumber, size_t size);
void HALSIM_RegisterRoboRioAllCallbacks(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);

View File

@@ -280,6 +280,10 @@ int64_t HAL_GetFPGARevision(int32_t* status) {
return 0; // TODO: Find a better number to return;
}
size_t HAL_GetSerialNumber(char* buffer, size_t size) {
return HALSIM_GetRoboRioSerialNumber(buffer, size);
}
uint64_t HAL_GetFPGATime(int32_t* status) {
return hal::GetFPGATime();
}

View File

@@ -32,6 +32,44 @@ void RoboRioData::ResetData() {
userFaults5V.Reset(0);
userFaults3V3.Reset(0);
brownoutVoltage.Reset(6.75);
m_serialNumber = "";
}
int32_t RoboRioData::RegisterSerialNumberCallback(
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {
std::scoped_lock lock(m_serialNumberMutex);
int32_t uid = m_serialNumberCallbacks.Register(callback, param);
if (initialNotify) {
callback(GetSerialNumberName(), param, m_serialNumber.c_str(),
m_serialNumber.size());
}
return uid;
}
void RoboRioData::CancelSerialNumberCallback(int32_t uid) {
m_serialNumberCallbacks.Cancel(uid);
}
size_t RoboRioData::GetSerialNumber(char* buffer, size_t size) {
std::scoped_lock lock(m_serialNumberMutex);
size_t copied = m_serialNumber.copy(buffer, size);
// Null terminate
if (copied == size) {
copied -= 1;
}
buffer[copied] = '\0';
return copied;
}
void RoboRioData::SetSerialNumber(const char* serialNumber, size_t size) {
// Limit serial number to 8 characters internally- serialnum environment
// variable is always 8 characters
if (size > 8) {
size = 8;
}
std::scoped_lock lock(m_serialNumberMutex);
m_serialNumber = std::string(serialNumber, size);
m_serialNumberCallbacks(m_serialNumber.c_str(), m_serialNumber.size());
}
extern "C" {
@@ -60,6 +98,24 @@ DEFINE_CAPI(int32_t, UserFaults5V, userFaults5V)
DEFINE_CAPI(int32_t, UserFaults3V3, userFaults3V3)
DEFINE_CAPI(double, BrownoutVoltage, brownoutVoltage)
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {
return SimRoboRioData->RegisterSerialNumberCallback(callback, param,
initialNotify);
}
void HALSIM_CancelRoboRioSerialNumberCallback(int32_t uid) {
return SimRoboRioData->CancelSerialNumberCallback(uid);
}
size_t HALSIM_GetRoboRioSerialNumber(char* buffer, size_t size) {
return SimRoboRioData->GetSerialNumber(buffer, size);
}
void HALSIM_SetRoboRioSerialNumber(const char* serialNumber, size_t size) {
SimRoboRioData->SetSerialNumber(serialNumber, size);
}
void HALSIM_RegisterRoboRioAllCallbacks(HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
#define REGISTER(NAME) \
SimRoboRioData->NAME.RegisterCallback(callback, param, initialNotify)

View File

@@ -4,6 +4,11 @@
#pragma once
#include <cstddef>
#include <string>
#include <wpi/spinlock.h>
#include "hal/simulation/RoboRioData.h"
#include "hal/simulation/SimDataValue.h"
@@ -26,6 +31,8 @@ class RoboRioData {
HAL_SIMDATAVALUE_DEFINE_NAME(UserFaults3V3)
HAL_SIMDATAVALUE_DEFINE_NAME(BrownoutVoltage)
HAL_SIMCALLBACKREGISTRY_DEFINE_NAME(SerialNumber)
public:
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetFPGAButtonName> fpgaButton{false};
SimDataValue<double, HAL_MakeDouble, GetVInVoltageName> vInVoltage{12.0};
@@ -50,7 +57,20 @@ class RoboRioData {
SimDataValue<double, HAL_MakeDouble, GetBrownoutVoltageName> brownoutVoltage{
6.75};
int32_t RegisterSerialNumberCallback(HAL_RoboRioStringCallback callback,
void* param, HAL_Bool initialNotify);
void CancelSerialNumberCallback(int32_t uid);
size_t GetSerialNumber(char* buffer, size_t size);
void SetSerialNumber(const char* serialNumber, size_t size);
virtual void ResetData();
private:
wpi::spinlock m_serialNumberMutex;
std::string m_serialNumber;
SimCallbackRegistry<HAL_RoboRioStringCallback, GetSerialNumberName>
m_serialNumberCallbacks;
};
extern RoboRioData* SimRoboRioData;
} // namespace hal