Skip to content

Commit

Permalink
Revert preset-RTS/DTR code
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Oct 29, 2023
1 parent 10bcf48 commit 6b792b5
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 172 deletions.
112 changes: 0 additions & 112 deletions src/main/c/Posix/SerialPort_Posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,62 +1104,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNI
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;

// Fix user permissions so that they can access the port, if allowed
int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0);
if (!userCanAccess && requestElevatedPermissions)
verifyAndSetUserPortGroup(portName);

// Send a system command to preset the RTS mode of the serial port
char commandString[128];
#if defined(__linux__)
sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);

(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
checkJniError(env, __LINE__ - 1);
return (result == 0);
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;

// Fix user permissions so that they can access the port, if allowed
int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0);
if (!userCanAccess && requestElevatedPermissions)
verifyAndSetUserPortGroup(portName);

// Send a system command to preclear the RTS mode of the serial port
char commandString[128];
#if defined(__linux__)
sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s -hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);

(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
checkJniError(env, __LINE__ - 1);
return (result == 0);
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortPointer)
{
const int modemBits = TIOCM_DTR;
Expand All @@ -1186,62 +1130,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;

// Fix user permissions so that they can access the port, if allowed
int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0);
if (!userCanAccess && requestElevatedPermissions)
verifyAndSetUserPortGroup(portName);

// Send a system command to preset the DTR mode of the serial port
char commandString[128];
#if defined(__linux__)
sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);

(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
checkJniError(env, __LINE__ - 1);
return (result == 0);
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;
unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField);
if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE;

// Fix user permissions so that they can access the port, if allowed
int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0);
if (!userCanAccess && requestElevatedPermissions)
verifyAndSetUserPortGroup(portName);

// Send a system command to preclear the DTR mode of the serial port
char commandString[128];
#if defined(__linux__)
sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s -hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);

(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
checkJniError(env, __LINE__ - 1);
return (result == 0);
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer)
{
int modemBits = 0;
Expand Down
32 changes: 0 additions & 32 deletions src/main/c/Posix/com_fazecast_jSerialComm_SerialPort.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions src/main/c/Windows/SerialPort_Windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -1264,16 +1264,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNI
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JNIEnv *env, jobject obj)
{
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(JNIEnv *env, jobject obj)
{
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortPointer)
{
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
Expand All @@ -1298,16 +1288,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JNIEnv *env, jobject obj)
{
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(JNIEnv *env, jobject obj)
{
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer)
{
DWORD modemStatus = 0;
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/fazecast/jSerialComm/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,8 @@ public final int getLastErrorCode()
private native boolean clearBreak(long portHandle); // Clear BREAK status on serial line
private native boolean setRTS(long portHandle); // Set RTS line to 1
private native boolean clearRTS(long portHandle); // Clear RTS line to 0
private native boolean presetRTS(); // Set RTS line to 1 prior to opening
private native boolean preclearRTS(); // Clear RTS line to 0 prior to opening
private native boolean setDTR(long portHandle); // Set DTR line to 1
private native boolean clearDTR(long portHandle); // Clear DTR line to 0
private native boolean presetDTR(); // Set DTR line to 1 prior to opening
private native boolean preclearDTR(); // Clear DTR line to 0 prior to opening
private native boolean getCTS(long portHandle); // Returns whether the CTS signal is 1
private native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1
private native boolean getDCD(long portHandle); // Returns whether the DCD signal is 1
Expand Down Expand Up @@ -924,7 +920,7 @@ public final int writeBytes(byte[] buffer, int bytesToWrite, int offset)
public final boolean setRTS()
{
isRtsEnabled = true;
return (androidPort != null) ? androidPort.setRTS() : ((portHandle != 0) ? setRTS(portHandle) : presetRTS());
return (androidPort != null) ? androidPort.setRTS() : ((portHandle == 0) || setRTS(portHandle));
}

/**
Expand All @@ -934,7 +930,7 @@ public final boolean setRTS()
public final boolean clearRTS()
{
isRtsEnabled = false;
return (androidPort != null) ? androidPort.clearRTS() : ((portHandle != 0) ? clearRTS(portHandle) : preclearRTS());
return (androidPort != null) ? androidPort.clearRTS() : ((portHandle == 0) || clearRTS(portHandle));
}

/**
Expand All @@ -944,7 +940,7 @@ public final boolean clearRTS()
public final boolean setDTR()
{
isDtrEnabled = true;
return (androidPort != null) ? androidPort.setDTR() : ((portHandle != 0) ? setDTR(portHandle) : presetDTR());
return (androidPort != null) ? androidPort.setDTR() : ((portHandle == 0) || setDTR(portHandle));
}

/**
Expand All @@ -954,7 +950,7 @@ public final boolean setDTR()
public final boolean clearDTR()
{
isDtrEnabled = false;
return (androidPort != null) ? androidPort.clearDTR() : ((portHandle != 0) ? clearDTR(portHandle) : preclearDTR());
return (androidPort != null) ? androidPort.clearDTR() : ((portHandle == 0) || clearDTR(portHandle));
}

/**
Expand Down

0 comments on commit 6b792b5

Please sign in to comment.