blob: 6178c64b6ab3648af9e6030d944302f3cb5983af (
plain)
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
71
|
#!/usr/bin/env python
#
# Copyright 2020 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#
from gnuradio import gr
import pmt
class meta_to_pair(gr.sync_block):
"""
This block converts a metadata dictionary item to a pmt pair that is
compatible with other blocks expecting a pair in. You can specify
which item in the incoming metadata to output as a pair and what
the pair name is.
"""
def __init__(self, incomingKeyName, outgoingPairName):
gr.sync_block.__init__(self, name="meta_to_pair",
in_sig=None, out_sig=None)
self.incomingKeyName = str(incomingKeyName)
self.outgoingPairName = str(outgoingPairName)
self.message_port_register_in(pmt.intern("inmeta"))
self.set_msg_handler(pmt.intern("inmeta"), self.msg_handler)
self.message_port_register_out(pmt.intern("outpair"))
def msg_handler(self, msg):
if not pmt.is_pair(msg):
gr.log.warn("Incoming message is not a pair. Only pairs are supported. "
"No message generated.")
return
meta = pmt.to_python(pmt.car(msg))
if not type(meta) is dict:
gr.log.warn("Incoming message does not contain a dictionary. "
"No message generated.")
return
if not self.incomingKeyName in meta:
gr.log.warn("Incoming message dictionary does not contain key %s. "
"No message generated." % self.incomingKeyName)
return
incomingVal = meta[self.incomingKeyName]
new_pair = None
try:
new_pair = pmt.cons(pmt.intern(
self.outgoingPairName), pmt.to_pmt(incomingVal))
except Exception as e:
gr.log.error("Cannot construct new message: %s" % str(e))
return
try:
self.message_port_pub(pmt.intern("outpair"), new_pair)
except Exception as e:
gr.log.error("Cannot send message: %s" % str(e))
gr.log.error("Incoming dictionary (%s):" % str(type(meta)))
gr.log.error(str(meta))
def stop(self):
return True
|