From 69934151807315f2475ce9e71e8d328b8f1eb51b Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Mon, 6 Apr 2015 16:51:18 -0400 Subject: 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 --- native/org.eclipse.cdt.native.serial/.classpath | 7 + native/org.eclipse.cdt.native.serial/.gitignore | 1 + native/org.eclipse.cdt.native.serial/.project | 28 +++ .../.settings/org.eclipse.jdt.core.prefs | 7 + .../META-INF/MANIFEST.MF | 8 + native/org.eclipse.cdt.native.serial/about.html | 24 +++ native/org.eclipse.cdt.native.serial/about.ini | 24 +++ .../org.eclipse.cdt.native.serial/about.mappings | 9 + .../org.eclipse.cdt.native.serial/about.properties | 24 +++ .../org.eclipse.cdt.native.serial/build.properties | 11 + .../cdt_logo_icon32.png | Bin 0 -> 1885 bytes .../org.eclipse.cdt.native.serial/jni/.gitignore | 1 + .../jni/posix/Makefile | 69 +++++++ .../jni/posix/serial.c | 125 +++++++++++ .../os/macosx/x86_64/libserial.jnilib | Bin 0 -> 9492 bytes native/org.eclipse.cdt.native.serial/pom.xml | 17 ++ .../src/org/eclipse/cdt/serial/BaudRate.java | 92 +++++++++ .../src/org/eclipse/cdt/serial/ByteSize.java | 68 ++++++ .../src/org/eclipse/cdt/serial/Parity.java | 55 +++++ .../src/org/eclipse/cdt/serial/SerialPort.java | 228 +++++++++++++++++++++ .../src/org/eclipse/cdt/serial/StopBits.java | 52 +++++ .../org/eclipse/cdt/serial/internal/Messages.java | 31 +++ .../cdt/serial/internal/messages.properties | 11 + 23 files changed, 892 insertions(+) create mode 100644 native/org.eclipse.cdt.native.serial/.classpath create mode 100644 native/org.eclipse.cdt.native.serial/.gitignore create mode 100644 native/org.eclipse.cdt.native.serial/.project create mode 100644 native/org.eclipse.cdt.native.serial/.settings/org.eclipse.jdt.core.prefs create mode 100644 native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF create mode 100644 native/org.eclipse.cdt.native.serial/about.html create mode 100644 native/org.eclipse.cdt.native.serial/about.ini create mode 100644 native/org.eclipse.cdt.native.serial/about.mappings create mode 100644 native/org.eclipse.cdt.native.serial/about.properties create mode 100644 native/org.eclipse.cdt.native.serial/build.properties create mode 100644 native/org.eclipse.cdt.native.serial/cdt_logo_icon32.png create mode 100644 native/org.eclipse.cdt.native.serial/jni/.gitignore create mode 100644 native/org.eclipse.cdt.native.serial/jni/posix/Makefile create mode 100644 native/org.eclipse.cdt.native.serial/jni/posix/serial.c create mode 100755 native/org.eclipse.cdt.native.serial/os/macosx/x86_64/libserial.jnilib create mode 100644 native/org.eclipse.cdt.native.serial/pom.xml create mode 100644 native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java create mode 100644 native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java create mode 100644 native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java create mode 100644 native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java create mode 100644 native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java create mode 100644 native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java create mode 100644 native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties (limited to 'native/org.eclipse.cdt.native.serial') diff --git a/native/org.eclipse.cdt.native.serial/.classpath b/native/org.eclipse.cdt.native.serial/.classpath new file mode 100644 index 00000000000..098194ca4b7 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/native/org.eclipse.cdt.native.serial/.gitignore b/native/org.eclipse.cdt.native.serial/.gitignore new file mode 100644 index 00000000000..ae3c1726048 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/native/org.eclipse.cdt.native.serial/.project b/native/org.eclipse.cdt.native.serial/.project new file mode 100644 index 00000000000..39705da7693 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/.project @@ -0,0 +1,28 @@ + + + org.eclipse.cdt.native.serial + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/native/org.eclipse.cdt.native.serial/.settings/org.eclipse.jdt.core.prefs b/native/org.eclipse.cdt.native.serial/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000000..f42de363afa --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF b/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..ea6bd38806d --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Serial Port +Bundle-SymbolicName: org.eclipse.cdt.native.serial +Bundle-Version: 1.0.0.qualifier +Bundle-Vendor: Eclipse CDT +Bundle-RequiredExecutionEnvironment: JavaSE-1.7 +Export-Package: org.eclipse.cdt.serial diff --git a/native/org.eclipse.cdt.native.serial/about.html b/native/org.eclipse.cdt.native.serial/about.html new file mode 100644 index 00000000000..d7c511887d6 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/about.html @@ -0,0 +1,24 @@ + + +About + + +

About This Content

+ +

June 22, 2007

+

License

+ +

The Eclipse Foundation makes available all content in this plug-in ("Content"). Unless otherwise +indicated below, the Content is provided to you under the terms and conditions of the +Eclipse Public License Version 1.0 ("EPL"). A copy of the EPL is available +at http://www.eclipse.org/legal/epl-v10.html. +For purposes of the EPL, "Program" will mean the Content.

+ +

If you did not receive this Content directly from the Eclipse Foundation, the Content is +being redistributed by another party ("Redistributor") and different terms and conditions may +apply to your use of any object code in the Content. Check the Redistributor's license that was +provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise +indicated below, the terms and conditions of the EPL still apply to any source code in the Content +and such source code may be obtained at http://www.eclipse.org.

+ + \ No newline at end of file diff --git a/native/org.eclipse.cdt.native.serial/about.ini b/native/org.eclipse.cdt.native.serial/about.ini new file mode 100644 index 00000000000..e07a7bb377e --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/about.ini @@ -0,0 +1,24 @@ +# about.ini +# contains information about a feature +# java.io.Properties file (ISO 8859-1 with "\" escapes) +# "%key" are externalized strings defined in about.properties +# This file does not need to be translated. + +# Property "aboutText" contains blurb for "About" dialog (translated) +aboutText=%blurb + +# Property "windowImage" contains path to window icon (16x16) +# needed for primary features only + +# Property "featureImage" contains path to feature image (32x32) +featureImage=cdt_logo_icon32.png + +# Property "aboutImage" contains path to product image (500x330 or 115x164) +# needed for primary features only + +# Property "appName" contains name of the application (translated) +# needed for primary features only + +# Property "welcomePerspective" contains the id of the perspective in which the +# welcome page is to be opened. +# optional diff --git a/native/org.eclipse.cdt.native.serial/about.mappings b/native/org.eclipse.cdt.native.serial/about.mappings new file mode 100644 index 00000000000..0824105e69d --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/about.mappings @@ -0,0 +1,9 @@ +# about.mappings +# contains fill-ins for about.properties +# java.io.Properties file (ISO 8859-1 with "\" escapes) +# This file does not need to be translated. + +# The following should contain the build version. +# e.g. "0=20020612" +# This value will be added automaticaly via the build scripts +0=@build@ \ No newline at end of file diff --git a/native/org.eclipse.cdt.native.serial/about.properties b/native/org.eclipse.cdt.native.serial/about.properties new file mode 100644 index 00000000000..a090552503d --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/about.properties @@ -0,0 +1,24 @@ +############################################################################### +# Copyright (c) 2002, 2009 Wind River 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: +# Wind River Systems - initial API and implementation +############################################################################### +# about.properties +# contains externalized strings for about.ini +# java.io.Properties file (ISO 8859-1 with "\" escapes) +# fill-ins are supplied by about.mappings +# This file should be translated. +# NOTE TO TRANSLATOR: Please do not translate the featureVersion variable. + +blurb=Eclipse CDT P2 Customizations for SDK installation\n\ +\n\ +Version: {featureVersion}\n\ +Build id: {0}\n\ +\n\ +(c) Copyright Eclipse contributors and others, 2000, 2010. All rights reserved.\n\ +Visit http://www.eclipse.org/cdt diff --git a/native/org.eclipse.cdt.native.serial/build.properties b/native/org.eclipse.cdt.native.serial/build.properties new file mode 100644 index 00000000000..6fd7e5923e8 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/build.properties @@ -0,0 +1,11 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + os/,\ + about.html,\ + about.ini,\ + about.mappings,\ + about.properties,\ + cdt_logo_icon32.png +src.includes = jni/ diff --git a/native/org.eclipse.cdt.native.serial/cdt_logo_icon32.png b/native/org.eclipse.cdt.native.serial/cdt_logo_icon32.png new file mode 100644 index 00000000000..470ca81b327 Binary files /dev/null and b/native/org.eclipse.cdt.native.serial/cdt_logo_icon32.png differ diff --git a/native/org.eclipse.cdt.native.serial/jni/.gitignore b/native/org.eclipse.cdt.native.serial/jni/.gitignore new file mode 100644 index 00000000000..5761abcfdf0 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/jni/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/native/org.eclipse.cdt.native.serial/jni/posix/Makefile b/native/org.eclipse.cdt.native.serial/jni/posix/Makefile new file mode 100644 index 00000000000..9952967f322 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/jni/posix/Makefile @@ -0,0 +1,69 @@ +#******************************************************************************* +# Copyright (c) 2002, 2009 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 +# Alex Blewitt - MacOSX with a 64-bit vm +#*******************************************************************************/ + +JAVA_HOME = $(shell echo /Library/Java/JavaVirtualMachines/jdk1.8.0_*.jdk/Contents/Home) +UNAME = $(shell uname) + +# Defaults which can be overridden. +ifeq ($(UNAME),Darwin) +OS = macosx +ARCHS = x86_64 +endif + +ARCH_X86 = x86 +ARCH_X86_64 = x86_64 + +CC=gcc +LD=libtool +CPPFLAGS = -I. -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin +CFLAGS +=-fPIC -D_REENTRANT + +ARCH_FLAG_X86 = -arch i386 +ARCH_FLAG_X86_64 = -arch x86_64 + +INSTALL_DIR_X86 = ../../os/$(OS)/$(ARCH_X86) +INSTALL_DIR_X86_64 = ../../os/$(OS)/$(ARCH_X86_64) + +LIB_NAME_FULL_SERIAL_X86 = $(INSTALL_DIR_X86)/libserial.jnilib +LIB_NAME_FULL_SERIAL_X86_64 = $(INSTALL_DIR_X86_64)/libserial.jnilib + +OBJS_SERIAL_X86 = serial_$(ARCH_X86).o +OBJS_SERIAL_X86_64 = serial_$(ARCH_X86_64).o + +OBJS_X86 = $(OBJS_SERIAL_X86) +OBJS_X86_64 = $(OBJS_SERIAL_X86_64) + +all: $(ARCHS) + +x86: $(LIB_NAME_FULL_SERIAL_X86) + +x86_64: $(LIB_NAME_FULL_SERIAL_X86_64) + +rebuild: clean all + +$(LIB_NAME_FULL_SERIAL_X86): $(OBJS_SERIAL_X86) + mkdir -p $(INSTALL_DIR_X86) + $(CC) -dynamiclib $(ARCH_FLAG_X86) -o $(LIB_NAME_FULL_SERIAL_X86) $(OBJS_SERIAL_X86) -lc -framework JavaVM + +$(LIB_NAME_FULL_SERIAL_X86_64): $(OBJS_SERIAL_X86_64) + mkdir -p $(INSTALL_DIR_X86_64) + $(CC) -dynamiclib $(ARCH_FLAG_X86_64) -o $(LIB_NAME_FULL_SERIAL_X86_64) $(OBJS_SERIAL_X86_64) -lc -framework JavaVM + +serial_$(ARCH_X86).o: serial.c + $(CC) $(CFLAGS) $(ARCH_FLAG_X86) $(CPPFLAGS) -c -o $@ serial.c + +serial_$(ARCH_X86_64).o: serial.c + $(CC) $(CFLAGS) $(ARCH_FLAG_X86_64) $(CPPFLAGS) -c -o $@ serial.c + +clean : + $(RM) $(OBJS_X86) + $(RM) $(OBJS_X86_64) 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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); + } diff --git a/native/org.eclipse.cdt.native.serial/os/macosx/x86_64/libserial.jnilib b/native/org.eclipse.cdt.native.serial/os/macosx/x86_64/libserial.jnilib new file mode 100755 index 00000000000..bf96f91430c Binary files /dev/null and b/native/org.eclipse.cdt.native.serial/os/macosx/x86_64/libserial.jnilib differ diff --git a/native/org.eclipse.cdt.native.serial/pom.xml b/native/org.eclipse.cdt.native.serial/pom.xml new file mode 100644 index 00000000000..71d47f62c60 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + + org.eclipse.cdt + cdt-parent + 8.6.0-SNAPSHOT + ../../pom.xml + + + 1.0.0-SNAPSHOT + org.eclipse.cdt.native.serial + eclipse-plugin + diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java new file mode 100644 index 00000000000..70965fdeed4 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.cdt.serial; + +/** + * @since 1.0 + */ +public enum BaudRate { + + B110(110), + B300(300), + B600(600), + B1200(1200), + B2400(2400), + B4800(4800), + B9600(9600), + B14400(14400), + B19200(19200), + B38400(38400), + B57600(57600), + B115200(115200); + + private final int rate; + + private BaudRate(int rate) { + this.rate = rate; + } + + public int getRate() { + return rate; + } + + private static final String[] strings = { + "110", //$NON-NLS-1$ + "300", //$NON-NLS-1$ + "600", //$NON-NLS-1$ + "1200", //$NON-NLS-1$ + "2400", //$NON-NLS-1$ + "4800", //$NON-NLS-1$ + "9600", //$NON-NLS-1$ + "14400", //$NON-NLS-1$ + "19200", //$NON-NLS-1$ + "38400", //$NON-NLS-1$ + "57600", //$NON-NLS-1$ + "115200" //$NON-NLS-1$ + }; + + public static String[] getStrings() { + return strings; + } + + private static final BaudRate[] rates = { + B110, + B300, + B600, + B1200, + B2400, + B4800, + B9600, + B14400, + B19200, + B38400, + B57600, + B115200 + }; + + public static BaudRate fromStringIndex(int rate) { + return rates[rate]; + } + + public static int getStringIndex(BaudRate rate) { + for (int i = 0; i < rates.length; ++i) { + if (rate.equals(rates[i])) { + return i; + } + } + return getStringIndex(getDefault()); + } + + public static BaudRate getDefault() { + return B9600; + } + +} diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java new file mode 100644 index 00000000000..ad37c63afc6 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.cdt.serial; + +/** + * @since 5.8 + */ +public enum ByteSize { + + B5(5), + B6(6), + B7(7), + B8(8); + + private final int size; + + private ByteSize(int size) { + this.size = size; + } + + public int getSize() { + return size; + } + + private static final String[] strings = { + "5", //$NON-NLS-1$ + "6", //$NON-NLS-1$ + "7", //$NON-NLS-1$ + "8" //$NON-NLS-1$ + }; + + public static String[] getStrings() { + return strings; + } + + private static final ByteSize[] sizes = { + B5, + B6, + B7, + B8 + }; + + public static ByteSize fromStringIndex(int size) { + return sizes[size]; + } + + public static int getStringIndex(ByteSize size) { + for (int i = 0; i < sizes.length; ++i) { + if (size.equals(sizes[i])) { + return i; + } + } + return getStringIndex(getDefault()); + } + + public static ByteSize getDefault() { + return B8; + } + +} diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java new file mode 100644 index 00000000000..c93fa815d16 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.cdt.serial; + +/** + * @since 5.8 + */ +public enum Parity { + + None, + Even, + Odd; + + private static final String[] strings = { + "None", //$NON-NLS-1$ + "Even", //$NON-NLS-1$ + "Odd" //$NON-NLS-1$ + }; + + public static String[] getStrings() { + return strings; + } + + private static final Parity[] parities = { + None, + Even, + Odd + }; + + public static Parity fromStringIndex(int index) { + return parities[index]; + } + + public static int getStringIndex(Parity parity) { + for (int i = 0; i < parities.length; ++i) { + if (parity.equals(parities[i])) { + return i; + } + } + return getStringIndex(getDefault()); + } + + public static Parity getDefault() { + return None; + } + +} diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java new file mode 100644 index 00000000000..62c7a9bdabb --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java @@ -0,0 +1,228 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.cdt.serial; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.regex.Pattern; + +import org.eclipse.cdt.serial.internal.Messages; + +/** + * @since 5.8 + */ +public class SerialPort { + + private final String portName; + private boolean isOpen; + private BaudRate baudRate = BaudRate.B115200; + private ByteSize byteSize = ByteSize.B8; + private Parity parity = Parity.None; + private StopBits stopBits = StopBits.S1; + private long handle; + + private static final String SERIAL_KEY = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; //$NON-NLS-1$ + private static final String PORT_OPEN = Messages.getString("SerialPort.PortIsOpen"); //$NON-NLS-1$ + + static { + try { + System.loadLibrary("serial"); //$NON-NLS-1$ + } catch (UnsatisfiedLinkError e) { + e.printStackTrace(); + } + } + + private InputStream inputStream = new InputStream() { + @Override + public int read() throws IOException { + if (isOpen()) { + return read0(handle); + } else { + return -1; + } + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (isOpen()) { + return read1(handle, b, off, len); + } else { + return -1; + } + } + + @Override + public void close() throws IOException { + SerialPort.this.close(); + } + }; + + private OutputStream outputStream = new OutputStream() { + @Override + public void write(int b) throws IOException { + if (isOpen()) { + write0(handle, b); + } + } + + @Override + public void close() throws IOException { + SerialPort.this.close(); + } + }; + + /** + * Create a serial port that connect to the given serial device. + * + * @param portName name for the serial device. + */ + public SerialPort(String portName) { + this.portName = portName; + } + + /** + * List the available serial ports. + * + * @return serial ports + */ + public static String[] list() { + if (System.getProperty("os.name").equals("Mac OS X")) { //$NON-NLS-1$//$NON-NLS-2$ + File dev = new File("/dev"); //$NON-NLS-1$ + final Pattern pattern = Pattern.compile("tty\\.(usbserial|usbmodem).*"); //$NON-NLS-1$ + File[] files = dev.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return pattern.matcher(name).matches(); + } + }); + + if (files == null) { + return new String[0]; + } + String[] names = new String[files.length]; + for (int i = 0; i < files.length; i++) { + names[i] = files[i].getAbsolutePath(); + } + return names; + } else if (System.getProperty("os.name").equals("Windows NT")) { //$NON-NLS-1$//$NON-NLS-2$ +// WindowsRegistry reg = WindowsRegistry.getRegistry(); +// if (reg != null) { +// List ports = new ArrayList<>(); +// int i = 0; +// String name = reg.getLocalMachineValueName(SERIAL_KEY, i); +// while (name != null) { +// String value = reg.getLocalMachineValue(SERIAL_KEY, name); +// ports.add(value); +// i++; +// name = reg.getLocalMachineValueName(SERIAL_KEY, i); +// } +// return ports.toArray(new String[ports.size()]); +// } else { +// return new String[0]; +// } + return new String[0]; + } else { + return new String[0]; + } + } + + private native long open0(String portName, int baudRate, int byteSize, int parity, int stopBits) throws IOException; + + private native void close0(long handle) throws IOException; + + private native int read0(long handle) throws IOException; + + private native int read1(long handle, byte[] b, int off, int len); + + private native void write0(long handle, int b) throws IOException; + + /** + * Return the name for this serial port. + * + * @return serial port name + */ + public String getPortName() { + return portName; + } + + public InputStream getInputStream() { + return inputStream; + } + + public OutputStream getOutputStream() { + return outputStream; + } + + public void open() throws IOException { + handle = open0(portName, baudRate.getRate(), byteSize.getSize(), parity.ordinal(), stopBits.ordinal()); + isOpen = true; + } + + public synchronized void close() throws IOException { + if (isOpen) { + close0(handle); + isOpen = false; + handle = 0; + } + } + + public boolean isOpen() { + return isOpen; + } + + public void setBaudRate(BaudRate rate) throws IOException { + if (isOpen) { + throw new IOException(PORT_OPEN); + } + this.baudRate = rate; + } + + public BaudRate getBaudRate() { + return baudRate; + } + + public void setByteSize(ByteSize size) throws IOException { + if (isOpen) { + throw new IOException(PORT_OPEN); + } + this.byteSize = size; + } + + public ByteSize getByteSize() { + return byteSize; + } + + public void setParity(Parity parity) throws IOException { + if (isOpen) { + throw new IOException(PORT_OPEN); + } + this.parity = parity; + } + + public Parity getParity() { + return parity; + } + + public void setStopBits(StopBits stopBits) throws IOException { + if (isOpen) { + throw new IOException(PORT_OPEN); + } + this.stopBits = stopBits; + } + + public StopBits getStopBits() { + return stopBits; + } + +} diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java new file mode 100644 index 00000000000..92752ce8e19 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.cdt.serial; + +/** + * @since 5.8 + */ +public enum StopBits { + + S1, + S2; + + private static final String[] strings = { + "1", //$NON-NLS-1$ + "2" //$NON-NLS-1$ + }; + + public static String[] getStrings() { + return strings; + } + + private static final StopBits[] stopBits = { + S1, + S2 + }; + + public static StopBits fromStringIndex(int index) { + return stopBits[index]; + } + + public static int getStringIndex(StopBits sb) { + for (int i = 0; i < stopBits.length; ++i) { + if (sb.equals(stopBits[i])) { + return i; + } + } + return getStringIndex(getDefault()); + } + + public static StopBits getDefault() { + return S1; + } + +} diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java new file mode 100644 index 00000000000..45a3d53817a --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.cdt.serial.internal; + +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +public class Messages { + private static final String BUNDLE_NAME = "org.eclipse.cdt.serial.internal.messages"; //$NON-NLS-1$ + + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); + + private Messages() { + } + + public static String getString(String key) { + try { + return RESOURCE_BUNDLE.getString(key); + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } +} diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties new file mode 100644 index 00000000000..9a39f7958ce --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties @@ -0,0 +1,11 @@ +################################################################################## +# 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 +################################################################################## +SerialPort.PortIsOpen=Port is open -- cgit v1.2.3