Read the timestamp and version
In the native quickstart, the decoder returns tag-keyed JSON. Read tag 2 as an unsigned 64-bit integer and tag 65 as the packet version. The supplied packet has values 1795433587745520 and 19. Fields are sparse: check for presence before using a value.
Append --fields to the run command from the quickstart. For example, on Linux run ./lib/read example.klv --fields with the C executable. The final line reads 1795433587745520 19.
- C
- C++
- C#
- Go
- Java
- Python
#include <inttypes.h>
#include <stdio.h>
#include "misb0601_typed.h"
static void read_fields(uint8_t *packet, size_t size) {
misb0601_tag_value_result tags = {0};
int rc = Misb0601DecodeTagValues(packet, size, MISB0601_SORT_BY_TAG, &tags);
if (rc == MISB0601_STATUS_OK || rc == MISB0601_STATUS_PARTIAL) {
misb0601_local_set fields;
if (misb0601_local_set_from_tag_values(&tags, &fields)) {
uint64_t timestamp;
uint8_t version;
if (misb0601_local_set_try_get_uint64(&fields, MISB0601_FIELD_PRECISION_TIMESTAMP, ×tamp) &&
misb0601_local_set_try_get_uint8(&fields, MISB0601_FIELD_VERSION, &version))
printf("%" PRIu64 " %u\n", timestamp, (unsigned)version);
misb0601_local_set_free(&fields);
}
}
Misb0601TagValueResultFree(&tags);
}
#include "misb0601_typed.hpp"
#include <iostream>
#include <vector>
static void read_fields(misb0601::Library &lib, const std::vector<uint8_t> &packet) {
misb0601::LocalSet fields(lib.DecodeTagValues(packet.data(), packet.size()));
auto timestamp = fields.UInt64(MISB0601_FIELD_PRECISION_TIMESTAMP);
auto version = fields.UInt8(MISB0601_FIELD_VERSION);
if (timestamp && version)
std::cout << *timestamp << ' ' << unsigned(*version) << '\n';
}
using LibMisb.Misb0601;
static class Fields
{
internal static void Read(Misb0601Decoder decoder, byte[] packet)
{
var fields = decoder.DecodeLocalSet(packet);
if (fields.TryGetUInt64(Misb0601Field.PrecisionTimestamp, out var timestamp) &&
fields.TryGetUInt8(Misb0601Field.Version, out var version))
Console.WriteLine($"{timestamp} {version}");
}
}
package main
import (
"fmt"
sdk "github.com/impleotv/libmisb-sdk-go/misb0601abi"
)
func readFields(decoder *sdk.Decoder, packet []byte) {
fields, err := decoder.DecodeLocalSet(packet)
must(err)
timestamp, hasTime := fields.UInt64(2)
version, hasVersion := fields.UInt8(65)
if hasTime && hasVersion {
fmt.Println(timestamp, version)
}
}
import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.nio.ByteOrder;
import static java.lang.foreign.MemoryLayout.PathElement.groupElement;
import static java.lang.foreign.ValueLayout.*;
class Fields {
// misb0601_tag_value and misb0601_tag_value_result from misb0601.h (64-bit ABI).
private static final MemoryLayout ITEM = MemoryLayout.structLayout(
JAVA_BYTE.withName("tag"), MemoryLayout.paddingLayout(7),
JAVA_LONG.withName("value_offset"), JAVA_LONG.withName("value_len"));
static final MemoryLayout RESULT = MemoryLayout.structLayout(
JAVA_INT.withName("revision"), MemoryLayout.paddingLayout(4), DecodePacket.BUFFER.withName("payload"),
ADDRESS.withName("items"), JAVA_LONG.withName("item_count"));
static void read(DecodePacket api, Arena arena, MemorySegment packet) throws Throwable {
MethodHandle decode = api.bind("Misb0601DecodeTagValues",
FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_LONG, JAVA_INT, ADDRESS));
MethodHandle free = api.bind("Misb0601TagValueResultFree", FunctionDescriptor.ofVoid(ADDRESS));
MemorySegment result = arena.allocate(RESULT);
try {
api.checkDecode(arena, (int) decode.invokeExact(packet, packet.byteSize(), 1, result));
MemorySegment payload = DecodePacket.data(result.asSlice(RESULT.byteOffset(groupElement("payload")), DecodePacket.BUFFER));
long count = result.get(JAVA_LONG, RESULT.byteOffset(groupElement("item_count")));
MemorySegment items = result.get(ADDRESS, RESULT.byteOffset(groupElement("items")))
.reinterpret(Math.multiplyExact(count, ITEM.byteSize()));
String timestamp = null;
Integer version = null;
for (long i = 0; i < count; i++) {
MemorySegment item = items.asSlice(i * ITEM.byteSize(), ITEM);
int tag = Byte.toUnsignedInt(item.get(JAVA_BYTE, ITEM.byteOffset(groupElement("tag"))));
long offset = item.get(JAVA_LONG, ITEM.byteOffset(groupElement("value_offset")));
long length = item.get(JAVA_LONG, ITEM.byteOffset(groupElement("value_len")));
MemorySegment value = payload.asSlice(offset, length);
if (tag == 2 && length == Long.BYTES) {
long bits = value.get(JAVA_LONG_UNALIGNED.withOrder(ByteOrder.BIG_ENDIAN), 0);
timestamp = Long.toUnsignedString(bits);
} else if (tag == 65 && length == 1) {
version = Byte.toUnsignedInt(value.get(JAVA_BYTE, 0));
}
}
if (timestamp != null && version != null) System.out.println(timestamp + " " + version);
} finally {
free.invokeExact(result);
}
}
}
def read_fields(decoder, packet):
fields = decoder.decode_local_set(packet)
timestamp = fields.try_get_uint64(2)
version = fields.try_get_uint8(65)
if timestamp is not None and version is not None:
print(timestamp, version)
Browser and Node.js
When you already have the packet bytes and an open web decoder from the web quickstart, read the version from the parsed object. Store the string containing the timestamp before crossing a JavaScript Number boundary:
const exact = decoder.requestJSON(packet);
const {status, value} = JSON.parse(exact);
if (status === 'ok') {
console.log(value.Version); // 19
console.log(exact); // contains the full integer 1795433587745520
}
Next, encode a packet and inspect its validation report with the native SDK. For JavaScript, keep the exact JSON envelope when exchanging the timestamp. See revisions, JSON, and partial results.