Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal PDU patching (DIS 6) #99

Merged
merged 9 commits into from
Dec 12, 2022
79 changes: 78 additions & 1 deletion src/main/java/edu/nps/moves/dis/SignalPdu.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,22 @@ public int getMarshalledSize() {
marshalSize = marshalSize + 2; // tdlType
marshalSize = marshalSize + 4; // sampleRate
marshalSize = marshalSize + 2; // dataLength
marshalSize = marshalSize + 2; // samples
marshalSize = marshalSize + 2; // samples
marshalSize = marshalSize + data.length;

switch (data.length % 4) {
case 0:
break;//No padding needed
case 1:
marshalSize = marshalSize + 3;
break;//adding 3 byte padding
case 2:
marshalSize = marshalSize + 2;
break;//adding 2 byte padding
case 3:
marshalSize = marshalSize + 1;
break;//adding 1 byte padding
}
return marshalSize;
}

Expand Down Expand Up @@ -105,6 +118,52 @@ public int getEncodingScheme() {
return encodingScheme;
}

public void setEncodingClass(int pEncodingClass) {
int newEncodingClass = 0;
int newEncodingScheme = 0;
// Save encoding class and create the encoding scheme
newEncodingClass = pEncodingClass << 14; // Move bits 0 - 1 to bit position 14 - 15
newEncodingScheme = newEncodingClass | this.getEncodingType();
this.setEncodingScheme(newEncodingScheme);
}

public int getEncodingClass() {
int extractEncodingClass = 0;
extractEncodingClass = this.getEncodingScheme() & 0xC000; // Lose bits 0 - 13
extractEncodingClass = extractEncodingClass >>> 14; // Move bits 14 - 15 to bit position 0 - 1
return extractEncodingClass;
}

public void setEncodingType(int pEncodingType) {
int newEncodingScheme = 0;
// Save encoding type and create the encoding scheme
newEncodingScheme = this.getEncodingScheme() & 0xC000;
newEncodingScheme = newEncodingScheme | pEncodingType;
this.setEncodingScheme(newEncodingScheme); //
}

public int getEncodingType() {
int extractEncodingType = 0;
extractEncodingType = this.getEncodingScheme() & 0x3FFF; // Lose bits 14 - 15
return extractEncodingType;
}

public void setNumberofTDLMessages(int pEncodingType) {
int newEncodingScheme = 0;
// Save number of TDLs and create the encoding scheme
newEncodingScheme = this.getEncodingScheme() & 0xC000; // Lose bits 0 - 13
newEncodingScheme = newEncodingScheme | pEncodingType;
this.setEncodingScheme(newEncodingScheme); //

}

public int getNumberofTDLMessages() {
int extractEncodingType = 0;
extractEncodingType = this.getEncodingScheme() & 0x3FFF; // Lose bits 14 - 15
return extractEncodingType;

}

public void setTdlType(int pTdlType) {
tdlType = pTdlType;
}
Expand Down Expand Up @@ -169,7 +228,25 @@ public void marshal(java.nio.ByteBuffer buff) {
buff.putInt((int) sampleRate);
buff.putShort((short) dataLength);
buff.putShort((short) samples);
int nrOfBytes = 0;
nrOfBytes = dataLength / Byte.SIZE;

buff.put(data);
int paddingBytes = nrOfBytes % 4;//Padding to hit 32 bit boundry
switch (paddingBytes) {
case 0:
break;//No padding needed
case 1:
buff.put((byte) 0);
buff.putShort((short) 0);
break;//adding 3 byte padding
case 2:
buff.putShort((short) 0);
break;//adding 2 byte padding
case 3:
buff.put((byte) 0);
break;//adding 1 byte padding
}
} // end of marshal method

/**
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/edu/nps/moves/dis/SignalPduTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public void unmarshal() throws IOException {
assertEquals(169, spdu.getEntityId().getEntity());
assertEquals(1, spdu.getRadioId());
assertEquals(4, spdu.getEncodingScheme());
assertEquals(0, spdu.getEncodingClass());
assertEquals(4, spdu.getEncodingType());
assertEquals(0, spdu.getTdlType());
assertEquals(22050, spdu.getSampleRate());
assertEquals(8192, spdu.getDataLength());
Expand All @@ -50,4 +52,34 @@ public void marshal() {

assertEquals(buffer.length, spdu.getLength());
}

@Test
public void unmarshalVrF() throws IOException {
PduFactory factory = new PduFactory();
Pdu aPdu = factory.createPdu(PduFileLoader.load("SignalPDIVrForces.raw"));

// Expected field values were determined from Wireshark: Decode As -> DIS.
assertEquals(6, aPdu.getProtocolVersion());
assertEquals(1, aPdu.getExerciseID());
assertEquals(26, aPdu.getPduType());
assertEquals(4, aPdu.getProtocolFamily());
//FIXME is timestamp wrong? assertEquals((long) 0.001000000, aPdu.getTimestamp());
assertEquals(156, aPdu.getLength());
assertEquals(0, aPdu.getPadding());

SignalPdu spdu = (SignalPdu) aPdu;

assertEquals(1, spdu.getEntityId().getSite());
assertEquals(3001, spdu.getEntityId().getApplication());
assertEquals(1, spdu.getEntityId().getEntity());
assertEquals(0, spdu.getRadioId());
assertEquals(32768, spdu.getEncodingScheme());
assertEquals(2, spdu.getEncodingClass());
assertEquals(0, spdu.getEncodingType());
assertEquals(0, spdu.getTdlType());
assertEquals(1000000, spdu.getSampleRate());
assertEquals(984, spdu.getDataLength());
assertEquals(0, spdu.getSamples());
assertEquals(984 / Byte.SIZE, spdu.getData().length);
}
}
Binary file not shown.