diff options
Diffstat (limited to 'native/org.eclipse.cdt.native.serial/jni/posix/serial.c')
-rw-r--r-- | native/org.eclipse.cdt.native.serial/jni/posix/serial.c | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/native/org.eclipse.cdt.native.serial/jni/posix/serial.c b/native/org.eclipse.cdt.native.serial/jni/posix/serial.c index bf1e51da279..3bc71e493ef 100644 --- a/native/org.eclipse.cdt.native.serial/jni/posix/serial.c +++ b/native/org.eclipse.cdt.native.serial/jni/posix/serial.c @@ -8,9 +8,11 @@ * Contributors: * QNX Software Systems - initial API and implementation *******************************************************************************/ +#ifdef __APPLE__ #include <sys/types.h> #include <sys/stat.h> #include <sys/uio.h> +#endif #include <unistd.h> #include <fcntl.h> #include <termios.h> @@ -26,6 +28,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName, const char * cportName = (*env)->GetStringUTFChars(env, portName, NULL); int fd = open(cportName, O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { + perror(cportName); return fd; } @@ -88,7 +91,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName, options.c_iflag |= IGNPAR; options.c_cc[VMIN] = 0; // min chars to read - options.c_cc[VTIME] = 10; // 10ths second timeout + options.c_cc[VTIME] = 2; // 10ths second timeout tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &options); @@ -97,18 +100,12 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName, } JNIEXPORT void JNICALL FUNC(close0)(JNIEnv *env, jobject jobj, jlong handle) - { +{ close(handle); - } - -JNIEXPORT jint JNICALL FUNC(read0)(JNIEnv *env, jobject jobj, jlong handle) - { - char buff; - int res = read(handle, &buff, 1); - return res < 0 ? -1 : buff; - } +} -JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong handle, jbyteArray bytes, jint offset, jint size) { +JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong handle, jbyteArray bytes, jint offset, jint size) +{ jbyte buff[256]; int n = size < sizeof(buff) ? size : sizeof(buff); n = read(handle, buff, n); @@ -119,7 +116,22 @@ JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong handle, jby } JNIEXPORT void JNICALL FUNC(write0)(JNIEnv *env, jobject jobj, jlong handle, jint b) - { +{ char buff = b; write(handle, &buff, 1); +} + +JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong handle, jbyteArray bytes, jint offset, jint size) +{ + while (size > 0) { + jbyte buff[256]; + int n = size < sizeof(buff) ? size : sizeof(buff); + (*env)->GetByteArrayRegion(env, bytes, offset, n, buff); + n = write(handle, buff, n); + if (n < 0) { + return; } + size -= n; + offset += n; + } +} |