File: qa_hdlc.py

package info (click to toggle)
gr-satellites 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,836 kB
  • sloc: python: 29,546; cpp: 5,448; ansic: 1,247; sh: 118; makefile: 24
file content (70 lines) | stat: -rwxr-xr-x 2,067 bytes parent folder | download | duplicates (2)
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
69
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright 2020 Daniel Estevez <daniel@destevez.net>
#
# This file is part of gr-satellites
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

from gnuradio import gr, blocks, gr_unittest
import numpy as np
import pmt

# bootstrap satellites module, even from build dir
try:
    import python as satellites
except ImportError:
    pass
else:
    import sys
    sys.modules['satellites'] = satellites

from satellites import hdlc_framer, hdlc_deframer
from satellites.grtypes import byte_t


class qa_hdlc(gr_unittest.TestCase):
    def setUp(self):
        self.tb = gr.top_block()

    def tearDown(self):
        self.tb = None

    def test_framer_deframer(self):
        """Connects an HDLC framer to a deframer and sends PDUs through"""
        framer = hdlc_framer(100, 20)
        deframer = hdlc_deframer(True, 10000)
        pdu2tag = blocks.pdu_to_tagged_stream(byte_t)
        dbg = blocks.message_debug()

        self.tb.connect(pdu2tag, deframer)
        self.tb.msg_connect((framer, 'out'), (pdu2tag, 'pdus'))
        self.tb.msg_connect((deframer, 'out'), (dbg, 'store'))

        test_size = 150
        test_number_frames = 7
        test_data = [bytes(np.random.randint(0, 256, test_size, dtype='uint8'))
                     for _ in range(test_number_frames)]
        for td in test_data:
            test_frame = pmt.cons(pmt.PMT_NIL,
                                  pmt.init_u8vector(test_size, list(td)))
            framer.to_basic_block()._post(pmt.intern('in'), test_frame)
        framer.to_basic_block()._post(
            pmt.intern('system'),
            pmt.cons(pmt.intern('done'), pmt.from_long(1)))

        self.tb.start()
        self.tb.wait()

        for j, td in enumerate(test_data):
            result_data = bytes(
                pmt.u8vector_elements(pmt.cdr(dbg.get_message(j))))
            self.assertEqual(
                td, result_data,
                'HDLC deframer output does not match expected frame')


if __name__ == '__main__':
    gr_unittest.run(qa_hdlc)