1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2017 Daniel Estevez <daniel@destevez.net>
#
# This file is part of gr-satellites
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
import collections
from gnuradio import gr
import numpy
import pmt
from . import crc, hdlc
class hdlc_framer(gr.basic_block):
"""docstring for block hdlc_framer"""
def __init__(self, preamble_bytes, postamble_bytes):
gr.basic_block.__init__(
self,
name='hdlc_framer',
in_sig=None,
out_sig=None)
self.crc_calc = crc(16, 0x1021, 0xFFFF, 0xFFFF, True, True)
self.preamble_bytes = preamble_bytes
self.postamble_bytes = postamble_bytes
self.message_port_register_in(pmt.intern('in'))
self.set_msg_handler(pmt.intern('in'), self.handle_msg)
self.message_port_register_out(pmt.intern('out'))
def handle_msg(self, msg_pmt):
meta = pmt.car(msg_pmt)
msg = pmt.cdr(msg_pmt)
if not pmt.is_u8vector(msg):
print('[ERROR] Received invalid message type. Expected u8vector')
return
data = list(pmt.u8vector_elements(msg))
crc_val = self.crc_calc.compute(data)
data.append(crc_val & 0xff)
data.append((crc_val >> 8) & 0xff)
buff = list(hdlc.flag * self.preamble_bytes)
ones = 0 # number of consecutive ones
for byte in data:
for _ in range(8):
# Transmit byte LSB first
x = byte & 1
buff.append(x)
if x:
ones += 1
else:
ones = 0
if ones == 5:
# Bit-stuff
buff.append(0)
ones = 0
byte >>= 1
buff.extend(hdlc.flag * self.postamble_bytes)
self.message_port_pub(
pmt.intern('out'),
pmt.cons(meta, pmt.init_u8vector(len(buff), buff)))
|