Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDoug Schaefer2015-04-06 20:51:18 +0000
committerGerrit Code Review @ Eclipse.org2015-04-06 23:39:30 +0000
commit69934151807315f2475ce9e71e8d328b8f1eb51b (patch)
tree356a281986cfc1bf88888e96d72945d53e95860a /native/org.eclipse.cdt.native.serial/jni/posix/serial.c
parent711ec8df95456d971ee3c6e6c0eeff586cdc6ba1 (diff)
downloadorg.eclipse.cdt-69934151807315f2475ce9e71e8d328b8f1eb51b.tar.gz
org.eclipse.cdt-69934151807315f2475ce9e71e8d328b8f1eb51b.tar.xz
org.eclipse.cdt-69934151807315f2475ce9e71e8d328b8f1eb51b.zip
Bug 459971 - Move serial port to it's own plug-in. Starting with Mac.
There are a number of requests to support serial port independent of CDT and independent of Eclipse. Putting the serial port into it's own plug-in so it's jar can be loaded into pure Java apps. Change-Id: I9b35d9bedeee0a0b1c16ad1c884830894320a726
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.c125
1 files changed, 125 insertions, 0 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
new file mode 100644
index 00000000000..bf1e51da279
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/jni/posix/serial.c
@@ -0,0 +1,125 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * QNX Software Systems - initial API and implementation
+ *******************************************************************************/
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <jni.h>
+
+#define FUNC(x) Java_org_eclipse_cdt_serial_SerialPort_ ## x
+
+JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName, jint baudRate, jint byteSize, jint parity, jint stopBits)
+{
+ const char * cportName = (*env)->GetStringUTFChars(env, portName, NULL);
+ int fd = open(cportName, O_RDWR | O_NOCTTY | O_NDELAY);
+ if (fd < 0) {
+ return fd;
+ }
+
+ // Turn off all flags
+ fcntl(fd, F_SETFL, 0);
+
+ struct termios options;
+ tcgetattr(fd, &options);
+ options.c_cflag |= (CLOCAL | CREAD);
+
+ // Set baud rate
+ cfsetispeed(&options, baudRate);
+ cfsetospeed(&options, baudRate);
+
+ // set data size
+ options.c_cflag &= ~CSIZE;
+ switch (byteSize) {
+ case 5:
+ options.c_cflag |= CS5;
+ break;
+ case 6:
+ options.c_cflag |= CS6;
+ break;
+ case 7:
+ options.c_cflag |= CS7;
+ break;
+ case 8:
+ options.c_cflag |= CS8;
+ break;
+
+ }
+
+ // set parity
+ switch (parity) {
+ case 0: // None
+ options.c_cflag &= ~PARENB;
+ break;
+ case 1: // Even
+ options.c_cflag |= PARENB;
+ options.c_cflag &= ~PARODD;
+ break;
+ case 2: // Odd
+ options.c_cflag |= (PARENB | PARODD);
+ break;
+ }
+
+ switch (stopBits) {
+ case 0: // 1
+ options.c_cflag &= ~CSTOPB;
+ break;
+ case 1: // 2
+ options.c_cflag |= CSTOPB;
+ break;
+ }
+
+ // raw input
+ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
+
+ // ignore parity
+ options.c_iflag |= IGNPAR;
+
+ options.c_cc[VMIN] = 0; // min chars to read
+ options.c_cc[VTIME] = 10; // 10ths second timeout
+
+ tcflush(fd, TCIFLUSH);
+ tcsetattr(fd, TCSANOW, &options);
+
+ return fd;
+}
+
+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) {
+ jbyte buff[256];
+ int n = size < sizeof(buff) ? size : sizeof(buff);
+ n = read(handle, buff, n);
+ if (n > 0) {
+ (*env)->SetByteArrayRegion(env, bytes, offset, n, buff);
+ }
+ return n;
+}
+
+JNIEXPORT void JNICALL FUNC(write0)(JNIEnv *env, jobject jobj, jlong handle, jint b)
+ {
+ char buff = b;
+ write(handle, &buff, 1);
+ }

Back to the top