diff options
Diffstat (limited to 'flow/src/main/native/posix')
-rw-r--r-- | flow/src/main/native/posix/Makefile | 27 | ||||
-rw-r--r-- | flow/src/main/native/posix/flow.c | 247 | ||||
-rw-r--r-- | flow/src/main/native/posix/flow_jni.c | 169 |
3 files changed, 0 insertions, 443 deletions
diff --git a/flow/src/main/native/posix/Makefile b/flow/src/main/native/posix/Makefile deleted file mode 100644 index 22338e5..0000000 --- a/flow/src/main/native/posix/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -# Build native binaries for flow. -# This makefile is intended for linux-based operating systems. - -TARGET?=libflow.so -MAJOR_VERSION=2 - -CC=$(CROSS_COMPILE)gcc -CFLAGS= -O2 -fPIC -Wall -LD=$(CROSS_COMPILE)ld -LDFLAGS=-shared -Wl,-soname,$(TARGET).$(MAJOR_VERSION) -INCLUDES?=../include/ /usr/lib/jvm/java-7-oracle/include/ /usr/lib/jvm/java-7-oracle/include/linux/ - -OBJECTS=flow.o flow_jni.o - -all: $(TARGET) - -$(TARGET): $(OBJECTS) - $(CC) $(LDFLAGS) -o $@ $^ - -%.o: %.c - $(CC) $(CFLAGS) $(addprefix -I, $(INCLUDES)) -o $@ -c $< - -clean: - rm -f *.o - rm -f *.so - -.PHONY: clean diff --git a/flow/src/main/native/posix/flow.c b/flow/src/main/native/posix/flow.c deleted file mode 100644 index 7f7d54b..0000000 --- a/flow/src/main/native/posix/flow.c +++ /dev/null @@ -1,247 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <unistd.h> -#include <errno.h> -#include <termios.h> -#include <fcntl.h> -#include <sys/file.h> -#include "flow.h" - - -#define DATA_CANCEL 0xffffffff - -static bool debug = false; -#define DEBUG(f) if (debug) {f} - -void serial_debug(bool value) { - debug = value; -} - -//contains file descriptors used in managing a serial port -struct serial_config { - int port_fd; // file descriptor of serial port - - /* a pipe is used to abort a serial read by writing something into the - * write end of the pipe */ - int pipe_read_fd; // file descriptor, read end of pipe - int pipe_write_fd; // file descriptor, write end of pipe -}; - -int serial_open( - const char* const port_name, - int baud, - int char_size, - bool two_stop_bits, - int parity, - struct serial_config** serial) { - - int fd = open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK); - - if (fd < 0) { - int en = errno; - DEBUG(perror("error obtaining port file descriptor");); - if (en == EACCES) return E_ACCESS_DENIED; - if (en == ENOENT) return E_NO_PORT; - return E_IO; - } - - if (flock(fd, LOCK_EX | LOCK_NB) < 0) { - DEBUG(perror("error acquiring lock on port");); - close(fd); - return E_BUSY; - } - - /* configure new port settings */ - struct termios newtio; - - /* following calls correspond to makeraw() */ - newtio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); - newtio.c_oflag &= ~OPOST; - newtio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); - newtio.c_cflag &= ~(CSIZE | PARENB); - - /* set speed */ - speed_t bd; - switch (baud) { - case 50: bd = B50; break; - case 75: bd = B75; break; - case 110: bd = B110; break; - case 134: bd = B134; break; - case 150: bd = B150; break; - case 200: bd = B200; break; - case 300: bd = B300; break; - case 600: bd = B600; break; - case 1200: bd = B1200; break; - case 1800: bd = B1800; break; - case 2400: bd = B2400; break; - case 4800: bd = B4800; break; - case 9600: bd = B9600; break; - case 19200: bd = B19200; break; - case 38400: bd = B38400; break; - case 57600: bd = B57600; break; - case 115200: bd = B115200; break; - case 230400: bd = B230400; break; - default: - close(fd); - DEBUG(fprintf(stderr, "invalid baud rate %d\n", baud);); - return E_INVALID_SETTINGS; - } - - if (cfsetspeed(&newtio, bd) < 0) { - DEBUG(perror("error setting baud rate");); - close(fd); - return E_IO; - } - - /* set char size*/ - switch (char_size) { - case 5: newtio.c_cflag |= CS5; break; - case 6: newtio.c_cflag |= CS6; break; - case 7: newtio.c_cflag |= CS7; break; - case 8: newtio.c_cflag |= CS8; break; - default: - close(fd); - DEBUG(fprintf(stderr, "invalid character size %d\n", char_size);); - return E_INVALID_SETTINGS; - } - - /* use two stop bits */ - if (two_stop_bits){ - newtio.c_cflag |= CSTOPB; - } - - /* set parity */ - switch (parity) { - case PARITY_NONE: break; - case PARITY_ODD: newtio.c_cflag |= (PARENB | PARODD); break; - case PARITY_EVEN: newtio.c_cflag |= PARENB; break; - default: - close(fd); - DEBUG(fprintf(stderr, "invalid parity %d\n", parity);); - return E_INVALID_SETTINGS; - } - - if (tcflush(fd, TCIOFLUSH) < 0) { - DEBUG(perror("error flushing serial settings");); - close(fd); - return E_IO; - } - - if (tcsetattr(fd, TCSANOW, &newtio) < 0) { - DEBUG(perror("error applying serial settings");); - close(fd); - return E_IO; - } - - int pipe_fd[2]; - if (pipe(pipe_fd) < 0) { - DEBUG(perror("error opening pipe");); - close(fd); - return E_IO; - } - - if (fcntl(pipe_fd[0], F_SETFL, O_NONBLOCK) < 0 || fcntl(pipe_fd[1], F_SETFL, O_NONBLOCK) < 0) { - DEBUG(perror("error setting pipe to non-blocking");); - close(fd); - close(pipe_fd[0]); - close(pipe_fd[1]); - return E_IO; - } - - struct serial_config* s = malloc(sizeof(s)); - if (s == NULL) { - DEBUG(perror("error allocating memory for serial configuration");); - close(fd); - close(pipe_fd[0]); - close(pipe_fd[1]); - return E_IO; - } - - s->port_fd = fd; - s->pipe_read_fd = pipe_fd[0]; - s->pipe_write_fd = pipe_fd[1]; - (*serial) = s; - - return 0; -} - - -int serial_close(struct serial_config* const serial) { - if (close(serial->pipe_write_fd) < 0) { - DEBUG(perror("error closing write end of pipe");); - return E_IO; - } - if (close(serial->pipe_read_fd) < 0) { - DEBUG(perror("error closing read end of pipe");); - return E_IO; - } - - if (flock(serial->port_fd, LOCK_UN) < 0){ - DEBUG(perror("error releasing lock on port");); - return E_IO; - } - if (close(serial->port_fd) < 0) { - DEBUG(perror("error closing port");); - return E_IO; - } - - free(serial); - return 0; -} - -int serial_read(struct serial_config* const serial, char* const buffer, size_t size) { - int port = serial->port_fd; - int pipe = serial->pipe_read_fd; - - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(port, &rfds); - FD_SET(pipe, &rfds); - - int nfds = pipe + 1; - if (pipe < port) nfds = port + 1; - - int n = select(nfds, &rfds, NULL, NULL, NULL); - if (n < 0) { - DEBUG(perror("error trying to call select on port and pipe");); - return E_IO; - } - - if (FD_ISSET(pipe, &rfds)) { - return E_INTERRUPT; - } else if (FD_ISSET(port, &rfds)) { - int r = read(port, buffer, size); - - //treat 0 bytes read as an error to avoid problems on disconnect - //anyway, after a poll there should be more than 0 bytes available to read - if (r <= 0) { - DEBUG(perror("error data not available after select");); - return E_IO; - } - return r; - } else { - fprintf(stderr, "select returned unknown read sets\n"); - return E_IO; - } -} - -int serial_cancel_read(struct serial_config* const serial) { - int data = DATA_CANCEL; - - //write to pipe to wake up any blocked read thread (self-pipe trick) - if (write(serial->pipe_write_fd, &data, 1) < 0) { - DEBUG(perror("error writing to pipe during read cancel");); - return E_IO; - } - - return 0; -} - -int serial_write(struct serial_config* const serial, char* const data, size_t size) { - int r = write(serial->port_fd, data, size); - if (r < 0) { - DEBUG(perror("error writing to port");); - return E_IO; - } - return r; -}
\ No newline at end of file diff --git a/flow/src/main/native/posix/flow_jni.c b/flow/src/main/native/posix/flow_jni.c deleted file mode 100644 index 8ec2aed..0000000 --- a/flow/src/main/native/posix/flow_jni.c +++ /dev/null @@ -1,169 +0,0 @@ -#include "flow.h" -#include "com_github_jodersky_flow_internal_NativeSerial.h" - - -static inline void throwException(JNIEnv* env, const char* const exception, const char * const message) { - (*env)->ThrowNew(env, (*env)->FindClass(env, exception), message); -} - -static inline void check(JNIEnv* env, int id) { - switch (id) { - case E_IO: throwException(env, "java/io/IOException", ""); break; - case E_BUSY: throwException(env, "com/github/jodersky/flow/PortInUseException", ""); break; - case E_ACCESS_DENIED: throwException(env, "com/github/jodersky/flow/AccessDeniedException", ""); break; - case E_INVALID_SETTINGS: throwException(env, "com/github/jodersky/flow/InvalidSettingsException", ""); break; - case E_INTERRUPT: throwException(env, "com/github/jodersky/flow/PortInterruptedException", ""); break; - case E_NO_PORT: throwException(env, "com/github/jodersky/flow/NoSuchPortException", ""); break; - default: return; - } -} - - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: open - * Signature: (Ljava/lang/String;IIZI)J - */ -JNIEXPORT jlong JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_open - (JNIEnv *env, jclass clazz, jstring port_name, jint baud, jint char_size, jboolean two_stop_bits, jint parity) { - - const char *dev = (*env)->GetStringUTFChars(env, port_name, 0); - struct serial_config* config; - int r = serial_open(dev, baud, char_size, two_stop_bits, parity, &config); - (*env)->ReleaseStringUTFChars(env, port_name, dev); - - - if (r < 0) { - check(env, r); - return 0; - } - - long jpointer = (long) config; - return jpointer; - -} - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: readDirect - * Signature: (JLjava/nio/ByteBuffer;)I - */ -JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_readDirect - (JNIEnv *env, jclass clazz, jlong config, jobject buffer) { - - char* local_buffer = (char*) (*env)->GetDirectBufferAddress(env, buffer); - if (local_buffer == NULL) { - throwException(env, "java/lang/IllegalArgumentException", "ByteBuffer not direct"); - return -1; - } - jlong size = (*env)->GetDirectBufferCapacity(env, buffer); - - int r = serial_read((struct serial_config*) config, local_buffer, (size_t) size); - if (r < 0) { - check(env, r); - return -1; - } - return r; - -} - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: read - * Signature: (J[B)I - */ -JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_read - (JNIEnv *env, jclass clazz, jlong config, jbyteArray buffer) { - - jsize size = (*env)->GetArrayLength(env, buffer); - char local_buffer[size]; - int r = serial_read((struct serial_config*) config, local_buffer, size); - if (r < 0) { - check(env, r); - return -1; - } - - (*env)->SetByteArrayRegion(env, buffer, 0, r, (signed char *) local_buffer); - return r; - -} - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: cancelRead - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_cancelRead - (JNIEnv *env, jclass clazz, jlong config) { - - int r = serial_cancel_read((struct serial_config*) config); - if (r < 0) { - check(env, r); - } - -} - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: writeDirect - * Signature: (JLjava/nio/ByteBuffer;I)I - */ -JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_writeDirect - (JNIEnv *env, jclass clazz, jlong config, jobject buffer, jint size) { - - char* local_buffer = (char *) (*env)->GetDirectBufferAddress(env, buffer); - if (local_buffer == NULL) { - throwException(env, "java/lang/IllegalArgumentException", "ByteBuffer not direct"); - return -1; - } - - int r = serial_write((struct serial_config*) config, local_buffer, (size_t) size); - if (r < 0) { - check(env, r); - return -1; - } - return r; - -} - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: write - * Signature: (J[BI)I - */ -JNIEXPORT jint JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_write - (JNIEnv *env, jclass clazz, jlong config, jbyteArray buffer, jint size) { - - char* local_buffer = (char*) (*env)->GetByteArrayElements(env, buffer, NULL); - int r = serial_write((struct serial_config*) config, local_buffer, size); - (*env)->ReleaseByteArrayElements(env, buffer, (signed char*) local_buffer, JNI_ABORT); - if (r < 0) { - check(env, r); - return -1; - } - return r; - -} - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: close - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_close - (JNIEnv *env, jclass clazz, jlong config) { - int r = serial_close((struct serial_config*) config); - if (r < 0) { - check(env, r); - } -} - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: debug - * Signature: (Z)V - */ -JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_debug - (JNIEnv *env, jclass clazz, jboolean value) { - serial_debug((bool) value); -}
\ No newline at end of file |