mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add braces to C++ single-line loops and conditionals (NFC) (#2973)
This makes code easier to read and more consistent between C++ and Java. Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
@@ -52,10 +52,16 @@ void HAL_SetMain(void* param, void (*mainFunc)(void*),
|
||||
gExitFunc = exitFunc;
|
||||
}
|
||||
|
||||
HAL_Bool HAL_HasMain(void) { return gHasMain; }
|
||||
HAL_Bool HAL_HasMain(void) {
|
||||
return gHasMain;
|
||||
}
|
||||
|
||||
void HAL_RunMain(void) { gMainFunc(gMainParam); }
|
||||
void HAL_RunMain(void) {
|
||||
gMainFunc(gMainParam);
|
||||
}
|
||||
|
||||
void HAL_ExitMain(void) { gExitFunc(gMainParam); }
|
||||
void HAL_ExitMain(void) {
|
||||
gExitFunc(gMainParam);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -77,9 +77,13 @@ HAL_PortHandle createPortHandleForSPI(uint8_t channel) {
|
||||
}
|
||||
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType,
|
||||
int16_t version) {
|
||||
if (index < 0) return HAL_kInvalidHandle;
|
||||
if (index < 0) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
uint8_t hType = static_cast<uint8_t>(handleType);
|
||||
if (hType == 0 || hType > 127) return HAL_kInvalidHandle;
|
||||
if (hType == 0 || hType > 127) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
// set last 8 bits, then shift to first 8 bits
|
||||
HAL_Handle handle = hType;
|
||||
handle = handle << 8;
|
||||
|
||||
@@ -194,11 +194,15 @@ Java_edu_wpi_first_hal_CANAPIJNI_readCANPacketNew
|
||||
if (!CheckStatus(env, status)) {
|
||||
return false;
|
||||
}
|
||||
if (dataLength > 8) dataLength = 8;
|
||||
if (dataLength > 8) {
|
||||
dataLength = 8;
|
||||
}
|
||||
|
||||
jbyteArray toSetArray = SetCANDataObject(env, data, dataLength, timestamp);
|
||||
auto javaLen = env->GetArrayLength(toSetArray);
|
||||
if (javaLen < dataLength) dataLength = javaLen;
|
||||
if (javaLen < dataLength) {
|
||||
dataLength = javaLen;
|
||||
}
|
||||
env->SetByteArrayRegion(toSetArray, 0, dataLength,
|
||||
reinterpret_cast<jbyte*>(dataTemp));
|
||||
return true;
|
||||
@@ -226,11 +230,15 @@ Java_edu_wpi_first_hal_CANAPIJNI_readCANPacketLatest
|
||||
if (!CheckStatus(env, status)) {
|
||||
return false;
|
||||
}
|
||||
if (dataLength > 8) dataLength = 8;
|
||||
if (dataLength > 8) {
|
||||
dataLength = 8;
|
||||
}
|
||||
|
||||
jbyteArray toSetArray = SetCANDataObject(env, data, dataLength, timestamp);
|
||||
auto javaLen = env->GetArrayLength(toSetArray);
|
||||
if (javaLen < dataLength) dataLength = javaLen;
|
||||
if (javaLen < dataLength) {
|
||||
dataLength = javaLen;
|
||||
}
|
||||
env->SetByteArrayRegion(toSetArray, 0, dataLength,
|
||||
reinterpret_cast<jbyte*>(dataTemp));
|
||||
return true;
|
||||
@@ -259,11 +267,15 @@ Java_edu_wpi_first_hal_CANAPIJNI_readCANPacketTimeout
|
||||
if (!CheckStatus(env, status)) {
|
||||
return false;
|
||||
}
|
||||
if (dataLength > 8) dataLength = 8;
|
||||
if (dataLength > 8) {
|
||||
dataLength = 8;
|
||||
}
|
||||
|
||||
jbyteArray toSetArray = SetCANDataObject(env, data, dataLength, timestamp);
|
||||
auto javaLen = env->GetArrayLength(toSetArray);
|
||||
if (javaLen < dataLength) dataLength = javaLen;
|
||||
if (javaLen < dataLength) {
|
||||
dataLength = javaLen;
|
||||
}
|
||||
env->SetByteArrayRegion(toSetArray, 0, dataLength,
|
||||
reinterpret_cast<jbyte*>(dataTemp));
|
||||
return true;
|
||||
|
||||
@@ -61,7 +61,9 @@ Java_edu_wpi_first_hal_can_CANJNI_FRCNetCommCANSessionMuxReceiveMessage
|
||||
HAL_CAN_ReceiveMessage(messageIDPtr, messageIDMask, buffer, &dataSize,
|
||||
timeStampPtr, &status);
|
||||
|
||||
if (!CheckCANStatus(env, status, *messageIDPtr)) return nullptr;
|
||||
if (!CheckCANStatus(env, status, *messageIDPtr)) {
|
||||
return nullptr;
|
||||
}
|
||||
return MakeJByteArray(env,
|
||||
wpi::StringRef{reinterpret_cast<const char*>(buffer),
|
||||
static_cast<size_t>(dataSize)});
|
||||
@@ -85,7 +87,9 @@ Java_edu_wpi_first_hal_can_CANJNI_GetCANStatus
|
||||
HAL_CAN_GetCANStatus(&percentBusUtilization, &busOffCount, &txFullCount,
|
||||
&receiveErrorCount, &transmitErrorCount, &status);
|
||||
|
||||
if (!CheckStatus(env, status)) return;
|
||||
if (!CheckStatus(env, status)) {
|
||||
return;
|
||||
}
|
||||
|
||||
SetCanStatusObject(env, canStatus, percentBusUtilization, busOffCount,
|
||||
txFullCount, receiveErrorCount, transmitErrorCount);
|
||||
|
||||
@@ -104,7 +104,9 @@ void ThrowHalHandleException(JNIEnv* env, int32_t status) {
|
||||
}
|
||||
|
||||
void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
|
||||
if (status == 0) return;
|
||||
if (status == 0) {
|
||||
return;
|
||||
}
|
||||
if (status == HAL_HANDLE_ERROR) {
|
||||
ThrowHalHandleException(env, status);
|
||||
}
|
||||
@@ -123,7 +125,9 @@ void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
|
||||
|
||||
void ThrowError(JNIEnv* env, int32_t status, int32_t minRange, int32_t maxRange,
|
||||
int32_t requestedValue) {
|
||||
if (status == 0) return;
|
||||
if (status == 0) {
|
||||
return;
|
||||
}
|
||||
if (status == NO_AVAILABLE_RESOURCES || status == RESOURCE_IS_ALLOCATED ||
|
||||
status == RESOURCE_OUT_OF_RANGE) {
|
||||
ThrowAllocationException(env, minRange, maxRange, requestedValue, status);
|
||||
@@ -139,7 +143,9 @@ void ThrowError(JNIEnv* env, int32_t status, int32_t minRange, int32_t maxRange,
|
||||
}
|
||||
|
||||
void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
||||
if (status >= 0) return;
|
||||
if (status >= 0) {
|
||||
return;
|
||||
}
|
||||
switch (status) {
|
||||
case kRioStatusSuccess:
|
||||
// Everything is ok... don't throw.
|
||||
@@ -147,9 +153,10 @@ void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
||||
case HAL_ERR_CANSessionMux_InvalidBuffer:
|
||||
case kRIOStatusBufferInvalidSize: {
|
||||
static jmethodID invalidBufConstruct = nullptr;
|
||||
if (!invalidBufConstruct)
|
||||
if (!invalidBufConstruct) {
|
||||
invalidBufConstruct =
|
||||
env->GetMethodID(canInvalidBufferExCls, "<init>", "()V");
|
||||
}
|
||||
jobject exception =
|
||||
env->NewObject(canInvalidBufferExCls, invalidBufConstruct);
|
||||
env->Throw(static_cast<jthrowable>(exception));
|
||||
@@ -158,9 +165,10 @@ void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
||||
case HAL_ERR_CANSessionMux_MessageNotFound:
|
||||
case kRIOStatusOperationTimedOut: {
|
||||
static jmethodID messageNotFoundConstruct = nullptr;
|
||||
if (!messageNotFoundConstruct)
|
||||
if (!messageNotFoundConstruct) {
|
||||
messageNotFoundConstruct =
|
||||
env->GetMethodID(canMessageNotFoundExCls, "<init>", "()V");
|
||||
}
|
||||
jobject exception =
|
||||
env->NewObject(canMessageNotFoundExCls, messageNotFoundConstruct);
|
||||
env->Throw(static_cast<jthrowable>(exception));
|
||||
@@ -177,9 +185,10 @@ void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
||||
case HAL_ERR_CANSessionMux_NotInitialized:
|
||||
case kRIOStatusResourceNotInitialized: {
|
||||
static jmethodID notInitConstruct = nullptr;
|
||||
if (!notInitConstruct)
|
||||
if (!notInitConstruct) {
|
||||
notInitConstruct =
|
||||
env->GetMethodID(canNotInitializedExCls, "<init>", "()V");
|
||||
}
|
||||
jobject exception =
|
||||
env->NewObject(canNotInitializedExCls, notInitConstruct);
|
||||
env->Throw(static_cast<jthrowable>(exception));
|
||||
@@ -202,14 +211,16 @@ void ThrowIllegalArgumentException(JNIEnv* env, wpi::StringRef msg) {
|
||||
void ThrowBoundaryException(JNIEnv* env, double value, double lower,
|
||||
double upper) {
|
||||
static jmethodID getMessage = nullptr;
|
||||
if (!getMessage)
|
||||
if (!getMessage) {
|
||||
getMessage = env->GetStaticMethodID(boundaryExCls, "getMessage",
|
||||
"(DDD)Ljava/lang/String;");
|
||||
}
|
||||
|
||||
static jmethodID constructor = nullptr;
|
||||
if (!constructor)
|
||||
if (!constructor) {
|
||||
constructor =
|
||||
env->GetMethodID(boundaryExCls, "<init>", "(Ljava/lang/String;)V");
|
||||
}
|
||||
|
||||
jobject msg = env->CallStaticObjectMethod(
|
||||
boundaryExCls, getMessage, static_cast<jdouble>(value),
|
||||
@@ -299,7 +310,9 @@ jobject CreateHALValue(JNIEnv* env, const HAL_Value& value) {
|
||||
value1, value2);
|
||||
}
|
||||
|
||||
JavaVM* GetJVM() { return jvm; }
|
||||
JavaVM* GetJVM() {
|
||||
return jvm;
|
||||
}
|
||||
|
||||
namespace sim {
|
||||
jint SimOnLoad(JavaVM* vm, void* reserved);
|
||||
@@ -319,17 +332,22 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
||||
jvm = vm;
|
||||
|
||||
JNIEnv* env;
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
for (auto& c : classes) {
|
||||
*c.cls = JClass(env, c.name);
|
||||
if (!*c.cls) return JNI_ERR;
|
||||
if (!*c.cls) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& c : exceptions) {
|
||||
*c.cls = JException(env, c.name);
|
||||
if (!*c.cls) return JNI_ERR;
|
||||
if (!*c.cls) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
return sim::SimOnLoad(vm, reserved);
|
||||
@@ -339,8 +357,9 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
|
||||
sim::SimOnUnload(vm, reserved);
|
||||
|
||||
JNIEnv* env;
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||
return;
|
||||
}
|
||||
// Delete global references
|
||||
|
||||
for (auto& c : classes) {
|
||||
|
||||
@@ -21,25 +21,33 @@ void ThrowError(JNIEnv* env, int32_t status, int32_t minRange, int32_t maxRange,
|
||||
int32_t requestedValue);
|
||||
|
||||
inline bool CheckStatus(JNIEnv* env, int32_t status, bool doThrow = true) {
|
||||
if (status != 0) ReportError(env, status, doThrow);
|
||||
if (status != 0) {
|
||||
ReportError(env, status, doThrow);
|
||||
}
|
||||
return status == 0;
|
||||
}
|
||||
|
||||
inline bool CheckStatusRange(JNIEnv* env, int32_t status, int32_t minRange,
|
||||
int32_t maxRange, int32_t requestedValue) {
|
||||
if (status != 0) ThrowError(env, status, minRange, maxRange, requestedValue);
|
||||
if (status != 0) {
|
||||
ThrowError(env, status, minRange, maxRange, requestedValue);
|
||||
}
|
||||
return status == 0;
|
||||
}
|
||||
|
||||
inline bool CheckStatusForceThrow(JNIEnv* env, int32_t status) {
|
||||
if (status != 0) ThrowError(env, status, 0, 0, 0);
|
||||
if (status != 0) {
|
||||
ThrowError(env, status, 0, 0, 0);
|
||||
}
|
||||
return status == 0;
|
||||
}
|
||||
|
||||
void ReportCANError(JNIEnv* env, int32_t status, int32_t message_id);
|
||||
|
||||
inline bool CheckCANStatus(JNIEnv* env, int32_t status, int32_t message_id) {
|
||||
if (status != 0) ReportCANError(env, status, message_id);
|
||||
if (status != 0) {
|
||||
ReportCANError(env, status, message_id);
|
||||
}
|
||||
return status == 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,9 @@ class InterruptJNI : public wpi::SafeThreadOwner<InterruptThreadJNI> {
|
||||
|
||||
void Notify(uint32_t mask) {
|
||||
auto thr = GetThread();
|
||||
if (!thr) return;
|
||||
if (!thr) {
|
||||
return;
|
||||
}
|
||||
thr->m_notify = true;
|
||||
thr->m_mask = mask;
|
||||
thr->m_cond.notify_one();
|
||||
@@ -56,10 +58,16 @@ class InterruptJNI : public wpi::SafeThreadOwner<InterruptThreadJNI> {
|
||||
void InterruptJNI::SetFunc(JNIEnv* env, jobject func, jmethodID mid,
|
||||
jobject param) {
|
||||
auto thr = GetThread();
|
||||
if (!thr) return;
|
||||
if (!thr) {
|
||||
return;
|
||||
}
|
||||
// free global references
|
||||
if (thr->m_func) env->DeleteGlobalRef(thr->m_func);
|
||||
if (thr->m_param) env->DeleteGlobalRef(thr->m_param);
|
||||
if (thr->m_func) {
|
||||
env->DeleteGlobalRef(thr->m_func);
|
||||
}
|
||||
if (thr->m_param) {
|
||||
env->DeleteGlobalRef(thr->m_param);
|
||||
}
|
||||
// create global references
|
||||
thr->m_func = env->NewGlobalRef(func);
|
||||
thr->m_param = param ? env->NewGlobalRef(param) : nullptr;
|
||||
@@ -74,14 +82,20 @@ void InterruptThreadJNI::Main() {
|
||||
args.group = nullptr;
|
||||
jint rs = GetJVM()->AttachCurrentThreadAsDaemon(
|
||||
reinterpret_cast<void**>(&env), &args);
|
||||
if (rs != JNI_OK) return;
|
||||
if (rs != JNI_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_lock lock(m_mutex);
|
||||
while (m_active) {
|
||||
m_cond.wait(lock, [&] { return !m_active || m_notify; });
|
||||
if (!m_active) break;
|
||||
if (!m_active) {
|
||||
break;
|
||||
}
|
||||
m_notify = false;
|
||||
if (!m_func) continue;
|
||||
if (!m_func) {
|
||||
continue;
|
||||
}
|
||||
jobject func = m_func;
|
||||
jmethodID mid = m_mid;
|
||||
uint32_t mask = m_mask;
|
||||
@@ -96,8 +110,12 @@ void InterruptThreadJNI::Main() {
|
||||
}
|
||||
|
||||
// free global references
|
||||
if (m_func) env->DeleteGlobalRef(m_func);
|
||||
if (m_param) env->DeleteGlobalRef(m_param);
|
||||
if (m_func) {
|
||||
env->DeleteGlobalRef(m_func);
|
||||
}
|
||||
if (m_param) {
|
||||
env->DeleteGlobalRef(m_param);
|
||||
}
|
||||
|
||||
GetJVM()->DetachCurrentThread();
|
||||
}
|
||||
|
||||
@@ -367,7 +367,9 @@ Java_edu_wpi_first_hal_SPIJNI_spiReadAutoReceivedData__I_3IID
|
||||
jint retval =
|
||||
HAL_ReadSPIAutoReceivedData(static_cast<HAL_SPIPort>(port),
|
||||
recvBuf.data(), numToRead, timeout, &status);
|
||||
if (!CheckStatus(env, status)) return retval;
|
||||
if (!CheckStatus(env, status)) {
|
||||
return retval;
|
||||
}
|
||||
if (numToRead > 0) {
|
||||
env->SetIntArrayRegion(buffer, 0, numToRead,
|
||||
reinterpret_cast<const jint*>(recvBuf.data()));
|
||||
|
||||
@@ -93,11 +93,15 @@ Java_edu_wpi_first_hal_SimDeviceJNI_createSimValueEnum
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
JLocal<jstring> elem{
|
||||
env, static_cast<jstring>(env->GetObjectArrayElement(options, i))};
|
||||
if (!elem) return 0;
|
||||
if (!elem) {
|
||||
return 0;
|
||||
}
|
||||
arr.push_back(JStringRef{env, elem}.str());
|
||||
}
|
||||
wpi::SmallVector<const char*, 8> carr;
|
||||
for (auto&& val : arr) carr.push_back(val.c_str());
|
||||
for (auto&& val : arr) {
|
||||
carr.push_back(val.c_str());
|
||||
}
|
||||
return HAL_CreateSimValueEnum(device, JStringRef{env, name}.c_str(),
|
||||
direction, len, carr.data(), initialValue);
|
||||
}
|
||||
@@ -114,18 +118,24 @@ Java_edu_wpi_first_hal_SimDeviceJNI_createSimValueEnumDouble
|
||||
{
|
||||
size_t len = env->GetArrayLength(options);
|
||||
size_t len2 = env->GetArrayLength(optionValues);
|
||||
if (len != len2) return 0;
|
||||
if (len != len2) {
|
||||
return 0;
|
||||
}
|
||||
std::vector<std::string> arr;
|
||||
arr.reserve(len);
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
JLocal<jstring> elem{
|
||||
env, static_cast<jstring>(env->GetObjectArrayElement(options, i))};
|
||||
if (!elem) return 0;
|
||||
if (!elem) {
|
||||
return 0;
|
||||
}
|
||||
arr.push_back(JStringRef{env, elem}.str());
|
||||
}
|
||||
|
||||
wpi::SmallVector<const char*, 8> carr;
|
||||
for (auto&& val : arr) carr.push_back(val.c_str());
|
||||
for (auto&& val : arr) {
|
||||
carr.push_back(val.c_str());
|
||||
}
|
||||
return HAL_CreateSimValueEnumDouble(
|
||||
device, JStringRef{env, name}.c_str(), direction, len, carr.data(),
|
||||
JDoubleArrayRef{env, optionValues}.array().data(), initialValue);
|
||||
|
||||
@@ -82,7 +82,9 @@ void BufferCallbackStore::performCallback(const char* name, uint8_t* buffer,
|
||||
}
|
||||
}
|
||||
|
||||
void BufferCallbackStore::free(JNIEnv* env) { m_call.free(env); }
|
||||
void BufferCallbackStore::free(JNIEnv* env) {
|
||||
m_call.free(env);
|
||||
}
|
||||
|
||||
SIM_JniHandle sim::AllocateBufferCallback(
|
||||
JNIEnv* env, jint index, jobject callback,
|
||||
@@ -105,7 +107,9 @@ SIM_JniHandle sim::AllocateBufferCallback(
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle handle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = callbackHandles->Get(handle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
data->performCallback(name, buffer, length);
|
||||
};
|
||||
|
||||
@@ -69,7 +69,9 @@ void CallbackStore::performCallback(const char* name, const HAL_Value* value) {
|
||||
}
|
||||
}
|
||||
|
||||
void CallbackStore::free(JNIEnv* env) { m_call.free(env); }
|
||||
void CallbackStore::free(JNIEnv* env) {
|
||||
m_call.free(env);
|
||||
}
|
||||
|
||||
SIM_JniHandle sim::AllocateCallback(JNIEnv* env, jint index, jobject callback,
|
||||
jboolean initialNotify,
|
||||
@@ -92,7 +94,9 @@ SIM_JniHandle sim::AllocateCallback(JNIEnv* env, jint index, jobject callback,
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle handle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = callbackHandles->Get(handle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
data->performCallback(name, value);
|
||||
};
|
||||
@@ -132,7 +136,9 @@ SIM_JniHandle sim::AllocateChannelCallback(
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle handle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = callbackHandles->Get(handle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
data->performCallback(name, value);
|
||||
};
|
||||
@@ -174,7 +180,9 @@ SIM_JniHandle sim::AllocateCallbackNoIndex(
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle handle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = callbackHandles->Get(handle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
data->performCallback(name, value);
|
||||
};
|
||||
|
||||
@@ -74,7 +74,9 @@ void ConstBufferCallbackStore::performCallback(const char* name,
|
||||
}
|
||||
}
|
||||
|
||||
void ConstBufferCallbackStore::free(JNIEnv* env) { m_call.free(env); }
|
||||
void ConstBufferCallbackStore::free(JNIEnv* env) {
|
||||
m_call.free(env);
|
||||
}
|
||||
|
||||
SIM_JniHandle sim::AllocateConstBufferCallback(
|
||||
JNIEnv* env, jint index, jobject callback,
|
||||
@@ -97,7 +99,9 @@ SIM_JniHandle sim::AllocateConstBufferCallback(
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle handle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = callbackHandles->Get(handle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
data->performCallback(name, buffer, length);
|
||||
};
|
||||
|
||||
@@ -233,7 +233,9 @@ static SIM_JniHandle AllocateDeviceCallback(
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle jnihandle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = deviceCallbackHandles->Get(jnihandle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
data->performCallback(name, handle);
|
||||
};
|
||||
@@ -287,7 +289,9 @@ static SIM_JniHandle AllocateValueCallback(
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle jnihandle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = valueCallbackHandles->Get(jnihandle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
data->performCallback(name, handle, direction, *value);
|
||||
};
|
||||
@@ -312,27 +316,39 @@ namespace sim {
|
||||
bool InitializeSimDeviceDataJNI(JNIEnv* env) {
|
||||
simDeviceInfoCls = JClass(
|
||||
env, "edu/wpi/first/hal/simulation/SimDeviceDataJNI$SimDeviceInfo");
|
||||
if (!simDeviceInfoCls) return false;
|
||||
if (!simDeviceInfoCls) {
|
||||
return false;
|
||||
}
|
||||
|
||||
simValueInfoCls =
|
||||
JClass(env, "edu/wpi/first/hal/simulation/SimDeviceDataJNI$SimValueInfo");
|
||||
if (!simValueInfoCls) return false;
|
||||
if (!simValueInfoCls) {
|
||||
return false;
|
||||
}
|
||||
|
||||
simDeviceCallbackCls =
|
||||
JClass(env, "edu/wpi/first/hal/simulation/SimDeviceCallback");
|
||||
if (!simDeviceCallbackCls) return false;
|
||||
if (!simDeviceCallbackCls) {
|
||||
return false;
|
||||
}
|
||||
|
||||
simDeviceCallbackCallback = env->GetMethodID(simDeviceCallbackCls, "callback",
|
||||
"(Ljava/lang/String;I)V");
|
||||
if (!simDeviceCallbackCallback) return false;
|
||||
if (!simDeviceCallbackCallback) {
|
||||
return false;
|
||||
}
|
||||
|
||||
simValueCallbackCls =
|
||||
JClass(env, "edu/wpi/first/hal/simulation/SimValueCallback");
|
||||
if (!simValueCallbackCls) return false;
|
||||
if (!simValueCallbackCls) {
|
||||
return false;
|
||||
}
|
||||
|
||||
simValueCallbackCallback = env->GetMethodID(
|
||||
simValueCallbackCls, "callbackNative", "(Ljava/lang/String;IZIJD)V");
|
||||
if (!simValueCallbackCallback) return false;
|
||||
if (!simValueCallbackCallback) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static hal::UnlimitedHandleResource<SIM_JniHandle, DeviceCallbackStore,
|
||||
hal::HAL_HandleEnum::SimulationJni>
|
||||
@@ -495,7 +511,9 @@ Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_enumerateSimDevices
|
||||
size_t numElems = arr.size();
|
||||
jobjectArray jarr =
|
||||
env->NewObjectArray(arr.size(), simDeviceInfoCls, nullptr);
|
||||
if (!jarr) return nullptr;
|
||||
if (!jarr) {
|
||||
return nullptr;
|
||||
}
|
||||
for (size_t i = 0; i < numElems; ++i) {
|
||||
JLocal<jobject> elem{env, arr[i].MakeJava(env)};
|
||||
env->SetObjectArrayElement(jarr, i, elem.obj());
|
||||
@@ -617,7 +635,9 @@ Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_enumerateSimValues
|
||||
// convert to java
|
||||
size_t numElems = arr.size();
|
||||
jobjectArray jarr = env->NewObjectArray(arr.size(), simValueInfoCls, nullptr);
|
||||
if (!jarr) return nullptr;
|
||||
if (!jarr) {
|
||||
return nullptr;
|
||||
}
|
||||
for (size_t i = 0; i < numElems; ++i) {
|
||||
JLocal<jobject> elem{env, arr[i].MakeJava(env)};
|
||||
env->SetObjectArrayElement(jarr, i, elem.obj());
|
||||
@@ -635,11 +655,15 @@ Java_edu_wpi_first_hal_simulation_SimDeviceDataJNI_getSimValueEnumOptions
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
{
|
||||
static JClass stringCls{env, "java/lang/String"};
|
||||
if (!stringCls) return nullptr;
|
||||
if (!stringCls) {
|
||||
return nullptr;
|
||||
}
|
||||
int32_t numElems = 0;
|
||||
const char** elems = HALSIM_GetSimValueEnumOptions(handle, &numElems);
|
||||
jobjectArray jarr = env->NewObjectArray(numElems, stringCls, nullptr);
|
||||
if (!jarr) return nullptr;
|
||||
if (!jarr) {
|
||||
return nullptr;
|
||||
}
|
||||
for (int32_t i = 0; i < numElems; ++i) {
|
||||
JLocal<jstring> elem{env, MakeJString(env, elems[i])};
|
||||
env->SetObjectArrayElement(jarr, i, elem.obj());
|
||||
|
||||
@@ -34,55 +34,75 @@ jint SimOnLoad(JavaVM* vm, void* reserved) {
|
||||
jvm = vm;
|
||||
|
||||
JNIEnv* env;
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
notifyCallbackCls =
|
||||
JClass(env, "edu/wpi/first/hal/simulation/NotifyCallback");
|
||||
if (!notifyCallbackCls) return JNI_ERR;
|
||||
if (!notifyCallbackCls) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
notifyCallbackCallback = env->GetMethodID(notifyCallbackCls, "callbackNative",
|
||||
"(Ljava/lang/String;IJD)V");
|
||||
if (!notifyCallbackCallback) return JNI_ERR;
|
||||
if (!notifyCallbackCallback) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
bufferCallbackCls =
|
||||
JClass(env, "edu/wpi/first/hal/simulation/BufferCallback");
|
||||
if (!bufferCallbackCls) return JNI_ERR;
|
||||
if (!bufferCallbackCls) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
bufferCallbackCallback = env->GetMethodID(bufferCallbackCls, "callback",
|
||||
"(Ljava/lang/String;[BI)V");
|
||||
if (!bufferCallbackCallback) return JNI_ERR;
|
||||
if (!bufferCallbackCallback) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
constBufferCallbackCls =
|
||||
JClass(env, "edu/wpi/first/hal/simulation/ConstBufferCallback");
|
||||
if (!constBufferCallbackCls) return JNI_ERR;
|
||||
if (!constBufferCallbackCls) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
constBufferCallbackCallback = env->GetMethodID(
|
||||
constBufferCallbackCls, "callback", "(Ljava/lang/String;[BI)V");
|
||||
if (!constBufferCallbackCallback) return JNI_ERR;
|
||||
if (!constBufferCallbackCallback) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
spiReadAutoReceiveBufferCallbackCls = JClass(
|
||||
env, "edu/wpi/first/hal/simulation/SpiReadAutoReceiveBufferCallback");
|
||||
if (!spiReadAutoReceiveBufferCallbackCls) return JNI_ERR;
|
||||
if (!spiReadAutoReceiveBufferCallbackCls) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
spiReadAutoReceiveBufferCallbackCallback =
|
||||
env->GetMethodID(spiReadAutoReceiveBufferCallbackCls, "callback",
|
||||
"(Ljava/lang/String;[II)I");
|
||||
if (!spiReadAutoReceiveBufferCallbackCallback) return JNI_ERR;
|
||||
if (!spiReadAutoReceiveBufferCallbackCallback) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
InitializeStore();
|
||||
InitializeBufferStore();
|
||||
InitializeConstBufferStore();
|
||||
InitializeSpiBufferStore();
|
||||
if (!InitializeSimDeviceDataJNI(env)) return JNI_ERR;
|
||||
if (!InitializeSimDeviceDataJNI(env)) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
|
||||
void SimOnUnload(JavaVM* vm, void* reserved) {
|
||||
JNIEnv* env;
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
||||
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
notifyCallbackCls.free(env);
|
||||
bufferCallbackCls.free(env);
|
||||
@@ -92,13 +112,21 @@ void SimOnUnload(JavaVM* vm, void* reserved) {
|
||||
jvm = nullptr;
|
||||
}
|
||||
|
||||
JavaVM* GetJVM() { return jvm; }
|
||||
JavaVM* GetJVM() {
|
||||
return jvm;
|
||||
}
|
||||
|
||||
jmethodID GetNotifyCallback() { return notifyCallbackCallback; }
|
||||
jmethodID GetNotifyCallback() {
|
||||
return notifyCallbackCallback;
|
||||
}
|
||||
|
||||
jmethodID GetBufferCallback() { return bufferCallbackCallback; }
|
||||
jmethodID GetBufferCallback() {
|
||||
return bufferCallbackCallback;
|
||||
}
|
||||
|
||||
jmethodID GetConstBufferCallback() { return constBufferCallbackCallback; }
|
||||
jmethodID GetConstBufferCallback() {
|
||||
return constBufferCallbackCallback;
|
||||
}
|
||||
|
||||
jmethodID GetSpiReadAutoReceiveBufferCallback() {
|
||||
return spiReadAutoReceiveBufferCallbackCallback;
|
||||
|
||||
@@ -110,7 +110,9 @@ SIM_JniHandle sim::AllocateSpiBufferCallback(
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
SIM_JniHandle handle = static_cast<SIM_JniHandle>(handleTmp);
|
||||
auto data = callbackHandles->Get(handle);
|
||||
if (!data) return;
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
*outputCount = data->performCallback(name, buffer, numToRead);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user