Search in sources :

Example 1 with KlvLocalSet

use of org.codice.ddf.libs.klv.data.set.KlvLocalSet in project alliance by codice.

the class MetadataPacketTest method testThatPesHeadersWithOptionalFieldsAreHandled.

/**
 * The binary data in this test was captured from a data stream where the PES header contains an
 * optional timestamp.
 */
@Test
public void testThatPesHeadersWithOptionalFieldsAreHandled() throws KlvDecodingException {
    byte[] packet = new byte[] { 0x00, 0x00, 0x01, (byte) 0xbd, 0x01, 0x08, (byte) 0x84, (byte) 0x80, 0x05, 0x21, 0x00, 0x09, 0x41, (byte) 0xd9, 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x0b, 0x01, 0x01, 0x0e, 0x01, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00, (byte) 0x82, 0x00, (byte) 0xed, 0x02, 0x08, 0x00, 0x04, (byte) 0xe4, (byte) 0xea, (byte) 0xad, 0x53, (byte) 0xfb, 0x48, 0x03, 0x05, 0x30, 0x30, 0x30, 0x30, 0x31, 0x04, 0x03, 0x30, 0x30, 0x31, 0x05, 0x02, 0x5a, (byte) 0xb6, 0x06, 0x02, 0x00, 0x00, 0x07, 0x02, 0x00, 0x00, 0x0a, 0x08, 0x56, 0x52, 0x53, 0x47, 0x20, 0x35, 0x2e, 0x36, 0x0b, 0x18, 0x56, 0x52, 0x53, 0x47, 0x20, 0x56, 0x35, 0x2e, 0x36, 0x2e, 0x37, 0x32, 0x20, 0x41, 0x70, 0x72, 0x20, 0x20, 0x37, 0x20, 0x32, 0x30, 0x31, 0x31, 0x0c, 0x0e, 0x47, 0x65, 0x6f, 0x64, 0x65, 0x74, 0x69, 0x63, 0x20, 0x57, 0x47, 0x53, 0x38, 0x34, 0x0d, 0x04, 0x30, (byte) 0xf7, 0x04, (byte) 0xea, 0x0e, 0x04, 0x31, 0x2d, (byte) 0xb4, 0x76, 0x0f, 0x02, 0x3f, 0x08, 0x10, 0x02, 0x03, (byte) 0x89, 0x11, 0x02, 0x02, (byte) 0xa7, 0x12, 0x04, 0x06, (byte) 0xfd, 0x6b, (byte) 0xff, 0x13, 0x04, (byte) 0xd7, (byte) 0xc3, 0x3c, (byte) 0xce, 0x14, 0x04, 0x00, 0x00, 0x00, 0x00, 0x15, 0x04, 0x00, 0x21, (byte) 0xac, 0x7e, 0x16, 0x02, 0x02, (byte) 0xdb, 0x17, 0x04, 0x30, (byte) 0xf3, (byte) 0x98, 0x3a, 0x18, 0x04, 0x31, 0x2f, (byte) 0x99, 0x59, 0x19, 0x02, 0x23, 0x72, 0x1a, 0x02, (byte) 0xff, (byte) 0xfe, 0x1b, 0x02, 0x01, 0x6e, 0x1c, 0x02, (byte) 0xfe, (byte) 0xc7, 0x1d, 0x02, (byte) 0xff, (byte) 0xee, 0x1e, 0x02, (byte) 0xff, (byte) 0xf8, 0x1f, 0x02, (byte) 0xfe, (byte) 0xa6, 0x20, 0x02, 0x01, 0x22, 0x21, 0x02, 0x00, 0x22, 0x30, 0x20, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x03, 0x04, 0x2f, 0x2f, 0x55, 0x53, 0x06, 0x05, 0x43, 0x41, 0x20, 0x55, 0x53, 0x0c, 0x01, 0x01, 0x0d, 0x04, 0x2f, 0x2f, 0x55, 0x53, 0x16, 0x02, 0x00, 0x07, 0x41, 0x01, 0x01, 0x48, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, (byte) 0xb7, (byte) 0x8d };
    final MPSDemuxer.PESPacket pesHeader = MPSUtils.readPESHeader(ByteBuffer.wrap(packet), 0);
    AsynchronousMetadataPacket asynchronousMetadataPacket = new AsynchronousMetadataPacket(packet, pesHeader, new KlvDecoder(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET_CONTEXT));
    DecodedKLVMetadataPacket p = asynchronousMetadataPacket.decodeKLV();
    assertThat(p, notNullValue());
    KlvLocalSet e = (KlvLocalSet) p.getDecodedKLV().getDataElementByName(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET);
    KlvDataElement timestamp = e.getValue().getDataElementByName(Stanag4609TransportStreamParser.TIMESTAMP);
    assertThat(timestamp.getValue(), is(1377596488285000L));
}
Also used : KlvLocalSet(org.codice.ddf.libs.klv.data.set.KlvLocalSet) KlvDecoder(org.codice.ddf.libs.klv.KlvDecoder) KlvDataElement(org.codice.ddf.libs.klv.KlvDataElement) MPSDemuxer(org.jcodec.containers.mps.MPSDemuxer) Test(org.junit.Test)

Example 2 with KlvLocalSet

use of org.codice.ddf.libs.klv.data.set.KlvLocalSet in project alliance by codice.

the class MetadataPacketTest method verifyDecodedKLV.

private void verifyDecodedKLV(final DecodedKLVMetadataPacket decodedPacket) {
    assertThat(decodedPacket, notNullValue());
    assertThat(decodedPacket.getDecodedKLV().hasDataElement(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET), is(true));
    final KlvContext localSetContext = ((KlvLocalSet) decodedPacket.getDecodedKLV().getDataElementByName(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET)).getValue();
    assertThat(((KlvUnsignedShort) localSetContext.getDataElementByName(Stanag4609TransportStreamParser.CHECKSUM)).getValue(), is(19537));
}
Also used : KlvLocalSet(org.codice.ddf.libs.klv.data.set.KlvLocalSet) KlvContext(org.codice.ddf.libs.klv.KlvContext)

Example 3 with KlvLocalSet

use of org.codice.ddf.libs.klv.data.set.KlvLocalSet in project alliance by codice.

the class AbstractMetadataPacket method validateChecksum.

private boolean validateChecksum(final KlvContext klvContext, final byte[] klvBytes) throws KlvDecodingException {
    if (!klvContext.hasDataElement(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET)) {
        throw new KlvDecodingException("KLV did not contain the UAS Datalink Local Set");
    }
    final KlvContext localSetContext = ((KlvLocalSet) klvContext.getDataElementByName(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET)).getValue();
    if (localSetContext.hasDataElement(Stanag4609TransportStreamParser.CHECKSUM)) {
        final int packetChecksum = ((KlvUnsignedShort) localSetContext.getDataElementByName(Stanag4609TransportStreamParser.CHECKSUM)).getValue();
        short calculatedChecksum = 0;
        // checksum length (the checksum value is 2 bytes, which is why we subtract 2).
        for (int i = 0; i < klvBytes.length - 2; ++i) {
            calculatedChecksum += (klvBytes[i] & 0xFF) << (8 * ((i + 1) % 2));
        }
        return (calculatedChecksum & 0xFFFF) == packetChecksum;
    }
    throw new KlvDecodingException("Decoded KLV packet didn't contain checksum (which is required).");
}
Also used : KlvLocalSet(org.codice.ddf.libs.klv.data.set.KlvLocalSet) KlvUnsignedShort(org.codice.ddf.libs.klv.data.numerical.KlvUnsignedShort) KlvContext(org.codice.ddf.libs.klv.KlvContext) KlvDecodingException(org.codice.ddf.libs.klv.KlvDecodingException)

Example 4 with KlvLocalSet

use of org.codice.ddf.libs.klv.data.set.KlvLocalSet in project alliance by codice.

the class Stanag4609TransportStreamParserTest method verifyDecodedMetadataPacket.

private void verifyDecodedMetadataPacket(final DecodedKLVMetadataPacket packet) {
    final KlvContext outerContext = packet.getDecodedKLV();
    assertThat(outerContext.getDataElements().size(), is(1));
    assertThat(outerContext.hasDataElement(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET), is(true));
    final KlvContext localSetContext = ((KlvLocalSet) outerContext.getDataElementByName(Stanag4609TransportStreamParser.UAS_DATALINK_LOCAL_SET)).getValue();
    final Map<String, KlvDataElement> localSetDataElements = localSetContext.getDataElements();
    assertThat(localSetDataElements.size(), is(EXPECTED_VALUES.size()));
    localSetDataElements.forEach((name, dataElement) -> {
        final Object expectedValue = EXPECTED_VALUES.get(name);
        final Object actualValue = dataElement.getValue();
        if (actualValue instanceof Double) {
            assertThat(String.format("%s is not close to %s", name, expectedValue), (Double) actualValue, is(closeTo((Double) expectedValue, 1e-6)));
        } else {
            assertThat(String.format("%s is not %s", name, expectedValue), actualValue, is(expectedValue));
        }
    });
}
Also used : KlvLocalSet(org.codice.ddf.libs.klv.data.set.KlvLocalSet) KlvContext(org.codice.ddf.libs.klv.KlvContext) KlvDataElement(org.codice.ddf.libs.klv.KlvDataElement)

Example 5 with KlvLocalSet

use of org.codice.ddf.libs.klv.data.set.KlvLocalSet in project ddf by codice.

the class KlvDecoderTest method testKLVSet.

@Test
public void testKLVSet() throws Exception {
    byte[] klvBytes;
    try (final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("testKLV.klv")) {
        klvBytes = IOUtils.toByteArray(inputStream);
    }
    final KlvContext klvContext = getKLVContext(DATA_ELEMENTS);
    final Map<String, KlvDataElement> decodedDataElements = new KlvDecoder(klvContext).decode(klvBytes).getDataElements();
    assertThat(decodedDataElements.size(), is(1));
    assertThat(decodedDataElements, hasKey(UAS_DATALINK_LOCAL_SET_UNIVERSAL_KEY));
    final KlvContext localSet = ((KlvLocalSet) decodedDataElements.get(UAS_DATALINK_LOCAL_SET_UNIVERSAL_KEY)).getValue();
    final Map<String, KlvDataElement> localSetDataElements = localSet.getDataElements();
    assertThat(localSetDataElements.size(), is(DATA_ELEMENTS.size()));
    localSetDataElements.forEach((name, dataElement) -> {
        final Object expectedValue = EXPECTED_VALUES.get(name);
        assertThat(String.format("%s is not %s", name, expectedValue), dataElement.getValue(), is(expectedValue));
    });
}
Also used : KlvLocalSet(org.codice.ddf.libs.klv.data.set.KlvLocalSet) InputStream(java.io.InputStream) KlvString(org.codice.ddf.libs.klv.data.text.KlvString) Test(org.junit.Test)

Aggregations

KlvLocalSet (org.codice.ddf.libs.klv.data.set.KlvLocalSet)6 KlvContext (org.codice.ddf.libs.klv.KlvContext)3 KlvDataElement (org.codice.ddf.libs.klv.KlvDataElement)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 KlvDecoder (org.codice.ddf.libs.klv.KlvDecoder)1 KlvDecodingException (org.codice.ddf.libs.klv.KlvDecodingException)1 KlvUnsignedShort (org.codice.ddf.libs.klv.data.numerical.KlvUnsignedShort)1 KlvString (org.codice.ddf.libs.klv.data.text.KlvString)1 MPSDemuxer (org.jcodec.containers.mps.MPSDemuxer)1