aboutsummaryrefslogtreecommitdiff
path: root/src/main/native
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/native')
-rw-r--r--src/main/native/flow.c33
-rw-r--r--src/main/native/flow.h6
2 files changed, 25 insertions, 14 deletions
diff --git a/src/main/native/flow.c b/src/main/native/flow.c
index 4353e80..43dd750 100644
--- a/src/main/native/flow.c
+++ b/src/main/native/flow.c
@@ -143,19 +143,23 @@ int serial_open(const char* port_name, int baud, struct serial_config** serial)
return 0;
}
-void serial_close(struct serial_config* serial) {
+int serial_close(struct serial_config* serial) {
if (close(serial->pipe_write_fd) < 0) {
DEBUG(perror("close write end of pipe"););
+ return E_IO;
}
if (close(serial->pipe_read_fd) < 0) {
DEBUG(perror("close read end of pipe"););
+ return E_IO;
}
if (flock(serial->port_fd, LOCK_UN) < 0){
DEBUG(perror("release lock on port"););
+ return E_IO;
}
if (close(serial->port_fd) < 0) {
DEBUG(perror("close port"););
+ return E_IO;
}
free(serial);
@@ -194,6 +198,15 @@ int serial_read(struct serial_config* serial, unsigned char* buffer, size_t size
}
}
+int serial_write(struct serial_config* serial, unsigned char* data, size_t size) {
+ int r = write(serial->port_fd, data, size);
+ if (r < 0) {
+ DEBUG(perror("write"););
+ return E_IO;
+ }
+ return r;
+}
+
int serial_interrupt(struct serial_config* serial) {
int data = 0xffffffff;
@@ -206,16 +219,6 @@ int serial_interrupt(struct serial_config* serial) {
return 0;
}
-int serial_write(struct serial_config* serial, unsigned char* data, size_t size) {
- int r = write(serial->port_fd, data, size);
- if (r < 0) {
- DEBUG(perror("write"););
- return E_IO;
- }
- return r;
-}
-
-
// JNI bindings
// ============
@@ -242,7 +245,7 @@ JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_low_NativeSerial_open
return r;
}
-JNIEXPORT void JNICALL Java_com_github_jodersky_flow_low_NativeSerial_close
+JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_low_NativeSerial_close
(JNIEnv * env, jclass clazz, jlong serial)
{
serial_close(j2s(serial));
@@ -276,6 +279,12 @@ JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_low_NativeSerial_write
return r;
}
+JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_low_NativeSerial_interrupt
+ (JNIEnv * env, jclass clazz, jlong serial)
+{
+ return serial_interrupt(j2s(serial));
+}
+
JNIEXPORT void JNICALL Java_com_github_jodersky_flow_low_NativeSerial_debug
(JNIEnv *env, jclass clazz, jboolean value)
{
diff --git a/src/main/native/flow.h b/src/main/native/flow.h
index 97eae83..d1823c1 100644
--- a/src/main/native/flow.h
+++ b/src/main/native/flow.h
@@ -38,8 +38,10 @@ int serial_open(const char* port_name, int baud, struct serial_config** serial);
* this function, the 'serial' pointer will become invalid, make sure you only call it once. This function is NOT
* thread safe, make sure no read or write is in prgress when this function is called (the reason is that per
* close manual page, close should not be called on a file descriptor that is in use by another thread).
- * @param serial pointer to serial configuration that is to be closed (and freed) */
-void serial_close(struct serial_config* serial);
+ * @param serial pointer to serial configuration that is to be closed (and freed)
+ * @return 0 on success
+ * @return E_IO on error */
+int serial_close(struct serial_config* serial);
/**Starts a blocking read from a previously opened serial port. The read is blocking, however it may be
* interrupted by calling 'serial_interrupt' on the given serial port.