Skip to content

Commit

Permalink
Change system calls from select() to poll() to overcome max FD limit …
Browse files Browse the repository at this point in the history
…of 1024
  • Loading branch information
hedgecrw committed Dec 11, 2018
1 parent c5741a2 commit 5a6dbfd
Show file tree
Hide file tree
Showing 21 changed files with 19 additions and 33 deletions.
20 changes: 6 additions & 14 deletions src/main/c/Android/jni/SerialPort_Android.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SerialPort_Android.c
*
* Created on: Mar 13, 2015
* Last Updated on: Oct 08, 2018
* Last Updated on: Dec 07, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
Expand Down Expand Up @@ -34,6 +34,7 @@
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
Expand Down Expand Up @@ -380,24 +381,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF

JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
{
// Initialize the waiting set
if (serialPortFD <= 0)
return 0;

// Initialize the waiting set
fd_set waitingSet;
FD_ZERO(&waitingSet);
FD_SET(serialPortFD, &waitingSet);
struct pollfd waitingSet = { serialPortFD, POLLIN, 0 };

// Wait for a serial port event
int retVal;
do
{
struct timeval timeout = { 1, 0 };
retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout);
} while ((retVal < 0) && ((errno == EINTR) || (errno == EAGAIN)));
if (retVal <= 0)
if (poll(&waitingSet, 1, 1000) <= 0)
return 0;
return (FD_ISSET(serialPortFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
return (waitingSet.revents & POLLIN) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortFD)
Expand Down
5 changes: 3 additions & 2 deletions src/main/c/Posix/PosixHelperFunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* PosixHelperFunctions.c
*
* Created on: Mar 10, 2015
* Last Updated on: Nov 12, 2018
* Last Updated on: Dec 07, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
Expand Down Expand Up @@ -511,7 +511,8 @@ int setBaudRateCustom(int portFD, baud_rate baudRate)
#else
struct serial_struct serInfo;
int retVal = ioctl(portFD, TIOCGSERIAL, &serInfo);
if (retVal == 0) {
if (retVal == 0)
{
serInfo.flags &= ~ASYNC_SPD_MASK;
serInfo.flags |= ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
serInfo.custom_divisor = serInfo.baud_base / baudRate;
Expand Down
25 changes: 9 additions & 16 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: Nov 12, 2018
* Last Updated on: Dec 07, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
Expand All @@ -25,6 +25,7 @@

#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -336,8 +337,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
// Attempt to set the transmit buffer size and any necessary custom baud rates
#if defined(__linux__)
struct serial_struct serInfo;
int tiocgserialRetVal = ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
if (tiocgserialRetVal == 0) {
int ioctlRetVal = ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
if (ioctlRetVal == 0)
{
serInfo.xmit_fifo_size = sendDeviceQueueSize;
ioctl(serialPortFD, TIOCSSERIAL, &serInfo);
}
Expand Down Expand Up @@ -447,24 +449,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF

JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
{
// Initialize the waiting set
if (serialPortFD <= 0)
return 0;

// Initialize the waiting set
fd_set waitingSet;
FD_ZERO(&waitingSet);
FD_SET(serialPortFD, &waitingSet);
struct pollfd waitingSet = { serialPortFD, POLLIN, 0 };

// Wait for a serial port event
int retVal;
do
{
struct timeval timeout = { 1, 0 };
retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout);
} while ((retVal < 0) && ((errno == EINTR) || (errno == EAGAIN)));
if (retVal <= 0)
if (poll(&waitingSet, 1, 1000) <= 0)
return 0;
return (FD_ISSET(serialPortFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
return (waitingSet.revents & POLLIN) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
}

JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortFD)
Expand Down
Binary file modified src/main/resources/Android/arm64-v8a/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/armeabi-v7a/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/armeabi/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/mips/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/mips64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/x86/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Android/x86_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv5/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv6-hf/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv6/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv7-hf/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv7/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv8_32/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/armv8_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/x86/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/Linux/x86_64/libjSerialComm.so
Binary file not shown.
Binary file modified src/main/resources/OSX/x86_64/libjSerialComm.jnilib
Binary file not shown.
2 changes: 1 addition & 1 deletion src/test/java/com/fazecast/jSerialComm/SerialPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* This class provides a test case for the jSerialComm library.
*
* @author Will Hedgecock &lt;will.hedgecock@gmail.com&gt;
* @version 2.3.0
* @version 2.4.0
* @see java.io.InputStream
* @see java.io.OutputStream
*/
Expand Down

0 comments on commit 5a6dbfd

Please sign in to comment.