Skip to content

Commit

Permalink
Merge pull request #129 from fo-ifad/dis7_signal_fix
Browse files Browse the repository at this point in the history
DIS 7 Signal Pdu Fixes
  • Loading branch information
leif81 authored Feb 22, 2024
2 parents 1ff6c44 + 81dd2c2 commit c109654
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 18 deletions.
18 changes: 0 additions & 18 deletions src/main/java/edu/nps/moves/dis7/RadioIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public class RadioIdentifier extends Object implements Serializable {
*/
protected int referenceNumber;

/**
* Radio number
*/
protected int radioNumber;

/**
* Constructor
Expand All @@ -46,7 +42,6 @@ public int getMarshalledSize() {
marshalSize = marshalSize + 2; // siteNumber
marshalSize = marshalSize + 2; // applicationNumber
marshalSize = marshalSize + 2; // referenceNumber
marshalSize = marshalSize + 2; // radioNumber

return marshalSize;
}
Expand Down Expand Up @@ -75,20 +70,13 @@ public int getReferenceNumber() {
return referenceNumber;
}

public void setRadioNumber(int pRadioNumber) {
radioNumber = pRadioNumber;
}

public int getRadioNumber() {
return radioNumber;
}

public void marshal(DataOutputStream dos) {
try {
dos.writeShort((short) siteNumber);
dos.writeShort((short) applicationNumber);
dos.writeShort((short) referenceNumber);
dos.writeShort((short) radioNumber);
} // end try
catch (Exception e) {
System.out.println(e);
Expand All @@ -100,7 +88,6 @@ public void unmarshal(DataInputStream dis) {
siteNumber = (int) dis.readUnsignedShort();
applicationNumber = (int) dis.readUnsignedShort();
referenceNumber = (int) dis.readUnsignedShort();
radioNumber = (int) dis.readUnsignedShort();
} // end try
catch (Exception e) {
System.out.println(e);
Expand All @@ -120,7 +107,6 @@ public void marshal(java.nio.ByteBuffer buff) {
buff.putShort((short) siteNumber);
buff.putShort((short) applicationNumber);
buff.putShort((short) referenceNumber);
buff.putShort((short) radioNumber);
} // end of marshal method

/**
Expand All @@ -135,7 +121,6 @@ public void unmarshal(java.nio.ByteBuffer buff) {
siteNumber = (int) (buff.getShort() & 0xFFFF);
applicationNumber = (int) (buff.getShort() & 0xFFFF);
referenceNumber = (int) (buff.getShort() & 0xFFFF);
radioNumber = (int) (buff.getShort() & 0xFFFF);
} // end of unmarshal method


Expand Down Expand Up @@ -185,9 +170,6 @@ public boolean equalsImpl(Object obj) {
if (!(referenceNumber == rhs.referenceNumber)) {
ivarsEqual = false;
}
if (!(radioNumber == rhs.radioNumber)) {
ivarsEqual = false;
}

return ivarsEqual;
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/edu/nps/moves/dis7/SignalPdu.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class SignalPdu extends RadioCommunicationsFamilyPdu implements Serializa
*/
protected RadioIdentifier radioIdentifier = new RadioIdentifier();

/**
* Radio number
*/
protected int radioNumber;

/**
* encoding scheme used, and enumeration
*/
Expand Down Expand Up @@ -63,6 +68,7 @@ public int getMarshalledSize() {

marshalSize = super.getMarshalledSize();
marshalSize = marshalSize + radioIdentifier.getMarshalledSize();
marshalSize = marshalSize + 2; // radioNumber
marshalSize = marshalSize + 2; // encodingScheme
marshalSize = marshalSize + 2; // tdlType
marshalSize = marshalSize + 4; // sampleRate
Expand Down Expand Up @@ -94,14 +100,68 @@ public RadioIdentifier getRadioIdentifier() {
return radioIdentifier;
}

public void setRadioNumber(int pRadioNumber) {
radioNumber = pRadioNumber;
}

public int getRadioNumber() {
return radioNumber;
}

public void setEncodingScheme(int pEncodingScheme) {
encodingScheme = pEncodingScheme;
}

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 @@ -150,6 +210,7 @@ public void marshal(DataOutputStream dos) {
super.marshal(dos);
try {
radioIdentifier.marshal(dos);
dos.writeShort((short) radioNumber);
dos.writeShort((short) encodingScheme);
dos.writeShort((short) tdlType);
dos.writeInt((int) sampleRate);
Expand Down Expand Up @@ -184,6 +245,7 @@ public void unmarshal(DataInputStream dis) {

try {
radioIdentifier.unmarshal(dis);
radioNumber = (int) dis.readUnsignedShort();
encodingScheme = (int) dis.readUnsignedShort();
tdlType = (int) dis.readUnsignedShort();
sampleRate = dis.readInt();
Expand All @@ -209,6 +271,7 @@ public void unmarshal(DataInputStream dis) {
public void marshal(java.nio.ByteBuffer buff) {
super.marshal(buff);
radioIdentifier.marshal(buff);
buff.putShort((short) radioNumber);
buff.putShort((short) encodingScheme);
buff.putShort((short) tdlType);
buff.putInt((int) sampleRate);
Expand Down Expand Up @@ -245,6 +308,7 @@ public void marshal(java.nio.ByteBuffer buff) {
public void unmarshal(java.nio.ByteBuffer buff) {
super.unmarshal(buff);
radioIdentifier.unmarshal(buff);
radioNumber = (int) (buff.getShort() & 0xFFFF);
encodingScheme = (int) (buff.getShort() & 0xFFFF);
tdlType = (int) (buff.getShort() & 0xFFFF);
sampleRate = buff.getInt();
Expand Down Expand Up @@ -289,6 +353,9 @@ public boolean equalsImpl(Object obj) {
if (!(radioIdentifier.equals(rhs.radioIdentifier))) {
ivarsEqual = false;
}
if (!(radioNumber == rhs.radioNumber)) {
ivarsEqual = false;
}
if (!(encodingScheme == rhs.encodingScheme)) {
ivarsEqual = false;
}
Expand Down
89 changes: 89 additions & 0 deletions src/test/java/edu/nps/moves/dis7/SignalPduTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package edu.nps.moves.dis7;

import java.io.IOException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author fo
*/
public class SignalPduTest {

public SignalPduTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

@Test
public void marshal() {
SignalPdu spdu = new SignalPdu();
byte[] buffer = spdu.marshal();
assertEquals(buffer.length, spdu.getLength());
}

@Test
public void unmarshal() throws IOException {
PduFactory factory = new PduFactory();
Pdu pdu = factory.createPdu(edu.nps.moves.dis7.PduFileLoader.load("SignalPdu.raw"));
assertEquals(7, pdu.getProtocolVersion());
assertEquals(1, pdu.getExerciseID());
assertEquals(26, pdu.getPduType());
assertEquals(4, pdu.getProtocolFamily());
assertEquals(40, pdu.getLength());
assertEquals(47, pdu.getPduStatus());
assertEquals(0, pdu.getPadding());

SignalPdu spdu = (SignalPdu) pdu;

// RadioIdentifier
assertEquals(1, spdu.getRadioIdentifier().getSiteNumber());
assertEquals(2, spdu.getRadioIdentifier().getApplicationNumber());
assertEquals(1, spdu.getRadioIdentifier().getReferenceNumber());

//RadioNumber
assertEquals(1, spdu.getRadioNumber());

//EncodingScheme
assertEquals(16384, spdu.getEncodingScheme());
assertEquals(1, spdu.getEncodingClass());
assertEquals(0, spdu.getEncodingType());

//TDL Type
assertEquals(15, spdu.getTdlType());

//Sample Rate
assertEquals(55000, spdu.getSampleRate());

//Data Length
assertEquals(64, spdu.getDataLength());

//Data
assertEquals('S', spdu.getData()[0]);
assertEquals('u', spdu.getData()[1]);
assertEquals('c', spdu.getData()[2]);
assertEquals('c', spdu.getData()[3]);
assertEquals('e', spdu.getData()[4]);
assertEquals('s', spdu.getData()[5]);
assertEquals('s', spdu.getData()[6]);
assertEquals('!', spdu.getData()[7]);
}
}
Binary file added src/test/resources/edu/nps/moves/dis7/SignalPdu.raw
Binary file not shown.

0 comments on commit c109654

Please sign in to comment.