26,6 → 26,7 |
#include <fcntl.h> |
#include <unistd.h> |
#include <dirent.h> |
#include <pthread.h> |
|
#ifdef CRTSCTS |
#define HW_FLOW CRTSCTS |
65,6 → 66,13 |
HANDLE port; |
#else |
int port; |
/* There appears to be a case where we can free() the port_descriptor |
* after the select() call in SerialInputStream_readByte(). |
* This means that we segfault on the FD_SET() call. |
* This mutex will keep us alive until we have exited the |
* readByte() call. |
*/ |
pthread_mutex_t in_use; |
#endif |
}; |
|
71,8 → 79,8 |
// |
// Local Variables |
// |
struct port_descriptor** port_list = NULL; |
int port_list_size; |
static struct port_descriptor** port_list = NULL; |
static int port_list_size; |
|
#ifdef _WIN32 |
//Unfortunately, Windows does not let us get the state of the DTR/RTS lines. |
474,6 → 482,7 |
} |
|
#else |
pthread_mutex_init( &(new_port->in_use), NULL ); |
new_port->port = open( port_to_open, O_RDWR ); |
if( new_port->port < 0 && errno == ENOENT ){ |
//That's not a valid serial port, error out |
636,6 → 645,7 |
} |
} |
#else |
pthread_mutex_init( &(new_port->in_use), NULL ); |
new_port->port = open( port_to_open, O_RDWR ); |
if( new_port->port < 0 && errno == ENOENT ){ |
//That's not a valid serial port, error out |
705,6 → 715,10 |
} |
|
close( desc->port ); |
#ifndef _WIN32 |
pthread_mutex_lock( &(desc->in_use) ); |
pthread_mutex_unlock( &(desc->in_use) ); |
#endif |
free( port_list[ array_pos ] ); |
port_list[ array_pos ] = NULL; |
} |
1275,9 → 1289,12 |
int originalState; |
int selectStatus; |
|
pthread_mutex_lock( &(desc->in_use) ); |
|
//first get the original state of the serial port lines |
if( ioctl( desc->port, TIOCMGET, &originalState ) < 0 ){ |
throw_io_exception( env, errno ); |
pthread_mutex_unlock( &(desc->in_use) ); |
return -1; |
} |
|
1285,12 → 1302,20 |
FD_ZERO( &fdset ); |
FD_SET( desc->port, &fdset ); |
timeout.tv_sec = 0; |
timeout.tv_usec = 1000; // 1,000 microseconds = 100ms |
timeout.tv_usec = 10000; // 10,000 microseconds = 10ms |
|
selectStatus = select( desc->port + 1, &fdset, NULL, NULL, &timeout ); |
if( selectStatus < 0 ){ |
throw_io_exception( env, errno ); |
return -1; |
int errval; |
if( errno == EBADF ){ |
// EOF |
errval= 0; |
}else{ |
throw_io_exception( env, errno ); |
errval = -1; |
} |
pthread_mutex_unlock( &(desc->in_use) ); |
return errval; |
} |
|
if( selectStatus == 0 ){ |
1297,6 → 1322,7 |
//This was a timeout |
if( ioctl( desc->port, TIOCMGET, &get_val ) < 0 ){ |
throw_io_exception( env, errno ); |
pthread_mutex_unlock( &(desc->in_use) ); |
return -1; |
} |
|
1313,6 → 1339,7 |
if( stat < 0 ){ |
//throw new exception |
throw_io_exception( env, errno ); |
pthread_mutex_unlock( &(desc->in_use) ); |
return -1; |
} |
|
1332,6 → 1359,7 |
//information on our serial port state. |
if( ioctl( desc->port, TIOCMGET, &get_val ) < 0 ){ |
throw_io_exception( env, errno ); |
pthread_mutex_unlock( &(desc->in_use) ); |
return -1; |
} |
|
1365,6 → 1393,7 |
ret_val |= ( 0x01 << 14 ); |
} |
|
pthread_mutex_unlock( &(desc->in_use) ); |
#endif |
|
return ret_val; |