From eab2e148c7605ca0fd836bbe9230734895cce7d0 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Tue, 8 Dec 2015 21:44:31 -0800 Subject: Use sbt-jni plugin --- .gitignore | 1 + flow-main/build.sbt | 10 + .../jodersky/flow/internal/NativeSerial.java | 4 +- flow-native/.gitignore | 31 -- flow-native/Makefile.am | 4 - flow-native/bootstrap | 3 - flow-native/build.sbt | 3 + flow-native/configure.ac | 48 --- flow-native/debian/changelog | 6 - flow-native/debian/compat | 1 - flow-native/debian/control | 17 - flow-native/debian/copyright | 36 -- flow-native/debian/rules | 21 -- flow-native/m4/ax_jni_include_dir.m4 | 124 ------ flow-native/pkg/mkpkg | 20 - flow-native/readme.md | 3 - flow-native/src/.gitignore | 31 ++ flow-native/src/Makefile.am | 11 +- flow-native/src/bootstrap | 3 + ...om_github_jodersky_flow_internal_NativeSerial.h | 83 ---- flow-native/src/configure.ac | 48 +++ flow-native/src/debian/changelog | 6 + flow-native/src/debian/compat | 1 + flow-native/src/debian/control | 17 + flow-native/src/debian/copyright | 36 ++ flow-native/src/debian/rules | 21 ++ flow-native/src/flow.h | 90 ----- flow-native/src/flow_jni.c | 169 --------- flow-native/src/pkg/mkpkg | 20 + flow-native/src/platform/posix/flow.c | 247 ------------ flow-native/src/platform/windows/README | 1 - flow-native/src/platform/windows/flow.c.disabled | 416 --------------------- flow-native/src/readme.md | 3 + flow-native/src/src/Makefile.am | 9 + ...om_github_jodersky_flow_internal_NativeSerial.h | 83 ++++ flow-native/src/src/flow.h | 90 +++++ flow-native/src/src/flow_jni.c | 169 +++++++++ flow-native/src/src/platform/posix/flow.c | 247 ++++++++++++ flow-native/src/src/platform/windows/README | 1 + .../src/src/platform/windows/flow.c.disabled | 416 +++++++++++++++++++++ project/Build.scala | 96 ----- project/FlowBuild.scala | 75 ++++ project/jni.scala | 38 -- project/native.scala | 83 ---- project/plugins.sbt | 1 + 45 files changed, 1297 insertions(+), 1546 deletions(-) create mode 100644 flow-main/build.sbt delete mode 100644 flow-native/.gitignore delete mode 100644 flow-native/Makefile.am delete mode 100755 flow-native/bootstrap create mode 100644 flow-native/build.sbt delete mode 100644 flow-native/configure.ac delete mode 100644 flow-native/debian/changelog delete mode 100644 flow-native/debian/compat delete mode 100644 flow-native/debian/control delete mode 100644 flow-native/debian/copyright delete mode 100755 flow-native/debian/rules delete mode 100644 flow-native/m4/ax_jni_include_dir.m4 delete mode 100755 flow-native/pkg/mkpkg delete mode 100644 flow-native/readme.md create mode 100644 flow-native/src/.gitignore create mode 100755 flow-native/src/bootstrap delete mode 100644 flow-native/src/com_github_jodersky_flow_internal_NativeSerial.h create mode 100644 flow-native/src/configure.ac create mode 100644 flow-native/src/debian/changelog create mode 100644 flow-native/src/debian/compat create mode 100644 flow-native/src/debian/control create mode 100644 flow-native/src/debian/copyright create mode 100755 flow-native/src/debian/rules delete mode 100644 flow-native/src/flow.h delete mode 100644 flow-native/src/flow_jni.c create mode 100755 flow-native/src/pkg/mkpkg delete mode 100644 flow-native/src/platform/posix/flow.c delete mode 100644 flow-native/src/platform/windows/README delete mode 100644 flow-native/src/platform/windows/flow.c.disabled create mode 100644 flow-native/src/readme.md create mode 100644 flow-native/src/src/Makefile.am create mode 100644 flow-native/src/src/com_github_jodersky_flow_internal_NativeSerial.h create mode 100644 flow-native/src/src/flow.h create mode 100644 flow-native/src/src/flow_jni.c create mode 100644 flow-native/src/src/platform/posix/flow.c create mode 100644 flow-native/src/src/platform/windows/README create mode 100644 flow-native/src/src/platform/windows/flow.c.disabled delete mode 100644 project/Build.scala create mode 100644 project/FlowBuild.scala delete mode 100644 project/jni.scala delete mode 100644 project/native.scala create mode 100644 project/plugins.sbt diff --git a/.gitignore b/.gitignore index bffb670..0754cd6 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ project/plugins/project/ .ensime_cache # Native +lib_native/ *.o *.so* *.jnilib diff --git a/flow-main/build.sbt b/flow-main/build.sbt new file mode 100644 index 0000000..a852935 --- /dev/null +++ b/flow-main/build.sbt @@ -0,0 +1,10 @@ +enablePlugins(JniJvm) + +//there are also java sources in this project +compileOrder in Compile := CompileOrder.Mixed + +libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.4.1" + +target in (Compile, javah) := (baseDirectory in ThisBuild).value / "flow-native" / "src" / "src" + +name := "flow" diff --git a/flow-main/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java b/flow-main/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java index c159058..6fac8da 100644 --- a/flow-main/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java +++ b/flow-main/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java @@ -3,6 +3,8 @@ package com.github.jodersky.flow.internal; import java.io.IOException; import java.nio.ByteBuffer; +import ch.jodersky.jni.NativeLoader; + import com.github.jodersky.flow.AccessDeniedException; import com.github.jodersky.flow.InvalidSettingsException; import com.github.jodersky.flow.NoSuchPortException; @@ -25,7 +27,7 @@ import com.github.jodersky.flow.PortInterruptedException; final class NativeSerial { static { - NativeLoader.load("flow3"); + NativeLoader.load("/com/github/jodersky/flow", "flow3"); } final static int PARITY_NONE = 0; diff --git a/flow-native/.gitignore b/flow-native/.gitignore deleted file mode 100644 index b8d894c..0000000 --- a/flow-native/.gitignore +++ /dev/null @@ -1,31 +0,0 @@ -/autom4te.cache -/libtool -/aclocal.m4 -/config.sub -/config.log -/config.guess -/config.status -/config.h.in -/config.h -/configure -/depcomp -/install-sh -/ltmain.sh -/missing -/stamp-h1 -/compile -/m4 -!/m4/ax_jni_include_dir.m4 - -Makefile.in -Makefile -.deps -.libs -*.o -*.la -*.lo -*.so* -*.dylib -*.a -*~ - diff --git a/flow-native/Makefile.am b/flow-native/Makefile.am deleted file mode 100644 index e06bd75..0000000 --- a/flow-native/Makefile.am +++ /dev/null @@ -1,4 +0,0 @@ -AUTOMAKE_OPTIONS = foreign -ACLOCAL_AMFLAGS = -I m4 - -SUBDIRS = src \ No newline at end of file diff --git a/flow-native/bootstrap b/flow-native/bootstrap deleted file mode 100755 index d4d064f..0000000 --- a/flow-native/bootstrap +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -autoreconf -vfi \ No newline at end of file diff --git a/flow-native/build.sbt b/flow-native/build.sbt new file mode 100644 index 0000000..b175e05 --- /dev/null +++ b/flow-native/build.sbt @@ -0,0 +1,3 @@ +enablePlugins(JniNative) + +jniLibraryPath in (Compile, jni) := "com/github/jodersky/flow" diff --git a/flow-native/configure.ac b/flow-native/configure.ac deleted file mode 100644 index 1b622c4..0000000 --- a/flow-native/configure.ac +++ /dev/null @@ -1,48 +0,0 @@ -# -*- Autoconf -*- -# Process this file with autoconf to produce a configure script. - -AC_PREREQ([2.68]) -AC_INIT(flow, [3.0.1], [jodersky@gmail.com]) -AC_CONFIG_MACRO_DIR([m4]) -AC_CONFIG_SRCDIR([src/flow_jni.c]) - -AM_INIT_AUTOMAKE - -AC_CONFIG_HEADERS([config.h]) - -# Checks for programs. -AC_PROG_CC - -# Checks for libraries. - -# Checks for header files. -AC_CHECK_HEADERS([fcntl.h stddef.h stdlib.h sys/file.h termios.h unistd.h]) -#AC_CHECK_HEADERS([jni.h],,AC_MSG_WARN(No jni.h found. Is a JDK installed?)) - -LT_INIT([disable-static]) - -# Checks for typedefs, structures, and compiler characteristics. -AC_HEADER_STDBOOL -AC_C_INLINE -AC_TYPE_SIZE_T - -# Checks for library functions. -AC_FUNC_MALLOC -AC_CHECK_FUNCS([select]) - -AX_JNI_INCLUDE_DIR -for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS -do - CPPFLAGS="$CPPFLAGS -I$JNI_INCLUDE_DIR" -done - -AC_ARG_ENABLE([versioned-lib], - [AS_HELP_STRING([--disable-versioned-lib], [Disable producing versioned library files.])], - [:], - [enable_versioned_lib=yes]) - -AM_CONDITIONAL([ENABLE_VERSIONED_LIB], [test "$enable_versioned_lib" = "yes"]) - -AC_CONFIG_FILES([Makefile]) -AC_CONFIG_FILES([src/Makefile]) -AC_OUTPUT diff --git a/flow-native/debian/changelog b/flow-native/debian/changelog deleted file mode 100644 index 00bb566..0000000 --- a/flow-native/debian/changelog +++ /dev/null @@ -1,6 +0,0 @@ -flow (3.0.1) UNRELEASED; urgency=low - - * Initial release. - * Fix termios initialization issue. - - -- Jakob Odersky Sun, 11 Jan 2015 10:56:29 +0100 diff --git a/flow-native/debian/compat b/flow-native/debian/compat deleted file mode 100644 index ec63514..0000000 --- a/flow-native/debian/compat +++ /dev/null @@ -1 +0,0 @@ -9 diff --git a/flow-native/debian/control b/flow-native/debian/control deleted file mode 100644 index f335e0e..0000000 --- a/flow-native/debian/control +++ /dev/null @@ -1,17 +0,0 @@ -Source: flow -Maintainer: Jakob Odersky -Section: java -Priority: optional -Build-Depends: debhelper (>= 9), java7-jdk | openjdk-7-jdk -Standards-Version: 3.9.6 -Homepage: https://github.com/jodersky/flow -Vcs-Git: git://github.com/jodersky/flow.git - -Package: libflow3-jni -Architecture: any -Section: java -Priority: optional -Depends: ${shlibs:Depends}, ${misc:Depends} -Description: Provides the native backend and JNI bindings for flow. - This package contains a native library to fully enable flow, a - serial communication library for Scala. \ No newline at end of file diff --git a/flow-native/debian/copyright b/flow-native/debian/copyright deleted file mode 100644 index d26a00a..0000000 --- a/flow-native/debian/copyright +++ /dev/null @@ -1,36 +0,0 @@ -Format: http://dep.debian.net/deps/dep5 -Upstream-Name: flow -Source: https://github.com/jodersky/flow - -Files: * -Copyright: 2014 Jakob Odersky -License: BSD-3-Clause - -Files: debian/* -Copyright: 2014 Jakob Odersky -License: BSD-3-Clause - -License: BSD-3-Clause - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the University nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - . - THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - SUCH DAMAGE. diff --git a/flow-native/debian/rules b/flow-native/debian/rules deleted file mode 100755 index c062736..0000000 --- a/flow-native/debian/rules +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/make -f -# -*- makefile -*- -# Sample debian/rules that uses debhelper. -# This file was originally written by Joey Hess and Craig Small. -# As a special exception, when this file is copied by dh-make into a -# dh-make output file, you may use that output file without restriction. -# This special exception was added by Craig Small in version 0.37 of dh-make. - -# Uncomment this to turn on verbose mode. -#export DH_VERBOSE=1 - -%: - dh $@ - -# override library install directory -override_dh_auto_configure: - dh_auto_configure -- --libdir=/usr/lib/jni - -# used to suppress ldconfig warnings in preinst and postrm -override_dh_makeshlibs: - dh_makeshlibs -X/usr/lib/jni diff --git a/flow-native/m4/ax_jni_include_dir.m4 b/flow-native/m4/ax_jni_include_dir.m4 deleted file mode 100644 index 20fa380..0000000 --- a/flow-native/m4/ax_jni_include_dir.m4 +++ /dev/null @@ -1,124 +0,0 @@ -# =========================================================================== -# http://www.gnu.org/software/autoconf-archive/ax_jni_include_dir.html -# =========================================================================== -# -# SYNOPSIS -# -# AX_JNI_INCLUDE_DIR -# -# DESCRIPTION -# -# AX_JNI_INCLUDE_DIR finds include directories needed for compiling -# programs using the JNI interface. -# -# JNI include directories are usually in the Java distribution. This is -# deduced from the value of $JAVA_HOME, $JAVAC, or the path to "javac", in -# that order. When this macro completes, a list of directories is left in -# the variable JNI_INCLUDE_DIRS. -# -# Example usage follows: -# -# AX_JNI_INCLUDE_DIR -# -# for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS -# do -# CPPFLAGS="$CPPFLAGS -I$JNI_INCLUDE_DIR" -# done -# -# If you want to force a specific compiler: -# -# - at the configure.in level, set JAVAC=yourcompiler before calling -# AX_JNI_INCLUDE_DIR -# -# - at the configure level, setenv JAVAC -# -# Note: This macro can work with the autoconf M4 macros for Java programs. -# This particular macro is not part of the original set of macros. -# -# LICENSE -# -# Copyright (c) 2008 Don Anderson -# -# Copying and distribution of this file, with or without modification, are -# permitted in any medium without royalty provided the copyright notice -# and this notice are preserved. This file is offered as-is, without any -# warranty. - -#serial 10 - -AU_ALIAS([AC_JNI_INCLUDE_DIR], [AX_JNI_INCLUDE_DIR]) -AC_DEFUN([AX_JNI_INCLUDE_DIR],[ - -JNI_INCLUDE_DIRS="" - -if test "x$JAVA_HOME" != x; then - _JTOPDIR="$JAVA_HOME" -else - if test "x$JAVAC" = x; then - JAVAC=javac - fi - AC_PATH_PROG([_ACJNI_JAVAC], [$JAVAC], [no]) - if test "x$_ACJNI_JAVAC" = xno; then - AC_MSG_ERROR([cannot find JDK; try setting \$JAVAC or \$JAVA_HOME]) - fi - _ACJNI_FOLLOW_SYMLINKS("$_ACJNI_JAVAC") - _JTOPDIR=`echo "$_ACJNI_FOLLOWED" | sed -e 's://*:/:g' -e 's:/[[^/]]*$::'` -fi - -_JINC="$_JTOPDIR/include" - -_AS_ECHO_LOG([_JTOPDIR=$_JTOPDIR]) -_AS_ECHO_LOG([_JINC=$_JINC]) - -# On Mac OS X 10.6.4, jni.h is a symlink: -# /System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/jni.h -# -> ../../CurrentJDK/Headers/jni.h. -AC_CHECK_FILE([$_JINC/jni.h], - [JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $_JINC"], - [_JTOPDIR=`echo "$_JTOPDIR" | sed -e 's:/[[^/]]*$::'` - AC_CHECK_FILE([$_JTOPDIR/include/jni.h], - [JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $_JTOPDIR/include"], - AC_MSG_ERROR([cannot find JDK header files])) - ]) - -# get the likely subdirectories for system specific java includes -case "$host_os" in -bsdi*) _JNI_INC_SUBDIRS="bsdos";; -freebsd*) _JNI_INC_SUBDIRS="freebsd";; -linux*) _JNI_INC_SUBDIRS="linux genunix";; -osf*) _JNI_INC_SUBDIRS="alpha";; -solaris*) _JNI_INC_SUBDIRS="solaris";; -mingw*) _JNI_INC_SUBDIRS="win32";; -cygwin*) _JNI_INC_SUBDIRS="win32";; -darwin*) _JNI_INC_SUBDIRS="darwin";; -*) _JNI_INC_SUBDIRS="genunix";; -esac - -# add any subdirectories that are present -for JINCSUBDIR in $_JNI_INC_SUBDIRS -do - if test -d "$_JTOPDIR/include/$JINCSUBDIR"; then - JNI_INCLUDE_DIRS="$JNI_INCLUDE_DIRS $_JTOPDIR/include/$JINCSUBDIR" - fi -done -]) - -# _ACJNI_FOLLOW_SYMLINKS -# Follows symbolic links on , -# finally setting variable _ACJNI_FOLLOWED -# ---------------------------------------- -AC_DEFUN([_ACJNI_FOLLOW_SYMLINKS],[ -# find the include directory relative to the javac executable -_cur="$1" -while ls -ld "$_cur" 2>/dev/null | grep " -> " >/dev/null; do - AC_MSG_CHECKING([symlink for $_cur]) - _slink=`ls -ld "$_cur" | sed 's/.* -> //'` - case "$_slink" in - /*) _cur="$_slink";; - # 'X' avoids triggering unwanted echo options. - *) _cur=`echo "X$_cur" | sed -e 's/^X//' -e 's:[[^/]]*$::'`"$_slink";; - esac - AC_MSG_RESULT([$_cur]) -done -_ACJNI_FOLLOWED="$_cur" -])# _ACJNI diff --git a/flow-native/pkg/mkpkg b/flow-native/pkg/mkpkg deleted file mode 100755 index c82d9cb..0000000 --- a/flow-native/pkg/mkpkg +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -# -# This script produces a .pkg installable package on a mac. - -# Name of the package -PACKAGE=flow3 - -# Place to install libraries (should be on java.library.path) -LIBDIR=/Library/Java/Extensions - -# Temporary directory to install libraries -TEMPDIR=`pwd`/out - -mkdir ${TEMPDIR} && \ -cd .. && \ -./configure --libdir=${LIBDIR} && \ -make && \ -DESTDIR=${TEMPDIR} make install && \ -productbuild --root=pkg ${PACKAGE}.pkg - diff --git a/flow-native/readme.md b/flow-native/readme.md deleted file mode 100644 index 4bee987..0000000 --- a/flow-native/readme.md +++ /dev/null @@ -1,3 +0,0 @@ -# Native backend for flow - -Refer to Documentation/building.md for information on how to build. diff --git a/flow-native/src/.gitignore b/flow-native/src/.gitignore new file mode 100644 index 0000000..b8d894c --- /dev/null +++ b/flow-native/src/.gitignore @@ -0,0 +1,31 @@ +/autom4te.cache +/libtool +/aclocal.m4 +/config.sub +/config.log +/config.guess +/config.status +/config.h.in +/config.h +/configure +/depcomp +/install-sh +/ltmain.sh +/missing +/stamp-h1 +/compile +/m4 +!/m4/ax_jni_include_dir.m4 + +Makefile.in +Makefile +.deps +.libs +*.o +*.la +*.lo +*.so* +*.dylib +*.a +*~ + diff --git a/flow-native/src/Makefile.am b/flow-native/src/Makefile.am index 9fefbd1..e06bd75 100644 --- a/flow-native/src/Makefile.am +++ b/flow-native/src/Makefile.am @@ -1,9 +1,4 @@ -lib_LTLIBRARIES = libflow3.la +AUTOMAKE_OPTIONS = foreign +ACLOCAL_AMFLAGS = -I m4 -libflow3_la_SOURCES = flow_jni.c platform/posix/flow.c flow.h com_github_jodersky_flow_internal_NativeSerial.h - -if ENABLE_VERSIONED_LIB -libflow3_la_LDFLAGS = -version-info 0:1:0 -else -libflow3_la_LDFLAGS = -avoid-version -endif \ No newline at end of file +SUBDIRS = src \ No newline at end of file diff --git a/flow-native/src/bootstrap b/flow-native/src/bootstrap new file mode 100755 index 0000000..d4d064f --- /dev/null +++ b/flow-native/src/bootstrap @@ -0,0 +1,3 @@ +#!/bin/sh + +autoreconf -vfi \ No newline at end of file diff --git a/flow-native/src/com_github_jodersky_flow_internal_NativeSerial.h b/flow-native/src/com_github_jodersky_flow_internal_NativeSerial.h deleted file mode 100644 index 04364fb..0000000 --- a/flow-native/src/com_github_jodersky_flow_internal_NativeSerial.h +++ /dev/null @@ -1,83 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class com_github_jodersky_flow_internal_NativeSerial */ - -#ifndef _Included_com_github_jodersky_flow_internal_NativeSerial -#define _Included_com_github_jodersky_flow_internal_NativeSerial -#ifdef __cplusplus -extern "C" { -#endif -#undef com_github_jodersky_flow_internal_NativeSerial_PARITY_NONE -#define com_github_jodersky_flow_internal_NativeSerial_PARITY_NONE 0L -#undef com_github_jodersky_flow_internal_NativeSerial_PARITY_ODD -#define com_github_jodersky_flow_internal_NativeSerial_PARITY_ODD 1L -#undef com_github_jodersky_flow_internal_NativeSerial_PARITY_EVEN -#define com_github_jodersky_flow_internal_NativeSerial_PARITY_EVEN 2L -/* - * 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 *, jclass, jstring, jint, jint, jboolean, jint); - -/* - * 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 *, jclass, jlong, jobject); - -/* - * 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 *, jclass, jlong, jbyteArray); - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: cancelRead - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_cancelRead - (JNIEnv *, jclass, jlong); - -/* - * 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 *, jclass, jlong, jobject, jint); - -/* - * 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 *, jclass, jlong, jbyteArray, jint); - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: close - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_close - (JNIEnv *, jclass, jlong); - -/* - * Class: com_github_jodersky_flow_internal_NativeSerial - * Method: debug - * Signature: (Z)V - */ -JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_debug - (JNIEnv *, jclass, jboolean); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/flow-native/src/configure.ac b/flow-native/src/configure.ac new file mode 100644 index 0000000..1b622c4 --- /dev/null +++ b/flow-native/src/configure.ac @@ -0,0 +1,48 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ([2.68]) +AC_INIT(flow, [3.0.1], [jodersky@gmail.com]) +AC_CONFIG_MACRO_DIR([m4]) +AC_CONFIG_SRCDIR([src/flow_jni.c]) + +AM_INIT_AUTOMAKE + +AC_CONFIG_HEADERS([config.h]) + +# Checks for programs. +AC_PROG_CC + +# Checks for libraries. + +# Checks for header files. +AC_CHECK_HEADERS([fcntl.h stddef.h stdlib.h sys/file.h termios.h unistd.h]) +#AC_CHECK_HEADERS([jni.h],,AC_MSG_WARN(No jni.h found. Is a JDK installed?)) + +LT_INIT([disable-static]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_INLINE +AC_TYPE_SIZE_T + +# Checks for library functions. +AC_FUNC_MALLOC +AC_CHECK_FUNCS([select]) + +AX_JNI_INCLUDE_DIR +for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS +do + CPPFLAGS="$CPPFLAGS -I$JNI_INCLUDE_DIR" +done + +AC_ARG_ENABLE([versioned-lib], + [AS_HELP_STRING([--disable-versioned-lib], [Disable producing versioned library files.])], + [:], + [enable_versioned_lib=yes]) + +AM_CONDITIONAL([ENABLE_VERSIONED_LIB], [test "$enable_versioned_lib" = "yes"]) + +AC_CONFIG_FILES([Makefile]) +AC_CONFIG_FILES([src/Makefile]) +AC_OUTPUT diff --git a/flow-native/src/debian/changelog b/flow-native/src/debian/changelog new file mode 100644 index 0000000..00bb566 --- /dev/null +++ b/flow-native/src/debian/changelog @@ -0,0 +1,6 @@ +flow (3.0.1) UNRELEASED; urgency=low + + * Initial release. + * Fix termios initialization issue. + + -- Jakob Odersky Sun, 11 Jan 2015 10:56:29 +0100 diff --git a/flow-native/src/debian/compat b/flow-native/src/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/flow-native/src/debian/compat @@ -0,0 +1 @@ +9 diff --git a/flow-native/src/debian/control b/flow-native/src/debian/control new file mode 100644 index 0000000..f335e0e --- /dev/null +++ b/flow-native/src/debian/control @@ -0,0 +1,17 @@ +Source: flow +Maintainer: Jakob Odersky +Section: java +Priority: optional +Build-Depends: debhelper (>= 9), java7-jdk | openjdk-7-jdk +Standards-Version: 3.9.6 +Homepage: https://github.com/jodersky/flow +Vcs-Git: git://github.com/jodersky/flow.git + +Package: libflow3-jni +Architecture: any +Section: java +Priority: optional +Depends: ${shlibs:Depends}, ${misc:Depends} +Description: Provides the native backend and JNI bindings for flow. + This package contains a native library to fully enable flow, a + serial communication library for Scala. \ No newline at end of file diff --git a/flow-native/src/debian/copyright b/flow-native/src/debian/copyright new file mode 100644 index 0000000..d26a00a --- /dev/null +++ b/flow-native/src/debian/copyright @@ -0,0 +1,36 @@ +Format: http://dep.debian.net/deps/dep5 +Upstream-Name: flow +Source: https://github.com/jodersky/flow + +Files: * +Copyright: 2014 Jakob Odersky +License: BSD-3-Clause + +Files: debian/* +Copyright: 2014 Jakob Odersky +License: BSD-3-Clause + +License: BSD-3-Clause + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + . + THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. diff --git a/flow-native/src/debian/rules b/flow-native/src/debian/rules new file mode 100755 index 0000000..c062736 --- /dev/null +++ b/flow-native/src/debian/rules @@ -0,0 +1,21 @@ +#!/usr/bin/make -f +# -*- makefile -*- +# Sample debian/rules that uses debhelper. +# This file was originally written by Joey Hess and Craig Small. +# As a special exception, when this file is copied by dh-make into a +# dh-make output file, you may use that output file without restriction. +# This special exception was added by Craig Small in version 0.37 of dh-make. + +# Uncomment this to turn on verbose mode. +#export DH_VERBOSE=1 + +%: + dh $@ + +# override library install directory +override_dh_auto_configure: + dh_auto_configure -- --libdir=/usr/lib/jni + +# used to suppress ldconfig warnings in preinst and postrm +override_dh_makeshlibs: + dh_makeshlibs -X/usr/lib/jni diff --git a/flow-native/src/flow.h b/flow-native/src/flow.h deleted file mode 100644 index 44e2a47..0000000 --- a/flow-native/src/flow.h +++ /dev/null @@ -1,90 +0,0 @@ -#ifndef FLOW_H -#define FLOW_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -//general error codes that are returned by functions -#define E_IO -1 //IO error -#define E_ACCESS_DENIED -2 //access denied -#define E_BUSY -3 // port is busy -#define E_INVALID_SETTINGS -4 // some port settings are invalid -#define E_INTERRUPT -5 // not really an error, function call aborted because port is closed -#define E_NO_PORT -6 //requested port does not exist - -#define PARITY_NONE 0 -#define PARITY_ODD 1 -#define PARITY_EVEN 2 - -/** Contains internal configuration of an open serial port. */ -struct serial_config; - -/**Opens a serial port and allocates memory for storing configuration. Note: if this function fails, - * any internally allocated resources will be freed. - * @param port_name name of port - * @param baud baud rate - * @param char_size character size of data transmitted through serial device - * @param two_stop_bits set to use two stop bits instead of one - * @param parity kind of parity checking to use - * @param serial pointer to memory that will be allocated with a serial structure - * @return 0 on success - * @return E_NO_PORT if the given port does not exist - * @return E_ACCESS_DENIED if permissions are not sufficient to open port - * @return E_BUSY if port is already in use - * @return E_INVALID_SETTINGS if any of the specified settings are invalid - * @return E_IO on other error */ -int serial_open( - const char* port_name, - int baud, - int char_size, - bool two_stop_bits, - int parity, - struct serial_config** const serial); - -/**Closes a previously opened serial port and frees memory containing the configuration. Note: after a call to - * this function, the 'serial' pointer will become invalid, make sure you only call it once. This function is NOT - * thread safe, make sure no read or write is in prgress when this function is called (the reason is that per - * close manual page, close should not be called on a file descriptor that is in use by another thread). - * @param serial pointer to serial configuration that is to be closed (and freed) - * @return 0 on success - * @return E_IO on error */ -int serial_close(struct serial_config* const serial); - -/**Starts a read from a previously opened serial port. The read is blocking, however it may be - * interrupted by calling 'serial_cancel_read' on the given serial port. - * @param serial pointer to serial configuration from which to read - * @param buffer buffer into which data is read - * @param size maximum buffer size - * @return n>0 the number of bytes read into buffer - * @return E_INTERRUPT if the call to this function was interrupted - * @return E_IO on IO error */ -int serial_read(struct serial_config* const serial, char* const buffer, size_t size); - -/**Cancels a blocked read call. This function is thread safe, i.e. it may be called from a thread even - * while another thread is blocked in a read call. - * @param serial_config the serial port to interrupt - * @return 0 on success - * @return E_IO on error */ -int serial_cancel_read(struct serial_config* const serial); - -/**Writes data to a previously opened serial port. Non bocking. - * @param serial pointer to serial configuration to which to write - * @param data data to write - * @param size number of bytes to write from data - * @return n>0 the number of bytes written - * @return E_IO on IO error */ -int serial_write(struct serial_config* const serial, char* const data, size_t size); - -/**Sets debugging option. If debugging is enabled, detailed error message are printed from method calls. */ -void serial_debug(bool value); - -#ifdef __cplusplus -} -#endif - - -#endif /* FLOW_H */ diff --git a/flow-native/src/flow_jni.c b/flow-native/src/flow_jni.c deleted file mode 100644 index 8ec2aed..0000000 --- a/flow-native/src/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 diff --git a/flow-native/src/pkg/mkpkg b/flow-native/src/pkg/mkpkg new file mode 100755 index 0000000..c82d9cb --- /dev/null +++ b/flow-native/src/pkg/mkpkg @@ -0,0 +1,20 @@ +#!/bin/sh +# +# This script produces a .pkg installable package on a mac. + +# Name of the package +PACKAGE=flow3 + +# Place to install libraries (should be on java.library.path) +LIBDIR=/Library/Java/Extensions + +# Temporary directory to install libraries +TEMPDIR=`pwd`/out + +mkdir ${TEMPDIR} && \ +cd .. && \ +./configure --libdir=${LIBDIR} && \ +make && \ +DESTDIR=${TEMPDIR} make install && \ +productbuild --root=pkg ${PACKAGE}.pkg + diff --git a/flow-native/src/platform/posix/flow.c b/flow-native/src/platform/posix/flow.c deleted file mode 100644 index a2a239c..0000000 --- a/flow-native/src/platform/posix/flow.c +++ /dev/null @@ -1,247 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#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; - - /* initialize serial interface */ - newtio.c_iflag = 0; - newtio.c_oflag = 0; - newtio.c_lflag = 0; - newtio.c_cflag = CREAD; - - /* 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-native/src/platform/windows/README b/flow-native/src/platform/windows/README deleted file mode 100644 index 3d24410..0000000 --- a/flow-native/src/platform/windows/README +++ /dev/null @@ -1 +0,0 @@ -The contents of the file flow.c were found in the avrdude project. They look like they may be a good starting point for serial communication on windows. diff --git a/flow-native/src/platform/windows/flow.c.disabled b/flow-native/src/platform/windows/flow.c.disabled deleted file mode 100644 index 86a267c..0000000 --- a/flow-native/src/platform/windows/flow.c.disabled +++ /dev/null @@ -1,416 +0,0 @@ -/* - * avrdude - A Downloader/Uploader for AVR device programmers - * Copyright (C) 2003, 2004 Martin J. Thomas - * Copyright (C) 2006 Joerg Wunsch - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* $Id$ */ - -/* - * Native Win32 serial interface for avrdude. - */ - -#include "avrdude.h" - -#if defined(WIN32NATIVE) - -#include -#include -#include /* for isprint */ - -#include "serial.h" - -long serial_recv_timeout = 5000; /* ms */ - -#define W32SERBUFSIZE 1024 - -struct baud_mapping { - long baud; - DWORD speed; -}; - -/* HANDLE hComPort=INVALID_HANDLE_VALUE; */ - -static struct baud_mapping baud_lookup_table [] = { - { 1200, CBR_1200 }, - { 2400, CBR_2400 }, - { 4800, CBR_4800 }, - { 9600, CBR_9600 }, - { 19200, CBR_19200 }, - { 38400, CBR_38400 }, - { 57600, CBR_57600 }, - { 115200, CBR_115200 }, - { 0, 0 } /* Terminator. */ -}; - -static DWORD serial_baud_lookup(long baud) -{ - struct baud_mapping *map = baud_lookup_table; - - while (map->baud) { - if (map->baud == baud) - return map->speed; - map++; - } - - /* - * If a non-standard BAUD rate is used, issue - * a warning (if we are verbose) and return the raw rate - */ - if (verbose > 0) - fprintf(stderr, "%s: serial_baud_lookup(): Using non-standard baud rate: %ld", - progname, baud); - - return baud; -} - - -static BOOL serial_w32SetTimeOut(HANDLE hComPort, DWORD timeout) // in ms -{ - COMMTIMEOUTS ctmo; - ZeroMemory (&ctmo, sizeof(COMMTIMEOUTS)); - ctmo.ReadIntervalTimeout = timeout; - ctmo.ReadTotalTimeoutMultiplier = timeout; - ctmo.ReadTotalTimeoutConstant = timeout; - - return SetCommTimeouts(hComPort, &ctmo); -} - -static int ser_setspeed(union filedescriptor *fd, long baud) -{ - DCB dcb; - HANDLE hComPort = (HANDLE)fd->pfd; - - ZeroMemory (&dcb, sizeof(DCB)); - dcb.DCBlength = sizeof(DCB); - dcb.BaudRate = serial_baud_lookup (baud); - dcb.fBinary = 1; - dcb.fDtrControl = DTR_CONTROL_DISABLE; - dcb.fRtsControl = RTS_CONTROL_DISABLE; - dcb.ByteSize = 8; - dcb.Parity = NOPARITY; - dcb.StopBits = ONESTOPBIT; - - if (!SetCommState(hComPort, &dcb)) - return -1; - - return 0; -} - - -static int ser_open(char * port, long baud, union filedescriptor *fdp) -{ - LPVOID lpMsgBuf; - HANDLE hComPort=INVALID_HANDLE_VALUE; - char *newname = 0; - - /* - * If the port is of the form "net::", then - * handle it as a TCP connection to a terminal server. - * - * This is curently not implemented for Win32. - */ - if (strncmp(port, "net:", strlen("net:")) == 0) { - fprintf(stderr, - "%s: ser_open(): network connects are currently not" - "implemented for Win32 environments\n", - progname); - return -1; - } - - if (strncasecmp(port, "com", strlen("com")) == 0) { - - // prepend "\\\\.\\" to name, required for port # >= 10 - newname = malloc(strlen("\\\\.\\") + strlen(port) + 1); - - if (newname == 0) { - fprintf(stderr, - "%s: ser_open(): out of memory\n", - progname); - exit(1); - } - strcpy(newname, "\\\\.\\"); - strcat(newname, port); - - port = newname; - } - - hComPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if (hComPort == INVALID_HANDLE_VALUE) { - FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR) &lpMsgBuf, - 0, - NULL); - fprintf(stderr, "%s: ser_open(): can't open device \"%s\": %s\n", - progname, port, (char*)lpMsgBuf); - LocalFree( lpMsgBuf ); - return -1; - } - - if (!SetupComm(hComPort, W32SERBUFSIZE, W32SERBUFSIZE)) - { - CloseHandle(hComPort); - fprintf(stderr, "%s: ser_open(): can't set buffers for \"%s\"\n", - progname, port); - return -1; - } - - fdp->pfd = (void *)hComPort; - if (ser_setspeed(fdp, baud) != 0) - { - CloseHandle(hComPort); - fprintf(stderr, "%s: ser_open(): can't set com-state for \"%s\"\n", - progname, port); - return -1; - } - - if (!serial_w32SetTimeOut(hComPort,0)) - { - CloseHandle(hComPort); - fprintf(stderr, "%s: ser_open(): can't set initial timeout for \"%s\"\n", - progname, port); - return -1; - } - - if (newname != 0) { - free(newname); - } - return 0; -} - - -static void ser_close(union filedescriptor *fd) -{ - HANDLE hComPort=(HANDLE)fd->pfd; - if (hComPort != INVALID_HANDLE_VALUE) - CloseHandle (hComPort); - - hComPort = INVALID_HANDLE_VALUE; -} - -static int ser_set_dtr_rts(union filedescriptor *fd, int is_on) -{ - HANDLE hComPort=(HANDLE)fd->pfd; - - if (is_on) { - EscapeCommFunction(hComPort, SETDTR); - EscapeCommFunction(hComPort, SETRTS); - } else { - EscapeCommFunction(hComPort, CLRDTR); - EscapeCommFunction(hComPort, CLRRTS); - } - return 0; -} - - -static int ser_send(union filedescriptor *fd, unsigned char * buf, size_t buflen) -{ - size_t len = buflen; - unsigned char c='\0'; - DWORD written; - unsigned char * b = buf; - - HANDLE hComPort=(HANDLE)fd->pfd; - - if (hComPort == INVALID_HANDLE_VALUE) { - fprintf(stderr, "%s: ser_send(): port not open\n", - progname); - exit(1); - } - - if (!len) - return 0; - - if (verbose > 3) - { - fprintf(stderr, "%s: Send: ", progname); - - while (len) { - c = *b; - if (isprint(c)) { - fprintf(stderr, "%c ", c); - } - else { - fprintf(stderr, ". "); - } - fprintf(stderr, "[%02x] ", c); - b++; - len--; - } - fprintf(stderr, "\n"); - } - - serial_w32SetTimeOut(hComPort,500); - - if (!WriteFile (hComPort, buf, buflen, &written, NULL)) { - fprintf(stderr, "%s: ser_send(): write error: %s\n", - progname, "sorry no info avail"); // TODO - exit(1); - } - - if (written != buflen) { - fprintf(stderr, "%s: ser_send(): size/send mismatch\n", - progname); - exit(1); - } - - return 0; -} - - -static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen) -{ - unsigned char c; - unsigned char * p = buf; - DWORD read; - - HANDLE hComPort=(HANDLE)fd->pfd; - - if (hComPort == INVALID_HANDLE_VALUE) { - fprintf(stderr, "%s: ser_read(): port not open\n", - progname); - exit(1); - } - - serial_w32SetTimeOut(hComPort, serial_recv_timeout); - - if (!ReadFile(hComPort, buf, buflen, &read, NULL)) { - LPVOID lpMsgBuf; - FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR) &lpMsgBuf, - 0, - NULL ); - fprintf(stderr, "%s: ser_recv(): read error: %s\n", - progname, (char*)lpMsgBuf); - LocalFree( lpMsgBuf ); - exit(1); - } - - /* time out detected */ - if (read == 0) { - if (verbose > 1) - fprintf(stderr, - "%s: ser_recv(): programmer is not responding\n", - progname); - return -1; - } - - p = buf; - - if (verbose > 3) - { - fprintf(stderr, "%s: Recv: ", progname); - - while (read) { - c = *p; - if (isprint(c)) { - fprintf(stderr, "%c ", c); - } - else { - fprintf(stderr, ". "); - } - fprintf(stderr, "[%02x] ", c); - - p++; - read--; - } - fprintf(stderr, "\n"); - } - return 0; -} - - -static int ser_drain(union filedescriptor *fd, int display) -{ - // int rc; - unsigned char buf[10]; - BOOL readres; - DWORD read; - - HANDLE hComPort=(HANDLE)fd->pfd; - - if (hComPort == INVALID_HANDLE_VALUE) { - fprintf(stderr, "%s: ser_drain(): port not open\n", - progname); - exit(1); - } - - serial_w32SetTimeOut(hComPort,250); - - if (display) { - fprintf(stderr, "drain>"); - } - - while (1) { - readres=ReadFile(hComPort, buf, 1, &read, NULL); - if (!readres) { - LPVOID lpMsgBuf; - FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR) &lpMsgBuf, - 0, - NULL ); - fprintf(stderr, "%s: ser_drain(): read error: %s\n", - progname, (char*)lpMsgBuf); - LocalFree( lpMsgBuf ); - exit(1); - } - - if (read) { // data avail - if (display) fprintf(stderr, "%02x ", buf[0]); - } - else { // no more data - if (display) fprintf(stderr, " +/* Header for class com_github_jodersky_flow_internal_NativeSerial */ + +#ifndef _Included_com_github_jodersky_flow_internal_NativeSerial +#define _Included_com_github_jodersky_flow_internal_NativeSerial +#ifdef __cplusplus +extern "C" { +#endif +#undef com_github_jodersky_flow_internal_NativeSerial_PARITY_NONE +#define com_github_jodersky_flow_internal_NativeSerial_PARITY_NONE 0L +#undef com_github_jodersky_flow_internal_NativeSerial_PARITY_ODD +#define com_github_jodersky_flow_internal_NativeSerial_PARITY_ODD 1L +#undef com_github_jodersky_flow_internal_NativeSerial_PARITY_EVEN +#define com_github_jodersky_flow_internal_NativeSerial_PARITY_EVEN 2L +/* + * 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 *, jclass, jstring, jint, jint, jboolean, jint); + +/* + * 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 *, jclass, jlong, jobject); + +/* + * 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 *, jclass, jlong, jbyteArray); + +/* + * Class: com_github_jodersky_flow_internal_NativeSerial + * Method: cancelRead + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_cancelRead + (JNIEnv *, jclass, jlong); + +/* + * 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 *, jclass, jlong, jobject, jint); + +/* + * 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 *, jclass, jlong, jbyteArray, jint); + +/* + * Class: com_github_jodersky_flow_internal_NativeSerial + * Method: close + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_close + (JNIEnv *, jclass, jlong); + +/* + * Class: com_github_jodersky_flow_internal_NativeSerial + * Method: debug + * Signature: (Z)V + */ +JNIEXPORT void JNICALL Java_com_github_jodersky_flow_internal_NativeSerial_debug + (JNIEnv *, jclass, jboolean); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/flow-native/src/src/flow.h b/flow-native/src/src/flow.h new file mode 100644 index 0000000..44e2a47 --- /dev/null +++ b/flow-native/src/src/flow.h @@ -0,0 +1,90 @@ +#ifndef FLOW_H +#define FLOW_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +//general error codes that are returned by functions +#define E_IO -1 //IO error +#define E_ACCESS_DENIED -2 //access denied +#define E_BUSY -3 // port is busy +#define E_INVALID_SETTINGS -4 // some port settings are invalid +#define E_INTERRUPT -5 // not really an error, function call aborted because port is closed +#define E_NO_PORT -6 //requested port does not exist + +#define PARITY_NONE 0 +#define PARITY_ODD 1 +#define PARITY_EVEN 2 + +/** Contains internal configuration of an open serial port. */ +struct serial_config; + +/**Opens a serial port and allocates memory for storing configuration. Note: if this function fails, + * any internally allocated resources will be freed. + * @param port_name name of port + * @param baud baud rate + * @param char_size character size of data transmitted through serial device + * @param two_stop_bits set to use two stop bits instead of one + * @param parity kind of parity checking to use + * @param serial pointer to memory that will be allocated with a serial structure + * @return 0 on success + * @return E_NO_PORT if the given port does not exist + * @return E_ACCESS_DENIED if permissions are not sufficient to open port + * @return E_BUSY if port is already in use + * @return E_INVALID_SETTINGS if any of the specified settings are invalid + * @return E_IO on other error */ +int serial_open( + const char* port_name, + int baud, + int char_size, + bool two_stop_bits, + int parity, + struct serial_config** const serial); + +/**Closes a previously opened serial port and frees memory containing the configuration. Note: after a call to + * this function, the 'serial' pointer will become invalid, make sure you only call it once. This function is NOT + * thread safe, make sure no read or write is in prgress when this function is called (the reason is that per + * close manual page, close should not be called on a file descriptor that is in use by another thread). + * @param serial pointer to serial configuration that is to be closed (and freed) + * @return 0 on success + * @return E_IO on error */ +int serial_close(struct serial_config* const serial); + +/**Starts a read from a previously opened serial port. The read is blocking, however it may be + * interrupted by calling 'serial_cancel_read' on the given serial port. + * @param serial pointer to serial configuration from which to read + * @param buffer buffer into which data is read + * @param size maximum buffer size + * @return n>0 the number of bytes read into buffer + * @return E_INTERRUPT if the call to this function was interrupted + * @return E_IO on IO error */ +int serial_read(struct serial_config* const serial, char* const buffer, size_t size); + +/**Cancels a blocked read call. This function is thread safe, i.e. it may be called from a thread even + * while another thread is blocked in a read call. + * @param serial_config the serial port to interrupt + * @return 0 on success + * @return E_IO on error */ +int serial_cancel_read(struct serial_config* const serial); + +/**Writes data to a previously opened serial port. Non bocking. + * @param serial pointer to serial configuration to which to write + * @param data data to write + * @param size number of bytes to write from data + * @return n>0 the number of bytes written + * @return E_IO on IO error */ +int serial_write(struct serial_config* const serial, char* const data, size_t size); + +/**Sets debugging option. If debugging is enabled, detailed error message are printed from method calls. */ +void serial_debug(bool value); + +#ifdef __cplusplus +} +#endif + + +#endif /* FLOW_H */ diff --git a/flow-native/src/src/flow_jni.c b/flow-native/src/src/flow_jni.c new file mode 100644 index 0000000..8ec2aed --- /dev/null +++ b/flow-native/src/src/flow_jni.c @@ -0,0 +1,169 @@ +#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 diff --git a/flow-native/src/src/platform/posix/flow.c b/flow-native/src/src/platform/posix/flow.c new file mode 100644 index 0000000..a2a239c --- /dev/null +++ b/flow-native/src/src/platform/posix/flow.c @@ -0,0 +1,247 @@ +#include +#include +#include +#include +#include +#include +#include +#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; + + /* initialize serial interface */ + newtio.c_iflag = 0; + newtio.c_oflag = 0; + newtio.c_lflag = 0; + newtio.c_cflag = CREAD; + + /* 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-native/src/src/platform/windows/README b/flow-native/src/src/platform/windows/README new file mode 100644 index 0000000..3d24410 --- /dev/null +++ b/flow-native/src/src/platform/windows/README @@ -0,0 +1 @@ +The contents of the file flow.c were found in the avrdude project. They look like they may be a good starting point for serial communication on windows. diff --git a/flow-native/src/src/platform/windows/flow.c.disabled b/flow-native/src/src/platform/windows/flow.c.disabled new file mode 100644 index 0000000..86a267c --- /dev/null +++ b/flow-native/src/src/platform/windows/flow.c.disabled @@ -0,0 +1,416 @@ +/* + * avrdude - A Downloader/Uploader for AVR device programmers + * Copyright (C) 2003, 2004 Martin J. Thomas + * Copyright (C) 2006 Joerg Wunsch + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* $Id$ */ + +/* + * Native Win32 serial interface for avrdude. + */ + +#include "avrdude.h" + +#if defined(WIN32NATIVE) + +#include +#include +#include /* for isprint */ + +#include "serial.h" + +long serial_recv_timeout = 5000; /* ms */ + +#define W32SERBUFSIZE 1024 + +struct baud_mapping { + long baud; + DWORD speed; +}; + +/* HANDLE hComPort=INVALID_HANDLE_VALUE; */ + +static struct baud_mapping baud_lookup_table [] = { + { 1200, CBR_1200 }, + { 2400, CBR_2400 }, + { 4800, CBR_4800 }, + { 9600, CBR_9600 }, + { 19200, CBR_19200 }, + { 38400, CBR_38400 }, + { 57600, CBR_57600 }, + { 115200, CBR_115200 }, + { 0, 0 } /* Terminator. */ +}; + +static DWORD serial_baud_lookup(long baud) +{ + struct baud_mapping *map = baud_lookup_table; + + while (map->baud) { + if (map->baud == baud) + return map->speed; + map++; + } + + /* + * If a non-standard BAUD rate is used, issue + * a warning (if we are verbose) and return the raw rate + */ + if (verbose > 0) + fprintf(stderr, "%s: serial_baud_lookup(): Using non-standard baud rate: %ld", + progname, baud); + + return baud; +} + + +static BOOL serial_w32SetTimeOut(HANDLE hComPort, DWORD timeout) // in ms +{ + COMMTIMEOUTS ctmo; + ZeroMemory (&ctmo, sizeof(COMMTIMEOUTS)); + ctmo.ReadIntervalTimeout = timeout; + ctmo.ReadTotalTimeoutMultiplier = timeout; + ctmo.ReadTotalTimeoutConstant = timeout; + + return SetCommTimeouts(hComPort, &ctmo); +} + +static int ser_setspeed(union filedescriptor *fd, long baud) +{ + DCB dcb; + HANDLE hComPort = (HANDLE)fd->pfd; + + ZeroMemory (&dcb, sizeof(DCB)); + dcb.DCBlength = sizeof(DCB); + dcb.BaudRate = serial_baud_lookup (baud); + dcb.fBinary = 1; + dcb.fDtrControl = DTR_CONTROL_DISABLE; + dcb.fRtsControl = RTS_CONTROL_DISABLE; + dcb.ByteSize = 8; + dcb.Parity = NOPARITY; + dcb.StopBits = ONESTOPBIT; + + if (!SetCommState(hComPort, &dcb)) + return -1; + + return 0; +} + + +static int ser_open(char * port, long baud, union filedescriptor *fdp) +{ + LPVOID lpMsgBuf; + HANDLE hComPort=INVALID_HANDLE_VALUE; + char *newname = 0; + + /* + * If the port is of the form "net::", then + * handle it as a TCP connection to a terminal server. + * + * This is curently not implemented for Win32. + */ + if (strncmp(port, "net:", strlen("net:")) == 0) { + fprintf(stderr, + "%s: ser_open(): network connects are currently not" + "implemented for Win32 environments\n", + progname); + return -1; + } + + if (strncasecmp(port, "com", strlen("com")) == 0) { + + // prepend "\\\\.\\" to name, required for port # >= 10 + newname = malloc(strlen("\\\\.\\") + strlen(port) + 1); + + if (newname == 0) { + fprintf(stderr, + "%s: ser_open(): out of memory\n", + progname); + exit(1); + } + strcpy(newname, "\\\\.\\"); + strcat(newname, port); + + port = newname; + } + + hComPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if (hComPort == INVALID_HANDLE_VALUE) { + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &lpMsgBuf, + 0, + NULL); + fprintf(stderr, "%s: ser_open(): can't open device \"%s\": %s\n", + progname, port, (char*)lpMsgBuf); + LocalFree( lpMsgBuf ); + return -1; + } + + if (!SetupComm(hComPort, W32SERBUFSIZE, W32SERBUFSIZE)) + { + CloseHandle(hComPort); + fprintf(stderr, "%s: ser_open(): can't set buffers for \"%s\"\n", + progname, port); + return -1; + } + + fdp->pfd = (void *)hComPort; + if (ser_setspeed(fdp, baud) != 0) + { + CloseHandle(hComPort); + fprintf(stderr, "%s: ser_open(): can't set com-state for \"%s\"\n", + progname, port); + return -1; + } + + if (!serial_w32SetTimeOut(hComPort,0)) + { + CloseHandle(hComPort); + fprintf(stderr, "%s: ser_open(): can't set initial timeout for \"%s\"\n", + progname, port); + return -1; + } + + if (newname != 0) { + free(newname); + } + return 0; +} + + +static void ser_close(union filedescriptor *fd) +{ + HANDLE hComPort=(HANDLE)fd->pfd; + if (hComPort != INVALID_HANDLE_VALUE) + CloseHandle (hComPort); + + hComPort = INVALID_HANDLE_VALUE; +} + +static int ser_set_dtr_rts(union filedescriptor *fd, int is_on) +{ + HANDLE hComPort=(HANDLE)fd->pfd; + + if (is_on) { + EscapeCommFunction(hComPort, SETDTR); + EscapeCommFunction(hComPort, SETRTS); + } else { + EscapeCommFunction(hComPort, CLRDTR); + EscapeCommFunction(hComPort, CLRRTS); + } + return 0; +} + + +static int ser_send(union filedescriptor *fd, unsigned char * buf, size_t buflen) +{ + size_t len = buflen; + unsigned char c='\0'; + DWORD written; + unsigned char * b = buf; + + HANDLE hComPort=(HANDLE)fd->pfd; + + if (hComPort == INVALID_HANDLE_VALUE) { + fprintf(stderr, "%s: ser_send(): port not open\n", + progname); + exit(1); + } + + if (!len) + return 0; + + if (verbose > 3) + { + fprintf(stderr, "%s: Send: ", progname); + + while (len) { + c = *b; + if (isprint(c)) { + fprintf(stderr, "%c ", c); + } + else { + fprintf(stderr, ". "); + } + fprintf(stderr, "[%02x] ", c); + b++; + len--; + } + fprintf(stderr, "\n"); + } + + serial_w32SetTimeOut(hComPort,500); + + if (!WriteFile (hComPort, buf, buflen, &written, NULL)) { + fprintf(stderr, "%s: ser_send(): write error: %s\n", + progname, "sorry no info avail"); // TODO + exit(1); + } + + if (written != buflen) { + fprintf(stderr, "%s: ser_send(): size/send mismatch\n", + progname); + exit(1); + } + + return 0; +} + + +static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen) +{ + unsigned char c; + unsigned char * p = buf; + DWORD read; + + HANDLE hComPort=(HANDLE)fd->pfd; + + if (hComPort == INVALID_HANDLE_VALUE) { + fprintf(stderr, "%s: ser_read(): port not open\n", + progname); + exit(1); + } + + serial_w32SetTimeOut(hComPort, serial_recv_timeout); + + if (!ReadFile(hComPort, buf, buflen, &read, NULL)) { + LPVOID lpMsgBuf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &lpMsgBuf, + 0, + NULL ); + fprintf(stderr, "%s: ser_recv(): read error: %s\n", + progname, (char*)lpMsgBuf); + LocalFree( lpMsgBuf ); + exit(1); + } + + /* time out detected */ + if (read == 0) { + if (verbose > 1) + fprintf(stderr, + "%s: ser_recv(): programmer is not responding\n", + progname); + return -1; + } + + p = buf; + + if (verbose > 3) + { + fprintf(stderr, "%s: Recv: ", progname); + + while (read) { + c = *p; + if (isprint(c)) { + fprintf(stderr, "%c ", c); + } + else { + fprintf(stderr, ". "); + } + fprintf(stderr, "[%02x] ", c); + + p++; + read--; + } + fprintf(stderr, "\n"); + } + return 0; +} + + +static int ser_drain(union filedescriptor *fd, int display) +{ + // int rc; + unsigned char buf[10]; + BOOL readres; + DWORD read; + + HANDLE hComPort=(HANDLE)fd->pfd; + + if (hComPort == INVALID_HANDLE_VALUE) { + fprintf(stderr, "%s: ser_drain(): port not open\n", + progname); + exit(1); + } + + serial_w32SetTimeOut(hComPort,250); + + if (display) { + fprintf(stderr, "drain>"); + } + + while (1) { + readres=ReadFile(hComPort, buf, 1, &read, NULL); + if (!readres) { + LPVOID lpMsgBuf; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &lpMsgBuf, + 0, + NULL ); + fprintf(stderr, "%s: ser_drain(): read error: %s\n", + progname, (char*)lpMsgBuf); + LocalFree( lpMsgBuf ); + exit(1); + } + + if (read) { // data avail + if (display) fprintf(stderr, "%02x ", buf[0]); + } + else { // no more data + if (display) fprintf(stderr, " false }, - pomExtra := { - - git@github.com:jodersky/flow.git - scm:git:git@github.com:jodersky/flow.git - - - - jodersky - Jakob Odersky - - - } - ) - - lazy val runSettings: Seq[Setting[_]] = Seq( - fork := true, - connectInput in run := true, - outputStrategy := Some(StdoutOutput) - ) - - lazy val root: Project = ( - Project("root", file(".")) - aggregate(main, native) - settings(commonSettings: _*) - settings( - publishArtifact := false, - publish := (), - publishLocal := (), - publishTo := Some(Resolver.file("Unused transient repository", target.value / "unusedrepo")) // make sbt-pgp happy - ) - ) - - lazy val main: Project = ( - Project("flow-main", file("flow-main")) - settings(commonSettings: _*) - settings(JniDefaults.settings: _*) - settings( - name := "flow", - javahHeaderDirectory := (baseDirectory in ThisBuild).value / "flow-native" / "src", - javahClasses := Seq("com.github.jodersky.flow.internal.NativeSerial"), - compileOrder in Compile := CompileOrder.Mixed, - libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.4.1" - ) - ) - - lazy val native: Project = ( - Project("flow-native", file("flow-native-sbt")) - settings(commonSettings: _*) - settings(NativeDefaults.settings: _*) - settings( - name := "flow-native", - crossPaths := false, - nativeBuildDirectory := (baseDirectory in ThisBuild).value / "flow-native" - ) - ) - - lazy val samplesTerminal = ( - Project("flow-samples-terminal", file("flow-samples") / "terminal") - settings(commonSettings: _*) - settings(runSettings: _*) - dependsOn(main) - - //kind of dirty, but it gets the sample to run without installing native libraries - settings( - (run in Compile) <<= (run in Compile).dependsOn(nativeBuild in native), - javaOptions += "-Djava.library.path=" + (nativeOutputDirectory in native).value.getAbsolutePath() - ) - ) - - lazy val samplesWatcher = ( - Project("flow-samples-watcher", file("flow-samples") / "watcher") - settings(commonSettings: _*) - settings(runSettings: _*) - dependsOn(main) - ) - -} diff --git a/project/FlowBuild.scala b/project/FlowBuild.scala new file mode 100644 index 0000000..15a3fe4 --- /dev/null +++ b/project/FlowBuild.scala @@ -0,0 +1,75 @@ +import sbt._ +import Keys._ + +object FlowBuild extends Build { + + val scalaVersions = List("2.11.7", "2.12.0-M3") + + lazy val commonSettings: Seq[Setting[_]] = Seq( + version := "2.4.0-SNAPSHOT", + scalaVersion in ThisBuild := scalaVersions.head, + crossScalaVersions in ThisBuild := scalaVersions.reverse, + scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature", "-target:jvm-1.8"), + organization := "com.github.jodersky", + licenses := Seq(("BSD New", url("http://opensource.org/licenses/BSD-3-Clause"))), + homepage := Some(url("https://github.com/jodersky/flow")), + pomIncludeRepository := { _ => false }, + pomExtra := { + + git@github.com:jodersky/flow.git + scm:git:git@github.com:jodersky/flow.git + + + + jodersky + Jakob Odersky + + + } + ) + + lazy val runSettings: Seq[Setting[_]] = Seq( + fork := true, + connectInput in run := true, + outputStrategy := Some(StdoutOutput) + ) + + lazy val root: Project = ( + Project("root", file(".")) + aggregate(main, native) + settings(commonSettings: _*) + settings( + publishArtifact := false, + publish := (), + publishLocal := (), + publishTo := Some(Resolver.file("Unused transient repository", target.value / "unusedrepo")) // make sbt-pgp happy + ) + ) + + lazy val main = Project( + id = "flow-main", + base = file("flow-main"), + settings = commonSettings + ) + + lazy val native = Project( + id = "flow-native", + base = file("flow-native"), + settings = commonSettings + ) + + lazy val samplesTerminal = Project( + id = "flow-samples-terminal", + base = file("flow-samples") / "terminal", + settings = commonSettings, + dependencies = Seq(main, native % Runtime) + ) + + lazy val samplesWatcher = Project( + id = "flow-samples-watcher", + base = file("flow-samples"), + settings = commonSettings, + dependencies = Seq(main, native % Runtime) + ) + +} diff --git a/project/jni.scala b/project/jni.scala deleted file mode 100644 index 07d8406..0000000 --- a/project/jni.scala +++ /dev/null @@ -1,38 +0,0 @@ -import sbt._ -import Keys._ -import scala.util.Try - -object JniKeys { - val javahHeaderDirectory = settingKey[File]("Directory where generated javah header files are placed.") - val javahClasses = settingKey[Seq[String]]("Fully qualified names of classes containing native declarations.") - val javahClasspath = taskKey[Seq[File]]("Classpath to use in javah.") - val javah = taskKey[Seq[File]]("Generate JNI headers.") -} - -object JniDefaults { - import JniKeys._ - - val settings: Seq[Setting[_]] = Seq( - javahHeaderDirectory := baseDirectory.value, - javahClasspath := Seq((classDirectory in Compile).value), - javah := javahImpl.value - ) - - def javahImpl = Def.task { - val jcp = javahClasspath.value - val cp = jcp.mkString(sys.props("path.separator")) - for (clazz <- javahClasses.value) { - val parts = Seq( - "javah", - "-d", javahHeaderDirectory.value, - "-classpath", cp, - clazz) - val cmd = parts.mkString(" ") - val ev = Process(cmd) ! streams.value.log - if (ev != 0) throw new RuntimeException(s"Error occured running javah. Exit code: ${ev}") - } - IO.listFiles(javahHeaderDirectory.value) - } - -} - diff --git a/project/native.scala b/project/native.scala deleted file mode 100644 index 342a863..0000000 --- a/project/native.scala +++ /dev/null @@ -1,83 +0,0 @@ -import sbt._ -import Keys._ -import java.io.File -import java.util.jar.Manifest - -object NativeKeys { - - val nativeBuildDirectory = settingKey[File]("Directory containing native build scripts.") - val nativeTargetDirectory = settingKey[File]("Base directory to store native products.") - val nativeOutputDirectory = settingKey[File]("Actual directory where native products are stored.") - val nativePackageUnmanagedDirectory = settingKey[File]("Directory containing external products that will be copied to the native jar.") - - val nativeClean = taskKey[Unit]("Clean native build.") - val nativeBuild = taskKey[File]("Invoke native build.") -} - -object NativeDefaults { - import NativeKeys._ - - val autoClean = Def.task { - val log = streams.value.log - val build = nativeBuildDirectory.value - - Process("make distclean", build) #|| Process("make clean", build) ! log - } - - val autoLib = Def.task { - val log = streams.value.log - val build = nativeBuildDirectory.value - val out = nativeOutputDirectory.value - - val configure = Process( - "./configure " + - "--prefix=" + out.getAbsolutePath + " " + - "--libdir=" + out.getAbsolutePath + " " + - "--disable-versioned-lib", //Disable producing versioned library files, not needed for fat jars. - build) - - val make = Process("make", build) - - val makeInstall = Process("make install", build) - - val ev = configure #&& make #&& makeInstall ! log - if (ev != 0) - throw new RuntimeException(s"Building native library failed. Exit code: ${ev}") - - (out ** ("*.la")).get.foreach(_.delete()) - - out - } - - val nativePackageMappings = Def.task { - val managedDir = nativeTargetDirectory.value - val unmanagedDir = nativePackageUnmanagedDirectory.value - - val managed = (nativeBuild.value ** "*").get - val unmanaged = (unmanagedDir ** "*").get - - val managedMappings: Seq[(File, String)] = for (file <- managed; if file.isFile) yield { - file -> ("native/" + (file relativeTo managedDir).get.getPath) - } - - val unmanagedMappings: Seq[(File, String)] = for (file <- unmanaged; if file.isFile) yield { - file -> ("native/" + (file relativeTo unmanagedDir).get.getPath) - } - - managedMappings ++ unmanagedMappings - } - - def os = System.getProperty("os.name").toLowerCase.filter(c => !c.isWhitespace) - def arch = System.getProperty("os.arch").toLowerCase - - val settings: Seq[Setting[_]] = Seq( - nativeTargetDirectory := target.value / "native", - nativeOutputDirectory := nativeTargetDirectory.value / (os + "-" + arch), - nativeClean := autoClean.value, - nativeBuild := autoLib.value, - nativePackageUnmanagedDirectory := baseDirectory.value / "lib_native", - mappings in (Compile, packageBin) ++= nativePackageMappings.value - ) - -} - diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..489fdbc --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("ch.jodersky" %% "sbt-jni" % "0.2-SNAPSHOT") -- cgit v1.2.3