From c2c52e1e5c7723ce7c51df5589ed270f97146fbc Mon Sep 17 00:00:00 2001 From: scream3r Date: Wed, 17 Apr 2013 13:48:55 +0400 Subject: [PATCH] Added termios structure checking while port opening, and "Permission denied" exception support --- src/cpp/_nix_based/jssc.cpp | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/cpp/_nix_based/jssc.cpp b/src/cpp/_nix_based/jssc.cpp index bdb5c9d..674a3c8 100644 --- a/src/cpp/_nix_based/jssc.cpp +++ b/src/cpp/_nix_based/jssc.cpp @@ -43,7 +43,7 @@ #include #include "../jssc_SerialNativeInterface.h" -//#include //-lCstd use for Solaris linker +#include //-lCstd use for Solaris linker /* OK */ @@ -53,12 +53,21 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job jint hComm; hComm = open(port, O_RDWR | O_NOCTTY | O_NDELAY); if(hComm != -1){ - #if defined TIOCEXCL && !defined __SunOS - ioctl(hComm, TIOCEXCL);//since 0.9 - #endif - int flags = fcntl(hComm, F_GETFL, 0); - flags &= ~O_NDELAY; - fcntl(hComm, F_SETFL, flags); + //since 2.2.0 -> + termios *settings = new termios(); + if(tcgetattr(hComm, settings) == 0){ + //<- since 2.2.0 + #if defined TIOCEXCL && !defined __SunOS + ioctl(hComm, TIOCEXCL);//since 0.9 + #endif + int flags = fcntl(hComm, F_GETFL, 0); + flags &= ~O_NDELAY; + fcntl(hComm, F_SETFL, flags); + } + else { + hComm = -2; + } + delete settings; } else {//since 0.9 -> if(errno == EBUSY){//Port busy @@ -67,6 +76,12 @@ JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_openPort(JNIEnv *env, job else if(errno == ENOENT){//Port not found hComm = -2; } + else if(errno == EACCES){//Permission denied + hComm = -3; + } + else { + hComm = -2; + } }//<- since 0.9 env->ReleaseStringUTFChars(portName, port); return hComm;