Skip to content

Commit

Permalink
extract ax25 frame from usp frame
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Aug 29, 2023
1 parent 821ceaf commit 739932e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 376 deletions.
45 changes: 14 additions & 31 deletions src/main/java/ru/r2cloud/jradio/usp/UspBeacon.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,33 @@

public class UspBeacon extends Beacon {

private int etherType;
private Header header;
private Integer ax25Length;
private byte[] payload;

@Override
public void readBeacon(byte[] data) throws IOException, UncorrectableException {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
etherType = dis.readUnsignedShort();
if (etherType == 0x08FF) {
int lengthByte1 = dis.readUnsignedByte();
int lengthByte2 = dis.readUnsignedByte();
// little-endian length
ax25Length = (lengthByte2 << 8) + lengthByte1;
header = new Header(dis);
if (data[0] == (byte) 0x08 && data[1] == (byte) 0xFF) {
// discard external USP beacon fields
int ax25Length = ((data[3] & 0xFF) << 8) + (data[2] & 0xFF);
if (ax25Length <= data.length - 4) {
byte[] ax25Frame = new byte[ax25Length];
System.arraycopy(data, 4, ax25Frame, 0, ax25Frame.length);
data = ax25Frame;
}
}
int available = dis.available();
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
header = new Header(dis);
try {
readBeacon(etherType, dis);
readBeacon(dis);
} catch (EOFException e) {
payload = new byte[available];
System.arraycopy(data, data.length - available, payload, 0, payload.length);
payload = new byte[data.length - Header.LENGTH_BYTES];
System.arraycopy(data, Header.LENGTH_BYTES, payload, 0, payload.length);
}

}

@SuppressWarnings("unused")
public void readBeacon(int etherType, DataInputStream dis) throws IOException, UncorrectableException {
public void readBeacon(DataInputStream dis) throws IOException, UncorrectableException {
payload = new byte[dis.available()];
dis.readFully(payload);
}
Expand All @@ -51,22 +50,6 @@ public void setPayload(byte[] payload) {
this.payload = payload;
}

public Integer getAx25Length() {
return ax25Length;
}

public void setAx25Length(Integer ax25Length) {
this.ax25Length = ax25Length;
}

public int getEtherType() {
return etherType;
}

public void setEtherType(int etherType) {
this.etherType = etherType;
}

public Header getHeader() {
return header;
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/ru/r2cloud/jradio/usp/UspDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,25 @@ public byte[] decode(byte[] raw) throws UncorrectableException {
decoded = viterbiShort.decode(dataFrame);
}
Scrambler.shuffle(decoded);
byte[] result;
if (code == 0) {
return ReedSolomon.CCSDS.decodeDualBasis(decoded);
result = ReedSolomon.CCSDS.decodeDualBasis(decoded);
} else {
System.arraycopy(decoded, 0, paddedFrame, paddedFrame.length - decoded.length, decoded.length);
byte[] padded = ReedSolomon.CCSDS.decodeDualBasis(paddedFrame);
System.arraycopy(padded, padded.length - shortFrame.length, shortFrame, 0, shortFrame.length);
return shortFrame;
result = shortFrame;
}
// extract ax25 frame
if (result[0] == (byte) 0x08 && result[1] == (byte) 0xFF) {
int ax25Length = ((result[3] & 0xFF) << 8) + (result[2] & 0xFF);
if (ax25Length <= result.length - 4) {
byte[] ax25Frame = new byte[ax25Length];
System.arraycopy(result, 4, ax25Frame, 0, ax25Frame.length);
result = ax25Frame;
}
}
return result;
}

@Override
Expand Down
22 changes: 8 additions & 14 deletions src/test/java/ru/r2cloud/jradio/usp/UspBeaconTest.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
package ru.r2cloud.jradio.usp;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;

import org.junit.Test;

import ru.r2cloud.jradio.AssertJson;
import ru.r2cloud.jradio.fec.ccsds.UncorrectableException;

public class UspBeaconTest {

@Test
public void testEofException() throws Exception {
public void testFullUSPFrame() throws Exception {
byte[] data = { 8, -1, 27, 0, -92, 100, -126, -100, -116, 64, 96, -92, -90, 96, 96, -90, 64, 111, 0, -16, -31, -1, 2, 0, 1, 0, 3, 0, 0, 38, 6, 61, 86, -113, 23, -116, 104, 84, -30, 119, -60, -1, -65, 0, 0, 0, 0, 0 };
TestUspBeacon beacon = new TestUspBeacon();
UspBeacon beacon = new UspBeacon();
beacon.readBeacon(data);
AssertJson.assertObjectsEqual("UspBeaconEof.json", beacon);
}

private static class TestUspBeacon extends UspBeacon {

@Override
public void readBeacon(int etherType, DataInputStream dis) throws IOException, UncorrectableException {
throw new EOFException();
}

@Test
public void testShortAx25Frame() throws Exception {
byte[] data = { -92, 100, -126, -100, -116, 64, 96, -92, -90, 96, 96, -90, 64, 111, 0, -16, -31, -1, 2, 0, 1, 0, 3, 0, 0, 38, 6 };
UspBeacon beacon = new UspBeacon();
beacon.readBeacon(data);
AssertJson.assertObjectsEqual("UspBeaconEof.json", beacon);
}

}
Loading

0 comments on commit 739932e

Please sign in to comment.