2016-12-04 00:21:00 +01:00
|
|
|
import com.sun.jna.Library;
|
|
|
|
import com.sun.jna.Native;
|
2016-12-04 20:55:29 +01:00
|
|
|
import com.sun.jna.Pointer;
|
|
|
|
import com.sun.jna.Memory;
|
2016-12-04 00:21:00 +01:00
|
|
|
|
|
|
|
public class SSS7 {
|
2016-12-19 11:55:22 +01:00
|
|
|
private static SSS7 instance = null;
|
|
|
|
|
|
|
|
public static SSS7 getInstance() {
|
|
|
|
if(instance == null) {
|
|
|
|
instance = new SSS7();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2016-12-04 00:21:00 +01:00
|
|
|
private interface NativeSSS7 extends Library {
|
|
|
|
public int libsss7_start(String serialport);
|
|
|
|
public int libsss7_can_send();
|
2016-12-04 20:55:29 +01:00
|
|
|
public void libsss7_send(Pointer data);
|
2016-12-04 00:21:00 +01:00
|
|
|
public int libsss7_send_failed();
|
|
|
|
public int libsss7_has_received();
|
2016-12-04 20:55:29 +01:00
|
|
|
public void libsss7_get_received(Pointer data);
|
2016-12-04 00:21:00 +01:00
|
|
|
public void libsss7_stop();
|
|
|
|
};
|
|
|
|
|
2016-12-04 20:55:29 +01:00
|
|
|
public final int payloadLength = 16;
|
|
|
|
|
2016-12-04 00:21:00 +01:00
|
|
|
private NativeSSS7 lib;
|
|
|
|
|
2016-12-19 11:55:22 +01:00
|
|
|
protected SSS7() {
|
2016-12-04 00:21:00 +01:00
|
|
|
this.lib = (NativeSSS7) Native.loadLibrary("libsss7.so", NativeSSS7.class);
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:30:21 +01:00
|
|
|
// All methods should be synchronized as libsss7 will a single mutex instance
|
|
|
|
// for all of them
|
|
|
|
public synchronized boolean start(String serial) {
|
2016-12-19 23:21:05 +01:00
|
|
|
return this.lib.libsss7_start(serial) == 0;
|
2016-12-04 00:21:00 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 12:30:21 +01:00
|
|
|
public synchronized boolean canSend() {
|
2016-12-04 00:21:00 +01:00
|
|
|
return this.lib.libsss7_can_send() == 1;
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:30:21 +01:00
|
|
|
public synchronized void send(byte[] data) {
|
2016-12-04 20:55:29 +01:00
|
|
|
Pointer p = new Memory(this.payloadLength);
|
|
|
|
for(long i = 0; i < this.payloadLength; i++) {
|
|
|
|
if(i < data.length) {
|
|
|
|
p.setByte(i, data[(int) i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.setByte(i, (byte) 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lib.libsss7_send(p);
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:30:21 +01:00
|
|
|
public synchronized boolean sendFailed() {
|
2016-12-04 00:21:00 +01:00
|
|
|
return this.lib.libsss7_send_failed() == 1;
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:30:21 +01:00
|
|
|
public synchronized boolean hasReceived() {
|
2016-12-04 00:21:00 +01:00
|
|
|
return this.lib.libsss7_has_received() == 1;
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:30:21 +01:00
|
|
|
public synchronized byte[] getReceived() {
|
2016-12-04 20:55:29 +01:00
|
|
|
Pointer p = new Memory(this.payloadLength);
|
|
|
|
this.lib.libsss7_get_received(p);
|
|
|
|
return p.getByteArray(0, this.payloadLength);
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:30:21 +01:00
|
|
|
public synchronized void stop() {
|
2016-12-04 20:55:29 +01:00
|
|
|
this.lib.libsss7_stop();
|
2016-12-04 00:21:00 +01:00
|
|
|
}
|
|
|
|
}
|