sss7modem/software/jna/SSS7.java

75 lines
1.6 KiB
Java
Raw Normal View History

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;
private String serial;
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 11:55:22 +01:00
public boolean start(String serial) {
2016-12-04 00:21:00 +01:00
return this.lib.libsss7_start(this.serial) == 0;
}
public boolean canSend() {
return this.lib.libsss7_can_send() == 1;
}
2016-12-04 20:55:29 +01:00
public void send(byte[] data) {
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-04 00:21:00 +01:00
public boolean sendFailed() {
return this.lib.libsss7_send_failed() == 1;
}
public boolean hasReceived() {
return this.lib.libsss7_has_received() == 1;
}
2016-12-04 20:55:29 +01:00
public byte[] getReceived() {
Pointer p = new Memory(this.payloadLength);
this.lib.libsss7_get_received(p);
return p.getByteArray(0, this.payloadLength);
}
public void stop() {
this.lib.libsss7_stop();
2016-12-04 00:21:00 +01:00
}
}