Skip to content

Commit

Permalink
Cache JNI SerialComm class instead of obtaining from jobject
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Apr 22, 2024
1 parent d255a25 commit 0d18669
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/main/c/Posix/SerialPort_Posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SerialPort_Posix.c
*
* Created on: Feb 25, 2012
* Last Updated on: Apr 10, 2024
* Last Updated on: Apr 22, 2024
* Author: Will Hedgecock
*
* Copyright (C) 2012-2024 Fazecast, Inc.
Expand Down Expand Up @@ -43,6 +43,7 @@
#include "PosixHelperFunctions.h"

// Cached class, method, and field IDs
jclass serialCommClass;
jclass jniErrorClass;
jmethodID serialCommConstructor;
jfieldID serialPortFdField;
Expand Down Expand Up @@ -243,7 +244,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
jint jniVersion = JNI_VERSION_1_2;
if ((*jvm)->GetEnv(jvm, (void**)&env, jniVersion))
return JNI_ERR;
jclass serialCommClass = (*env)->FindClass(env, "com/fazecast/jSerialComm/SerialPort");
serialCommClass = (*env)->FindClass(env, "com/fazecast/jSerialComm/SerialPort");
if (!serialCommClass) return JNI_ERR;
jniErrorClass = (*env)->FindClass(env, "java/lang/Exception");
if (!jniErrorClass) return JNI_ERR;
Expand Down
19 changes: 10 additions & 9 deletions src/main/c/Windows/SerialPort_Windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SerialPort_Windows.c
*
* Created on: Feb 25, 2012
* Last Updated on: Apr 11, 2024
* Last Updated on: Apr 22, 2024
* Author: Will Hedgecock
*
* Copyright (C) 2012-2024 Fazecast, Inc.
Expand Down Expand Up @@ -43,6 +43,7 @@
#include "WindowsHelperFunctions.h"

// Cached class, method, and field IDs
jclass serialCommClass;
jclass jniErrorClass;
jmethodID serialCommConstructor;
jfieldID serialPortHandleField;
Expand Down Expand Up @@ -114,7 +115,7 @@ static inline jboolean checkJniError(JNIEnv *env, int lineNumber)
}

// Generalized port enumeration function
static void enumeratePorts(JNIEnv *env, jclass serialComm)
static void enumeratePorts(JNIEnv *env)
{
// Reset the enumerated flag on all non-open serial ports
for (int i = 0; i < serialPorts.length; ++i)
Expand Down Expand Up @@ -233,7 +234,10 @@ static void enumeratePorts(JNIEnv *env, jclass serialComm)
if (!manufacturerString || !SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_MFG, NULL, (BYTE*)manufacturerString, manufacturerLength, NULL))
{
if (manufacturerString)
{
free(manufacturerString);
manufacturerString = NULL;
}
}
}

Expand Down Expand Up @@ -406,7 +410,7 @@ static void enumeratePorts(JNIEnv *env, jclass serialComm)
FT_CreateDeviceInfoListFunction FT_CreateDeviceInfoList = (FT_CreateDeviceInfoListFunction)GetProcAddress(ftdiLibInstance, "FT_CreateDeviceInfoList");
FT_GetDeviceInfoListFunction FT_GetDeviceInfoList = (FT_GetDeviceInfoListFunction)GetProcAddress(ftdiLibInstance, "FT_GetDeviceInfoList");
FT_EEPROM_ReadFunction FT_EEPROM_Read = (FT_EEPROM_ReadFunction)GetProcAddress(ftdiLibInstance, "FT_EEPROM_Read");
unsigned char allowOpenForEnumeration = (*env)->GetStaticBooleanField(env, serialComm, allowOpenForEnumerationField);
unsigned char allowOpenForEnumeration = (*env)->GetStaticBooleanField(env, serialCommClass, allowOpenForEnumerationField);
if (FT_CreateDeviceInfoList && FT_GetDeviceInfoList && FT_EEPROM_Read)
{
DWORD numDevs;
Expand Down Expand Up @@ -628,7 +632,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
jint jniVersion = JNI_VERSION_1_2;
if ((*jvm)->GetEnv(jvm, (void**)&env, jniVersion))
return JNI_ERR;
jclass serialCommClass = (*env)->FindClass(env, "com/fazecast/jSerialComm/SerialPort");
serialCommClass = (*env)->FindClass(env, "com/fazecast/jSerialComm/SerialPort");
if (!serialCommClass) return JNI_ERR;
jniErrorClass = (*env)->FindClass(env, "java/lang/Exception");
if (!jniErrorClass) return JNI_ERR;
Expand Down Expand Up @@ -754,7 +758,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
EnterCriticalSection(&criticalSection);

// Enumerate all ports on the current system
enumeratePorts(env, serialComm);
enumeratePorts(env);

// Get relevant SerialComm methods and fill in com port array
jobjectArray arrayObject = (*env)->NewObjectArray(env, serialPorts.length, serialComm, 0);
Expand Down Expand Up @@ -801,10 +805,7 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_retrievePortDeta
char continueRetrieval = 1;
EnterCriticalSection(&criticalSection);
if (!portsEnumerated)
{
jclass serialComm = (*env)->GetObjectClass(env, obj);
enumeratePorts(env, serialComm);
}
enumeratePorts(env);
serialPort *port = fetchPort(&serialPorts, portName);
if (!port)
continueRetrieval = 0;
Expand Down

0 comments on commit 0d18669

Please sign in to comment.