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 data length is in bytes but should be in bits (DIS 7) #116

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 73 additions & 25 deletions src/main/java/edu/nps/moves/dis7/IntercomSignalPdu.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,28 @@ public int getMarshalledSize() {
int marshalSize = 0;

marshalSize = super.getMarshalledSize();
marshalSize = marshalSize + entityID.getMarshalledSize(); // entityID
marshalSize = marshalSize + 2; // communicationsDeviceID
marshalSize = marshalSize + 2; // encodingScheme
marshalSize = marshalSize + 2; // tdlType
marshalSize = marshalSize + 4; // sampleRate
marshalSize = marshalSize + 2; // dataLength
marshalSize = marshalSize + 2; // samples
marshalSize = marshalSize + entityID.getMarshalledSize(); // entityID
marshalSize = marshalSize + 2; // communicationsDeviceID
marshalSize = marshalSize + 2; // encodingScheme
marshalSize = marshalSize + 2; // tdlType
marshalSize = marshalSize + 4; // sampleRate
marshalSize = marshalSize + 2; // dataLength
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 @@ -117,16 +130,17 @@ public long getSampleRate() {
return sampleRate;
}

public int getDataLength() {
return (int) data.length;
}

/**
* Note that setting this value will not change the marshalled value. The
* list whose length this describes is used for that purpose. The
* getdataLength method will also be based on the actual list length rather
* than this value. The method is simply here for java bean completeness.
* IAW IEEE 1278.1, this field shall specify the number of **bits** of digital
* voice audio or digital data being sent in this Signal PDU.
*/
public short getDataLength() {
if (dataLength == 0) {
return (short) (data.length * 8);
}
return (short) dataLength;
}

public void setDataLength(int pDataLength) {
dataLength = pDataLength;
}
Expand Down Expand Up @@ -155,10 +169,27 @@ public void marshal(DataOutputStream dos) {
dos.writeShort((short) encodingScheme);
dos.writeShort((short) tdlType);
dos.writeInt((int) sampleRate);
dos.writeShort((short) data.length);
dos.writeShort((short) dataLength);
dos.writeShort((short) samples);
dos.write(data);
} // end try

int nrOfBytes = dataLength / Byte.SIZE;
int paddingBytes = nrOfBytes % 4;// Padding to hit 32 bit boundry
switch (paddingBytes) {
case 0:
break;// No padding needed
case 1:
dos.write((byte) 0);
dos.writeShort((short) 0);
break;// adding 3 byte padding
case 2:
dos.writeShort((short) 0);
break;// adding 2 byte padding
case 3:
dos.write((byte) 0);
break;// adding 1 byte padding
}
} // end try
catch (Exception e) {
System.out.println(e);
}
Expand All @@ -175,13 +206,13 @@ public void unmarshal(DataInputStream dis) {
sampleRate = dis.readInt();
dataLength = (int) dis.readUnsignedShort();
samples = (int) dis.readUnsignedShort();
data = new byte[dataLength];
data = new byte[dataLength / Byte.SIZE];
dis.read(data);
} // end try
} // end try
catch (Exception e) {
System.out.println(e);
}
} // end of unmarshal method
} // end of unmarshal method

/**
* Packs a Pdu into the ByteBuffer.
Expand All @@ -199,9 +230,26 @@ public void marshal(java.nio.ByteBuffer buff) {
buff.putShort((short) encodingScheme);
buff.putShort((short) tdlType);
buff.putInt((int) sampleRate);
buff.putShort((short) data.length);
buff.putShort((short) dataLength);
buff.putShort((short) samples);
buff.put(data);

int nrOfBytes = dataLength / Byte.SIZE;
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 All @@ -222,13 +270,13 @@ public void unmarshal(java.nio.ByteBuffer buff) {
sampleRate = buff.getInt();
dataLength = (int) (buff.getShort() & 0xFFFF);
samples = (int) (buff.getShort() & 0xFFFF);
data = new byte[dataLength];
data = new byte[dataLength / Byte.SIZE];
buff.get(data);
} // end of unmarshal method

} // end of unmarshal method

/*
* The equals method doesn't always work--mostly it works only on classes that consist only of primitives. Be careful.
* The equals method doesn't always work--mostly it works only on classes that
* consist only of primitives. Be careful.
*/
@Override
public boolean equals(Object obj) {
Expand Down
69 changes: 57 additions & 12 deletions src/main/java/edu/nps/moves/dis7/SignalPdu.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SignalPdu extends RadioCommunicationsFamilyPdu implements Serializa
protected long sampleRate;

/**
* length od data
* length of data in bits
*/
protected short dataLength;

Expand Down Expand Up @@ -70,6 +70,19 @@ public int getMarshalledSize() {
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,16 +118,14 @@ public long getSampleRate() {
return sampleRate;
}

/**
* IAW IEEE 1278.1, this field shall specify the number of **bits** of digital
* voice audio or digital data being sent in this Signal PDU.
*/
public short getDataLength() {
return (short) data.length;
return (short) dataLength;
}

/**
* Note that setting this value will not change the marshalled value. The
* list whose length this describes is used for that purpose. The
* getdataLength method will also be based on the actual list length rather
* than this value. The method is simply here for java bean completeness.
*/
public void setDataLength(short pDataLength) {
dataLength = pDataLength;
}
Expand Down Expand Up @@ -142,9 +153,26 @@ public void marshal(DataOutputStream dos) {
dos.writeShort((short) encodingScheme);
dos.writeShort((short) tdlType);
dos.writeInt((int) sampleRate);
dos.writeShort((short) data.length);
dos.writeShort((short) dataLength);
dos.writeShort((short) samples);
dos.write(data);

int nrOfBytes = dataLength / Byte.SIZE;
int paddingBytes = nrOfBytes % 4;// Padding to hit 32 bit boundry
switch (paddingBytes) {
case 0:
break;// No padding needed
case 1:
dos.write((byte) 0);
dos.writeShort((short) 0);
break;// adding 3 byte padding
case 2:
dos.writeShort((short) 0);
break;// adding 2 byte padding
case 3:
dos.write((byte) 0);
break;// adding 1 byte padding
}
} // end try
catch (Exception e) {
System.out.println(e);
Expand All @@ -161,7 +189,7 @@ public void unmarshal(DataInputStream dis) {
sampleRate = dis.readInt();
dataLength = dis.readShort();
samples = dis.readShort();
data = new byte[dataLength];
data = new byte[dataLength / Byte.SIZE];
dis.read(data);
} // end try
catch (Exception e) {
Expand All @@ -184,9 +212,26 @@ public void marshal(java.nio.ByteBuffer buff) {
buff.putShort((short) encodingScheme);
buff.putShort((short) tdlType);
buff.putInt((int) sampleRate);
buff.putShort((short) data.length);
buff.putShort((short) dataLength);
buff.putShort((short) samples);
buff.put(data);

int nrOfBytes = dataLength / Byte.SIZE;
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 All @@ -205,7 +250,7 @@ public void unmarshal(java.nio.ByteBuffer buff) {
sampleRate = buff.getInt();
dataLength = buff.getShort();
samples = buff.getShort();
data = new byte[dataLength];
data = new byte[dataLength / Byte.SIZE];
buff.get(data);
} // end of unmarshal method

Expand Down