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);
}
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).");
}
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));
}
});
}
Aggregations