LibMISB - Examples - Example Python Application

From RidgeRun Developer Wiki

Follow Us On Twitter LinkedIn Email Share this page




Previous: Examples/Example_Application Index Next: Evaluating





Introduction

LibMISB includes Python bindings when you build it with -Dpython=enabled (see How to Compile). These bindings expose the same API as the C library. In the sections that follow, you’ll find some usage examples.

Meta Encoding from JSON

This example shows you how to encode metadata using a JSON formatted string as the input.

# Copyright (C) 2025 RidgeRun, LLC (http://www.ridgerun.com)
# All Rights Reserved.
#
# The contents of this software are proprietary and confidential to RidgeRun,
# LLC.  No part of this program may be photocopied, reproduced or translated
# into another programming language without prior written consent of
# RidgeRun, LLC.  The user is free to modify the source code after obtaining
# a software license from RidgeRun.  All source code changes must be provided
# back to RidgeRun without any encumbrance.


import json
from libmisb import LibMisb, LogLevel
from libmisb.formatter import JsonFormatter

data = { 
    "key": "060E2B34020B01010E01030101000000",
    "items": [
        { "tag": "2",   "value": "Oct. 24, 2008. 00:13:29.913" },
        { "tag": "3",   "value": "MISSION01" },
        { "tag": "4",   "value": "AF-101" },
        { "tag": "5",   "value": "159.97436" },
        { "tag": "15",  "value": "14190.7195" },
        { 
            "tag": "48", 
            "value": [
                { "tag": "1",  "value": "1" },
                { "tag": "2",  "value": "1" },
                { "tag": "3",  "value": "//USA" },
                { "tag": "4",  "value": "TS/SI/CompartmentA//" },
                { "tag": "5",  "value": "FOUO" },
                { "tag": "6",  "value": "506 82 81" },
                { "tag": "12", "value": "1" },
                { "tag": "13", "value": "USA" },
                { "tag": "22", "value": "10" }
            ]
        },
        { 
            "tag": "74", 
            "value": [
                { "tag": "2",  "value": "Apr. 19, 2001. 04:25:21.000" },
                { 
                    "tag": "101", 
                    "value": [
                        { "tag": "targetId", "value": "1234" },
                        { "tag": "1",        "value": "409600" },
                        { "tag": "3",        "value": "409600" },
                        { "tag": "5",        "value": "80" },
                        { 
                            "tag": "102", 
                            "value": [
                                { "tag": "1", "value": "Aircraft" },
                                { "tag": "2", "value": "Aircraft" }
                            ]
                        }
                    ]
                },
                { "tag": "4", "value": "6" },
                { "tag": "6", "value": "14" },
                { "tag": "7", "value": "78000" },
                { "tag": "8", "value": "1280" },
                { "tag": "9", "value": "720" }
            ]
        },
        { "tag": "94",  "value": "0170:F592-F023-7336-4AF8-AA91-62C0-0F2E-B2DA/16B7-4341-0008-41A0-BE36-5B5A-B96A-3645:D3" },
        { "tag": "96",  "value": "13,898.5463" },
        { "tag": "104", "value": "34,567.35" }
    ]
}

original_json = json.dumps(data, separators=(',', ':'))

lm = LibMisb()
lm.set_formatter(JsonFormatter())
lm.set_log_level(LogLevel.INFO)

klv_bytes = lm.encode(original_json)
print("Encoded KLV (hex):", klv_bytes.hex())

Encoding from Items

If you don't want to pass the full list of tags as a JSON string, you can also create and add individual items as shown in the following example:

# Copyright (C) 2025 RidgeRun, LLC (http://www.ridgerun.com)
# All Rights Reserved.
#
# The contents of this software are proprietary and confidential to RidgeRun,
# LLC.  No part of this program may be photocopied, reproduced or translated
# into another programming language without prior written consent of
# RidgeRun, LLC.  The user is free to modify the source code after obtaining
# a software license from RidgeRun.  All source code changes must be provided
# back to RidgeRun without any encumbrance.


import json
from libmisb import LibMisb, Metadata, metadata_item, LogLevel
from libmisb.formatter import JsonFormatter

raw = {
    "key": "060E2B34020B01010E01030101000000",
    "items": [
        { "tag": "2",  "value": "Oct. 24, 2008. 00:13:29.913" },
        { "tag": "3",  "value": "MISSION01"             },
        { "tag": "4",  "value": "AF-101"                 },
        { "tag": "5",  "value": "159.97436"              },
        { "tag": "15", "value": "14190.7195"             }
    ]
}

def build_items(lst):
    out = []
    for entry in lst:
        mi = metadata_item()
        mi.tag       = entry["tag"]
        mi.value     = entry["value"]
        mi.sub_items = []          # no nested items here
        mi.local_set = True
        out.append(mi)
    return out

# Build and populate Metadata
meta = Metadata()
meta.set_key(raw["key"])
meta.set_items(build_items(raw["items"]))

lm = LibMisb()
lm.set_formatter(JsonFormatter())
lm.set_log_level(LogLevel.INFO)

original_json = json.dumps(raw, separators=(',', ':'))

packet, status = lm.encode(meta)
if status != 0:
    raise RuntimeError(f"Encode failed with status {status}")

print("Encoded KLV (hex):", packet.hex())

Metadata Decoding

The following example shows you how to decode MISB-encoded metadata into a JSON string.

# Copyright (C) 2025 RidgeRun, LLC (http://www.ridgerun.com)
# All Rights Reserved.
#
# The contents of this software are proprietary and confidential to RidgeRun,
# LLC.  No part of this program may be photocopied, reproduced or translated
# into another programming language without prior written consent of
# RidgeRun, LLC.  The user is free to modify the source code after obtaining
# a software license from RidgeRun.  All source code changes must be provided
# back to RidgeRun without any encumbrance.


from libmisb import LibMisb, LogLevel
from libmisb.formatter import JsonFormatter
from binascii import unhexlify
import json

# Your hex‐encoded MISB KLV packet
hex_data = (
    "060e2b34020b01010e01030101000000"
    "2c0208000459f9ae2022a803094d4953"
    "53494f4e3031040641462d3130310502"
    "71c20f02c2214101130102a67d"
)

# Convert hex string to raw bytes
packet_bytes = unhexlify(hex_data)

lm = LibMisb()
lm.set_formatter(JsonFormatter())
lm.set_log_level(LogLevel.INFO)

# Decode the packet back into a JSON‐style string
decoded_str = lm.decode(packet_bytes)
print("Decoded string:")
print(decoded_str)

# Show tags
print("-------------------------------------------------------")
print("Tags and values:")
print("-------------------------------------------------------")
print(lm.get_tags_names(decoded_str))


Previous: Examples/Example_Application Index Next: Evaluating