From e44726097ce0c3ae808136206ace4e789c2ab4d8 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Thu, 21 Jan 2016 15:46:19 -0500 Subject: Bug 486307 - Throw exception when serial connection fails. Build windows using mingw cross on Ubuntu. Clean up Makefiles and remove unused Windows code. Change-Id: Ic2d36ddbef7e4eecfcee12a496b923326c8a51c6 Signed-off-by: Jonathan Williams --- .../org.eclipse.cdt.native.serial/jni/.gitignore | 1 + native/org.eclipse.cdt.native.serial/jni/Makefile | 58 +++ .../jni/posix/.gitignore | 1 - .../jni/posix/Makefile | 82 ---- .../jni/posix/serial.c | 137 ------- native/org.eclipse.cdt.native.serial/jni/serial.c | 432 +++++++++++++++++++++ .../jni/win32/serial/.gitignore | 6 - .../jni/win32/serial/dllmain.cpp | 19 - .../jni/win32/serial/serial.cpp | 242 ------------ .../jni/win32/serial/serial.sln | 28 -- .../jni/win32/serial/serial.vcxproj | 176 --------- .../jni/win32/serial/serial.vcxproj.filters | 39 -- .../jni/win32/serial/stdafx.cpp | 8 - .../jni/win32/serial/stdafx.h | 17 - .../jni/win32/serial/targetver.h | 8 - .../os/linux/x86/libserial.so | Bin 7589 -> 11915 bytes .../os/linux/x86_64/libserial.so | Bin 12906 -> 13168 bytes .../os/macosx/x86_64/libserial.jnilib | Bin 9492 -> 13620 bytes .../os/win32/x86/serial.dll | Bin 77312 -> 86995 bytes .../os/win32/x86_64/serial.dll | Bin 90112 -> 113978 bytes 20 files changed, 491 insertions(+), 763 deletions(-) create mode 100644 native/org.eclipse.cdt.native.serial/jni/.gitignore create mode 100644 native/org.eclipse.cdt.native.serial/jni/Makefile delete mode 100644 native/org.eclipse.cdt.native.serial/jni/posix/.gitignore delete mode 100644 native/org.eclipse.cdt.native.serial/jni/posix/Makefile delete mode 100644 native/org.eclipse.cdt.native.serial/jni/posix/serial.c create mode 100644 native/org.eclipse.cdt.native.serial/jni/serial.c delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/.gitignore delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/dllmain.cpp delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.cpp delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.sln delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj.filters delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.cpp delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.h delete mode 100644 native/org.eclipse.cdt.native.serial/jni/win32/serial/targetver.h mode change 100644 => 100755 native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll mode change 100644 => 100755 native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll (limited to 'native') 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/Makefile b/native/org.eclipse.cdt.native.serial/jni/Makefile new file mode 100644 index 00000000000..50693042ace --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/jni/Makefile @@ -0,0 +1,58 @@ +#******************************************************************************* +# 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 +#*******************************************************************************/ + +ifeq ($(JAVA_HOME),) +$(error Please define JAVA_HOME) +endif + +OS_DIR = ../os + +UNAME = $(shell uname) +ifeq ($(UNAME),Linux) +LIBS = \ + $(OS_DIR)/win32/x86/serial.dll \ + $(OS_DIR)/win32/x86_64/serial.dll \ + $(OS_DIR)/linux/x86/libserial.so \ + $(OS_DIR)/linux/x86_64/libserial.so +else +ifeq ($(UNAME),Darwin) +LIBS = \ + $(OS_DIR)/macosx/x86_64/libserial.jnilib +endif +endif + +all: $(LIBS) + +clean : + rm $(LIBS) + +rebuild: clean all + +$(OS_DIR)/win32/x86/serial.dll: serial.c + mkdir -p $(dir $@) + i686-w64-mingw32-gcc -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/win32 -shared -o $@ serial.c + +$(OS_DIR)/win32/x86_64/serial.dll: serial.c + mkdir -p $(dir $@) + x86_64-w64-mingw32-gcc -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/win32 -shared -o $@ serial.c + +$(OS_DIR)/linux/x86/libserial.so: serial.c + mkdir -p $(dir $@) + gcc -m32 -fPIC -D_REENTRANT -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux -shared -o $@ serial.c + +$(OS_DIR)/linux/x86_64/libserial.so: serial.c + mkdir -p $(dir $@) + gcc -m64 -fPIC -D_REENTRANT -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux -shared -o $@ serial.c + +$(OS_DIR)/macosx/x86_64/libserial.jnilib: serial.c + mkdir -p $(dir $@) + clang -fPIC -D_REENTRANT -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin -dynamiclib -o $@ serial.c diff --git a/native/org.eclipse.cdt.native.serial/jni/posix/.gitignore b/native/org.eclipse.cdt.native.serial/jni/posix/.gitignore deleted file mode 100644 index 5761abcfdf0..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/posix/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.o diff --git a/native/org.eclipse.cdt.native.serial/jni/posix/Makefile b/native/org.eclipse.cdt.native.serial/jni/posix/Makefile deleted file mode 100644 index 8dbea9ad859..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/posix/Makefile +++ /dev/null @@ -1,82 +0,0 @@ -#******************************************************************************* -# 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 -#*******************************************************************************/ - -UNAME = $(shell uname) - -# Defaults which can be overridden. -ifeq ($(UNAME),Darwin) -JAVA_HOME = $(shell echo /Library/Java/JavaVirtualMachines/jdk1.8.0_*.jdk/Contents/Home) -OS = macosx -JNI_OS = darwin -ARCHS = x86_64 -ARCH_FLAG_X86 = -arch i386 -ARCH_FLAG_X86_64 = -arch x86_64 -LDFLAGS = -dynamiclib -LIBEXT = jnilib -else -ifeq ($(UNAME),Linux) -JAVA_HOME = /usr/lib/jvm/java-8-oracle -OS = linux -JNI_OS = linux -ARCHS = x86 x86_64 -ARCH_FLAG_X86 = -m32 -ARCH_FLAG_X86_64 = -m64 -LDFLAGS = -shared -LIBEXT = so -endif -endif - -ARCH_X86 = x86 -ARCH_X86_64 = x86_64 - -CC=gcc -LD=libtool -CPPFLAGS = -I. -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/$(JNI_OS) -CFLAGS +=-fPIC -D_REENTRANT - -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.$(LIBEXT) -LIB_NAME_FULL_SERIAL_X86_64 = $(INSTALL_DIR_X86_64)/libserial.$(LIBEXT) - -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) $(LDFLAGS) $(ARCH_FLAG_X86) -o $(LIB_NAME_FULL_SERIAL_X86) $(OBJS_SERIAL_X86) - -$(LIB_NAME_FULL_SERIAL_X86_64): $(OBJS_SERIAL_X86_64) - mkdir -p $(INSTALL_DIR_X86_64) - $(CC) $(LDFLAGS) $(ARCH_FLAG_X86_64) -o $(LIB_NAME_FULL_SERIAL_X86_64) $(OBJS_SERIAL_X86_64) - -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 deleted file mode 100644 index 3bc71e493ef..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/posix/serial.c +++ /dev/null @@ -1,137 +0,0 @@ -/******************************************************************************* - * 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 - *******************************************************************************/ -#ifdef __APPLE__ -#include -#include -#include -#endif -#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) { - perror(cportName); - 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] = 2; // 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(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); -} - -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; - } -} diff --git a/native/org.eclipse.cdt.native.serial/jni/serial.c b/native/org.eclipse.cdt.native.serial/jni/serial.c new file mode 100644 index 00000000000..43f6a79af69 --- /dev/null +++ b/native/org.eclipse.cdt.native.serial/jni/serial.c @@ -0,0 +1,432 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +#ifdef __APPLE__ +#include +#include +#include +#endif +#ifndef __MINGW32__ +#include +#include +#include +#include +#include +#include +#include +#include +#else +#define WIN32_LEAN_AND_MEAN +#define UNICODE +#include +#endif +#include + +#define FUNC(x) Java_org_eclipse_cdt_serial_SerialPort_ ## x + +static void throwIOException(JNIEnv *env, const char *msg) +{ + char buff[256]; +#ifndef __MINGW32__ + sprintf(buff, "%s: %s", msg, strerror(errno)); +#else + sprintf_s(buff, sizeof(buff), "%s: %d", msg, GetLastError()); +#endif + jclass cls = (*env)->FindClass(env, "java/io/IOException"); + (*env)->ThrowNew(env, cls, buff); +} + +JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName, jint baudRate, jint byteSize, jint parity, jint stopBits) +{ +#ifndef __MINGW32__ + const char * cportName = (*env)->GetStringUTFChars(env, portName, NULL); + int fd = open(cportName, O_RDWR | O_NOCTTY | O_NDELAY); + if (fd < 0) { + char msg[256]; + sprintf(msg, "Error opening %s", cportName); + throwIOException(env, msg); + return fd; + } + + // Turn off all flags + fcntl(fd, F_SETFL, 0); + + struct termios options; + tcgetattr(fd, &options); + options.c_cflag |= (CLOCAL | CREAD); + + speed_t baud; + switch (baudRate) { + case 110: + baud = B110; + break; + case 300: + baud = B300; + break; + case 600: + baud = B600; + break; + case 1200: + baud = B1200; + break; + case 2400: + baud = B2400; + break; + case 4800: + baud = B4800; + break; + case 9600: + baud = B9600; + break; + case 19200: + baud = B19200; + break; + case 38400: + baud = B38400; + break; + case 57600: + baud = B57600; + break; + case 115200: + baud = B115200; + break; + default: + baud = B115200; + } + // Set baud rate + cfsetispeed(&options, baud); + cfsetospeed(&options, baud); + + // 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] = 2; // 10ths second timeout + + tcflush(fd, TCIFLUSH); + tcsetattr(fd, TCSANOW, &options); + + return fd; +#else // __MINGW32__ + const wchar_t * cportName = (const wchar_t *)(*env)->GetStringChars(env, portName, NULL); + HANDLE handle = CreateFile(cportName, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_FLAG_OVERLAPPED, + NULL); + + if (handle == INVALID_HANDLE_VALUE) { + char msg[256]; + sprintf_s(msg, sizeof(msg), "Error opening %s", cportName); + throwIOException(env, msg); + return -1; + } + + DCB dcb = { 0 }; + + if (!GetCommState(handle, &dcb)) { + throwIOException(env, "Error getting DCB"); + return -1; + } + + dcb.BaudRate = baudRate; + dcb.ByteSize = (BYTE)byteSize; + + switch (parity) { + case 0: // None + dcb.fParity = FALSE; + break; + case 1: // Even + dcb.fParity = TRUE; + dcb.Parity = EVENPARITY; + break; + case 2: // Odd + dcb.fParity = TRUE; + dcb.Parity = ODDPARITY; + break; + } + + switch (stopBits) { + case 0: + dcb.StopBits = ONESTOPBIT; + break; + case 1: + dcb.StopBits = TWOSTOPBITS; + break; + } + + if (!SetCommState(handle, &dcb)) { + throwIOException(env, "Error setting DCB"); + return -1; + } + + COMMTIMEOUTS timeouts = { 0 }; + timeouts.ReadIntervalTimeout = MAXDWORD; + timeouts.ReadTotalTimeoutMultiplier = MAXDWORD; + timeouts.ReadTotalTimeoutConstant = 200; + if (!SetCommTimeouts(handle, &timeouts)) { + throwIOException(env, "Error setting timeouts"); + return -1; + } + +#ifdef _WIN64 + return (jlong)handle; +#else + return (jlong)(unsigned)handle; +#endif +#endif // __MINGW32__ +} + +JNIEXPORT void JNICALL FUNC(close0)(JNIEnv *env, jobject jobj, jlong handle) +{ +#ifndef __MINGW32__ + close(handle); +#else +#ifdef _WIN64 + CloseHandle((HANDLE)handle); +#else + CloseHandle((HANDLE)(unsigned)handle); +#endif +#endif +} + +JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size) +{ +#ifndef __MINGW32__ + jbyte buff[256]; + int n = size < sizeof(buff) ? size : sizeof(buff); + n = read(jhandle, buff, n); + if (n > 0) { + (*env)->SetByteArrayRegion(env, bytes, offset, n, buff); + } + return n; +#else + OVERLAPPED olp = { 0 }; + + olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + if (olp.hEvent == NULL) { + throwIOException(env, "Error creating event"); + return -1; + } + + char buff[256]; + DWORD nread = sizeof(buff) < size ? sizeof(buff) : size; +#ifdef _WIN64 + HANDLE handle = (HANDLE)jhandle; +#else + HANDLE handle = (HANDLE)(unsigned)jhandle; +#endif + + if (!ReadFile(handle, buff, sizeof(buff), &nread, &olp)) { + if (GetLastError() != ERROR_IO_PENDING) { + throwIOException(env, "Error reading from port"); + CloseHandle(olp.hEvent); + return -1; + } + else { + switch (WaitForSingleObject(olp.hEvent, INFINITE)) { + case WAIT_OBJECT_0: + if (!GetOverlappedResult(handle, &olp, &nread, FALSE)) { + if (GetLastError() != ERROR_OPERATION_ABORTED) { + throwIOException(env, "Error waiting for read"); + } + CloseHandle(olp.hEvent); + return -1; + } + break; + } + } + } + + if (nread > 0) { + (*env)->SetByteArrayRegion(env, bytes, offset, nread, (jbyte *)buff); + } + CloseHandle(olp.hEvent); + return nread; +#endif +} + +JNIEXPORT void JNICALL FUNC(write0)(JNIEnv *env, jobject jobj, jlong jhandle, jint b) +{ +#ifndef __MINGW32__ + char buff = b; + write(jhandle, &buff, 1); +#else + OVERLAPPED olp = { 0 }; + + olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + if (olp.hEvent == NULL) { + throwIOException(env, "Error creating event"); + return; + } + + char buff = (char)b; + DWORD nwritten; +#ifdef _WIN64 + HANDLE handle = (HANDLE)jhandle; +#else + HANDLE handle = (HANDLE)(unsigned)jhandle; +#endif + + if (!WriteFile(handle, &buff, sizeof(buff), &nwritten, &olp)) { + if (GetLastError() != ERROR_IO_PENDING) { + throwIOException(env, "Error writing to port"); + } + else { + switch (WaitForSingleObject(olp.hEvent, INFINITE)) { + case WAIT_OBJECT_0: + if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) { + throwIOException(env, "Error waiting for write"); + } + } + } + } + + CloseHandle(olp.hEvent); +#endif +} + +JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size) +{ +#ifndef __MINGW32__ + while (size > 0) { + jbyte buff[256]; + int n = size < sizeof(buff) ? size : sizeof(buff); + (*env)->GetByteArrayRegion(env, bytes, offset, n, buff); + n = write(jhandle, buff, n); + if (n < 0) { + return; + } + size -= n; + offset += n; + } +#else + OVERLAPPED olp = { 0 }; + + olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + if (olp.hEvent == NULL) { + throwIOException(env, "Error creating event"); + return; + } + + while (size > 0) { + char buff[256]; + DWORD nwritten = sizeof(buff) < size ? sizeof(buff) : size; + (*env)->GetByteArrayRegion(env, bytes, offset, nwritten, (jbyte *)buff); +#ifdef _WIN64 + HANDLE handle = (HANDLE)jhandle; +#else + HANDLE handle = (HANDLE)(unsigned)jhandle; +#endif + + if (!WriteFile(handle, buff, nwritten, &nwritten, &olp)) { + if (GetLastError() != ERROR_IO_PENDING) { + throwIOException(env, "Error writing to port"); + return; + } + else { + switch (WaitForSingleObject(olp.hEvent, INFINITE)) { + case WAIT_OBJECT_0: + if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) { + throwIOException(env, "Error waiting for write"); + return; + } + } + } + } + size -= nwritten; + offset += nwritten; + } + + CloseHandle(olp.hEvent); +#endif +} + +#ifdef __MINGW32__ +JNIEXPORT jstring FUNC(getPortName)(JNIEnv *env, jclass cls, jint i) +{ + HKEY key; + + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &key) != ERROR_SUCCESS) { + throwIOException(env, "Can not find registry key"); + return NULL; + } + + wchar_t name[256]; + DWORD len = sizeof(name); + LONG rc = RegEnumValue(key, (DWORD)i, name, &len, NULL, NULL, NULL, NULL); + if (rc != ERROR_SUCCESS) { + if (rc != ERROR_NO_MORE_ITEMS) { + throwIOException(env, "Can not enum value"); + } + RegCloseKey(key); + return NULL; + } + + wchar_t value[256]; + DWORD type; + len = sizeof(value); + if (RegQueryValueEx(key, name, NULL, &type, (BYTE *)value, &len) != ERROR_SUCCESS) { + throwIOException(env, "Can not query value"); + RegCloseKey(key); + return NULL; + } + + jstring result = (*env)->NewString(env, (jchar *)value, (jsize) wcslen(value)); + RegCloseKey(key); + return result; +} +#endif diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/.gitignore b/native/org.eclipse.cdt.native.serial/jni/win32/serial/.gitignore deleted file mode 100644 index 8f10bd00b58..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -/ipch/ -/Release/ -/x64/ -/serial.opensdf -/serial.sdf -/serial.v12.suo diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/dllmain.cpp b/native/org.eclipse.cdt.native.serial/jni/win32/serial/dllmain.cpp deleted file mode 100644 index 8a4edd3105c..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/dllmain.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// dllmain.cpp : Defines the entry point for the DLL application. -#include "stdafx.h" - -BOOL APIENTRY DllMain( HMODULE hModule, - DWORD ul_reason_for_call, - LPVOID lpReserved - ) -{ - switch (ul_reason_for_call) - { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: - break; - } - return TRUE; -} - diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.cpp b/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.cpp deleted file mode 100644 index 6bbc8c4d68a..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.cpp +++ /dev/null @@ -1,242 +0,0 @@ -/******************************************************************************* -* 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 "stdafx.h" - -#define FUNC(x) Java_org_eclipse_cdt_serial_SerialPort_ ## x - -static void throwIOException(JNIEnv *env, char *msg) { - char buff[256]; - sprintf_s(buff, sizeof(buff), "%s: %d", msg, GetLastError()); - jclass cls = env->FindClass("java/io/IOException"); - env->ThrowNew(cls, buff); -} - -extern "C" -JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName, jint baudRate, jint byteSize, jint parity, jint stopBits) -{ - const wchar_t * cportName = (const wchar_t *)env->GetStringChars(portName, NULL); - HANDLE handle = CreateFile(cportName, - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_FLAG_OVERLAPPED, - NULL); - - if (handle == INVALID_HANDLE_VALUE) { - throwIOException(env, "Error opening serial port"); - return -1; - } - - DCB dcb = { 0 }; - - if (!GetCommState(handle, &dcb)) { - throwIOException(env, "Error getting DCB"); - return -1; - } - - dcb.BaudRate = baudRate; - dcb.ByteSize = (BYTE)byteSize; - - switch (parity) { - case 0: // None - dcb.fParity = FALSE; - break; - case 1: // Even - dcb.fParity = TRUE; - dcb.Parity = EVENPARITY; - break; - case 2: // Odd - dcb.fParity = TRUE; - dcb.Parity = ODDPARITY; - break; - } - - switch (stopBits) { - case 0: - dcb.StopBits = ONESTOPBIT; - break; - case 1: - dcb.StopBits = TWOSTOPBITS; - break; - } - - if (!SetCommState(handle, &dcb)) { - throwIOException(env, "Error setting DCB"); - return -1; - } - - COMMTIMEOUTS timeouts = { 0 }; - timeouts.ReadIntervalTimeout = MAXDWORD; - timeouts.ReadTotalTimeoutMultiplier = MAXDWORD; - timeouts.ReadTotalTimeoutConstant = 200; - if (!SetCommTimeouts(handle, &timeouts)) { - throwIOException(env, "Error setting timeouts"); - return -1; - } - - return (jlong)handle; -} - -extern "C" -JNIEXPORT void JNICALL FUNC(close0)(JNIEnv *env, jobject jobj, jlong handle) -{ - CloseHandle((HANDLE)handle); -} - -extern "C" -JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size) -{ - OVERLAPPED olp = { 0 }; - - olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - if (olp.hEvent == NULL) { - throwIOException(env, "Error creating event"); - return -1; - } - - char buff[256]; - DWORD nread = sizeof(buff) < size ? sizeof(buff) : size; - HANDLE handle = (HANDLE)jhandle; - - if (!ReadFile(handle, buff, sizeof(buff), &nread, &olp)) { - if (GetLastError() != ERROR_IO_PENDING) { - throwIOException(env, "Error reading from port"); - CloseHandle(olp.hEvent); - return -1; - } - else { - switch (WaitForSingleObject(olp.hEvent, INFINITE)) { - case WAIT_OBJECT_0: - if (!GetOverlappedResult(handle, &olp, &nread, FALSE)) { - if (GetLastError() != ERROR_OPERATION_ABORTED) { - throwIOException(env, "Error waiting for read"); - } - CloseHandle(olp.hEvent); - return -1; - } - break; - } - } - } - - if (nread > 0) { - env->SetByteArrayRegion(bytes, offset, nread, (jbyte *)buff); - } - CloseHandle(olp.hEvent); - return nread; -} - -extern "C" -JNIEXPORT void JNICALL FUNC(write0)(JNIEnv *env, jobject jobj, jlong jhandle, jint b) -{ - OVERLAPPED olp = { 0 }; - - olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - if (olp.hEvent == NULL) { - throwIOException(env, "Error creating event"); - return; - } - - char buff = (char)b; - DWORD nwritten; - HANDLE handle = (HANDLE)jhandle; - - if (!WriteFile(handle, &buff, sizeof(buff), &nwritten, &olp)) { - if (GetLastError() != ERROR_IO_PENDING) { - throwIOException(env, "Error writing to port"); - } - else { - switch (WaitForSingleObject(olp.hEvent, INFINITE)) { - case WAIT_OBJECT_0: - if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) { - throwIOException(env, "Error waiting for write"); - } - } - } - } - - CloseHandle(olp.hEvent); -} - -extern "C" -JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size) -{ - OVERLAPPED olp = { 0 }; - - olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - if (olp.hEvent == NULL) { - throwIOException(env, "Error creating event"); - return; - } - - while (size > 0) { - char buff[256]; - DWORD nwritten = sizeof(buff) < size ? sizeof(buff) : size; - env->GetByteArrayRegion(bytes, offset, nwritten, (jbyte *)buff); - HANDLE handle = (HANDLE)jhandle; - - if (!WriteFile(handle, buff, nwritten, &nwritten, &olp)) { - if (GetLastError() != ERROR_IO_PENDING) { - throwIOException(env, "Error writing to port"); - return; - } - else { - switch (WaitForSingleObject(olp.hEvent, INFINITE)) { - case WAIT_OBJECT_0: - if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) { - throwIOException(env, "Error waiting for write"); - return; - } - } - } - } - size -= nwritten; - offset += nwritten; - } - - CloseHandle(olp.hEvent); -} - -extern "C" -JNIEXPORT jstring FUNC(getPortName)(JNIEnv *env, jclass cls, jint i) -{ - HKEY key; - - if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &key) != ERROR_SUCCESS) { - throwIOException(env, "Can not find registry key"); - return NULL; - } - - wchar_t name[256]; - DWORD len = sizeof(name); - LONG rc = RegEnumValue(key, (DWORD)i, name, &len, NULL, NULL, NULL, NULL); - if (rc != ERROR_SUCCESS) { - if (rc != ERROR_NO_MORE_ITEMS) { - throwIOException(env, "Can not enum value"); - } - RegCloseKey(key); - return NULL; - } - - wchar_t value[256]; - DWORD type; - len = sizeof(value); - if (RegQueryValueEx(key, name, NULL, &type, (BYTE *)value, &len) != ERROR_SUCCESS) { - throwIOException(env, "Can not query value"); - RegCloseKey(key); - return NULL; - } - - jstring result = env->NewString((jchar *)value, (jsize) wcslen(value)); - RegCloseKey(key); - return result; -} diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.sln b/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.sln deleted file mode 100644 index 0a828bdd3bd..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.sln +++ /dev/null @@ -1,28 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "serial", "serial.vcxproj", "{48ED88D3-77CF-4E77-9554-10719E633142}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 - Release|Win32 = Release|Win32 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {48ED88D3-77CF-4E77-9554-10719E633142}.Debug|Win32.ActiveCfg = Debug|Win32 - {48ED88D3-77CF-4E77-9554-10719E633142}.Debug|Win32.Build.0 = Debug|Win32 - {48ED88D3-77CF-4E77-9554-10719E633142}.Debug|x64.ActiveCfg = Debug|x64 - {48ED88D3-77CF-4E77-9554-10719E633142}.Debug|x64.Build.0 = Debug|x64 - {48ED88D3-77CF-4E77-9554-10719E633142}.Release|Win32.ActiveCfg = Release|Win32 - {48ED88D3-77CF-4E77-9554-10719E633142}.Release|Win32.Build.0 = Release|Win32 - {48ED88D3-77CF-4E77-9554-10719E633142}.Release|x64.ActiveCfg = Release|x64 - {48ED88D3-77CF-4E77-9554-10719E633142}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj b/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj deleted file mode 100644 index 4009c5fec7e..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj +++ /dev/null @@ -1,176 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {48ED88D3-77CF-4E77-9554-10719E633142} - Win32Proj - serial - - - - DynamicLibrary - true - v120 - Unicode - - - DynamicLibrary - true - v120 - Unicode - - - DynamicLibrary - false - v120 - true - Unicode - - - DynamicLibrary - false - v120 - true - Unicode - - - - - - - - - - - - - - - - - - - true - - - true - - - false - $(SolutionDir)..\..\..\os\win32\x86\ - - - false - $(SolutionDir)..\..\..\os\win32\x86_64\ - - - - Use - Level3 - Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;SERIAL_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - - - - - Use - Level3 - Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;SERIAL_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - - - - - Level3 - Use - MaxSpeed - true - true - WIN32;NDEBUG;_WINDOWS;_USRDLL;SERIAL_EXPORTS;%(PreprocessorDefinitions) - C:\Program Files\Java\jdk1.8.0_31\include\win32;C:\Program Files\Java\jdk1.8.0_31\include;%(AdditionalIncludeDirectories) - MultiThreaded - - - Windows - true - true - true - - - - - Level3 - Use - MaxSpeed - true - true - WIN32;NDEBUG;_WINDOWS;_USRDLL;SERIAL_EXPORTS;%(PreprocessorDefinitions) - C:\Program Files\Java\jdk1.8.0_31\include\win32;C:\Program Files\Java\jdk1.8.0_31\include;%(AdditionalIncludeDirectories) - MultiThreaded - - - Windows - true - true - true - - - - - - - - - - - - false - false - - - - - false - false - - - - - - - - Create - Create - Create - Create - - - - - - \ No newline at end of file diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj.filters b/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj.filters deleted file mode 100644 index b6e39963926..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/serial.vcxproj.filters +++ /dev/null @@ -1,39 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - - - - Header Files - - - Header Files - - - - - Source Files - - - Source Files - - - Source Files - - - \ No newline at end of file diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.cpp b/native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.cpp deleted file mode 100644 index 4a32c69d470..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// stdafx.cpp : source file that includes just the standard includes -// serial.pch will be the pre-compiled header -// stdafx.obj will contain the pre-compiled type information - -#include "stdafx.h" - -// TODO: reference any additional headers you need in STDAFX.H -// and not in this file diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.h b/native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.h deleted file mode 100644 index 935ddb49a46..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/stdafx.h +++ /dev/null @@ -1,17 +0,0 @@ -// stdafx.h : include file for standard system include files, -// or project specific include files that are used frequently, but -// are changed infrequently -// - -#pragma once - -#include "targetver.h" - -#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers -// Windows Header Files: -#include - - - -// TODO: reference additional headers your program requires here -#include diff --git a/native/org.eclipse.cdt.native.serial/jni/win32/serial/targetver.h b/native/org.eclipse.cdt.native.serial/jni/win32/serial/targetver.h deleted file mode 100644 index 90e767bfce7..00000000000 --- a/native/org.eclipse.cdt.native.serial/jni/win32/serial/targetver.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -// Including SDKDDKVer.h defines the highest available Windows platform. - -// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and -// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. - -#include diff --git a/native/org.eclipse.cdt.native.serial/os/linux/x86/libserial.so b/native/org.eclipse.cdt.native.serial/os/linux/x86/libserial.so index a42fdfbdb1b..f834601fe03 100755 Binary files a/native/org.eclipse.cdt.native.serial/os/linux/x86/libserial.so and b/native/org.eclipse.cdt.native.serial/os/linux/x86/libserial.so differ diff --git a/native/org.eclipse.cdt.native.serial/os/linux/x86_64/libserial.so b/native/org.eclipse.cdt.native.serial/os/linux/x86_64/libserial.so index cd5e3455ce4..1ab52166455 100755 Binary files a/native/org.eclipse.cdt.native.serial/os/linux/x86_64/libserial.so and b/native/org.eclipse.cdt.native.serial/os/linux/x86_64/libserial.so differ 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 index a8eefeeb4a6..1631744a7c0 100755 Binary files a/native/org.eclipse.cdt.native.serial/os/macosx/x86_64/libserial.jnilib and b/native/org.eclipse.cdt.native.serial/os/macosx/x86_64/libserial.jnilib differ diff --git a/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll b/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll old mode 100644 new mode 100755 index 3ac1554c02b..1a0a0083d54 Binary files a/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll and b/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll differ diff --git a/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll b/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll old mode 100644 new mode 100755 index 331384f0a7b..04a5f463f64 Binary files a/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll and b/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll differ -- cgit v1.2.3