Commit bdfcda54 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Add java support

parent 42c50f16
......@@ -32,6 +32,10 @@ build/lib/%.o: %.c Makefile | build/lib
$(ADAPTORS) $(PLUGINS):
$(MAKE) -C $@
.PHONY: $(PLUGINS) $(ADAPTORS)
$(ADAPTORS) $(PLUGINS):
$(MAKE) -C $@
.PHONY: TAR
TAR:
git archive \
......@@ -60,6 +64,7 @@ clean:
rm -f moberg-*.spec
rm -f moberg-*.tar.gz
make -C test clean
for d in $(ADAPTORS) ; do make -C $$d clean ; done
build/libmoberg.so: build/lib/moberg.o
build/libmoberg.so: build/lib/moberg_config.o
......
all:
make -f src/Makefile
clean:
make -f src/Makefile clean
Moberg support for java
\ No newline at end of file
VERSION=1.0
JARNAME=Moberg
TARFILE=java_se.lth.control.realtime.Moberg-$(VERSION).tar.gz
JNINAME=se_lth_control_realtime_moberg_Moberg
JAVAH_PATH=$(shell javah -Xbootclasspath/p:build -version >/dev/null 2>&1 && \
echo -Xbootclasspath/p:build || echo -bootclasspath build)
JNI_INCLUDE=/usr/lib/jvm/java/include/
CC=gcc
CC_JNI_FLAGS=-Wall -Werror -shared -fPIC \
-I$(JNI_INCLUDE) -I$(JNI_INCLUDE)/linux -Ibuild \
-lmoberg
JAVADIR=/tmp
INSTALL_DIR=$(JAVADIR)/$(JARNAME)
JAR_DIR=$(INSTALL_DIR)/jre/lib/ext/
SO_DIR=$(INSTALL_DIR)/jre/lib/$(shell /bin/sh ./src/getProperty_os_arch)
SRCFILES=README \
src/Makefile \
src/getProperty_os_arch \
$(JNINAME:%=src/%.c) \
$(JAVAFILES:%=src/%)
JAVAFILES=$(shell find src -name '*.java' | sed -e 's:^src/::g')
all: JAVAC build/$(JARNAME).jar $(JNINAME:%=build/lib%.so)
install: install_src install_jar install_so
install_src:
for f in $(SRCFILES) ; do install -D $$f $(INSTALL_DIR)/$$f ; done
install_jar: build/$(JARNAME).jar
install -d $(JAR_DIR)
install build/$(JARNAME).jar $(JAR_DIR)
install_so: $(JNINAME:%=install_%.so)
install_%.so: build/lib%.so
install -d $(SO_DIR)
install $< $(SO_DIR)
JAVAC: $(JAVAFILES:%.java=build/%.class)
build/%.class: src/%.java
# Recompile all javafiles at once (do JIT compilation once)
mkdir -p build
cd src ; javac -d ../build -target 1.5 -source 1.5 $(JAVAFILES)
build/$(JARNAME).jar: JAVAC
jar -cf $@ \
-C build se \
$(SRCFILES)
build/%.h: $(shell echo $(JNINAME:%=build/%.class) | sed -e 's:_:/:g')
# Too many dependencies, base the ones we need on $* (matches % above)
javah -o $@ -force $(JAVAH_PATH) $(shell echo $* | sed -e s/_/./g)
build/lib%.so: $(JNINAME:%=build/%.h) $(JNINAME:%=src/%.c)
# Too many dependencies, base the ones we need on $* (matches % above)
$(CC) -o $@ $(CC_JNI_FLAGS) -I. src/$*.c
clean:
rm -rf build *~
TAR:
mkdir -p build
tar -C .. -cvzf build/$(TARFILE) $(SRCFILES:%=$(shell basename $$(pwd))/%)
#!/usr/bin/sh
BASENAME=`basename $0`
cat > ${BASENAME}.java << EOF
public class ${BASENAME} {
public static void main(String[] args) {
System.out.println(System.getProperty("os.arch"));
}
}
EOF
javac ${BASENAME}.java
java ${BASENAME}
rm ${BASENAME}.java ${BASENAME}.class
/**
* se.lth.control.realtime.moberg.AlreadyAllocatedException.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
public class AlreadyAllocatedException extends IOChannelException {
private static final long serialVersionUID = 1L;
public AlreadyAllocatedException(String kind, int index) {
super(kind, index, "is already allocated");
}
}
/**
* se.lth.control.realtime.AnalogIn.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import se.lth.control.realtime.moberg.Moberg;
public class AnalogIn extends IOChannel {
public AnalogIn(final int index) throws IOChannelException {
super(index);
}
protected void open() throws IOChannelException {
Moberg.analogInOpen(index);
}
protected void close() throws IOChannelException {
Moberg.analogInClose(index);
}
public double get() throws IOChannelException {
return Moberg.analogIn(index);
}
}
/**
* se.lth.control.realtime.AnalogOut.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.moberg.Moberg;
public class AnalogOut extends IOChannel {
public AnalogOut(int index) throws IOChannelException {
super(index);
}
public void open() throws IOChannelException {
Moberg.analogOutOpen(index);
}
public void close() throws IOChannelException {
Moberg.analogOutClose(index);
}
public void set(double value) throws IOChannelException {
Moberg.analogOut(index, value);
}
}
/**
* se.lth.control.realtime.DigitalIn.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.moberg.Moberg;
public class DigitalIn extends IOChannel {
public DigitalIn(int index) throws IOChannelException {
super(index);
}
public void open() throws IOChannelException {
Moberg.digitalInOpen(index);
}
public void close() throws IOChannelException {
Moberg.digitalInClose(index);
}
public boolean get() throws IOChannelException {
return Moberg.digitalIn(index);
}
}
/**
* se.lth.control.realtime.DigitalOut.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.moberg.Moberg;
public class DigitalOut extends IOChannel {
public DigitalOut(int index) throws IOChannelException {
super(index);
}
public void open() throws IOChannelException {
Moberg.digitalOutOpen(index);
}
public void close() throws IOChannelException {
Moberg.digitalOutClose(index);
}
public void set(boolean value) throws IOChannelException {
Moberg.digitalOut(index, value);
}
}
/**
* se.lth.control.realtime.EncoderIn.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.BitSet;
import se.lth.control.realtime.moberg.Moberg;
public class EncoderIn extends IOChannel {
public EncoderIn(int index) throws IOChannelException {
super(index);
}
protected void open() throws IOChannelException {
Moberg.encoderInOpen(index);
}
protected void close() throws IOChannelException {
Moberg.encoderInClose(index);
}
public long get() throws IOChannelException {
return Moberg.encoderIn(index);
}
}
/**
* se.lth.control.realtime.moberg.IOChannel.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
* Copyright (C) 2014 Alfred Theorin <alfred.theorin@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.util.HashMap;
import java.util.BitSet;
public abstract class IOChannel {
private static HashMap allocations = new HashMap();
public final int index;
private BitSet allocation;
private boolean isAllocated = false;
public IOChannel(int index) throws IOChannelException {
this.index = index;
open();
allocate();
}
private BitSet getAllocation() {
BitSet result = allocation;
if (result == null) {
result = (BitSet)allocations.get(this.getClass().getName());
if (result == null) {
result = new BitSet();
allocations.put(this.getClass().getName(), result);
}
}
return result;
}
private synchronized void allocate() throws IOChannelException {
if (getAllocation().get(index)) {
// Try to free unreferenced channels
System.gc();
System.runFinalization();
}
if (getAllocation().get(index)) {
try {
close();
} catch (IOChannelException e) {
}
throw new AlreadyAllocatedException(this.getClass().getName(), index);
}
getAllocation().set(index);
isAllocated = true;
}
private synchronized void deallocate() {
if (isAllocated) {
getAllocation().clear(index);
try {
close();
} catch (IOChannelException e) {
}
isAllocated = false;
}
}
protected abstract void open() throws IOChannelException;
protected abstract void close() throws IOChannelException;
protected void finalize() throws IOChannelException {
deallocate();
}
}
/**
* se.lth.control.realtime.IOChannelException.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime;
import java.io.IOException;
public class IOChannelException extends IOException {
private static final long serialVersionUID = 1L;
public IOChannelException(String kind, int index, String s) {
super(kind + " channel #" + index + " " + s);
}
}
/**
* se.lth.control.realtime.moberg.Moberg.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime.moberg;
import java.io.File;
public class Moberg {
public static native void analogInOpen(int index) throws MobergException;
public static native void analogInClose(int index) throws MobergException;
public static native double analogIn(int index) throws MobergException;
public static native void analogOutOpen(int index) throws MobergException;
public static native void analogOutClose(int index) throws MobergException;
public static native void analogOut(int index, double value) throws MobergException;
public static native void digitalInOpen(int index) throws MobergException;
public static native void digitalInClose(int index) throws MobergException;
public static native boolean digitalIn(int index) throws MobergException;
public static native void digitalOutOpen(int index) throws MobergException;
public static native void digitalOutClose(int index) throws MobergException;
public static native void digitalOut(int index, boolean value) throws MobergException;
public static native void encoderInOpen(int index) throws MobergException;
public static native void encoderInClose(int index) throws MobergException;
public static native long encoderIn(int index) throws MobergException;
private static native void Init();
static {
System.loadLibrary("se_lth_control_realtime_moberg_Moberg");
Init();
}
}
/**
* se.lth.control.realtime.moberg.MobergConfigurationFileNotFoundError.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime.moberg;
public class MobergConfigurationFileNotFoundError extends Error {
private static final long serialVersionUID = 1L;
public MobergConfigurationFileNotFoundError(int index) {
super("Moberg configuration file '/etc/moberg.conf' was not found");
}
}
/**
* se.lth.control.realtime.moberg.MobergDeviceDoesNotExistException.java
*
* Copyright (C) 2005-2019 Anders Blomdell <anders.blomdell@control.lth.se>
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*/
package se.lth.control.realtime.moberg;
public class MobergDeviceDoesNotExistException extends MobergException {