Search in sources :

Example 6 with KlvContext

use of org.codice.ddf.libs.klv.KlvContext in project alliance by codice.

the class Stanag4609ProcessorImplTest method testHandleWithKlvContext.

@Test
public void testHandleWithKlvContext() throws KlvDecodingException {
    KlvContext klvContext = new KlvContext(Klv.KeyLength.OneByte, Klv.LengthEncoding.OneByte, Collections.singleton(klvIntegerEncodedFloatingPoint));
    stanag4609Processor.handle(Collections.singletonMap(FIELD_NAME, klvHandler), defaultKlvHandler, klvContext, dataElements);
    verify(klvHandler, atLeastOnce()).accept(klvIntegerEncodedFloatingPoint);
}
Also used : KlvContext(org.codice.ddf.libs.klv.KlvContext) Test(org.junit.Test)

Example 7 with KlvContext

use of org.codice.ddf.libs.klv.KlvContext 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 8 with KlvContext

use of org.codice.ddf.libs.klv.KlvContext 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)

Aggregations

KlvContext (org.codice.ddf.libs.klv.KlvContext)8 KlvLocalSet (org.codice.ddf.libs.klv.data.set.KlvLocalSet)3 Test (org.junit.Test)3 KlvDataElement (org.codice.ddf.libs.klv.KlvDataElement)2 List (java.util.List)1 DecodedKLVMetadataPacket (org.codice.alliance.libs.stanag4609.DecodedKLVMetadataPacket)1 KlvDecoder (org.codice.ddf.libs.klv.KlvDecoder)1 KlvDecodingException (org.codice.ddf.libs.klv.KlvDecodingException)1 KlvInt (org.codice.ddf.libs.klv.data.numerical.KlvInt)1 KlvIntegerEncodedFloatingPoint (org.codice.ddf.libs.klv.data.numerical.KlvIntegerEncodedFloatingPoint)1 KlvUnsignedShort (org.codice.ddf.libs.klv.data.numerical.KlvUnsignedShort)1