Added termios structure checking while port opening, and "Permission denied" exception support

This commit is contained in:
scream3r 2013-04-17 13:48:55 +04:00
parent 82a01329a6
commit c2c52e1e5c
1 changed files with 22 additions and 7 deletions

View File

@ -43,7 +43,7 @@
#include <jni.h> #include <jni.h>
#include "../jssc_SerialNativeInterface.h" #include "../jssc_SerialNativeInterface.h"
//#include <iostream.h> //-lCstd use for Solaris linker #include <iostream> //-lCstd use for Solaris linker
/* OK */ /* OK */
@ -53,6 +53,10 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job
jint hComm; jint hComm;
hComm = open(port, O_RDWR | O_NOCTTY | O_NDELAY); hComm = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if(hComm != -1){ if(hComm != -1){
//since 2.2.0 ->
termios *settings = new termios();
if(tcgetattr(hComm, settings) == 0){
//<- since 2.2.0
#if defined TIOCEXCL && !defined __SunOS #if defined TIOCEXCL && !defined __SunOS
ioctl(hComm, TIOCEXCL);//since 0.9 ioctl(hComm, TIOCEXCL);//since 0.9
#endif #endif
@ -60,6 +64,11 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job
flags &= ~O_NDELAY; flags &= ~O_NDELAY;
fcntl(hComm, F_SETFL, flags); fcntl(hComm, F_SETFL, flags);
} }
else {
hComm = -2;
}
delete settings;
}
else {//since 0.9 -> else {//since 0.9 ->
if(errno == EBUSY){//Port busy if(errno == EBUSY){//Port busy
hComm = -1; hComm = -1;
@ -67,6 +76,12 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job
else if(errno == ENOENT){//Port not found else if(errno == ENOENT){//Port not found
hComm = -2; hComm = -2;
} }
else if(errno == EACCES){//Permission denied
hComm = -3;
}
else {
hComm = -2;
}
}//<- since 0.9 }//<- since 0.9
env->ReleaseStringUTFChars(portName, port); env->ReleaseStringUTFChars(portName, port);
return hComm; return hComm;