File: csp_zmq_pub.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 (47 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

import pmt
from gnuradio import gr
import zmq


class csp_zmq_pub(gr.sync_block):
    """CSP ZMQ PUB block"""
    def __init__(self, address):
        gr.sync_block.__init__(
            self,
            name='csp_zmq_pub',
            in_sig=[],
            out_sig=[]
        )

        ctx = zmq.Context()
        self.socket = ctx.socket(zmq.PUB)
        self.socket.connect(address)

        self.message_port_register_in(pmt.intern('in'))
        self.set_msg_handler(pmt.intern('in'), self.handle_msg)

    def handle_msg(self, msg_pmt):
        msg = pmt.cdr(msg_pmt)
        if not pmt.is_u8vector(msg):
            print('[ERROR] received invalid message type. Expected u8vector')
            return
        msg = bytes(pmt.u8vector_elements(msg))
        if len(msg) < 4:
            print('[csp_zmq_pub] message too short; dropping')
        csp_header = msg[:4]
        payload = msg[4:]
        # extract destination from CSP header
        dest = ((csp_header[0] & 1) << 4) | (csp_header[1] >> 4)
        # byte-swap csp_header, since CSP ZMQ uses headers in the opposite
        # endianness compared to over-the-air
        self.socket.send(bytes([dest]) + csp_header[::-1] + payload)