diff --git a/src/main/c/Android/jni/SerialPort_Android.c b/src/main/c/Android/jni/SerialPort_Android.c index 820632f..96f40a1 100644 --- a/src/main/c/Android/jni/SerialPort_Android.c +++ b/src/main/c/Android/jni/SerialPort_Android.c @@ -2,7 +2,7 @@ * SerialPort_Android.c * * Created on: Mar 13, 2015 - * Last Updated on: Oct 07, 2018 + * Last Updated on: Oct 08, 2018 * Author: Will Hedgecock * * Copyright (C) 2012-2018 Fazecast, Inc. @@ -38,6 +38,7 @@ #include #include #include +#include #include #include "com_fazecast_jSerialComm_SerialPort.h" #include "AndroidHelperFunctions.h" @@ -62,6 +63,8 @@ jfieldID dataBitsField; jfieldID stopBitsField; jfieldID parityField; jfieldID flowControlField; +jfieldID sendDeviceQueueSizeField; +jfieldID receiveDeviceQueueSizeField; jfieldID timeoutModeField; jfieldID readTimeoutField; jfieldID writeTimeoutField; @@ -115,6 +118,8 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrar stopBitsField = (*env)->GetFieldID(env, serialCommClass, "stopBits", "I"); parityField = (*env)->GetFieldID(env, serialCommClass, "parity", "I"); flowControlField = (*env)->GetFieldID(env, serialCommClass, "flowControl", "I"); + sendDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "sendDeviceQueueSize", "I"); + receiveDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "receiveDeviceQueueSize", "I"); timeoutModeField = (*env)->GetFieldID(env, serialCommClass, "timeoutMode", "I"); readTimeoutField = (*env)->GetFieldID(env, serialCommClass, "readTimeout", "I"); writeTimeoutField = (*env)->GetFieldID(env, serialCommClass, "writeTimeout", "I"); @@ -168,33 +173,11 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative( return serialPortFD; } -/*JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_associateNativeHandle(JNIEnv *env, jobject obj, jlong serialPortFD) -{ - // TODO: SET A FLAG SAYING THAT WE ARE USING USBFS - // Attempt to determine which serial port file this descriptor belongs to - char serialPortFdChars[16]; - sprintf(serialPortFdChars, "%d", (int)serialPortFD); - char* fileDescriptorName = (char*)malloc(256), *portName = (char*)malloc(256); - strcpy(fileDescriptorName, "/proc/self/fd/"); - strcat(fileDescriptorName, serialPortFdChars); - ssize_t result = readlink(fileDescriptorName, portName, 256); - free(fileDescriptorName); - if (result < 0) - { - free(portName); - return JNI_FALSE; - } - - // Set the port name in the Java object - (*env)->SetObjectField(env, obj, comPortField, (*env)->NewStringUTF(env, portName)); - free(portName); - return JNI_TRUE; -}*/ - JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(JNIEnv *env, jobject obj, jlong serialPortFD) { if (serialPortFD <= 0) return JNI_FALSE; + struct serial_struct serInfo; struct termios options = { 0 }; // Get port parameters from Java class @@ -203,6 +186,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField); int parityInt = (*env)->GetIntField(env, obj, parityField); int flowControl = (*env)->GetIntField(env, obj, flowControlField); + int sendDeviceQueueSize = (*env)->GetIntField(env, obj, sendDeviceQueueSizeField); + int receiveDeviceQueueSize = (*env)->GetIntField(env, obj, receiveDeviceQueueSizeField); unsigned char configDisabled = (*env)->GetBooleanField(env, obj, disableConfigField); unsigned char isDtrEnabled = (*env)->GetBooleanField(env, obj, isDtrEnabledField); unsigned char isRtsEnabled = (*env)->GetBooleanField(env, obj, isRtsEnabledField); @@ -261,7 +246,12 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J requestWrapper.data = &options; retVal = ioctl(serialPortFD, USBDEVFS_IOCTL, &requestWrapper); }*/ - if (baudRateCode == 0) // Set custom baud rate + + // Attempt to set the transmit buffer size and any necessary custom baud rates + ioctl(serialPortFD, TIOCGSERIAL, &serInfo); + serInfo.xmit_fifo_size = sendDeviceQueueSize; + ioctl(serialPortFD, TIOCSSERIAL, &serInfo); + if (baudRateCode == 0) setBaudRate(serialPortFD, baudRate); return ((retVal == 0) && Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD) ? JNI_TRUE : JNI_FALSE); } diff --git a/src/main/c/Linux/SerialPort_Linux.c b/src/main/c/Linux/SerialPort_Linux.c index 6178f95..f652f72 100644 --- a/src/main/c/Linux/SerialPort_Linux.c +++ b/src/main/c/Linux/SerialPort_Linux.c @@ -2,7 +2,7 @@ * SerialPort_Linux.c * * Created on: Feb 25, 2012 - * Last Updated on: Oct 07, 2018 + * Last Updated on: Oct 08, 2018 * Author: Will Hedgecock * * Copyright (C) 2012-2018 Fazecast, Inc. @@ -55,6 +55,8 @@ jfieldID dataBitsField; jfieldID stopBitsField; jfieldID parityField; jfieldID flowControlField; +jfieldID sendDeviceQueueSizeField; +jfieldID receiveDeviceQueueSizeField; jfieldID timeoutModeField; jfieldID readTimeoutField; jfieldID writeTimeoutField; @@ -109,6 +111,8 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrar stopBitsField = (*env)->GetFieldID(env, serialCommClass, "stopBits", "I"); parityField = (*env)->GetFieldID(env, serialCommClass, "parity", "I"); flowControlField = (*env)->GetFieldID(env, serialCommClass, "flowControl", "I"); + sendDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "sendDeviceQueueSize", "I"); + receiveDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "receiveDeviceQueueSize", "I"); timeoutModeField = (*env)->GetFieldID(env, serialCommClass, "timeoutMode", "I"); readTimeoutField = (*env)->GetFieldID(env, serialCommClass, "readTimeout", "I"); writeTimeoutField = (*env)->GetFieldID(env, serialCommClass, "writeTimeout", "I"); @@ -164,6 +168,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J { if (serialPortFD <= 0) return JNI_FALSE; + struct serial_struct serInfo; struct termios options = { 0 }; // Get port parameters from Java class @@ -172,6 +177,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField); int parityInt = (*env)->GetIntField(env, obj, parityField); int flowControl = (*env)->GetIntField(env, obj, flowControlField); + int sendDeviceQueueSize = (*env)->GetIntField(env, obj, sendDeviceQueueSizeField); + int receiveDeviceQueueSize = (*env)->GetIntField(env, obj, receiveDeviceQueueSizeField); unsigned char configDisabled = (*env)->GetBooleanField(env, obj, disableConfigField); unsigned char isDtrEnabled = (*env)->GetBooleanField(env, obj, isDtrEnabledField); unsigned char isRtsEnabled = (*env)->GetBooleanField(env, obj, isRtsEnabledField); @@ -205,10 +212,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J cfsetospeed(&options, baudRateCode); } - // Apply changes + // Apply changes and block non-root users from opening this port int retVal = configDisabled ? 0 : tcsetattr(serialPortFD, TCSANOW, &options); - ioctl(serialPortFD, TIOCEXCL); // Block non-root users from opening this port - if (baudRateCode == 0) // Set custom baud rate + ioctl(serialPortFD, TIOCEXCL); + + // Attempt to set the transmit buffer size and any necessary custom baud rates + ioctl(serialPortFD, TIOCGSERIAL, &serInfo); + serInfo.xmit_fifo_size = sendDeviceQueueSize; + ioctl(serialPortFD, TIOCSSERIAL, &serInfo); + if (baudRateCode == 0) setBaudRate(serialPortFD, baudRate); return ((retVal == 0) && Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD) ? JNI_TRUE : JNI_FALSE); } diff --git a/src/main/c/OSX/SerialPort_OSX.c b/src/main/c/OSX/SerialPort_OSX.c index 748022b..c4eebe8 100644 --- a/src/main/c/OSX/SerialPort_OSX.c +++ b/src/main/c/OSX/SerialPort_OSX.c @@ -2,7 +2,7 @@ * SerialPort_OSX.c * * Created on: Feb 25, 2012 - * Last Updated on: Oct 07, 2018 + * Last Updated on: Oct 08, 2018 * Author: Will Hedgecock * * Copyright (C) 2012-2018 Fazecast, Inc. @@ -57,6 +57,8 @@ jfieldID dataBitsField; jfieldID stopBitsField; jfieldID parityField; jfieldID flowControlField; +jfieldID sendDeviceQueueSizeField; +jfieldID receiveDeviceQueueSizeField; jfieldID timeoutModeField; jfieldID readTimeoutField; jfieldID writeTimeoutField; @@ -159,6 +161,8 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrar stopBitsField = (*env)->GetFieldID(env, serialCommClass, "stopBits", "I"); parityField = (*env)->GetFieldID(env, serialCommClass, "parity", "I"); flowControlField = (*env)->GetFieldID(env, serialCommClass, "flowControl", "I"); + sendDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "sendDeviceQueueSize", "I"); + receiveDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "receiveDeviceQueueSize", "I"); timeoutModeField = (*env)->GetFieldID(env, serialCommClass, "timeoutMode", "I"); readTimeoutField = (*env)->GetFieldID(env, serialCommClass, "readTimeout", "I"); writeTimeoutField = (*env)->GetFieldID(env, serialCommClass, "writeTimeout", "I"); @@ -222,6 +226,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField); int parityInt = (*env)->GetIntField(env, obj, parityField); int flowControl = (*env)->GetIntField(env, obj, flowControlField); + int sendDeviceQueueSize = (*env)->GetIntField(env, obj, sendDeviceQueueSizeField); + int receiveDeviceQueueSize = (*env)->GetIntField(env, obj, receiveDeviceQueueSizeField); unsigned char configDisabled = (*env)->GetBooleanField(env, obj, disableConfigField); unsigned char isDtrEnabled = (*env)->GetBooleanField(env, obj, isDtrEnabledField); unsigned char isRtsEnabled = (*env)->GetBooleanField(env, obj, isRtsEnabledField); @@ -255,10 +261,12 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J cfsetospeed(&options, baudRateCode); } - // Apply changes + // Apply changes and block non-root users from opening this port int retVal = configDisabled ? 0 : tcsetattr(serialPortFD, TCSANOW, &options); - ioctl(serialPortFD, TIOCEXCL); // Block non-root users from opening this port - if (baudRateCode == 0) // Set custom baud rate + ioctl(serialPortFD, TIOCEXCL); + + // Attempt to set any necessary custom baud rates + if (baudRateCode == 0) { speed_t speed = (speed_t)baudRate; ioctl(serialPortFD, IOSSIOSPEED, &speed); diff --git a/src/main/resources/Android/arm64-v8a/libjSerialComm.so b/src/main/resources/Android/arm64-v8a/libjSerialComm.so index 4420606..ca229e8 100644 Binary files a/src/main/resources/Android/arm64-v8a/libjSerialComm.so and b/src/main/resources/Android/arm64-v8a/libjSerialComm.so differ diff --git a/src/main/resources/Android/armeabi-v7a/libjSerialComm.so b/src/main/resources/Android/armeabi-v7a/libjSerialComm.so index ac1188d..f573793 100644 Binary files a/src/main/resources/Android/armeabi-v7a/libjSerialComm.so and b/src/main/resources/Android/armeabi-v7a/libjSerialComm.so differ diff --git a/src/main/resources/Android/armeabi/libjSerialComm.so b/src/main/resources/Android/armeabi/libjSerialComm.so index dddcf31..7586b55 100644 Binary files a/src/main/resources/Android/armeabi/libjSerialComm.so and b/src/main/resources/Android/armeabi/libjSerialComm.so differ diff --git a/src/main/resources/Android/mips/libjSerialComm.so b/src/main/resources/Android/mips/libjSerialComm.so index 029b025..1934bd0 100644 Binary files a/src/main/resources/Android/mips/libjSerialComm.so and b/src/main/resources/Android/mips/libjSerialComm.so differ diff --git a/src/main/resources/Android/mips64/libjSerialComm.so b/src/main/resources/Android/mips64/libjSerialComm.so index 9cde1ed..edf457e 100644 Binary files a/src/main/resources/Android/mips64/libjSerialComm.so and b/src/main/resources/Android/mips64/libjSerialComm.so differ diff --git a/src/main/resources/Android/x86/libjSerialComm.so b/src/main/resources/Android/x86/libjSerialComm.so index 877e959..9746b61 100644 Binary files a/src/main/resources/Android/x86/libjSerialComm.so and b/src/main/resources/Android/x86/libjSerialComm.so differ diff --git a/src/main/resources/Android/x86_64/libjSerialComm.so b/src/main/resources/Android/x86_64/libjSerialComm.so index 96417fb..b946cd0 100644 Binary files a/src/main/resources/Android/x86_64/libjSerialComm.so and b/src/main/resources/Android/x86_64/libjSerialComm.so differ diff --git a/src/main/resources/OSX/x86/libjSerialComm.jnilib b/src/main/resources/OSX/x86/libjSerialComm.jnilib index 75ad188..df7f06e 100755 Binary files a/src/main/resources/OSX/x86/libjSerialComm.jnilib and b/src/main/resources/OSX/x86/libjSerialComm.jnilib differ diff --git a/src/main/resources/OSX/x86_64/libjSerialComm.jnilib b/src/main/resources/OSX/x86_64/libjSerialComm.jnilib index 35b7d9a..c124c84 100755 Binary files a/src/main/resources/OSX/x86_64/libjSerialComm.jnilib and b/src/main/resources/OSX/x86_64/libjSerialComm.jnilib differ diff --git a/src/main/resources/Windows/x86/jSerialComm.dll b/src/main/resources/Windows/x86/jSerialComm.dll index 133a732..b89d5b1 100644 Binary files a/src/main/resources/Windows/x86/jSerialComm.dll and b/src/main/resources/Windows/x86/jSerialComm.dll differ diff --git a/src/main/resources/Windows/x86_64/jSerialComm.dll b/src/main/resources/Windows/x86_64/jSerialComm.dll index d89ab55..897fa05 100644 Binary files a/src/main/resources/Windows/x86_64/jSerialComm.dll and b/src/main/resources/Windows/x86_64/jSerialComm.dll differ