Skip to content

Commit

Permalink
Merge pull request #146 from hiddenalpha/report-write-bytes-errors-as…
Browse files Browse the repository at this point in the history
…-exceptions-20231108

Report writeBytes errors via java exceptions
  • Loading branch information
tresf authored Nov 8, 2023
2 parents 38e8371 + e36759a commit 4a8e4ac
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/main/cpp/_nix_based/jssc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
* e-mail: [email protected]
* web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/
*/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
Expand Down Expand Up @@ -527,11 +529,21 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR
*/
JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes
(JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){
jboolean ret = JNI_FALSE;
jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE);
jint bufferSize = env->GetArrayLength(buffer);
jint result = write(portHandle, jBuffer, (size_t)bufferSize);
if( result == -1 ){
int err = errno; /*bakup errno*/
jclass exClz = env->FindClass("java/io/IOException");
assert(exClz != NULL);
env->ThrowNew(exClz, strerror(err));
goto Finally;
}
ret = (result == bufferSize) ? JNI_TRUE : JNI_FALSE;
Finally:
env->ReleaseByteArrayElements(buffer, jBuffer, 0);
return result == bufferSize ? JNI_TRUE : JNI_FALSE;
return ret;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jssc/SerialNativeInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static String getLibraryVersion() {
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean writeBytes(long handle, byte[] buffer);
public native boolean writeBytes(long handle, byte[] buffer) throws IOException;

/**
* Get bytes count in buffers of port
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package jssc;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;

Expand Down Expand Up @@ -408,7 +409,11 @@ public boolean setDTR(boolean enabled) throws SerialPortException {
*/
public boolean writeBytes(byte[] buffer) throws SerialPortException {
checkPortOpened("writeBytes()");
return serialInterface.writeBytes(portHandle, buffer);
try {
return serialInterface.writeBytes(portHandle, buffer);
} catch(IOException ex) {
throw SerialPortException.wrapNativeException(ex, this, "writeBytes");
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/jssc/SerialPortException.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class SerialPortException extends Exception {
*/
final public static String TYPE_INCORRECT_SERIAL_PORT = "Incorrect serial port";

/** Exception occurred in native code */
final public static String TYPE_NATIVE_EXCEPTION = "Native exception occurred: %s";

/** Serial port object **/
private SerialPort port;
/** Method name **/
Expand Down Expand Up @@ -110,6 +113,10 @@ public SerialPortException(String portName, String methodName, String exceptionT
this.exceptionType = exceptionType;
}

public static SerialPortException wrapNativeException(Exception ex, SerialPort port, String methodName) {
return new SerialPortException(port, methodName, String.format(TYPE_NATIVE_EXCEPTION, ex.getLocalizedMessage()));
}

/**
* Getting port name during operation with which the exception was called
* Deprecated: Use <code>getPort().getName()</code> instead.
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/jssc/SerialNativeInterfaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public void testPrintVersion() {

}

@Test(expected = java.io.IOException.class)
public void reportsWriteErrorsAsIOException() throws Exception {
long fd = -1; /*bad file by intent*/
byte[] buf = new byte[]{ 0x6A, 0x73, 0x73, 0x63, 0x0A };
SerialNativeInterface testTarget = new SerialNativeInterface();
testTarget.writeBytes(fd, buf);
}

}

0 comments on commit 4a8e4ac

Please sign in to comment.