diff options
Diffstat (limited to 'gnuradio-runtime/python')
-rw-r--r-- | gnuradio-runtime/python/gnuradio/gr/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gnuradio-runtime/python/gnuradio/gr/__init__.py | 8 | ||||
-rw-r--r-- | gnuradio-runtime/python/gnuradio/gr/packet_utils.py | 137 | ||||
-rw-r--r-- | gnuradio-runtime/python/gnuradio/gr/prefs.py | 127 | ||||
-rw-r--r-- | gnuradio-runtime/python/pmt/__init__.py | 5 | ||||
-rw-r--r-- | gnuradio-runtime/python/pmt/pmt_to_python.py | 16 |
6 files changed, 162 insertions, 132 deletions
diff --git a/gnuradio-runtime/python/gnuradio/gr/CMakeLists.txt b/gnuradio-runtime/python/gnuradio/gr/CMakeLists.txt index 9d6f4dd718..ddad2c448a 100644 --- a/gnuradio-runtime/python/gnuradio/gr/CMakeLists.txt +++ b/gnuradio-runtime/python/gnuradio/gr/CMakeLists.txt @@ -23,6 +23,7 @@ include(GrPython) GR_PYTHON_INSTALL(FILES __init__.py tag_utils.py + packet_utils.py gateway.py gr_threading.py gr_threading_23.py diff --git a/gnuradio-runtime/python/gnuradio/gr/__init__.py b/gnuradio-runtime/python/gnuradio/gr/__init__.py index 94a5c9ec2b..4fc55c68b2 100644 --- a/gnuradio-runtime/python/gnuradio/gr/__init__.py +++ b/gnuradio-runtime/python/gnuradio/gr/__init__.py @@ -48,3 +48,11 @@ from gateway import basic_block, sync_block, decim_block, interp_block # Force the preference database to be initialized prefs = prefs.singleton + +log = gr.logger("log") +log.add_console_appender(prefs().get_string("LOG", "log_level", "off"), 'gr::log %d :%p: %m%n') +log.set_level(prefs().get_string("LOG", "log_level", "notset")) + +log_debug = gr.logger("log_debug") +log_debug.add_console_appender(prefs().get_string("LOG", "debug_level", "off"), 'gr::debug %d :%p: %m%n') +log_debug.set_level(prefs().get_string("LOG", "debug_level", "notset")) diff --git a/gnuradio-runtime/python/gnuradio/gr/packet_utils.py b/gnuradio-runtime/python/gnuradio/gr/packet_utils.py new file mode 100644 index 0000000000..7ae42e88e3 --- /dev/null +++ b/gnuradio-runtime/python/gnuradio/gr/packet_utils.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# +# Copyright 2013 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio import gr +import pmt + +def make_lengthtags(lengths, offsets, tagname='length', vlen=1): + tags = [] + assert(len(offsets) == len(lengths)) + for offset, length in zip(offsets, lengths): + tag = gr.tag_t() + tag.offset = offset/vlen + tag.key = pmt.string_to_symbol(tagname) + tag.value = pmt.from_long(length/vlen) + tags.append(tag) + return tags + +def string_to_vector(string): + v = [] + for s in string: + v.append(ord(s)) + return v + +def strings_to_vectors(strings, tsb_tag_key): + vs = [string_to_vector(string) for string in strings] + return packets_to_vectors(vs, tsb_tag_key) + +def vector_to_string(v): + s = [] + for d in v: + s.append(chr(d)) + return ''.join(s) + +def vectors_to_strings(data, tags, tsb_tag_key): + packets = vectors_to_packets(data, tags, tsb_tag_key) + return [vector_to_string(packet) for packet in packets] + +def count_bursts(data, tags, tsb_tag_key, vlen=1): + lengthtags = [t for t in tags + if pmt.symbol_to_string(t.key) == tsb_tag_key] + lengths = {} + for tag in lengthtags: + if tag.offset in lengths: + raise ValueError( + "More than one tags with key {0} with the same offset={1}." + .format(tsb_tag_key, tag.offset)) + lengths[tag.offset] = pmt.to_long(tag.value)*vlen + in_burst = False + in_packet = False + packet_length = None + packet_pos = None + burst_count = 0 + for pos in range(len(data)): + if pos in lengths: + if in_packet: + print("Got tag at pos {0} current packet_pos is {1}".format(pos, packet_pos)) + raise StandardError("Received packet tag while in packet.") + packet_pos = -1 + packet_length = lengths[pos] + in_packet = True + if not in_burst: + burst_count += 1 + in_burst = True + elif not in_packet: + in_burst = False + if in_packet: + packet_pos += 1 + if packet_pos == packet_length-1: + in_packet = False + packet_pos = None + return burst_count + +def vectors_to_packets(data, tags, tsb_tag_key, vlen=1): + lengthtags = [t for t in tags + if pmt.symbol_to_string(t.key) == tsb_tag_key] + lengths = {} + for tag in lengthtags: + if tag.offset in lengths: + raise ValueError( + "More than one tags with key {0} with the same offset={1}." + .format(tsb_tag_key, tag.offset)) + lengths[tag.offset] = pmt.to_long(tag.value)*vlen + if 0 not in lengths: + raise ValueError("There is no tag with key {0} and an offset of 0" + .format(tsb_tag_key)) + pos = 0 + packets = [] + while pos < len(data): + if pos not in lengths: + raise ValueError("There is no tag with key {0} and an offset of {1}." + "We were expecting one." + .format(tsb_tag_key, pos)) + length = lengths[pos] + if length == 0: + raise ValueError("Packets cannot have zero length.") + if pos+length > len(data): + raise ValueError("The final packet is incomplete.") + packets.append(data[pos: pos+length]) + pos += length + return packets + +def packets_to_vectors(packets, tsb_tag_key, vlen=1): + """ Returns a single data vector and a set of tags. + If used with blocks.vector_source_X, this set of data + and tags will produced a correct tagged stream. """ + tags = [] + data = [] + offset = 0 + for packet in packets: + data.extend(packet) + tag = gr.tag_t() + tag.offset = offset/vlen + tag.key = pmt.string_to_symbol(tsb_tag_key) + tag.value = pmt.from_long(len(packet)/vlen) + tags.append(tag) + offset = offset + len(packet) + return data, tags + diff --git a/gnuradio-runtime/python/gnuradio/gr/prefs.py b/gnuradio-runtime/python/gnuradio/gr/prefs.py deleted file mode 100644 index 17f5bfb54c..0000000000 --- a/gnuradio-runtime/python/gnuradio/gr/prefs.py +++ /dev/null @@ -1,127 +0,0 @@ -# -# Copyright 2006,2009 Free Software Foundation, Inc. -# -# This file is part of GNU Radio -# -# GNU Radio is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3, or (at your option) -# any later version. -# -# GNU Radio is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with GNU Radio; see the file COPYING. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, -# Boston, MA 02110-1301, USA. -# - -import gnuradio_runtime as gsp -_prefs_base = gsp.prefs - - -import ConfigParser -import os -import os.path -import sys -import glob - - -def _user_prefs_filename(): - return os.path.expanduser('~/.gnuradio/config.conf') - -def _sys_prefs_dirname(): - return gsp.prefsdir() - -def _bool(x): - """ - Try to coerce obj to a True or False - """ - if isinstance(x, bool): - return x - if isinstance(x, (float, int)): - return bool(x) - raise TypeError, x - - -class _prefs(_prefs_base): - """ - Derive our 'real class' from the stubbed out base class that has support - for SWIG directors. This allows C++ code to magically and transparently - invoke the methods in this python class. - """ - def __init__(self): - _prefs_base.__init__(self) - self.cp = ConfigParser.RawConfigParser() - self.__getattr__ = lambda self, name: getattr(self.cp, name) - - def _sys_prefs_filenames(self): - dir = _sys_prefs_dirname() - try: - fnames = glob.glob(os.path.join(dir, '*.conf')) - except (IOError, OSError): - return [] - fnames.sort() - return fnames - - def _read_files(self): - filenames = self._sys_prefs_filenames() - filenames.append(_user_prefs_filename()) - #print "filenames: ", filenames - self.cp.read(filenames) - - # ---------------------------------------------------------------- - # These methods override the C++ virtual methods of the same name - # ---------------------------------------------------------------- - def has_section(self, section): - return self.cp.has_section(section) - - def has_option(self, section, option): - return self.cp.has_option(section, option) - - def get_string(self, section, option, default_val): - try: - return self.cp.get(section, option) - except: - return default_val - - def get_bool(self, section, option, default_val): - try: - return self.cp.getboolean(section, option) - except: - return default_val - - def get_long(self, section, option, default_val): - try: - return self.cp.getint(section, option) - except: - return default_val - - def get_double(self, section, option, default_val): - try: - return self.cp.getfloat(section, option) - except: - return default_val - # ---------------------------------------------------------------- - # End override of C++ virtual methods - # ---------------------------------------------------------------- - - -_prefs_db = _prefs() - -# if GR_DONT_LOAD_PREFS is set, don't load them. -# (make check uses this to avoid interactions.) -if os.getenv("GR_DONT_LOAD_PREFS", None) is None: - _prefs_db._read_files() - - -_prefs_base.set_singleton(_prefs_db) # tell C++ what instance to use - -def prefs(): - """ - Return the global preference data base - """ - return _prefs_db diff --git a/gnuradio-runtime/python/pmt/__init__.py b/gnuradio-runtime/python/pmt/__init__.py index 00940e4cc1..1c7db73322 100644 --- a/gnuradio-runtime/python/pmt/__init__.py +++ b/gnuradio-runtime/python/pmt/__init__.py @@ -48,6 +48,9 @@ except ImportError: __path__.append(os.path.join(dirname, "..", "..", "swig")) from pmt_swig import * +# due to changes in the PMT_NIL singleton for static builds, we force +# this into Python here. +PMT_NIL = get_PMT_NIL() + from pmt_to_python import pmt_to_python as to_python from pmt_to_python import python_to_pmt as to_pmt - diff --git a/gnuradio-runtime/python/pmt/pmt_to_python.py b/gnuradio-runtime/python/pmt/pmt_to_python.py index 3344eba163..e08b7265de 100644 --- a/gnuradio-runtime/python/pmt/pmt_to_python.py +++ b/gnuradio-runtime/python/pmt/pmt_to_python.py @@ -21,6 +21,10 @@ try: import pmt_swig as pmt except: import pmt import numpy +# SWIG isn't taking in the #define PMT_NIL; +# getting the singleton locally. +PMT_NIL = pmt.get_PMT_NIL() + #define missing def pmt_to_tuple(p): elems = list() @@ -41,7 +45,7 @@ def pmt_to_vector(p): return v def pmt_from_vector(p): - v = pmt.make_vector(len(p), pmt.PMT_NIL) + v = pmt.make_vector(len(p), PMT_NIL) for i, elem in enumerate(p): pmt.vector_set(v, i, python_to_pmt(elem)) return v @@ -99,7 +103,7 @@ def uvector_to_numpy(uvector): raise ValueError("unsupported uvector data type for conversion to numpy array %s"%(uvector)) type_mappings = ( #python type, check pmt type, to python, from python - (None, pmt.is_null, lambda x: None, lambda x: pmt.PMT_NIL), + (None, pmt.is_null, lambda x: None, lambda x: PMT_NIL), (bool, pmt.is_bool, pmt.to_bool, pmt.from_bool), (str, pmt.is_symbol, pmt.symbol_to_string, pmt.string_to_symbol), (unicode, lambda x: False, None, lambda x: pmt.string_to_symbol(x.encode('utf-8'))), @@ -110,12 +114,17 @@ type_mappings = ( #python type, check pmt type, to python, from python (tuple, pmt.is_tuple, pmt_to_tuple, pmt_from_tuple), (list, pmt.is_vector, pmt_to_vector, pmt_from_vector), (dict, pmt.is_dict, pmt_to_dict, pmt_from_dict), + (tuple, pmt.is_pair, lambda x: (pmt_to_python(pmt.car(x)), pmt_to_python(pmt.cdr(x))), lambda x: pmt.cons(python_to_pmt(x[0]), python_to_pmt(x[1]))), (numpy.ndarray, pmt.is_uniform_vector, uvector_to_numpy, numpy_to_uvector), ) def pmt_to_python(p): for python_type, pmt_check, to_python, from_python in type_mappings: - if pmt_check(p): return to_python(p) + if pmt_check(p): + try: + return to_python(p) + except: + pass raise ValueError("can't convert %s type to pmt (%s)"%(type(p),p)) def python_to_pmt(p): @@ -124,4 +133,3 @@ def python_to_pmt(p): if p == None: return from_python(p) elif isinstance(p, python_type): return from_python(p) raise ValueError("can't convert %s type to pmt (%s)"%(type(p),p)) - |