Small example to write a frame decoder using kaitai structs

A minimal satnogs-decoders example

This example should eventually move to the wiki (but I didn’t figure out how to use formatted code listings in the wiki yet.

The protocol


source: Cubesat Space Protocol - Wikipedia

An example frame

0x00 0xAB 0x92 0x48

source: data_592806_2019-04-14T09-50-54 from observation 592806 (only the first four bytes)

A Python implementation of the parser

import struct

# Read the frame as binary file
with open("contrib/aausat4_20190414_095054_raw", 'rb') as f:
    frame = f.read()

# Unpack the header and parse the fields using bit-shifts & -masks
header=struct.unpack("<I", frame[0:4])[0]

src = ((header >> 25) & 0x1f)
dest = ((header >> 20) & 0x1f)
dest_port = ((header >> 14) & 0x3f)
src_port = ((header >> 8) & 0x3f)

print(src, dest, dest_port, src_port)

# Output: 4, 9, 10, 43

source: aausat4_beacon_parser/parser.py at master · aausat/aausat4_beacon_parser · GitHub

The Kaitai struct implementation

meta:
  id: aausat4
  file-extension: aausat4
  endian: le

seq:
  - id: aausat4_frame_raw
    type: aausat4_frame_raw
    doc-ref: 'https://github.com/aausat/aausat4_beacon_parser/blob/master/parser.py'

types:
  aausat4_frame_raw:
    seq:
      - id: csp_header
        type: csp_header
      - id: data
        type: data

  csp_header:
    seq:
      - id: header
        type: u4
    instances:
      src:
        value: ((header >> 25) & 0x1f)
      dest:
        value: ((header >> 20) & 0x1f)
      dest_port:
        value: ((header >> 14) & 0x3f)
      src_port:
        value: ((header >> 8) & 0x3f)
  data:
    seq:
      - id: data_raw
        size-eos: true

Example WebIDE output

edit:

  • 2019-04-24: Fixed broken hyperlink to the example frame.
7 Likes

Awesome <3 However, the https://gitlab.com/nickoe/satnogs-decoders/blob/aausat4/contrib/aausat4_20190414_095054_raw is broken?

Hm, what are you looking for?

@astrojuanlu you could try some of the frames submitted to the db, for example https://network.satnogs.org/media/data_obs/601711/data_601711_2019-04-18T23-29-51

@astrojuanlu: Thanks for pointing this out! The exact frame I referenced in the original post can also be found in satnogs-network directly, I updated the post accordingly.

As @nickoe & @DL4PD already finished writing the kaitai structure for AAUSAT-4 (btw, yay, congrats!) their dev branch I linked (which contained the example frame) will probably be deleted in the near future anyway.

1 Like