Skip to content

Commit

Permalink
[hal] Add RobotController.getSerialNumber() (wpilibsuite#4783)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue authored Dec 9, 2022
1 parent 69a66ec commit 5a52b51
Show file tree
Hide file tree
Showing 19 changed files with 292 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hal/src/main/java/edu/wpi/first/hal/HALUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public final class HALUtil extends JNIWrapper {

public static native int getFPGARevision();

public static native String getSerialNumber();

public static native long getFPGATime();

public static native int getHALRuntimeType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,9 @@ public static native int registerBrownoutVoltageCallback(

public static native void setBrownoutVoltage(double brownoutVoltage);

public static native String getSerialNumber();

public static native void setSerialNumber(String serialNumber);

public static native void resetData();
}
17 changes: 17 additions & 0 deletions hal/src/main/native/athena/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions hal/src/main/native/athena/mockdata/RoboRioData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 14 additions & 0 deletions hal/src/main/native/cpp/jni/HALUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions hal/src/main/native/cpp/jni/simulation/RoboRioDataJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" {

Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions hal/src/main/native/include/hal/HALBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

#include <stdint.h>

#ifdef __cplusplus
#include <cstddef>
#else

#include <stddef.h> // NOLINT(build/include_order)

#endif

#include "hal/Types.h"

/**
Expand Down Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions hal/src/main/native/include/hal/simulation/RoboRioData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions hal/src/main/native/sim/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
56 changes: 56 additions & 0 deletions hal/src/main/native/sim/mockdata/RoboRioData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions hal/src/main/native/sim/mockdata/RoboRioDataInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

#pragma once

#include <cstddef>
#include <string>

#include <wpi/spinlock.h>

#include "hal/simulation/RoboRioData.h"
#include "hal/simulation/SimDataValue.h"

Expand All @@ -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};
Expand All @@ -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
9 changes: 9 additions & 0 deletions wpilibc/src/main/native/cpp/RobotController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "frc/RobotController.h"

#include <cstddef>

#include <hal/CAN.h>
#include <hal/HALBase.h>
#include <hal/Power.h>
Expand All @@ -26,6 +28,13 @@ int64_t RobotController::GetFPGARevision() {
return revision;
}

std::string RobotController::GetSerialNumber() {
// Serial number is 8 characters
char serialNum[9];
size_t len = HAL_GetSerialNumber(serialNum, sizeof(serialNum));
return std::string(serialNum, len);
}

uint64_t RobotController::GetFPGATime() {
int32_t status = 0;
uint64_t time = HAL_GetFPGATime(&status);
Expand Down
10 changes: 10 additions & 0 deletions wpilibc/src/main/native/cpp/simulation/RoboRioSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ void RoboRioSim::SetBrownoutVoltage(units::volt_t vInVoltage) {
HALSIM_SetRoboRioBrownoutVoltage(vInVoltage.value());
}

std::string RoboRioSim::GetSerialNumber() {
char serialNum[9];
size_t len = HALSIM_GetRoboRioSerialNumber(serialNum, sizeof(serialNum));
return std::string(serialNum, len);
}

void RoboRioSim::SetSerialNumber(std::string_view serialNumber) {
HALSIM_SetRoboRioSerialNumber(serialNumber.data(), serialNumber.size());
}

void RoboRioSim::ResetData() {
HALSIM_ResetRoboRioData();
}
9 changes: 9 additions & 0 deletions wpilibc/src/main/native/include/frc/RobotController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <stdint.h>

#include <string>

#include <units/voltage.h>

namespace frc {
Expand Down Expand Up @@ -42,6 +44,13 @@ class RobotController {
*/
static int64_t GetFPGARevision();

/**
* Returns the serial number of the roboRIO.
*
* @return The serial number of the roboRIO.
*/
static std::string GetSerialNumber();

/**
* Read the microsecond-resolution timer on the FPGA.
*
Expand Down
Loading

0 comments on commit 5a52b51

Please sign in to comment.