Skip to content

Commit

Permalink
write packet to file
Browse files Browse the repository at this point in the history
kaitai-struct poc
  • Loading branch information
henrythasler committed May 5, 2024
1 parent 6687177 commit 877e0da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ top_block.py
*.cu8
*.fc32
*.pyc
*.bin

blocks/gr-bitmap/build/

Expand Down
15 changes: 12 additions & 3 deletions velux/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
terminology according to https://dev.to/stungnet/from-data-to-frame-the-evolution-of-pdus-across-the-osi-model-21gd
"""

OUTPUT_FILENAME = "sample.bin"

# raw bits incl. preamble, SOF and start/stop bit extracted via inspectrum from a transmission captured via rtl_sdr
# bits = bitarray([1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0])
Expand Down Expand Up @@ -44,20 +45,25 @@

# by now it's a packet
packet = frame
print("\r\nPacket: {}".format(packet.tobytes().hex(" ")))

# separate message from meta-information
_, control1, _ = packet.unpack("uint:16, uint:8, bin")
payload_length = control1 & 0x1F

# strip trailing noise
packet = packet[: (3 + payload_length + 2) * 8]

print("\r\nPacket: {}".format(packet.tobytes().hex(" ")))

_, crc16 = packet.unpack("bytes:{}, uintle:16".format(3 + payload_length))
crc_calc = Calculator(Crc16.KERMIT).checksum(packet.tobytes()[2 : 3 + payload_length])
payload = packet[3 * 8 : 3 * 8 + payload_length * 8] # strip SFD and control1
payload = packet[3 * 8 : (3 + payload_length) * 8] # strip SFD and control1
print(
" Payload Length: {} bytes (0x{:02X}, {:08b}b), {} bits)".format(
payload_length, payload_length, payload_length, payload_length * 8
)
)
print(" Control1: 0x{:02x} (0b{:08b})".format(control1 & ~0x1f, control1 & ~0x1f))
print(
" CRC16: {} (0x{:04x})".format(
(
Expand All @@ -69,6 +75,10 @@
)
)

with open(OUTPUT_FILENAME, "wb") as file:
file.write(packet.tobytes())
print("Packet saved to '{}'".format(OUTPUT_FILENAME))

print("\r\nMessage: {}".format(payload.tobytes().hex(" ")))
# deserialize message-content
control2, destination, source, command, payload, counter, mac = payload.unpack(
Expand All @@ -77,7 +87,6 @@
)
)

print(" Control1: 0x{:02x} (0b{:08b})".format(control1 & ~0x1f, control1 & ~0x1f))
print(" Control2: 0x{:02x} (0b{:08b})".format(control2, control2))
print(" Source Node-ID: 0x{:06x}".format(source))
print(" Target Node-ID: 0x{:06x}".format(destination))
Expand Down
40 changes: 40 additions & 0 deletions velux/iohomecontrol.ksy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
meta:
id: iohc
endian: le
seq:
- id: sfd
contents: [0xFF, 0x33]
- id: control1
type: u1
- id: control2
type: u1

instances:
order:
value: (control1 & 0b11000000) >> 6
enum: order
mode:
value: (control1 & 0b00100000) >> 5
enum: mode
payload_length:
value: control1 & 0b00011111
use_beacon:
value: (control2 & 0b10000000) >> 7
routed:
value: (control2 & 0b01000000) >> 6
low_power_mode:
value: (control2 & 0b00100000) >> 5
ack:
value: (control2 & 0b00010000) >> 4
protocol_version:
value: control2 & 0b00000011

enums:
order:
0: single
1: next_in_series
2: next_in_parallel
3: command_group_end
mode:
0: two_way
1: one_way

0 comments on commit 877e0da

Please sign in to comment.