Change system calls from select() to poll() to overcome max FD limit of 1024

This commit is contained in:
hedgecrw85 2018-12-11 16:07:29 -06:00
parent c5741a23e4
commit 5a6dbfd820
21 changed files with 19 additions and 33 deletions

View File

@ -2,7 +2,7 @@
* SerialPort_Android.c * SerialPort_Android.c
* *
* Created on: Mar 13, 2015 * Created on: Mar 13, 2015
* Last Updated on: Oct 08, 2018 * Last Updated on: Dec 07, 2018
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2018 Fazecast, Inc. * Copyright (C) 2012-2018 Fazecast, Inc.
@ -34,6 +34,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <poll.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <sys/time.h> #include <sys/time.h>
@ -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) JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
{ {
// Initialize the waiting set
if (serialPortFD <= 0) if (serialPortFD <= 0)
return 0; return 0;
struct pollfd waitingSet = { serialPortFD, POLLIN, 0 };
// Initialize the waiting set
fd_set waitingSet;
FD_ZERO(&waitingSet);
FD_SET(serialPortFD, &waitingSet);
// Wait for a serial port event // Wait for a serial port event
int retVal; if (poll(&waitingSet, 1, 1000) <= 0)
do
{
struct timeval timeout = { 1, 0 };
retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout);
} while ((retVal < 0) && ((errno == EINTR) || (errno == EAGAIN)));
if (retVal <= 0)
return 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) JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortFD)

View File

@ -2,7 +2,7 @@
* PosixHelperFunctions.c * PosixHelperFunctions.c
* *
* Created on: Mar 10, 2015 * Created on: Mar 10, 2015
* Last Updated on: Nov 12, 2018 * Last Updated on: Dec 07, 2018
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2018 Fazecast, Inc. * Copyright (C) 2012-2018 Fazecast, Inc.
@ -511,7 +511,8 @@ int setBaudRateCustom(int portFD, baud_rate baudRate)
#else #else
struct serial_struct serInfo; struct serial_struct serInfo;
int retVal = ioctl(portFD, TIOCGSERIAL, &serInfo); int retVal = ioctl(portFD, TIOCGSERIAL, &serInfo);
if (retVal == 0) { if (retVal == 0)
{
serInfo.flags &= ~ASYNC_SPD_MASK; serInfo.flags &= ~ASYNC_SPD_MASK;
serInfo.flags |= ASYNC_SPD_CUST | ASYNC_LOW_LATENCY; serInfo.flags |= ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
serInfo.custom_divisor = serInfo.baud_base / baudRate; serInfo.custom_divisor = serInfo.baud_base / baudRate;

View File

@ -2,7 +2,7 @@
* SerialPort_Posix.c * SerialPort_Posix.c
* *
* Created on: Feb 25, 2012 * Created on: Feb 25, 2012
* Last Updated on: Nov 12, 2018 * Last Updated on: Dec 07, 2018
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2018 Fazecast, Inc. * Copyright (C) 2012-2018 Fazecast, Inc.
@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -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 // Attempt to set the transmit buffer size and any necessary custom baud rates
#if defined(__linux__) #if defined(__linux__)
struct serial_struct serInfo; struct serial_struct serInfo;
int tiocgserialRetVal = ioctl(serialPortFD, TIOCGSERIAL, &serInfo); int ioctlRetVal = ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
if (tiocgserialRetVal == 0) { if (ioctlRetVal == 0)
{
serInfo.xmit_fifo_size = sendDeviceQueueSize; serInfo.xmit_fifo_size = sendDeviceQueueSize;
ioctl(serialPortFD, TIOCSSERIAL, &serInfo); ioctl(serialPortFD, TIOCSSERIAL, &serInfo);
} }
@ -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) JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
{ {
// Initialize the waiting set
if (serialPortFD <= 0) if (serialPortFD <= 0)
return 0; return 0;
struct pollfd waitingSet = { serialPortFD, POLLIN, 0 };
// Initialize the waiting set
fd_set waitingSet;
FD_ZERO(&waitingSet);
FD_SET(serialPortFD, &waitingSet);
// Wait for a serial port event // Wait for a serial port event
int retVal; if (poll(&waitingSet, 1, 1000) <= 0)
do
{
struct timeval timeout = { 1, 0 };
retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout);
} while ((retVal < 0) && ((errno == EINTR) || (errno == EAGAIN)));
if (retVal <= 0)
return 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) JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortFD)

View File

@ -32,7 +32,7 @@ import java.util.Scanner;
* This class provides a test case for the jSerialComm library. * This class provides a test case for the jSerialComm library.
* *
* @author Will Hedgecock &lt;will.hedgecock@gmail.com&gt; * @author Will Hedgecock &lt;will.hedgecock@gmail.com&gt;
* @version 2.3.0 * @version 2.4.0
* @see java.io.InputStream * @see java.io.InputStream
* @see java.io.OutputStream * @see java.io.OutputStream
*/ */