Skip to main content

Encode a packet and inspect a validation finding

Native SDK. Append --roundtrip example.json to your language's run command from the SDK root. From examples/go or examples/csharp, use ../../example.json. For setup and build commands, see Decode your first native packet.

For example, from examples/go on Linux run go run . ../../lib/misb0601.so ../../example.klv --roundtrip ../../example.json. From examples/csharp on Windows run dotnet run -- ../../lib/misb0601.dll ../../example.klv --roundtrip ../../example.json.

Encode the sample fields​

The bundled example.json contains the fields from example.klv, with the precision timestamp as an integer and the scaled values at full precision. The examples pass those JSON bytes to the revision 19 encoder. It sorts the tags and calculates a new checksum.

example.json
{
"2": 1795433587745520,
"65": 19,
"3": "Test mission",
"4": "24",
"13": 41.093374323609,
"14": -104.8687724978052,
"15": 2899.9328603036543,
"5": 147.99908445868618,
"10": "Test platform",
"1": 26780
}

The example driver calls the helper below when you supply the flag.

examples/c/roundtrip.h
#pragma once
#include "misb0601.h"
#include <stdio.h>

static int roundtrip(uint8_t *input, size_t input_size) {
misb0601_buffer packet = {0}, fields = {0}, report = {0};

// Encode the tag-keyed JSON as a revision 19 KLV packet with a new checksum.
int ok = Misb0601EncodeJSON(input, input_size, MISB0601_REVISION_19,
MISB0601_SORT_BY_TAG, &packet) == MISB0601_STATUS_OK;

// Decode the complete output buffer to inspect the encoded fields.
if (ok) ok = Misb0601DecodeJSON(packet.data, packet.len, MISB0601_SORT_BY_TAG,
MISB0601_JSON_KEYS_TAGS, MISB0601_JSON_COMPACT, MISB0601_TIMESTAMPS_RAW, &fields) == MISB0601_STATUS_OK;

// Omit the last byte to get a packet-decode finding in the validation report.
if (ok) ok = Misb0601ValidatePacket(packet.data, packet.len - 1, NULL, &report) == MISB0601_STATUS_OK;
if (ok) {
fwrite(fields.data, 1, fields.len, stdout);
putchar('\n');
fwrite(report.data, 1, report.len, stdout);
putchar('\n');
}
// Release all SDK-owned output buffers, including after a failed call.
Misb0601BufferFree(&report);
Misb0601BufferFree(&fields);
Misb0601BufferFree(&packet);
return ok;
}

Inspect the encoded buffer​

The decoder walkthrough shows the original 87-byte packet. Its BER length is 46 in hex (70 value bytes), and its checksum ends in 68 9C.

Hex dump of the ST 0601 sample packet, from the 16-byte key through the checksum 68 9C
Open the full-size hex dump

In demo mode, the encoder omits sensor true altitude (tag 15). The resulting 83-byte buffer has BER length 42 (66 value bytes) and checksum 92 4A (37450). With a licensed encoder, the buffer includes altitude, has 87 bytes, and ends in D6 2E (54830) in sorted order.

Encoded packet in demo mode
0000 06 0E 2B 34 02 0B 01 01 0E 01 03 01 01 00 00 00
0010 42 02 08 00 06 60 EF F5 37 22 F0 03 0C 54 65 73
0020 74 20 6D 69 73 73 69 6F 6E 04 02 32 34 05 02 69
0030 3E 0A 0D 54 65 73 74 20 70 6C 61 74 66 6F 72 6D
0040 0D 04 3A 71 A4 18 0E 04 B5 6D 38 FB 41 01 13 01
0050 02 92 4A

The examples decode the complete output buffer and print its fields. Look for "3":"Test mission", timestamp 1795433587745520, and revision 19.

Inspect a validation finding​

The examples pass the encoded buffer to the validator with the last byte omitted. The packet still declares its full value length, so the validator reports a truncated packet. Pass the complete buffer when validating application data.

The report has valid: false. The first finding has rule ID packet-decode, severity error, and a message identifying the packet decode failure. The C, C++, and Java examples print the JSON report. The C#, Go, and Python examples print the verdict followed by each finding. A successful validation call means a report was produced. Use its valid field to decide whether to accept the packet.